Skip to content

Commit

Permalink
fix: v-close-popper directive, #1022
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Jan 30, 2024
1 parent f897e0b commit 94a0ea3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
16 changes: 16 additions & 0 deletions docs/.vitepress/components/DropdownCloseDirectiveDemo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<template>
<div class="example flex justify-center">
<VDropdown>
<button class="border border-gray-500 rounded px-4 py-2">
Click me
</button>

<template #popper>
<div class="p-6 flex gap-2">
<button class="underline cursor-pointer">Other button</button>
<button v-close-popper class="underline cursor-pointer">Close</button>
</div>
</template>
</VDropdown>
</div>
</template>
2 changes: 2 additions & 0 deletions docs/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ export {}
declare module 'vue' {
export interface GlobalComponents {
ArrowPadding: typeof import('./.vitepress/components/ArrowPadding.vue')['default']
DropdownCloseDirectiveDemo: typeof import('./.vitepress/components/DropdownCloseDirectiveDemo.vue')['default']
DropdownMobileDemo: typeof import('./.vitepress/components/DropdownMobileDemo.vue')['default']
DropdownPlacement: typeof import('./.vitepress/components/DropdownPlacement.vue')['default']
DropdownSimpleExample: typeof import('./.vitepress/components/DropdownSimpleExample.vue')['default']
DropdownVClodePopperDemo: typeof import('./.vitepress/components/DropdownVClodePopperDemo.vue')['default']
MenuSimpleExample: typeof import('./.vitepress/components/MenuSimpleExample.vue')['default']
OffsetExample: typeof import('./.vitepress/components/OffsetExample.vue')['default']
RouterLink: typeof import('vue-router')['RouterLink']
Expand Down
12 changes: 7 additions & 5 deletions docs/guide/component.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ Use the `hide` slot prop to close the popper:
<button>Click me</button>

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

<template #popper>
<a v-close-popper>Close</a>
<button v-close-popper>Close</button>
</template>
</VDropdown>
```

<DropdownCloseDirectiveDemo />

The directive works even in nested components in the `popper` slot.

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

```html
<a v-close-popper="false">Close</a>
<a v-close-popper="true">Close</a>
<button v-close-popper="false">Close</button>
<button v-close-popper="true">Close</button>
```

You can also use a property:

```html
<a v-close-popper="myBooleanProp">Close</a>
<button v-close-popper="myBooleanProp">Close</button>
```

```js
Expand Down
8 changes: 5 additions & 3 deletions packages/floating-vue/src/directives/v-close-popper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { supportsPassive } from '../util/env'

function addListeners (el) {
el.addEventListener('mousedown', onMouseDown)
el.addEventListener('mousedown', addEventProps)
el.addEventListener('click', addEventProps)
el.addEventListener('touchstart', onTouchStart, supportsPassive
? {
passive: true,
Expand All @@ -10,13 +11,14 @@ function addListeners (el) {
}

function removeListeners (el) {
el.removeEventListener('mousedown', onMouseDown)
el.removeEventListener('mousedown', addEventProps)
el.removeEventListener('click', addEventProps)
el.removeEventListener('touchstart', onTouchStart)
el.removeEventListener('touchend', onTouchEnd)
el.removeEventListener('touchcancel', onTouchCancel)
}

function onMouseDown (event) {
function addEventProps (event) {
const el = event.currentTarget
event.closePopover = !el.$_vclosepopover_touch
event.closeAllPopover = el.$_closePopoverModifiers && !!el.$_closePopoverModifiers.all
Expand Down

0 comments on commit 94a0ea3

Please sign in to comment.