@allxsmith/bestax-bulma 5.29.0 → 5.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/dist/bestax.css +1 -1
  2. package/dist/bestax.css.map +1 -1
  3. package/dist/esm/components/ContextMenu.js +244 -0
  4. package/dist/esm/components/ContextMenu.js.map +1 -0
  5. package/dist/esm/components/HoverCard.js +3 -10
  6. package/dist/esm/components/HoverCard.js.map +1 -1
  7. package/dist/esm/components/Tooltip.js +36 -61
  8. package/dist/esm/components/Tooltip.js.map +1 -1
  9. package/dist/esm/elements/Button.js.map +1 -1
  10. package/dist/esm/elements/ToggleButton.js +60 -0
  11. package/dist/esm/elements/ToggleButton.js.map +1 -0
  12. package/dist/esm/form/DateTimeInputBase.js.map +1 -1
  13. package/dist/esm/form/TimeInputBase.js.map +1 -1
  14. package/dist/esm/form/_pickerInternals/pickerTypes.js +14 -6
  15. package/dist/esm/form/_pickerInternals/pickerTypes.js.map +1 -1
  16. package/dist/esm/helpers/useContextMenuTrigger.js +166 -0
  17. package/dist/esm/helpers/useContextMenuTrigger.js.map +1 -0
  18. package/dist/esm/helpers/useHoverOpen.js +40 -6
  19. package/dist/esm/helpers/useHoverOpen.js.map +1 -1
  20. package/dist/esm/helpers/useMenuKeys.js +182 -0
  21. package/dist/esm/helpers/useMenuKeys.js.map +1 -0
  22. package/dist/extras.css +1 -1
  23. package/dist/extras.css.map +1 -1
  24. package/dist/index.cjs +2078 -1451
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.esm.js +2 -0
  27. package/dist/index.esm.js.map +1 -1
  28. package/dist/types/components/ContextMenu.d.ts +66 -0
  29. package/dist/types/components/Tooltip.d.ts +45 -4
  30. package/dist/types/elements/Button.d.ts +1 -1
  31. package/dist/types/elements/ToggleButton.d.ts +90 -0
  32. package/dist/types/form/DateTimeInputBase.d.ts +4 -1
  33. package/dist/types/form/TimeInputBase.d.ts +5 -2
  34. package/dist/types/form/_pickerInternals/pickerTypes.d.ts +16 -1
  35. package/dist/types/helpers/useContextMenuTrigger.d.ts +67 -0
  36. package/dist/types/helpers/useHoverOpen.d.ts +19 -3
  37. package/dist/types/helpers/useMenuKeys.d.ts +57 -0
  38. package/dist/types/index.d.ts +2 -0
  39. package/dist/types-cjs/components/ContextMenu.d.ts +66 -0
  40. package/dist/types-cjs/components/Tooltip.d.ts +45 -4
  41. package/dist/types-cjs/elements/Button.d.ts +1 -1
  42. package/dist/types-cjs/elements/ToggleButton.d.ts +90 -0
  43. package/dist/types-cjs/form/DateTimeInputBase.d.ts +4 -1
  44. package/dist/types-cjs/form/TimeInputBase.d.ts +5 -2
  45. package/dist/types-cjs/form/_pickerInternals/pickerTypes.d.ts +16 -1
  46. package/dist/types-cjs/helpers/useContextMenuTrigger.d.ts +67 -0
  47. package/dist/types-cjs/helpers/useHoverOpen.d.ts +19 -3
  48. package/dist/types-cjs/helpers/useMenuKeys.d.ts +57 -0
  49. package/dist/types-cjs/index.d.ts +2 -0
  50. package/dist/versions/bestax-no-dark-mode.css +1 -1
  51. package/dist/versions/bestax-no-dark-mode.css.map +1 -1
  52. package/dist/versions/bestax-no-helpers-prefixed.css +1 -1
  53. package/dist/versions/bestax-no-helpers-prefixed.css.map +1 -1
  54. package/dist/versions/bestax-no-helpers.css +1 -1
  55. package/dist/versions/bestax-no-helpers.css.map +1 -1
  56. package/dist/versions/bestax-prefixed.css +1 -1
  57. package/dist/versions/bestax-prefixed.css.map +1 -1
  58. package/package.json +1 -1
  59. package/src/scss/components/_contextmenu.scss +50 -0
  60. package/src/scss/components/_index.scss +1 -0
  61. package/src/scss/components/_tooltip.scss +76 -1
@@ -1 +1 @@
1
- {"version":3,"file":"useHoverOpen.js","sources":["../../../src/helpers/useHoverOpen.ts"],"sourcesContent":["// INTERNAL: deliberately not exported from src/index.ts.\nimport { useCallback, useMemo, useRef } from 'react';\nimport type { FocusEvent, PointerEvent, RefObject } from 'react';\nimport { useClientLayoutEffect } from './useClientLayoutEffect';\nimport { getDeepestActiveElement, isEventInside } from './shadowDom';\n\n/**\n * Options for `useHoverOpen`.\n */\nexport interface UseHoverOpenOptions {\n /** Whether the floating element is open now, controlled or not. */\n open: boolean;\n /**\n * Asks for the floating element to open or close: when a delay runs out,\n * and at once on Escape. It isn't asked for the state it is already in,\n * except that an Escape between asking to open and the open showing takes\n * the ask back by asking to close.\n */\n onOpenChange: (open: boolean) => void;\n /** How long hover or keyboard focus waits before asking to open, in milliseconds. */\n openDelay: number;\n /**\n * How long it waits once the pointer and focus have both left before\n * asking to close, in milliseconds. Coming back in that time keeps it open,\n * which is how the pointer crosses a gap to the floating element.\n */\n closeDelay: number;\n /**\n * The element that opens it, which has to take focus itself. The Escape\n * listener goes on its document, an iframe's when it renders into one, and\n * focus left in the floating element comes back here when the floating\n * element closes.\n */\n triggerRef: RefObject<HTMLElement | null>;\n /** The floating element, while it is open. */\n floatingRef: RefObject<HTMLElement | null>;\n /**\n * Whether a tap on the trigger opens it at once, with the next tap closing\n * it. Without it, a touch pointer never opens it. While a tap holds it\n * open, no hover timer closes it, and a press outside the trigger and the\n * floating element does, as Escape does. The trigger's own tap action\n * still runs.\n * @defaultValue false\n */\n openOnTap?: boolean;\n}\n\n/**\n * Pointer and focus handlers for the element that holds the trigger. The\n * floating element has to be inside it in React's tree, in place or through\n * a portal, since React's events reach it from there either way, and it\n * should hold nothing else: focus inside it that isn't on the trigger counts\n * as focus in the floating element.\n */\nexport interface HoverOpenHandlers {\n onPointerEnter: (event: PointerEvent<HTMLElement>) => void;\n onPointerLeave: (event: PointerEvent<HTMLElement>) => void;\n onFocus: (event: FocusEvent<HTMLElement>) => void;\n onBlur: (event: FocusEvent<HTMLElement>) => void;\n onPointerDown: (event: PointerEvent<HTMLElement>) => void;\n onPointerUp: (event: PointerEvent<HTMLElement>) => void;\n}\n\n/**\n * Whether focus that just arrived on `el` is keyboard focus, as the browser\n * decides it for `:focus-visible`: focus from a key or from the browser's\n * own controls is, focus a press brings isn't, and focus a script moves\n * follows whichever came last. A browser that can't match the selector\n * throws, and then every focus counts, so keyboard users can still open it.\n */\nfunction isKeyboardFocus(el: Element): boolean {\n try {\n return el.matches(':focus-visible');\n } catch {\n return true;\n }\n}\n\n/** Whether `node` is `container` or inside it, when there are both. */\nconst within = (container: Node | null, node: Node | null): boolean =>\n Boolean(container && node && container.contains(node));\n\n/** A hook that wants Escape: its floating element is open, or an open is on its way. */\ninterface EscapeEntry {\n /** When its floating element opened, counted across the page, or 0 while it is closed. */\n openedAt: number;\n trigger: () => HTMLElement | null;\n floating: () => HTMLElement | null;\n /** Takes back an open that is on its way: waiting on its delay, or asked for and not showing yet. */\n cancel: () => void;\n /** Asks to close. */\n close: () => void;\n}\n\n/**\n * Per document, the hooks that want Escape and the one listener they share\n * while there are any, so a card inside another card doesn't add a second\n * listener that closes both on one keypress.\n */\nconst escapeListeners = new Map<\n Document,\n { entries: Set<EscapeEntry>; stop: () => void }\n>();\n\n/** Counts openings across the page, so the most recent one is known. */\nlet openings = 0;\n\n/**\n * Adds `entry` to the hooks that want Escape on `doc`, starting the shared\n * listener when it is the first.\n *\n * Escape takes back every open that is on its way, so nothing appears right\n * after it, and closes one open floating element, chosen by these rules in\n * turn:\n *\n * 1. One that focus is in, on its trigger or inside it, when there is one.\n * 2. The innermost: one whose floating element holds no other's trigger.\n * That follows nesting whether either one is portaled, since an inner\n * trigger sits in the outer floating element either way.\n * 3. The one that opened last.\n *\n * The keypress belongs to whatever has focus. When that is one of the open\n * floating elements or their triggers, or nothing, the listener stops it in\n * the capture phase, before anything around (a Popover, a Modal) sees it.\n * When focus is anywhere else, such as a combobox or a dialog of its own,\n * the floating element still closes, since hover put it there without\n * focus, and the key goes on to that owner.\n *\n * @returns What to call when the hook stops wanting Escape.\n */\nfunction wantEscape(doc: Document, entry: EscapeEntry): () => void {\n let listener = escapeListeners.get(doc);\n if (!listener) {\n const entries = new Set<EscapeEntry>();\n const handleKeyDown = (event: KeyboardEvent) => {\n if (event.key !== 'Escape') return;\n const open: EscapeEntry[] = [];\n // A copy, since taking an open back can take its hook out of the set.\n for (const each of [...entries]) {\n if (each.openedAt) open.push(each);\n else each.cancel();\n }\n const focused = getDeepestActiveElement(doc);\n const holding = open.filter(\n each =>\n within(each.trigger(), focused) || within(each.floating(), focused)\n );\n const pool = holding.length > 0 ? holding : open;\n let top: EscapeEntry | undefined;\n for (const each of pool) {\n const holdsAnother = pool.some(\n other => other !== each && within(each.floating(), other.trigger())\n );\n if (!holdsAnother && (!top || each.openedAt > top.openedAt)) {\n top = each;\n }\n }\n if (!top) return;\n if (holding.length > 0 || !focused || focused === doc.body) {\n event.preventDefault();\n event.stopPropagation();\n }\n top.close();\n };\n doc.addEventListener('keydown', handleKeyDown, true);\n listener = {\n entries,\n stop: () => {\n doc.removeEventListener('keydown', handleKeyDown, true);\n escapeListeners.delete(doc);\n },\n };\n escapeListeners.set(doc, listener);\n }\n const shared = listener;\n shared.entries.add(entry);\n return () => {\n shared.entries.delete(entry);\n if (shared.entries.size === 0) shared.stop();\n };\n}\n\ninterface HoverOpenState {\n /** The pointer, other than a touch, is over the trigger or the floating element. */\n pointerIn: boolean;\n /** Focus holds it open: any focus in the floating element, or keyboard focus on the trigger. */\n focusIn: boolean;\n /** Focus, however it got there, is in the floating element. */\n focusInFloating: boolean;\n /** Whether the pointer or focus was holding it open when last settled. */\n held: boolean;\n /** An open is waiting on its delay. */\n opening: boolean;\n /**\n * An open was asked for and isn't showing yet: no commit has answered it,\n * and the event that asked isn't over.\n */\n asked: boolean;\n /** Focus is on its way back to the trigger, which isn't focus arriving. */\n restoring: boolean;\n timer?: ReturnType<typeof setTimeout>;\n /** Ends an ask to open that no commit answered, once the event that made it is over. */\n askTimer?: ReturnType<typeof setTimeout>;\n /** Takes this hook out of the ones that want Escape, while it is in. */\n releaseEscape?: () => void;\n /**\n * The document of the tap that opened it, or asked to: a press outside in\n * that document closes it while it is open.\n */\n tappedIn?: Document;\n /** The last press React brought to the holder: on the trigger or in the floating element's tree. */\n pressedInside?: Event;\n /** Takes the listener for a press outside off its document, while it is on. */\n stopPress?: () => void;\n}\n\n/**\n * Decides when a floating element that opens on hover or keyboard focus asks\n * to open and to close. It renders nothing, and leaves roles, ARIA and\n * placement to the component that calls it.\n *\n * - The pointer entering, or keyboard focus arriving on the trigger, asks to\n * open after `openDelay`. A touch pointer doesn't count. Neither does focus\n * that a press brings to the trigger: it counts when the browser shows it\n * as `:focus-visible`.\n * - Focus in the floating element holds it open however it got there,\n * including focus in content it portals elsewhere, such as a nested card.\n * Whether a click focuses what it lands on is up to the browser: Safari\n * and Firefox on macOS don't focus a button on click, so there a clicked\n * button doesn't hold it once the pointer leaves.\n * - Once the pointer and focus have both left, it asks to close after\n * `closeDelay`, and either coming back before then keeps it open. Nothing\n * else closes it on a timer.\n * - Escape closes it at once, wherever focus is, and stops there when focus\n * is in it, on its trigger or nowhere, so a `Popover` or `Modal` around it\n * keeps its own. With focus somewhere else, the key goes on after closing\n * it. Inside another one, the inner one closes first. Escape before it\n * opens takes the opening back and goes on its way. One key listener per\n * document serves every hook on it, there only while one of them is open\n * or on its way to open.\n * - With `openOnTap`, a tap on the trigger opens it at once and the next tap\n * on the trigger closes it. While a tap holds it open, a press outside the\n * trigger and the floating element closes it, from a listener on the\n * document that is there only then. Taps never count as hover, and mouse\n * and pen work the same with it or without.\n * - Once it closes, for any reason, whatever was holding it open has to\n * leave and come back to open it again.\n * - When the floating element closes with focus inside it, focus goes back\n * to the trigger, unless the same update put it somewhere else. Handing it\n * back doesn't count as focus arriving.\n *\n * Nothing it scheduled runs after the component unmounts.\n *\n * @function useHoverOpen\n * @param options - The open state, how to ask for another, the delays, and the two elements.\n * @returns Handlers for the element that holds the trigger.\n */\nexport function useHoverOpen(options: UseHoverOpenOptions): HoverOpenHandlers {\n const latestRef = useRef(options);\n const stateRef = useRef<HoverOpenState>({\n pointerIn: false,\n focusIn: false,\n focusInFloating: false,\n held: false,\n opening: false,\n asked: false,\n restoring: false,\n });\n\n // In the hooks that want Escape from the moment an open is on its way until\n // the floating element closes, and out otherwise. Stamped with the order it\n // opened in.\n const listenForEscape = useCallback(function listen() {\n const s = stateRef.current;\n const entry = entryRef.current;\n const { open, triggerRef } = latestRef.current;\n if (!open) entry.openedAt = 0;\n else if (!entry.openedAt) {\n openings += 1;\n entry.openedAt = openings;\n }\n const wanted = open || s.opening || s.asked;\n if (wanted === Boolean(s.releaseEscape)) return;\n if (s.releaseEscape) {\n s.releaseEscape();\n s.releaseEscape = undefined;\n return;\n }\n const doc = triggerRef.current?.ownerDocument ?? document;\n s.releaseEscape = wantEscape(doc, entry);\n }, []);\n\n const entryRef = useRef<EscapeEntry>({\n openedAt: 0,\n trigger: () => latestRef.current.triggerRef.current,\n floating: () => latestRef.current.floatingRef.current,\n // Only ever called while an open is on its way, so it is one or the\n // other: still waiting on its delay, or asked for already.\n cancel: () => {\n const s = stateRef.current;\n clearTimeout(s.timer);\n // An ask already made is taken back by asking the other way.\n if (s.asked) latestRef.current.onOpenChange(false);\n s.opening = false;\n s.asked = false;\n listenForEscape();\n },\n close: () => {\n clearTimeout(stateRef.current.timer);\n latestRef.current.onOpenChange(false);\n },\n });\n\n // Asks to open or to close. Escape can take an ask to open back until it is\n // answered: by the commit that follows it, or, when the caller keeps it\n // closed without rendering at all, once the event that asked is over, by\n // which time React has rendered any answer the caller set.\n const ask = useCallback(\n (next: boolean) => {\n const s = stateRef.current;\n s.asked = next;\n latestRef.current.onOpenChange(next);\n listenForEscape();\n if (!next) return;\n clearTimeout(s.askTimer);\n s.askTimer = setTimeout(() => {\n // Still closed: the caller turned it down.\n if (latestRef.current.open) return;\n s.asked = false;\n s.tappedIn = undefined;\n listenForEscape();\n });\n },\n [listenForEscape]\n );\n\n // Asks for what the pointer and focus call for, after its delay, when that\n // changed since the last time and isn't the state already.\n const settle = useCallback(() => {\n const s = stateRef.current;\n const held = s.pointerIn || s.focusIn;\n if (held !== s.held) {\n s.held = held;\n clearTimeout(s.timer);\n const { open, openDelay, closeDelay } = latestRef.current;\n s.opening = held && !open;\n if (held !== open) {\n s.timer = setTimeout(\n () => {\n s.opening = false;\n if (latestRef.current.open !== held) ask(held);\n else listenForEscape();\n },\n held ? openDelay : closeDelay\n );\n }\n }\n listenForEscape();\n }, [listenForEscape, ask]);\n\n // While a tap holds the floating element open, a press outside the trigger\n // and the floating element closes it. Set up again each time it opens or\n // closes.\n const listenForPress = useCallback(() => {\n const s = stateRef.current;\n s.stopPress?.();\n s.stopPress = undefined;\n const doc = latestRef.current.open ? s.tappedIn : undefined;\n if (!doc) return;\n const handlePointerDown = (event: Event) => {\n // Inside is a press React brought to the holder, which covers content\n // the floating element portals elsewhere, or one whose path runs\n // through the trigger or the floating element, which covers a press\n // that something inside stopped before React got it to the holder.\n const { triggerRef, floatingRef } = latestRef.current;\n if (\n event === s.pressedInside ||\n isEventInside(event, triggerRef.current) ||\n isEventInside(event, floatingRef.current)\n ) {\n return;\n }\n latestRef.current.onOpenChange(false);\n };\n doc.addEventListener('pointerdown', handlePointerDown);\n s.stopPress = () =>\n doc.removeEventListener('pointerdown', handlePointerDown);\n }, []);\n\n useClientLayoutEffect(() => {\n latestRef.current = options;\n // The commit after an ask to open answers it, open or still closed. A\n // tap that asked and still finds it closed was turned down.\n const s = stateRef.current;\n if (s.asked) {\n s.asked = false;\n if (!options.open) s.tappedIn = undefined;\n listenForEscape();\n }\n });\n\n useClientLayoutEffect(() => {\n const s = stateRef.current;\n return () => {\n clearTimeout(s.timer);\n clearTimeout(s.askTimer);\n s.opening = false;\n s.asked = false;\n s.releaseEscape?.();\n s.releaseEscape = undefined;\n s.stopPress?.();\n s.stopPress = undefined;\n };\n }, []);\n\n const { open } = options;\n // Before paint, so a pointer or a key arriving next finds this settled.\n useClientLayoutEffect(() => {\n const s = stateRef.current;\n if (!open) {\n // The floating element left from under the pointer, if it was there,\n // and no leave event says so. Focus resting on the trigger has done\n // its job too, so neither opens it again until it comes back.\n s.pointerIn = false;\n s.focusIn = false;\n s.tappedIn = undefined;\n if (s.focusInFloating) {\n s.focusInFloating = false;\n const trigger = latestRef.current.triggerRef.current;\n // Focus that went down with the floating element sits on <body>.\n // Focus somewhere else was put there by the same update, so it stays.\n if (\n trigger &&\n trigger.ownerDocument.activeElement === trigger.ownerDocument.body\n ) {\n s.restoring = true;\n trigger.focus();\n s.restoring = false;\n }\n }\n }\n settle();\n listenForPress();\n }, [open, settle, listenForPress]);\n\n return useMemo<HoverOpenHandlers>(() => {\n const inTrigger = (node: EventTarget | null) =>\n within(latestRef.current.triggerRef.current, node as Node | null);\n return {\n onPointerEnter: event => {\n if (event.pointerType === 'touch') return;\n stateRef.current.pointerIn = true;\n settle();\n },\n onPointerLeave: event => {\n // A touch pointer never set pointerIn, so its leave has nothing to\n // clear, and on a device with a mouse too it would close a card the\n // mouse is still holding open.\n if (event.pointerType === 'touch') return;\n stateRef.current.pointerIn = false;\n settle();\n },\n onFocus: event => {\n const s = stateRef.current;\n // React brings focus here from the trigger and from the floating\n // element's whole tree, portals included, so focus not in the\n // trigger is in the floating element, wherever its DOM is.\n s.focusInFloating = !inTrigger(event.target);\n if (s.restoring) return;\n // Any focus in the floating element holds it. On the trigger, only\n // keyboard focus does, so a click there opens nothing.\n s.focusIn = s.focusInFloating || isKeyboardFocus(event.target);\n settle();\n },\n onBlur: event => {\n const s = stateRef.current;\n const next = event.relatedTarget as Node | null;\n // Where focus goes next, as far as the DOM can say. Focus going into\n // content the floating element portals elsewhere reads as leaving,\n // and the focus event that follows straight after brings it back.\n s.focusInFloating = within(latestRef.current.floatingRef.current, next);\n // Moving between the trigger and the floating element isn't leaving.\n if (s.focusInFloating || within(event.currentTarget, next)) return;\n s.focusIn = false;\n settle();\n },\n onPointerDown: event => {\n stateRef.current.pressedInside = event.nativeEvent;\n },\n onPointerUp: event => {\n const now = latestRef.current;\n // With openOnTap, a tap on the trigger opens it at once, and the\n // next one closes it. A tap anywhere else does nothing here.\n if (\n !now.openOnTap ||\n event.pointerType !== 'touch' ||\n !inTrigger(event.target)\n ) {\n return;\n }\n stateRef.current.tappedIn = now.open\n ? undefined\n : (event.target as Element).ownerDocument;\n ask(!now.open);\n },\n };\n }, [settle, ask]);\n}\n"],"names":[],"mappings":";;;;;AAAA;AA+DA;;;;;;AAMG;AACH,SAAS,eAAe,CAAC,EAAW,EAAA;AAClC,IAAA,IAAI;AACF,QAAA,OAAO,EAAE,CAAC,OAAO,CAAC,gBAAgB,CAAC;IACrC;AAAE,IAAA,MAAM;AACN,QAAA,OAAO,IAAI;IACb;AACF;AAEA;AACA,MAAM,MAAM,GAAG,CAAC,SAAsB,EAAE,IAAiB,KACvD,OAAO,CAAC,SAAS,IAAI,IAAI,IAAI,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;AAcxD;;;;AAIG;AACH,MAAM,eAAe,GAAG,IAAI,GAAG,EAG5B;AAEH;AACA,IAAI,QAAQ,GAAG,CAAC;AAEhB;;;;;;;;;;;;;;;;;;;;;;AAsBG;AACH,SAAS,UAAU,CAAC,GAAa,EAAE,KAAkB,EAAA;IACnD,IAAI,QAAQ,GAAG,eAAe,CAAC,GAAG,CAAC,GAAG,CAAC;IACvC,IAAI,CAAC,QAAQ,EAAE;AACb,QAAA,MAAM,OAAO,GAAG,IAAI,GAAG,EAAe;AACtC,QAAA,MAAM,aAAa,GAAG,CAAC,KAAoB,KAAI;AAC7C,YAAA,IAAI,KAAK,CAAC,GAAG,KAAK,QAAQ;gBAAE;YAC5B,MAAM,IAAI,GAAkB,EAAE;;YAE9B,KAAK,MAAM,IAAI,IAAI,CAAC,GAAG,OAAO,CAAC,EAAE;gBAC/B,IAAI,IAAI,CAAC,QAAQ;AAAE,oBAAA,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;;oBAC7B,IAAI,CAAC,MAAM,EAAE;YACpB;AACA,YAAA,MAAM,OAAO,GAAG,uBAAuB,CAAC,GAAG,CAAC;AAC5C,YAAA,MAAM,OAAO,GAAG,IAAI,CAAC,MAAM,CACzB,IAAI,I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,CAAC;oBAAE;AAC5D,gBAAA,CAAC,CAAC,OAAO,GAAG,KAAK;AACjB,gBAAA,MAAM,EAAE;YACV,CAAC;YACD,aAAa,EAAE,KAAK,IAAG;gBACrB,QAAQ,CAAC,OAAO,CAAC,aAAa,GAAG,KAAK,CAAC,WAAW;YACpD,CAAC;YACD,WAAW,EAAE,KAAK,IAAG;AACnB,gBAAA,MAAM,GAAG,GAAG,SAAS,CAAC,OAAO;;;gBAG7B,IACE,CAAC,GAAG,CAAC,SAAS;oBACd,KAAK,CAAC,WAAW,KAAK,OAAO;AAC7B,oBAAA,CAAC,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC,EACxB;oBACA;gBACF;AACA,gBAAA,QAAQ,CAAC,OAAO,CAAC,QAAQ,GAAG,GAAG,CAAC;AAC9B,sBAAE;AACF,sBAAG,KAAK,CAAC,MAAkB,CAAC,aAAa;AAC3C,gBAAA,GAAG,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC;YAChB,CAAC;SACF;AACH,IAAA,CAAC,EAAE,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AACnB;;"}
1
+ {"version":3,"file":"useHoverOpen.js","sources":["../../../src/helpers/useHoverOpen.ts"],"sourcesContent":["// INTERNAL: deliberately not exported from src/index.ts.\nimport { useCallback, useMemo, useRef } from 'react';\nimport type { FocusEvent, PointerEvent, RefObject } from 'react';\nimport { useClientLayoutEffect } from './useClientLayoutEffect';\nimport { getDeepestActiveElement, isEventInside } from './shadowDom';\n\n/**\n * Options for `useHoverOpen`.\n */\nexport interface UseHoverOpenOptions {\n /** Whether the floating element is open now, controlled or not. */\n open: boolean;\n /**\n * Asks for the floating element to open or close: when a delay runs out,\n * and at once on Escape. It isn't asked for the state it is already in,\n * except that an Escape between asking to open and the open showing takes\n * the ask back by asking to close.\n */\n onOpenChange: (open: boolean) => void;\n /** How long hover or keyboard focus waits before asking to open, in milliseconds. */\n openDelay: number;\n /**\n * How long it waits once the pointer and focus have both left before\n * asking to close, in milliseconds. Coming back in that time keeps it open,\n * which is how the pointer crosses a gap to the floating element.\n */\n closeDelay: number;\n /**\n * The element that opens it, which has to take focus itself. The Escape\n * listener goes on its document, an iframe's when it renders into one, and\n * focus left in the floating element comes back here when the floating\n * element closes.\n */\n triggerRef: RefObject<HTMLElement | null>;\n /** The floating element, while it is open. */\n floatingRef: RefObject<HTMLElement | null>;\n /**\n * Whether a tap on the trigger opens it at once, with the next tap closing\n * it. Without it, a touch pointer never opens it. While a tap holds it\n * open, no hover timer closes it, and a press outside the trigger and the\n * floating element does, as Escape does. The trigger's own tap action\n * still runs.\n * @defaultValue false\n */\n openOnTap?: boolean;\n /**\n * Whether an Escape that closes it stops there when focus is on the\n * trigger, in the floating element or nowhere, so a `Popover` or `Modal`\n * around it keeps its own. Off, Escape still closes it and always goes on\n * to whatever has focus, as suits content that only adds to the trigger.\n * @defaultValue true\n */\n consumeEscape?: boolean;\n}\n\n/**\n * Pointer and focus handlers for the element that holds the trigger. The\n * floating element has to be inside it in React's tree, in place or through\n * a portal, since React's events reach it from there either way, and it\n * should hold nothing else: focus inside it that isn't on the trigger counts\n * as focus in the floating element.\n */\nexport interface HoverOpenHandlers {\n onPointerEnter: (event: PointerEvent<HTMLElement>) => void;\n onPointerOver: (event: PointerEvent<HTMLElement>) => void;\n onPointerLeave: (event: PointerEvent<HTMLElement>) => void;\n onFocus: (event: FocusEvent<HTMLElement>) => void;\n onBlur: (event: FocusEvent<HTMLElement>) => void;\n onPointerDown: (event: PointerEvent<HTMLElement>) => void;\n onPointerUp: (event: PointerEvent<HTMLElement>) => void;\n}\n\n/**\n * Runs the caller's own handler on the element that holds the trigger, then\n * the hook's, so neither replaces the other.\n */\nexport function chain<E>(\n own: ((event: E) => void) | undefined,\n ours: (event: E) => void\n): (event: E) => void {\n return event => {\n own?.(event);\n ours(event);\n };\n}\n\n/**\n * Whether focus that just arrived on `el` is keyboard focus, as the browser\n * decides it for `:focus-visible`: focus from a key or from the browser's\n * own controls is, focus a press brings isn't, and focus a script moves\n * follows whichever came last. A browser that can't match the selector\n * throws, and then every focus counts, so keyboard users can still open it.\n */\nfunction isKeyboardFocus(el: Element): boolean {\n try {\n return el.matches(':focus-visible');\n } catch {\n return true;\n }\n}\n\n/** Whether `node` is `container` or inside it, when there are both. */\nconst within = (container: Node | null, node: Node | null): boolean =>\n Boolean(container && node && container.contains(node));\n\n/** A hook that wants Escape: its floating element is open, or an open is on its way. */\ninterface EscapeEntry {\n /** When its floating element opened, counted across the page, or 0 while it is closed. */\n openedAt: number;\n trigger: () => HTMLElement | null;\n floating: () => HTMLElement | null;\n /** Takes back an open that is on its way: waiting on its delay, or asked for and not showing yet. */\n cancel: () => void;\n /** Asks to close. */\n close: () => void;\n /** Whether an Escape that closes it can stop there. */\n consumes: () => boolean;\n}\n\n/**\n * Per document, the hooks that want Escape and the one listener they share\n * while there are any, so a card inside another card doesn't add a second\n * listener that closes both on one keypress.\n */\nconst escapeListeners = new Map<\n Document,\n { entries: Set<EscapeEntry>; stop: () => void }\n>();\n\n/** Counts openings across the page, so the most recent one is known. */\nlet openings = 0;\n\n/**\n * Adds `entry` to the hooks that want Escape on `doc`, starting the shared\n * listener when it is the first.\n *\n * Escape takes back every open that is on its way, so nothing appears right\n * after it, and closes one open floating element, chosen by these rules in\n * turn:\n *\n * 1. One that focus is in, on its trigger or inside it, when there is one.\n * 2. The innermost: one whose floating element holds no other's trigger.\n * That follows nesting whether either one is portaled, since an inner\n * trigger sits in the outer floating element either way.\n * 3. The one that opened last.\n *\n * The keypress belongs to whatever has focus. When that is one of the open\n * floating elements or their triggers, or nothing, the listener stops it in\n * the capture phase, before anything around (a Popover, a Modal) sees it,\n * unless the one it closes was set not to consume Escape.\n * When focus is anywhere else, such as a combobox or a dialog of its own,\n * the floating element still closes, since hover put it there without\n * focus, and the key goes on to that owner.\n *\n * @returns What to call when the hook stops wanting Escape.\n */\nfunction wantEscape(doc: Document, entry: EscapeEntry): () => void {\n let listener = escapeListeners.get(doc);\n if (!listener) {\n const entries = new Set<EscapeEntry>();\n const handleKeyDown = (event: KeyboardEvent) => {\n if (event.key !== 'Escape') return;\n const open: EscapeEntry[] = [];\n // A copy, since taking an open back can take its hook out of the set.\n for (const each of [...entries]) {\n if (each.openedAt) open.push(each);\n else each.cancel();\n }\n const focused = getDeepestActiveElement(doc);\n const holding = open.filter(\n each =>\n within(each.trigger(), focused) || within(each.floating(), focused)\n );\n const pool = holding.length > 0 ? holding : open;\n let top: EscapeEntry | undefined;\n for (const each of pool) {\n const holdsAnother = pool.some(\n other => other !== each && within(each.floating(), other.trigger())\n );\n if (!holdsAnother && (!top || each.openedAt > top.openedAt)) {\n top = each;\n }\n }\n if (!top) return;\n if (\n top.consumes() &&\n (holding.length > 0 || !focused || focused === doc.body)\n ) {\n event.preventDefault();\n event.stopPropagation();\n }\n top.close();\n };\n doc.addEventListener('keydown', handleKeyDown, true);\n listener = {\n entries,\n stop: () => {\n doc.removeEventListener('keydown', handleKeyDown, true);\n escapeListeners.delete(doc);\n },\n };\n escapeListeners.set(doc, listener);\n }\n const shared = listener;\n shared.entries.add(entry);\n return () => {\n shared.entries.delete(entry);\n if (shared.entries.size === 0) shared.stop();\n };\n}\n\ninterface HoverOpenState {\n /** The pointer, other than a touch, is over the trigger or the floating element. */\n pointerIn: boolean;\n /** Focus holds it open: any focus in the floating element, or keyboard focus on the trigger. */\n focusIn: boolean;\n /** Focus, however it got there, is in the floating element. */\n focusInFloating: boolean;\n /** Whether the pointer or focus was holding it open when last settled. */\n held: boolean;\n /** An open is waiting on its delay. */\n opening: boolean;\n /**\n * An open was asked for and isn't showing yet: no commit has answered it,\n * and the event that asked isn't over.\n */\n asked: boolean;\n /** Focus is on its way back to the trigger, which isn't focus arriving. */\n restoring: boolean;\n timer?: ReturnType<typeof setTimeout>;\n /** Ends an ask to open that no commit answered, once the event that made it is over. */\n askTimer?: ReturnType<typeof setTimeout>;\n /** Takes this hook out of the ones that want Escape, while it is in. */\n releaseEscape?: () => void;\n /**\n * The document of the tap that opened it, or asked to: a press outside in\n * that document closes it while it is open.\n */\n tappedIn?: Document;\n /** The last press React brought to the holder: on the trigger or in the floating element's tree. */\n pressedInside?: Event;\n /** Takes the listener for a press outside off its document, while it is on. */\n stopPress?: () => void;\n}\n\n/**\n * Decides when a floating element that opens on hover or keyboard focus asks\n * to open and to close. It renders nothing, and leaves roles, ARIA and\n * placement to the component that calls it.\n *\n * - The pointer entering, or keyboard focus arriving on the trigger, asks to\n * open after `openDelay`. A touch pointer doesn't count. Neither does focus\n * that a press brings to the trigger: it counts when the browser shows it\n * as `:focus-visible`. The pointer arriving from outside a shadow root the\n * trigger renders into counts too, though React sends no enter for it.\n * - Focus in the floating element holds it open however it got there,\n * including focus in content it portals elsewhere, such as a nested card.\n * Whether a click focuses what it lands on is up to the browser: Safari\n * and Firefox on macOS don't focus a button on click, so there a clicked\n * button doesn't hold it once the pointer leaves.\n * - Once the pointer and focus have both left, it asks to close after\n * `closeDelay`, and either coming back before then keeps it open. Nothing\n * else closes it on a timer.\n * - Escape closes it at once, wherever focus is, and stops there when focus\n * is in it, on its trigger or nowhere, so a `Popover` or `Modal` around it\n * keeps its own. With focus somewhere else, or with `consumeEscape` off,\n * the key goes on after closing it. Inside another one, the inner one\n * closes first. Escape before it\n * opens takes the opening back and goes on its way. One key listener per\n * document serves every hook on it, there only while one of them is open\n * or on its way to open.\n * - With `openOnTap`, a tap on the trigger opens it at once and the next tap\n * on the trigger closes it. While a tap holds it open, a press outside the\n * trigger and the floating element closes it, from a listener on the\n * document that is there only then. Taps never count as hover, and mouse\n * and pen work the same with it or without.\n * - Once it closes, for any reason, whatever was holding it open has to\n * leave and come back to open it again.\n * - When the floating element closes with focus inside it, focus goes back\n * to the trigger, unless the same update put it somewhere else. Handing it\n * back doesn't count as focus arriving.\n *\n * Nothing it scheduled runs after the component unmounts.\n *\n * @function useHoverOpen\n * @param options - The open state, how to ask for another, the delays, and the two elements.\n * @returns Handlers for the element that holds the trigger.\n */\nexport function useHoverOpen(options: UseHoverOpenOptions): HoverOpenHandlers {\n const latestRef = useRef(options);\n const stateRef = useRef<HoverOpenState>({\n pointerIn: false,\n focusIn: false,\n focusInFloating: false,\n held: false,\n opening: false,\n asked: false,\n restoring: false,\n });\n\n // In the hooks that want Escape from the moment an open is on its way until\n // the floating element closes, and out otherwise. Stamped with the order it\n // opened in.\n const listenForEscape = useCallback(function listen() {\n const s = stateRef.current;\n const entry = entryRef.current;\n const { open, triggerRef } = latestRef.current;\n if (!open) entry.openedAt = 0;\n else if (!entry.openedAt) {\n openings += 1;\n entry.openedAt = openings;\n }\n const wanted = open || s.opening || s.asked;\n if (wanted === Boolean(s.releaseEscape)) return;\n if (s.releaseEscape) {\n s.releaseEscape();\n s.releaseEscape = undefined;\n return;\n }\n const doc = triggerRef.current?.ownerDocument ?? document;\n s.releaseEscape = wantEscape(doc, entry);\n }, []);\n\n const entryRef = useRef<EscapeEntry>({\n openedAt: 0,\n trigger: () => latestRef.current.triggerRef.current,\n floating: () => latestRef.current.floatingRef.current,\n // Only ever called while an open is on its way, so it is one or the\n // other: still waiting on its delay, or asked for already.\n cancel: () => {\n const s = stateRef.current;\n clearTimeout(s.timer);\n // An ask already made is taken back by asking the other way.\n if (s.asked) latestRef.current.onOpenChange(false);\n s.opening = false;\n s.asked = false;\n listenForEscape();\n },\n close: () => {\n clearTimeout(stateRef.current.timer);\n latestRef.current.onOpenChange(false);\n },\n consumes: () => latestRef.current.consumeEscape !== false,\n });\n\n // Asks to open or to close. Escape can take an ask to open back until it is\n // answered: by the commit that follows it, or, when the caller keeps it\n // closed without rendering at all, once the event that asked is over, by\n // which time React has rendered any answer the caller set.\n const ask = useCallback(\n (next: boolean) => {\n const s = stateRef.current;\n s.asked = next;\n latestRef.current.onOpenChange(next);\n listenForEscape();\n if (!next) return;\n clearTimeout(s.askTimer);\n s.askTimer = setTimeout(() => {\n // Still closed: the caller turned it down.\n if (latestRef.current.open) return;\n s.asked = false;\n s.tappedIn = undefined;\n listenForEscape();\n });\n },\n [listenForEscape]\n );\n\n // Asks for what the pointer and focus call for, after its delay, when that\n // changed since the last time and isn't the state already.\n const settle = useCallback(() => {\n const s = stateRef.current;\n const held = s.pointerIn || s.focusIn;\n if (held !== s.held) {\n s.held = held;\n clearTimeout(s.timer);\n const { open, openDelay, closeDelay } = latestRef.current;\n s.opening = held && !open;\n if (held !== open) {\n s.timer = setTimeout(\n () => {\n s.opening = false;\n if (latestRef.current.open !== held) ask(held);\n else listenForEscape();\n },\n held ? openDelay : closeDelay\n );\n }\n }\n listenForEscape();\n }, [listenForEscape, ask]);\n\n // While a tap holds the floating element open, a press outside the trigger\n // and the floating element closes it. Set up again each time it opens or\n // closes.\n const listenForPress = useCallback(() => {\n const s = stateRef.current;\n s.stopPress?.();\n s.stopPress = undefined;\n const doc = latestRef.current.open ? s.tappedIn : undefined;\n if (!doc) return;\n const handlePointerDown = (event: Event) => {\n // Inside is a press React brought to the holder, which covers content\n // the floating element portals elsewhere, or one whose path runs\n // through the trigger or the floating element, which covers a press\n // that something inside stopped before React got it to the holder.\n const { triggerRef, floatingRef } = latestRef.current;\n if (\n event === s.pressedInside ||\n isEventInside(event, triggerRef.current) ||\n isEventInside(event, floatingRef.current)\n ) {\n return;\n }\n latestRef.current.onOpenChange(false);\n };\n doc.addEventListener('pointerdown', handlePointerDown);\n s.stopPress = () =>\n doc.removeEventListener('pointerdown', handlePointerDown);\n }, []);\n\n useClientLayoutEffect(() => {\n latestRef.current = options;\n // The commit after an ask to open answers it, open or still closed. A\n // tap that asked and still finds it closed was turned down.\n const s = stateRef.current;\n if (s.asked) {\n s.asked = false;\n if (!options.open) s.tappedIn = undefined;\n listenForEscape();\n }\n });\n\n useClientLayoutEffect(() => {\n const s = stateRef.current;\n return () => {\n clearTimeout(s.timer);\n clearTimeout(s.askTimer);\n s.opening = false;\n s.asked = false;\n s.releaseEscape?.();\n s.releaseEscape = undefined;\n s.stopPress?.();\n s.stopPress = undefined;\n };\n }, []);\n\n const { open } = options;\n // Before paint, so a pointer or a key arriving next finds this settled.\n useClientLayoutEffect(() => {\n const s = stateRef.current;\n if (!open) {\n // The floating element left from under the pointer, if it was there,\n // and no leave event says so. Focus resting on the trigger has done\n // its job too, so neither opens it again until it comes back.\n s.pointerIn = false;\n s.focusIn = false;\n s.tappedIn = undefined;\n if (s.focusInFloating) {\n s.focusInFloating = false;\n const trigger = latestRef.current.triggerRef.current;\n // Focus that went down with the floating element sits on <body>.\n // Focus somewhere else was put there by the same update, so it stays.\n if (\n trigger &&\n trigger.ownerDocument.activeElement === trigger.ownerDocument.body\n ) {\n s.restoring = true;\n trigger.focus();\n s.restoring = false;\n }\n }\n }\n settle();\n listenForPress();\n }, [open, settle, listenForPress]);\n\n return useMemo<HoverOpenHandlers>(() => {\n const inTrigger = (node: EventTarget | null) =>\n within(latestRef.current.triggerRef.current, node as Node | null);\n return {\n onPointerEnter: event => {\n if (event.pointerType === 'touch') return;\n stateRef.current.pointerIn = true;\n settle();\n },\n onPointerOver: event => {\n // React makes the enter out of the pointerout on the element the\n // pointer left, and skips this pointerover when that element is its\n // own. Leaving page content for a trigger inside a shadow root, that\n // pointerout names only the shadow host, so no enter reaches here.\n // A pointerover from outside the holder and the floating element is\n // the pointer arriving, then. One from inside them is the pointer\n // moving about within, which doesn't bring back a closed one.\n const s = stateRef.current;\n if (s.pointerIn || event.pointerType === 'touch') return;\n const from = event.relatedTarget as Node | null;\n if (\n within(event.currentTarget, from) ||\n within(latestRef.current.floatingRef.current, from)\n ) {\n return;\n }\n s.pointerIn = true;\n settle();\n },\n onPointerLeave: event => {\n // A touch pointer never set pointerIn, so its leave has nothing to\n // clear, and on a device with a mouse too it would close a card the\n // mouse is still holding open.\n if (event.pointerType === 'touch') return;\n stateRef.current.pointerIn = false;\n settle();\n },\n onFocus: event => {\n const s = stateRef.current;\n // React brings focus here from the trigger and from the floating\n // element's whole tree, portals included, so focus not in the\n // trigger is in the floating element, wherever its DOM is.\n s.focusInFloating = !inTrigger(event.target);\n if (s.restoring) return;\n // Any focus in the floating element holds it. On the trigger, only\n // keyboard focus does, so a click there opens nothing.\n s.focusIn = s.focusInFloating || isKeyboardFocus(event.target);\n settle();\n },\n onBlur: event => {\n const s = stateRef.current;\n const next = event.relatedTarget as Node | null;\n // Where focus goes next, as far as the DOM can say. Focus going into\n // content the floating element portals elsewhere reads as leaving,\n // and the focus event that follows straight after brings it back.\n s.focusInFloating = within(latestRef.current.floatingRef.current, next);\n // Moving between the trigger and the floating element isn't leaving.\n if (s.focusInFloating || within(event.currentTarget, next)) return;\n s.focusIn = false;\n settle();\n },\n onPointerDown: event => {\n stateRef.current.pressedInside = event.nativeEvent;\n },\n onPointerUp: event => {\n const now = latestRef.current;\n // With openOnTap, a tap on the trigger opens it at once, and the\n // next one closes it. A tap anywhere else does nothing here.\n if (\n !now.openOnTap ||\n event.pointerType !== 'touch' ||\n !inTrigger(event.target)\n ) {\n return;\n }\n stateRef.current.tappedIn = now.open\n ? undefined\n : (event.target as Element).ownerDocument;\n ask(!now.open);\n },\n };\n }, [settle, ask]);\n}\n"],"names":["chain"],"mappings":";;;;;AAAA;AAwEA;;;AAGG;AACG,SAAUA,OAAK,CACnB,GAAqC,EACrC,IAAwB,EAAA;IAExB,OAAO,KAAK,IAAG;AACb,QAAA,GAAG,GAAG,KAAK,CAAC;QACZ,IAAI,CAAC,KAAK,CAAC;AACb,IAAA,CAAC;AACH;AAEA;;;;;;AAMG;AACH,SAAS,eAAe,CAAC,EAAW,EAAA;AAClC,IAAA,IAAI;AACF,QAAA,OAAO,EAAE,CAAC,OAAO,CAAC,gBAAgB,CAAC;IACrC;AAAE,IAAA,MAAM;AACN,QAAA,OAAO,IAAI;IACb;AACF;AAEA;AACA,MAAM,MAAM,GAAG,CAAC,SAAsB,EAAE,IAAiB,KACvD,OAAO,CAAC,SAAS,IAAI,IAAI,IAAI,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;AAgBxD;;;;AAIG;AACH,MAAM,eAAe,GAAG,IAAI,GAAG,EAG5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IAAI,MAAM,CAAC,KAAK,CAAC,aAAa,EAAE,IAAI,CAAC;oBAAE;AAC5D,gBAAA,CAAC,CAAC,OAAO,GAAG,KAAK;AACjB,gBAAA,MAAM,EAAE;YACV,CAAC;YACD,aAAa,EAAE,KAAK,IAAG;gBACrB,QAAQ,CAAC,OAAO,CAAC,aAAa,GAAG,KAAK,CAAC,WAAW;YACpD,CAAC;YACD,WAAW,EAAE,KAAK,IAAG;AACnB,gBAAA,MAAM,GAAG,GAAG,SAAS,CAAC,OAAO;;;gBAG7B,IACE,CAAC,GAAG,CAAC,SAAS;oBACd,KAAK,CAAC,WAAW,KAAK,OAAO;AAC7B,oBAAA,CAAC,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC,EACxB;oBACA;gBACF;AACA,gBAAA,QAAQ,CAAC,OAAO,CAAC,QAAQ,GAAG,GAAG,CAAC;AAC9B,sBAAE;AACF,sBAAG,KAAK,CAAC,MAAkB,CAAC,aAAa;AAC3C,gBAAA,GAAG,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC;YAChB,CAAC;SACF;AACH,IAAA,CAAC,EAAE,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AACnB;;"}
@@ -0,0 +1,182 @@
1
+ /* @allxsmith/bestax-bulma — AI agents: see AGENTS.md in the package root, or https://bestax.io/llms.txt */
2
+ import { useRef, useEffect, useMemo } from 'react';
3
+ import { getActiveElementInTree } from './shadowDom.js';
4
+
5
+ // INTERNAL: deliberately not exported from src/index.ts.
6
+ /**
7
+ * Every menu-item role, so an item a caller gives `menuitemcheckbox` or
8
+ * `menuitemradio` (a filter or sort menu) stays reachable.
9
+ */
10
+ const MENU_ITEM_SELECTOR = '[role="menuitem"], [role="menuitemcheckbox"], [role="menuitemradio"]';
11
+ /**
12
+ * Whether a menu item can be reached and activated: it isn't disabled, as a
13
+ * native control or through `aria-disabled="true"`.
14
+ */
15
+ function isEnabledMenuItem(item) {
16
+ return (!item.hasAttribute('disabled') &&
17
+ item.getAttribute('aria-disabled') !== 'true');
18
+ }
19
+ /** Whether `node` is one of `menu`'s items, enabled or not. */
20
+ const isItemOf = (menu, node) => Boolean(node && menu.contains(node) && node.matches(MENU_ITEM_SELECTOR));
21
+ /** The items in `menu` the keys reach, in document order. */
22
+ function enabledMenuItems(menu) {
23
+ return Array.from(menu.querySelectorAll(MENU_ITEM_SELECTOR)).filter(isEnabledMenuItem);
24
+ }
25
+ /** How long typed characters keep adding up to one search, in milliseconds. */
26
+ const TYPEAHEAD_TIMEOUT = 500;
27
+ /**
28
+ * Whether the browser activates `item` on this key by itself: a `<button>`
29
+ * answers Enter and Space, and a link with an `href` answers Enter.
30
+ */
31
+ function activatesItself(item, key) {
32
+ if (item.tagName === 'BUTTON')
33
+ return true;
34
+ return key === 'Enter' && item.tagName === 'A' && item.hasAttribute('href');
35
+ }
36
+ /**
37
+ * The text typing finds an item by: what it says, without what's hidden from
38
+ * assistive technology, such as an icon whose glyph is a ligature's name.
39
+ */
40
+ function label(item) {
41
+ let text = '';
42
+ const read = (node) => {
43
+ node.childNodes.forEach(child => {
44
+ if (child.nodeType === 3)
45
+ text += child.textContent;
46
+ else if (child.nodeType === 1 &&
47
+ child.getAttribute('aria-hidden') !== 'true') {
48
+ read(child);
49
+ }
50
+ });
51
+ };
52
+ read(item);
53
+ return text.trim().toLowerCase();
54
+ }
55
+ /**
56
+ * The item typing `search` moves to, or none. A single character looks from
57
+ * the item after the focused one, so typing the same letter again cycles
58
+ * through the items that start with it, and so does a run of one letter
59
+ * ("dd"). A longer search looks from the focused item, so typing more of
60
+ * its own text keeps it.
61
+ */
62
+ function typeaheadMatch(items, search, current) {
63
+ const repeated = Array.from(search).every(char => char === search[0]);
64
+ const wanted = repeated ? search[0] : search;
65
+ const start = current < 0 ? 0 : current + (wanted.length === 1 ? 1 : 0);
66
+ for (let i = 0; i < items.length; i += 1) {
67
+ const item = items[(start + i) % items.length];
68
+ if (label(item).startsWith(wanted))
69
+ return item;
70
+ }
71
+ return undefined;
72
+ }
73
+ /**
74
+ * Moves through and activates the items of a `role="menu"` element from the
75
+ * keyboard. Put the `onKeyDown` it returns on that element. It reads the
76
+ * items from the element each time, so items that come and go are found,
77
+ * and it renders nothing.
78
+ *
79
+ * It acts only while focus is on the menu element itself or on one of its
80
+ * items, so anything else focusable inside the menu, such as a filter
81
+ * field, keeps its own keys.
82
+ *
83
+ * - Down and Up move to the next and previous enabled item, wrapping, and
84
+ * Home and End go to the first and last. From no item, Down goes to the
85
+ * first and Up to the last.
86
+ * - Enter and Space activate the focused item. A `<button>`, and a link with
87
+ * an `href` for Enter, get the browser's own click, and anything else gets
88
+ * one from here, so its `onClick` runs either way. A key held down
89
+ * activates it once. A key handler on the item that called
90
+ * `preventDefault()` has claimed the key, and nothing else runs it.
91
+ * - A printable character moves to the next enabled item whose text starts
92
+ * with what has been typed. Characters typed within `TYPEAHEAD_TIMEOUT` of
93
+ * each other add up to one search, until `reset` starts a new one.
94
+ *
95
+ * Items are `MENU_ITEM_SELECTOR` matches, and disabled ones are skipped by
96
+ * all of the above. Escape and Tab are left to the menu's owner, since what
97
+ * closing means is up to it, and so is any other key.
98
+ *
99
+ * @function useMenuKeys
100
+ * @returns The `keydown` handler for the menu element, and a way to forget
101
+ * what has been typed.
102
+ */
103
+ function useMenuKeys() {
104
+ const typedRef = useRef({ text: '' });
105
+ useEffect(() => {
106
+ const typed = typedRef.current;
107
+ return () => clearTimeout(typed.timer);
108
+ }, []);
109
+ return useMemo(() => {
110
+ const reset = () => {
111
+ const typed = typedRef.current;
112
+ clearTimeout(typed.timer);
113
+ typed.text = '';
114
+ };
115
+ const onKeyDown = (event) => {
116
+ const menu = event.currentTarget;
117
+ const focused = getActiveElementInTree(menu);
118
+ if (focused !== menu && !isItemOf(menu, focused))
119
+ return;
120
+ const items = enabledMenuItems(menu);
121
+ if (!items.length)
122
+ return;
123
+ const current = items.indexOf(focused);
124
+ switch (event.key) {
125
+ case 'ArrowDown':
126
+ event.preventDefault();
127
+ items[current >= 0 ? (current + 1) % items.length : 0].focus();
128
+ return;
129
+ case 'ArrowUp':
130
+ event.preventDefault();
131
+ items[current >= 0
132
+ ? (current - 1 + items.length) % items.length
133
+ : items.length - 1].focus();
134
+ return;
135
+ case 'Home':
136
+ event.preventDefault();
137
+ items[0].focus();
138
+ return;
139
+ case 'End':
140
+ event.preventDefault();
141
+ items[items.length - 1].focus();
142
+ return;
143
+ case 'Enter':
144
+ case ' ': {
145
+ if (current < 0 || event.defaultPrevented)
146
+ return;
147
+ const item = items[current];
148
+ if (activatesItself(item, event.key)) {
149
+ // The browser clicks on Enter's keydown, and a held key sends one
150
+ // per auto-repeat. Space clicks on keyup, once however long it's
151
+ // held.
152
+ if (event.repeat && event.key === 'Enter')
153
+ event.preventDefault();
154
+ return;
155
+ }
156
+ // Space would scroll the page, and a held key repeats.
157
+ event.preventDefault();
158
+ if (!event.repeat)
159
+ item.click();
160
+ return;
161
+ }
162
+ }
163
+ if (event.key.length !== 1 ||
164
+ event.ctrlKey ||
165
+ event.metaKey ||
166
+ event.altKey ||
167
+ event.defaultPrevented) {
168
+ return;
169
+ }
170
+ event.preventDefault();
171
+ const typed = typedRef.current;
172
+ typed.text += event.key.toLowerCase();
173
+ clearTimeout(typed.timer);
174
+ typed.timer = setTimeout(reset, TYPEAHEAD_TIMEOUT);
175
+ typeaheadMatch(items, typed.text, current)?.focus();
176
+ };
177
+ return { onKeyDown, reset };
178
+ }, []);
179
+ }
180
+
181
+ export { MENU_ITEM_SELECTOR, TYPEAHEAD_TIMEOUT, enabledMenuItems, isEnabledMenuItem, useMenuKeys };
182
+ //# sourceMappingURL=useMenuKeys.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useMenuKeys.js","sources":["../../../src/helpers/useMenuKeys.ts"],"sourcesContent":["// INTERNAL: deliberately not exported from src/index.ts.\nimport { useEffect, useMemo, useRef } from 'react';\nimport type { KeyboardEvent } from 'react';\nimport { getActiveElementInTree } from './shadowDom';\n\n/**\n * Every menu-item role, so an item a caller gives `menuitemcheckbox` or\n * `menuitemradio` (a filter or sort menu) stays reachable.\n */\nexport const MENU_ITEM_SELECTOR =\n '[role=\"menuitem\"], [role=\"menuitemcheckbox\"], [role=\"menuitemradio\"]';\n\n/**\n * Whether a menu item can be reached and activated: it isn't disabled, as a\n * native control or through `aria-disabled=\"true\"`.\n */\nexport function isEnabledMenuItem(item: Element): boolean {\n return (\n !item.hasAttribute('disabled') &&\n item.getAttribute('aria-disabled') !== 'true'\n );\n}\n\n/** Whether `node` is one of `menu`'s items, enabled or not. */\nconst isItemOf = (menu: Element, node: Element | null): boolean =>\n Boolean(node && menu.contains(node) && node.matches(MENU_ITEM_SELECTOR));\n\n/** The items in `menu` the keys reach, in document order. */\nexport function enabledMenuItems(menu: Element): HTMLElement[] {\n return Array.from(\n menu.querySelectorAll<HTMLElement>(MENU_ITEM_SELECTOR)\n ).filter(isEnabledMenuItem);\n}\n\n/** How long typed characters keep adding up to one search, in milliseconds. */\nexport const TYPEAHEAD_TIMEOUT = 500;\n\n/**\n * Whether the browser activates `item` on this key by itself: a `<button>`\n * answers Enter and Space, and a link with an `href` answers Enter.\n */\nfunction activatesItself(item: HTMLElement, key: string): boolean {\n if (item.tagName === 'BUTTON') return true;\n return key === 'Enter' && item.tagName === 'A' && item.hasAttribute('href');\n}\n\n/**\n * The text typing finds an item by: what it says, without what's hidden from\n * assistive technology, such as an icon whose glyph is a ligature's name.\n */\nfunction label(item: Element): string {\n let text = '';\n const read = (node: Node) => {\n node.childNodes.forEach(child => {\n if (child.nodeType === 3) text += child.textContent;\n else if (\n child.nodeType === 1 &&\n (child as Element).getAttribute('aria-hidden') !== 'true'\n ) {\n read(child);\n }\n });\n };\n read(item);\n return text.trim().toLowerCase();\n}\n\n/**\n * The item typing `search` moves to, or none. A single character looks from\n * the item after the focused one, so typing the same letter again cycles\n * through the items that start with it, and so does a run of one letter\n * (\"dd\"). A longer search looks from the focused item, so typing more of\n * its own text keeps it.\n */\nfunction typeaheadMatch(\n items: HTMLElement[],\n search: string,\n current: number\n): HTMLElement | undefined {\n const repeated = Array.from(search).every(char => char === search[0]);\n const wanted = repeated ? search[0] : search;\n const start = current < 0 ? 0 : current + (wanted.length === 1 ? 1 : 0);\n for (let i = 0; i < items.length; i += 1) {\n const item = items[(start + i) % items.length];\n if (label(item).startsWith(wanted)) return item;\n }\n return undefined;\n}\n\n/** What `useMenuKeys` returns. */\nexport interface MenuKeys {\n /** The `keydown` handler for the `role=\"menu\"` element. */\n onKeyDown: (event: KeyboardEvent<HTMLElement>) => void;\n /**\n * Forgets what has been typed, so the next character starts a new search.\n * Call it when the menu opens, so a search typed just before it last\n * closed doesn't carry over.\n */\n reset: () => void;\n}\n\n/**\n * Moves through and activates the items of a `role=\"menu\"` element from the\n * keyboard. Put the `onKeyDown` it returns on that element. It reads the\n * items from the element each time, so items that come and go are found,\n * and it renders nothing.\n *\n * It acts only while focus is on the menu element itself or on one of its\n * items, so anything else focusable inside the menu, such as a filter\n * field, keeps its own keys.\n *\n * - Down and Up move to the next and previous enabled item, wrapping, and\n * Home and End go to the first and last. From no item, Down goes to the\n * first and Up to the last.\n * - Enter and Space activate the focused item. A `<button>`, and a link with\n * an `href` for Enter, get the browser's own click, and anything else gets\n * one from here, so its `onClick` runs either way. A key held down\n * activates it once. A key handler on the item that called\n * `preventDefault()` has claimed the key, and nothing else runs it.\n * - A printable character moves to the next enabled item whose text starts\n * with what has been typed. Characters typed within `TYPEAHEAD_TIMEOUT` of\n * each other add up to one search, until `reset` starts a new one.\n *\n * Items are `MENU_ITEM_SELECTOR` matches, and disabled ones are skipped by\n * all of the above. Escape and Tab are left to the menu's owner, since what\n * closing means is up to it, and so is any other key.\n *\n * @function useMenuKeys\n * @returns The `keydown` handler for the menu element, and a way to forget\n * what has been typed.\n */\nexport function useMenuKeys(): MenuKeys {\n const typedRef = useRef<{\n text: string;\n timer?: ReturnType<typeof setTimeout>;\n }>({ text: '' });\n\n useEffect(() => {\n const typed = typedRef.current;\n return () => clearTimeout(typed.timer);\n }, []);\n\n return useMemo<MenuKeys>(() => {\n const reset = () => {\n const typed = typedRef.current;\n clearTimeout(typed.timer);\n typed.text = '';\n };\n const onKeyDown = (event: KeyboardEvent<HTMLElement>) => {\n const menu = event.currentTarget;\n const focused = getActiveElementInTree(menu);\n if (focused !== menu && !isItemOf(menu, focused)) return;\n const items = enabledMenuItems(menu);\n if (!items.length) return;\n const current = items.indexOf(focused as HTMLElement);\n switch (event.key) {\n case 'ArrowDown':\n event.preventDefault();\n items[current >= 0 ? (current + 1) % items.length : 0].focus();\n return;\n case 'ArrowUp':\n event.preventDefault();\n items[\n current >= 0\n ? (current - 1 + items.length) % items.length\n : items.length - 1\n ].focus();\n return;\n case 'Home':\n event.preventDefault();\n items[0].focus();\n return;\n case 'End':\n event.preventDefault();\n items[items.length - 1].focus();\n return;\n case 'Enter':\n case ' ': {\n if (current < 0 || event.defaultPrevented) return;\n const item = items[current];\n if (activatesItself(item, event.key)) {\n // The browser clicks on Enter's keydown, and a held key sends one\n // per auto-repeat. Space clicks on keyup, once however long it's\n // held.\n if (event.repeat && event.key === 'Enter') event.preventDefault();\n return;\n }\n // Space would scroll the page, and a held key repeats.\n event.preventDefault();\n if (!event.repeat) item.click();\n return;\n }\n default:\n break;\n }\n\n if (\n event.key.length !== 1 ||\n event.ctrlKey ||\n event.metaKey ||\n event.altKey ||\n event.defaultPrevented\n ) {\n return;\n }\n event.preventDefault();\n const typed = typedRef.current;\n typed.text += event.key.toLowerCase();\n clearTimeout(typed.timer);\n typed.timer = setTimeout(reset, TYPEAHEAD_TIMEOUT);\n typeaheadMatch(items, typed.text, current)?.focus();\n };\n return { onKeyDown, reset };\n }, []);\n}\n"],"names":[],"mappings":";;;;AAAA;AAKA;;;AAGG;AACI,MAAM,kBAAkB,GAC7B;AAEF;;;AAGG;AACG,SAAU,iBAAiB,CAAC,IAAa,EAAA;AAC7C,IAAA,QACE,CAAC,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC;QAC9B,IAAI,CAAC,Y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