|
| 1 | +import type { Action } from 'svelte/action'; |
| 2 | +import DropDownMenu, { type MenuItem } from './DropDownMenu.svelte'; |
| 3 | +import { mount, unmount } from 'svelte'; |
| 4 | + |
| 5 | +type Parameter = { |
| 6 | + placeAsSibling?: boolean; // place DropDownMenu next to node in the DOM (to force it on top of modal, for example) |
| 7 | + menuItems: MenuItem[]; |
| 8 | +}; |
| 9 | + |
| 10 | +export const dropdownMenu: Action<HTMLElement, Parameter> = ( |
| 11 | + node, |
| 12 | + { placeAsSibling, menuItems } |
| 13 | +) => { |
| 14 | + const ATTACH_DELAY = 500; |
| 15 | + const REMOVE_DELAY = 200; |
| 16 | + |
| 17 | + $effect(() => { |
| 18 | + // setup goes here |
| 19 | + let container = document.getElementById('floating-elements-container') || document.body; // See https://atfzl.com/articles/don-t-attach-tooltips-to-document-body |
| 20 | + |
| 21 | + if (placeAsSibling && node.parentElement) { |
| 22 | + container = node.parentElement; |
| 23 | + } |
| 24 | + |
| 25 | + let attached = false; |
| 26 | + let floatingElement: DropDownMenu | null; |
| 27 | + let cancelAttach: ((reason?: unknown) => void) | undefined; |
| 28 | + let cancelRemove: ((reason?: unknown) => void) | undefined; |
| 29 | + |
| 30 | + node.addEventListener('click', attachDropDownMenu); |
| 31 | + node.addEventListener('mouseover', attachDropDownMenu); |
| 32 | + node.addEventListener('mouseout', removeDropDownMenu); |
| 33 | + node.addEventListener('focus', attachDropDownMenu); |
| 34 | + node.addEventListener('blur', removeDropDownMenu); |
| 35 | + |
| 36 | + async function attachDropDownMenu(event: MouseEvent | FocusEvent) { |
| 37 | + try { |
| 38 | + cancelAttach?.(); // cancel earlier promises to ensure DropDownMenus doesn't appear after navigating |
| 39 | + cancelRemove?.(); |
| 40 | + if (!attached) { |
| 41 | + await new Promise((resolve, reject) => { |
| 42 | + cancelAttach = reject; // allows promise rejection from outside the promise constructor scope |
| 43 | + if (event.type === 'click') { |
| 44 | + resolve(undefined); |
| 45 | + } else { |
| 46 | + setTimeout(resolve, ATTACH_DELAY); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + floatingElement = mount(DropDownMenu, { |
| 51 | + target: container, |
| 52 | + props: { |
| 53 | + referenceElement: node, |
| 54 | + onmouseover: startFloatingElementInteraction, |
| 55 | + onfocus: startFloatingElementInteraction, |
| 56 | + onmouseleave: endFloatingElementInteraction, |
| 57 | + onblur: endFloatingElementInteraction, |
| 58 | + menuItems |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + attached = true; |
| 63 | + } |
| 64 | + // eslint-disable-next-line no-empty |
| 65 | + } catch {} |
| 66 | + } |
| 67 | + |
| 68 | + async function removeDropDownMenu() { |
| 69 | + try { |
| 70 | + cancelAttach?.(); |
| 71 | + await new Promise((resolve, reject) => { |
| 72 | + cancelRemove = reject; |
| 73 | + setTimeout(resolve, REMOVE_DELAY); |
| 74 | + }); |
| 75 | + destroyDropDownMenu(); |
| 76 | + // eslint-disable-next-line no-empty |
| 77 | + } catch {} |
| 78 | + } |
| 79 | + |
| 80 | + function destroyDropDownMenu() { |
| 81 | + cancelAttach?.(); |
| 82 | + cancelRemove?.(); |
| 83 | + if (attached && floatingElement) { |
| 84 | + unmount(floatingElement); |
| 85 | + } |
| 86 | + attached = false; |
| 87 | + } |
| 88 | + |
| 89 | + function startFloatingElementInteraction() { |
| 90 | + cancelRemove?.(); |
| 91 | + } |
| 92 | + |
| 93 | + function endFloatingElementInteraction() { |
| 94 | + removeDropDownMenu(); |
| 95 | + } |
| 96 | + |
| 97 | + return () => { |
| 98 | + destroyDropDownMenu(); |
| 99 | + node.removeEventListener('click', attachDropDownMenu); |
| 100 | + node.removeEventListener('mouseover', attachDropDownMenu); |
| 101 | + node.removeEventListener('mouseout', removeDropDownMenu); |
| 102 | + node.removeEventListener('focus', attachDropDownMenu); |
| 103 | + node.removeEventListener('blur', removeDropDownMenu); |
| 104 | + }; |
| 105 | + }); |
| 106 | +}; |
| 107 | + |
| 108 | +export default dropdownMenu; |
0 commit comments