Skip to content

Commit 94a0ea3

Browse files
committed
fix: v-close-popper directive, #1022
1 parent f897e0b commit 94a0ea3

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<template>
2+
<div class="example flex justify-center">
3+
<VDropdown>
4+
<button class="border border-gray-500 rounded px-4 py-2">
5+
Click me
6+
</button>
7+
8+
<template #popper>
9+
<div class="p-6 flex gap-2">
10+
<button class="underline cursor-pointer">Other button</button>
11+
<button v-close-popper class="underline cursor-pointer">Close</button>
12+
</div>
13+
</template>
14+
</VDropdown>
15+
</div>
16+
</template>

docs/components.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ export {}
88
declare module 'vue' {
99
export interface GlobalComponents {
1010
ArrowPadding: typeof import('./.vitepress/components/ArrowPadding.vue')['default']
11+
DropdownCloseDirectiveDemo: typeof import('./.vitepress/components/DropdownCloseDirectiveDemo.vue')['default']
1112
DropdownMobileDemo: typeof import('./.vitepress/components/DropdownMobileDemo.vue')['default']
1213
DropdownPlacement: typeof import('./.vitepress/components/DropdownPlacement.vue')['default']
1314
DropdownSimpleExample: typeof import('./.vitepress/components/DropdownSimpleExample.vue')['default']
15+
DropdownVClodePopperDemo: typeof import('./.vitepress/components/DropdownVClodePopperDemo.vue')['default']
1416
MenuSimpleExample: typeof import('./.vitepress/components/MenuSimpleExample.vue')['default']
1517
OffsetExample: typeof import('./.vitepress/components/OffsetExample.vue')['default']
1618
RouterLink: typeof import('vue-router')['RouterLink']

docs/guide/component.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ Use the `hide` slot prop to close the popper:
218218
<button>Click me</button>
219219

220220
<template #popper="{ hide }">
221-
<a @click="hide()">Close</a>
221+
<button @click="hide()">Close</button>
222222
</template>
223223
</VDropdown>
224224
```
@@ -232,24 +232,26 @@ Use the `v-close-popper` directive on an element inside the dropdown to close it
232232
<button>Click me</button>
233233

234234
<template #popper>
235-
<a v-close-popper>Close</a>
235+
<button v-close-popper>Close</button>
236236
</template>
237237
</VDropdown>
238238
```
239239

240+
<DropdownCloseDirectiveDemo />
241+
240242
The directive works even in nested components in the `popper` slot.
241243

242244
You can also set it to true or false to enable or disable the directive (enabled by default):
243245

244246
```html
245-
<a v-close-popper="false">Close</a>
246-
<a v-close-popper="true">Close</a>
247+
<button v-close-popper="false">Close</button>
248+
<button v-close-popper="true">Close</button>
247249
```
248250

249251
You can also use a property:
250252

251253
```html
252-
<a v-close-popper="myBooleanProp">Close</a>
254+
<button v-close-popper="myBooleanProp">Close</button>
253255
```
254256

255257
```js

packages/floating-vue/src/directives/v-close-popper.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { supportsPassive } from '../util/env'
22

33
function addListeners (el) {
4-
el.addEventListener('mousedown', onMouseDown)
4+
el.addEventListener('mousedown', addEventProps)
5+
el.addEventListener('click', addEventProps)
56
el.addEventListener('touchstart', onTouchStart, supportsPassive
67
? {
78
passive: true,
@@ -10,13 +11,14 @@ function addListeners (el) {
1011
}
1112

1213
function removeListeners (el) {
13-
el.removeEventListener('mousedown', onMouseDown)
14+
el.removeEventListener('mousedown', addEventProps)
15+
el.removeEventListener('click', addEventProps)
1416
el.removeEventListener('touchstart', onTouchStart)
1517
el.removeEventListener('touchend', onTouchEnd)
1618
el.removeEventListener('touchcancel', onTouchCancel)
1719
}
1820

19-
function onMouseDown (event) {
21+
function addEventProps (event) {
2022
const el = event.currentTarget
2123
event.closePopover = !el.$_vclosepopover_touch
2224
event.closeAllPopover = el.$_closePopoverModifiers && !!el.$_closePopoverModifiers.all

0 commit comments

Comments
 (0)