|
| 1 | +<template> |
| 2 | + <v-layout> |
| 3 | + <v-app-bar density="compact" title="My Website"> |
| 4 | + <v-btn |
| 5 | + v-for="group in groups" |
| 6 | + :key="group.name" |
| 7 | + :text="group.name" |
| 8 | + append-icon="mdi-chevron-down" |
| 9 | + @focus="activate($event, group)" |
| 10 | + @mouseenter="activate($event, group)" |
| 11 | + @mouseleave="delayedClose()" |
| 12 | + ></v-btn> |
| 13 | + </v-app-bar> |
| 14 | + |
| 15 | + <v-menu |
| 16 | + v-model="menu" |
| 17 | + :activator="activator" |
| 18 | + :content-class="{ 'menu-move-transition': menuMoving }" |
| 19 | + location="bottom end" |
| 20 | + offset="4" |
| 21 | + viewport-margin="0" |
| 22 | + > |
| 23 | + <v-list |
| 24 | + :items="menuItems" |
| 25 | + class="py-1" |
| 26 | + density="compact" |
| 27 | + rounded="lg" |
| 28 | + border |
| 29 | + @mouseenter="onListEnter()" |
| 30 | + @mouseleave="delayedClose()" |
| 31 | + > |
| 32 | + <template v-slot:append> |
| 33 | + <v-icon icon="mdi-arrow-top-right"></v-icon> |
| 34 | + </template> |
| 35 | + </v-list> |
| 36 | + </v-menu> |
| 37 | + |
| 38 | + <v-main> |
| 39 | + <!-- Main Content --> |
| 40 | + </v-main> |
| 41 | + </v-layout> |
| 42 | +</template> |
| 43 | + |
| 44 | +<script setup> |
| 45 | + import { ref } from 'vue' |
| 46 | +
|
| 47 | + const menu = ref(false) |
| 48 | + const activator = ref() |
| 49 | + const menuItems = ref([]) |
| 50 | + const menuMoving = ref(false) |
| 51 | +
|
| 52 | + const groups = [ |
| 53 | + { |
| 54 | + name: 'Home', |
| 55 | + submenu: [ |
| 56 | + { title: 'Welcome', value: 'welcome' }, |
| 57 | + { title: 'Updates', value: 'latest' }, |
| 58 | + ], |
| 59 | + }, |
| 60 | + { |
| 61 | + name: 'About Me', |
| 62 | + submenu: [ |
| 63 | + { title: 'Bio', value: 'bio' }, |
| 64 | + { title: 'Resume', value: 'resume' }, |
| 65 | + { title: 'Skills', value: 'skills' }, |
| 66 | + ], |
| 67 | + }, |
| 68 | + { |
| 69 | + name: 'Blog', |
| 70 | + submenu: [ |
| 71 | + { title: 'All Posts', value: 'all-posts' }, |
| 72 | + { title: 'Tech', value: 'tech-posts' }, |
| 73 | + { title: 'Life', value: 'life-posts' }, |
| 74 | + ], |
| 75 | + }, |
| 76 | + ] |
| 77 | +
|
| 78 | + let closeTimeout = -1 |
| 79 | + let movingTimeout = -1 |
| 80 | + function activate ({ currentTarget }, group) { |
| 81 | + clearTimeout(closeTimeout) |
| 82 | +
|
| 83 | + clearTimeout(movingTimeout) |
| 84 | + if (menu.value) { |
| 85 | + menuMoving.value = true |
| 86 | + movingTimeout = window.setTimeout(() => menuMoving.value = false, 300) |
| 87 | + } |
| 88 | +
|
| 89 | + activator.value = currentTarget |
| 90 | + menuItems.value = group.submenu |
| 91 | + menu.value = true |
| 92 | + } |
| 93 | +
|
| 94 | + function onListEnter () { |
| 95 | + clearTimeout(closeTimeout) |
| 96 | + } |
| 97 | +
|
| 98 | + function delayedClose () { |
| 99 | + clearTimeout(closeTimeout) |
| 100 | + closeTimeout = window.setTimeout(() => menu.value = false, 600) |
| 101 | + } |
| 102 | +</script> |
| 103 | + |
| 104 | +<style> |
| 105 | + .menu-move-transition { |
| 106 | + transition: 0.2s ease-out; |
| 107 | + transition-property: left, top; |
| 108 | + } |
| 109 | +</style> |
0 commit comments