-
Notifications
You must be signed in to change notification settings - Fork 463
Expand file tree
/
Copy pathuseDropdown.ts
More file actions
24 lines (20 loc) · 909 Bytes
/
useDropdown.ts
File metadata and controls
24 lines (20 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { computed, toValue } from 'vue';
import { onClickOutside, onKeyStroke, type MaybeElementRef, type MaybeRefOrGetter } from '@vueuse/core';
import { flip, offset, shift } from '@floating-ui/vue';
import { type UseDropdownOptions, usePopover } from '@storefront-ui/vue';
export function useDropdown(options: UseDropdownOptions) {
const { onClose, placement = 'bottom', middleware, isOpen, ...popoverOptions } = options;
const { floatingRef, referenceRef, style } = usePopover({
placement,
middleware: computed(() => toValue(middleware) || [offset(8), shift(), flip()]),
isOpen,
...popoverOptions,
});
onClickOutside(referenceRef as MaybeElementRef, () => {
if (toValue(isOpen)) {
onClose?.();
}
});
onKeyStroke('Escape', onClose, { target: referenceRef as MaybeRefOrGetter<EventTarget | null | undefined> });
return { floatingRef, referenceRef, style };
}