Skip to content

Commit 996e5ab

Browse files
docs: add positioning section and example for menus (#21988)
fixes: #19936
1 parent fc91e6d commit 996e5ab

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<template>
2+
<v-container max-width="300">
3+
<div
4+
v-for="n in 10"
5+
:key="n"
6+
class="py-2 border-b d-flex align-center ga-2"
7+
>
8+
Some Text Here
9+
<v-spacer></v-spacer>
10+
<v-icon-btn
11+
icon="mdi-dots-vertical"
12+
size="small"
13+
variant="outlined"
14+
@click="show"
15+
></v-icon-btn>
16+
</div>
17+
18+
<v-menu
19+
v-model="showMenu"
20+
:offset="[-8,-12]"
21+
:target="menuTarget"
22+
location="bottom end"
23+
scroll-strategy="close"
24+
>
25+
<v-list
26+
:items="menuItems"
27+
class="py-0"
28+
density="compact"
29+
item-value="code"
30+
item-props
31+
slim
32+
>
33+
<template v-slot:prepend>
34+
<v-icon class="mr-n2" size="small"></v-icon>
35+
</template>
36+
</v-list>
37+
</v-menu>
38+
</v-container>
39+
</template>
40+
41+
<script setup>
42+
import { ref } from 'vue'
43+
44+
const showMenu = ref(false)
45+
const menuTarget = ref(null)
46+
47+
const menuItems = [
48+
{ title: 'Create', prependIcon: 'mdi-plus-circle-outline', code: 'add' },
49+
{ type: 'divider' },
50+
{ title: 'Modify', prependIcon: 'mdi-pencil-outline', code: 'edit' },
51+
{ type: 'divider' },
52+
{ title: 'Remove', prependIcon: 'mdi-trash-can-outline', code: 'delete' },
53+
]
54+
55+
async function show(evt) {
56+
if (showMenu.value) {
57+
showMenu.value = false
58+
await new Promise(r => setTimeout(r, 100))
59+
}
60+
menuTarget.value = evt.target.closest('.v-icon-btn')
61+
showMenu.value = true
62+
}
63+
</script>

packages/docs/src/pages/en/components/menus.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ Menus with other menus inside them will not close until their children are close
9797

9898
<ExamplesExample file="v-menu/prop-submenu" />
9999

100+
#### Positioning Menus with Coordinates
101+
102+
`v-menu` can be positioned relative to a DOM element or explicit `[x, y]` coordinates.
103+
104+
* The most common use case is to pass an **event target element**. This allows the menu to anchor itself to the element that was clicked.
105+
* You can also use `[x, y]` screen coordinates, though this is less common and typically used for context menus.
106+
* `:offset` is used to shift the menu position relative to its anchor, not to define an absolute position.
107+
* Any DOM event with `clientX` and `clientY` can be used (e.g. `click`, `contextmenu`).
108+
109+
<ExamplesExample file="v-menu/prop-positioningmenu" />
110+
100111
### Slots
101112

102113
#### Activator and tooltip

0 commit comments

Comments
 (0)