@funstack/ui 0.1.0-alpha.7 → 0.1.0-alpha.9
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.
- package/dist/combobox/combobox-popup.mjs +1 -1
- package/dist/combobox/combobox-popup.mjs.map +1 -1
- package/dist/csp/csp-provider.d.mts +25 -0
- package/dist/csp/csp-provider.d.mts.map +1 -0
- package/dist/csp/csp-provider.mjs +24 -0
- package/dist/csp/csp-provider.mjs.map +1 -0
- package/dist/csp/index.d.mts +3 -0
- package/dist/csp/index.mjs +3 -0
- package/dist/csp/style-hashes.d.mts +19 -0
- package/dist/csp/style-hashes.d.mts.map +1 -0
- package/dist/csp/style-hashes.mjs +21 -0
- package/dist/csp/style-hashes.mjs.map +1 -0
- package/dist/index.d.mts +7 -3
- package/dist/index.mjs +3 -1
- package/dist/internal/anchor-position.d.mts +11 -3
- package/dist/internal/anchor-position.d.mts.map +1 -1
- package/dist/internal/anchor-position.mjs +180 -6
- package/dist/internal/anchor-position.mjs.map +1 -1
- package/dist/internal/csp-context.mjs +8 -0
- package/dist/internal/csp-context.mjs.map +1 -0
- package/dist/internal/tooltip-warm.mjs +29 -0
- package/dist/internal/tooltip-warm.mjs.map +1 -0
- package/dist/internal/use-popover-popup.mjs +22 -4
- package/dist/internal/use-popover-popup.mjs.map +1 -1
- package/dist/internal/use-resolved-placement.mjs +146 -0
- package/dist/internal/use-resolved-placement.mjs.map +1 -0
- package/dist/menu/index.d.mts +5 -1
- package/dist/menu/index.d.mts.map +1 -1
- package/dist/menu/index.mjs +5 -1
- package/dist/menu/index.mjs.map +1 -1
- package/dist/menu/menu-context-trigger.d.mts +46 -0
- package/dist/menu/menu-context-trigger.d.mts.map +1 -0
- package/dist/menu/menu-context-trigger.mjs +405 -0
- package/dist/menu/menu-context-trigger.mjs.map +1 -0
- package/dist/menu/menu-context.mjs +44 -1
- package/dist/menu/menu-context.mjs.map +1 -1
- package/dist/menu/menu-dom.mjs +42 -2
- package/dist/menu/menu-dom.mjs.map +1 -1
- package/dist/menu/menu-popup.d.mts +36 -8
- package/dist/menu/menu-popup.d.mts.map +1 -1
- package/dist/menu/menu-popup.mjs +214 -22
- package/dist/menu/menu-popup.mjs.map +1 -1
- package/dist/menu/menu-root.d.mts +5 -0
- package/dist/menu/menu-root.d.mts.map +1 -1
- package/dist/menu/menu-root.mjs +56 -6
- package/dist/menu/menu-root.mjs.map +1 -1
- package/dist/menu/menu-submenu-trigger.d.mts.map +1 -1
- package/dist/menu/menu-submenu-trigger.mjs +27 -11
- package/dist/menu/menu-submenu-trigger.mjs.map +1 -1
- package/dist/popover/popover-popup.d.mts +21 -7
- package/dist/popover/popover-popup.d.mts.map +1 -1
- package/dist/popover/popover-popup.mjs +16 -6
- package/dist/popover/popover-popup.mjs.map +1 -1
- package/dist/tooltip/index.d.mts +8 -5
- package/dist/tooltip/index.d.mts.map +1 -1
- package/dist/tooltip/index.mjs +6 -3
- package/dist/tooltip/index.mjs.map +1 -1
- package/dist/tooltip/tooltip-arrow.d.mts +30 -0
- package/dist/tooltip/tooltip-arrow.d.mts.map +1 -0
- package/dist/tooltip/tooltip-arrow.mjs +45 -0
- package/dist/tooltip/tooltip-arrow.mjs.map +1 -0
- package/dist/tooltip/tooltip-context.d.mts +7 -0
- package/dist/tooltip/tooltip-context.d.mts.map +1 -1
- package/dist/tooltip/tooltip-context.mjs +2 -1
- package/dist/tooltip/tooltip-context.mjs.map +1 -1
- package/dist/tooltip/tooltip-popup.d.mts +21 -6
- package/dist/tooltip/tooltip-popup.d.mts.map +1 -1
- package/dist/tooltip/tooltip-popup.mjs +28 -6
- package/dist/tooltip/tooltip-popup.mjs.map +1 -1
- package/dist/tooltip/tooltip-trigger.d.mts +9 -2
- package/dist/tooltip/tooltip-trigger.d.mts.map +1 -1
- package/dist/tooltip/tooltip-trigger.mjs +21 -5
- package/dist/tooltip/tooltip-trigger.mjs.map +1 -1
- package/dist/tooltip/use-tooltip-interaction.mjs +26 -2
- package/dist/tooltip/use-tooltip-interaction.mjs.map +1 -1
- package/package.json +2 -1
|
@@ -54,6 +54,7 @@ function usePopoverPopup(ctx, options) {
|
|
|
54
54
|
const isLibrary = ctx.mode === "library";
|
|
55
55
|
const { popupRef, triggerRef } = ctx;
|
|
56
56
|
const opening = useRef(false);
|
|
57
|
+
const reshowing = useRef(false);
|
|
57
58
|
const awaitingClose = useRef(false);
|
|
58
59
|
const focusWasInside = useRef(false);
|
|
59
60
|
const warnedFocus = useRef(false);
|
|
@@ -105,14 +106,30 @@ function usePopoverPopup(ctx, options) {
|
|
|
105
106
|
if (added) target.setAttribute("autofocus", "");
|
|
106
107
|
opening.current = true;
|
|
107
108
|
try {
|
|
108
|
-
const source = triggerRef.current;
|
|
109
|
-
element.showPopover(source
|
|
109
|
+
const source = options.source !== void 0 ? options.source() : triggerRef.current;
|
|
110
|
+
element.showPopover(source != null && source.isConnected ? { source } : void 0);
|
|
110
111
|
} finally {
|
|
111
112
|
opening.current = false;
|
|
112
113
|
if (added) target.removeAttribute("autofocus");
|
|
113
114
|
}
|
|
114
115
|
if (quiet && isPopoverOpen(element) && element.contains(target)) target.focus({ focusVisible: false });
|
|
115
116
|
};
|
|
117
|
+
const reshow = () => {
|
|
118
|
+
const element = popupRef.current;
|
|
119
|
+
if (!isLibrary || element === null || !isPopoverOpen(element)) return false;
|
|
120
|
+
opening.current = true;
|
|
121
|
+
reshowing.current = true;
|
|
122
|
+
try {
|
|
123
|
+
element.hidePopover();
|
|
124
|
+
showPopup(element);
|
|
125
|
+
} finally {
|
|
126
|
+
opening.current = false;
|
|
127
|
+
reshowing.current = false;
|
|
128
|
+
}
|
|
129
|
+
awaitingClose.current = false;
|
|
130
|
+
focusWasInside.current = false;
|
|
131
|
+
return isPopoverOpen(element);
|
|
132
|
+
};
|
|
116
133
|
const reconcileOnMount = useEffectEvent(() => {
|
|
117
134
|
const element = popupRef.current;
|
|
118
135
|
if (element === null) return;
|
|
@@ -150,7 +167,7 @@ function usePopoverPopup(ctx, options) {
|
|
|
150
167
|
if (isLibrary && !opening.current) {
|
|
151
168
|
event.preventDefault();
|
|
152
169
|
ctx.requestOpen(event, "activate");
|
|
153
|
-
} else ctx.nextOpenGeneration();
|
|
170
|
+
} else if (!reshowing.current) ctx.nextOpenGeneration();
|
|
154
171
|
contentProbe.forget();
|
|
155
172
|
} else {
|
|
156
173
|
awaitingClose.current = true;
|
|
@@ -252,7 +269,8 @@ function usePopoverPopup(ctx, options) {
|
|
|
252
269
|
return {
|
|
253
270
|
setElement,
|
|
254
271
|
visible: ctx.committed || exit.held,
|
|
255
|
-
wrapContent
|
|
272
|
+
wrapContent,
|
|
273
|
+
reshow
|
|
256
274
|
};
|
|
257
275
|
}
|
|
258
276
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-popover-popup.mjs","names":[],"sources":["../../src/internal/use-popover-popup.ts"],"sourcesContent":["\"use client\";\nimport {\n useCallback,\n useEffect,\n useEffectEvent,\n useInsertionEffect,\n useLayoutEffect,\n useRef,\n useState,\n} from \"react\";\nimport type { ReactNode, RefObject, ViewTransitionClass } from \"react\";\nimport { useContentFocusProbe } from \"./content-focus-probe.js\";\nimport { useExitAnimation } from \"./use-exit-animation.js\";\nimport type { PopoverRootValue } from \"./use-popover-root.js\";\n\nexport interface UsePopoverPopupOptions {\n /** Public name of the part for development warnings (`\"Popover.Popup\"`). */\n readonly part: string;\n /** Element to focus on open: a ref, or a selector resolved inside the popup (design 6.1). */\n readonly initialFocus: RefObject<HTMLElement | null> | string | undefined;\n /**\n * Replaces `initialFocus` with a function called right before each library open (Menu: the\n * first or last item by open cause). `null` leaves the choice to the platform. A\n * `QuietFocusTarget` asks for focus without the focus ring (a pointer-opened menu).\n */\n readonly resolveInitialFocus?: (popup: HTMLElement) => HTMLElement | QuietFocusTarget | null;\n readonly keepMounted: boolean | undefined;\n /** The consumer's `transition`; decides how a library-driven close keeps its content (4.4). */\n readonly transition: ViewTransitionClass | undefined;\n /** Runs after the library's own `beforetoggle` handling (Menu: focus bookkeeping). */\n readonly onBeforeToggle?: (event: ToggleEvent) => void;\n /** Runs after the library's own `toggle` handling (Menu: close the stack on Tab-out). */\n readonly onToggle?: (event: ToggleEvent) => void;\n}\n\n/**\n * An initial focus target opened by a pointer: where the engine reads `focus({ focusVisible })`,\n * the library focuses it with `focusVisible: false` right after `showPopover()` instead of\n * writing `autofocus` (WebKit shows the ring for an `autofocus` target after a click; review H1).\n * Elsewhere it is an ordinary target.\n */\nexport interface QuietFocusTarget {\n readonly element: HTMLElement;\n readonly focusVisible: false;\n}\n\nexport interface PopoverPopupHook {\n /** Callback ref for the popup element; pass as `useRender`'s `ref`. */\n readonly setElement: (element: HTMLElement) => () => void;\n /**\n * Whether the children are shown: committed open, or held while the hide animates (4.4).\n * Library mode only; JS-free mode always renders them.\n */\n readonly visible: boolean;\n /**\n * Wraps the library-mode content: call it only while `visible` (`visible ? wrapContent(x) :\n * null`), or inside the `<Activity>` for `keepMounted`. When a close commit unmounts the\n * content while it holds focus (a library-driven close with `transition`, where the focused\n * element is gone before `hidePopover()` runs and the platform restores nothing), the hide\n * that follows returns focus to the Trigger (design 6.2).\n */\n readonly wrapContent: (children: ReactNode) => ReactNode;\n}\n\nconst isDev = process.env.NODE_ENV !== \"production\";\n\nfunction isPopoverOpen(element: Element): boolean {\n try {\n return element.matches(\":popover-open\");\n } catch {\n return false;\n }\n}\n\nlet focusVisibleOption: boolean | undefined;\n\n/**\n * The engine reads the `focusVisible` member of `focus()`'s options (Chromium 145, Firefox 104,\n * Safari 18.4). Detected once through a getter: the bindings convert the dictionary before the\n * method runs, so a detached element is enough and nothing is focused.\n */\nfunction supportsFocusVisibleOption(doc: Document): boolean {\n if (focusVisibleOption === undefined) {\n let read = false;\n try {\n doc.createElement(\"div\").focus({\n get focusVisible() {\n read = true;\n return false;\n },\n } as FocusOptions);\n } catch {\n read = false;\n }\n focusVisibleOption = read;\n }\n return focusVisibleOption;\n}\n\n/** Resolves `initialFocus` against the popup (a ref, or a selector inside the popup). */\nfunction resolveInitialFocus(\n popup: HTMLElement,\n initialFocus: RefObject<HTMLElement | null> | string,\n): HTMLElement | null {\n if (typeof initialFocus === \"string\") return popup.querySelector<HTMLElement>(initialFocus);\n return initialFocus.current;\n}\n\n/** Whether `element` sits inside a popover that is not shown (an outer popup that closed). */\nfunction insideHiddenPopover(element: Element): boolean {\n for (let node = element.parentElement; node !== null; node = node.parentElement) {\n if (node.hasAttribute(\"popover\") && !isPopoverOpen(node)) return true;\n }\n return false;\n}\n\n/**\n * Everything in a popover-like Popup except rendering (design/components/popover.md 14.2): mount\n * reconciliation, the apply effect, the native `beforetoggle` / `toggle` / `command` listeners,\n * close intents, the focus-return fallback, the JS-free `aria-expanded` sync, the deferred\n * unmount while a hide animates, and `initialFocus`. Shared by Popover and Menu.\n */\nexport function usePopoverPopup(\n ctx: PopoverRootValue,\n options: UsePopoverPopupOptions,\n): PopoverPopupHook {\n const { part, initialFocus, transition, onBeforeToggle: onBeforeToggleOption } = options;\n const onToggleOption = options.onToggle;\n const isLibrary = ctx.mode === \"library\";\n const { popupRef, triggerRef } = ctx;\n\n // The opening `beforetoggle` lets this one through: the library's own `showPopover()`.\n const opening = useRef(false);\n // Set by the synchronous `beforetoggle` of a close, cleared by `toggle` a task later: the\n // popup is hidden but its `toggle` has not arrived, so the apply effect must not re-open it\n // from a commit that lands in between (design 2.5).\n const awaitingClose = useRef(false);\n // Whether focus was inside the popup when its last close began (design 6.2 rule 1).\n const focusWasInside = useRef(false);\n const warnedFocus = useRef(false);\n\n // --- `held`: children stay mounted while the hide animates (design 4.4) -------------------\n\n const exit = useExitAnimation();\n\n // A library-driven close commits `false` first and hides in the layout effect, so without\n // `transition` the children are held through the close commit (\"previous value in state\").\n // With `transition` the close commit unmounts them inside the view transition, whose old\n // snapshot carries the content out.\n const [prevCommitted, setPrevCommitted] = useState(ctx.committed);\n if (prevCommitted !== ctx.committed) {\n setPrevCommitted(ctx.committed);\n if (isLibrary && prevCommitted && transition === undefined) exit.holdDuringRender();\n }\n\n // Whether the content unmounted while holding focus since the popup was last shown (H1).\n const contentProbe = useContentFocusProbe(popupRef);\n const { wrapContent } = contentProbe;\n\n const syncExpanded = (element: HTMLElement): void => {\n // JS-free mode only (design 9): read the DOM, not `newState`, so a stale task cannot lie.\n triggerRef.current?.setAttribute(\"aria-expanded\", String(isPopoverOpen(element)));\n };\n\n /**\n * Design 6.2 rule 2, for a close that began with focus inside: when focus is now on `<body>`\n * or still inside the hidden popup, focus the Trigger (connected, not inside a hidden\n * popover), else blur what holds focus.\n */\n const returnFocus = (element: HTMLElement): void => {\n const doc = element.ownerDocument;\n const active = doc.activeElement;\n if (active !== null && active !== doc.body && !element.contains(active)) return;\n const trigger = triggerRef.current;\n if (trigger !== null && trigger.isConnected && !insideHiddenPopover(trigger)) {\n trigger.focus();\n } else if (active instanceof HTMLElement && active !== doc.body) {\n active.blur();\n }\n };\n\n // --- Opening ------------------------------------------------------------------------------\n\n const resolveFocusTarget = (element: HTMLElement): HTMLElement | QuietFocusTarget | null => {\n if (options.resolveInitialFocus !== undefined) {\n const resolved = options.resolveInitialFocus(element);\n if (resolved === null) return null;\n const target = resolved instanceof HTMLElement ? resolved : resolved.element;\n return element.contains(target) ? resolved : null;\n }\n if (initialFocus === undefined) return null;\n const target = resolveInitialFocus(element, initialFocus);\n if (target !== null && element.contains(target)) return target;\n if (isDev && !warnedFocus.current) {\n warnedFocus.current = true;\n console.error(\n target === null\n ? `${part}: \\`initialFocus\\` resolved to no element; the platform picks the focus target.`\n : `${part}: \\`initialFocus\\` is outside the popup; the platform picks the focus target.`,\n );\n }\n return null;\n };\n\n const showPopup = (element: HTMLElement): void => {\n const resolved = resolveFocusTarget(element);\n const target =\n resolved === null || resolved instanceof HTMLElement ? resolved : resolved.element;\n // A pointer open asks for no focus ring: `focus({ focusVisible: false })` after the show,\n // where the engine reads that option (QuietFocusTarget).\n const quiet =\n target !== null &&\n !(resolved instanceof HTMLElement) &&\n supportsFocusVisibleOption(element.ownerDocument);\n // Otherwise the popover focusing steps honour `autofocus` (and run the dialog focusing steps\n // for a <dialog>, in every engine); React drops `autoFocus` on client-rendered content.\n // Never `.focus()` (6.1) except for the quiet target above.\n const added = target !== null && !quiet && !target.hasAttribute(\"autofocus\");\n if (added) target.setAttribute(\"autofocus\", \"\");\n opening.current = true;\n try {\n const source = triggerRef.current;\n element.showPopover(source !== null && source.isConnected ? { source } : undefined);\n } finally {\n opening.current = false;\n if (added) target.removeAttribute(\"autofocus\");\n }\n if (quiet && isPopoverOpen(element) && element.contains(target)) {\n target.focus({ focusVisible: false } as FocusOptions);\n }\n };\n\n // Mount reconciliation (design 2.4): a click before hydration opened the popover natively.\n // Runs before the apply effect; the dispatch makes the apply effect wait for it.\n const reconcileOnMount = useEffectEvent(() => {\n const element = popupRef.current;\n if (element === null) return;\n if (!isLibrary) {\n syncExpanded(element);\n return;\n }\n if (isPopoverOpen(element)) ctx.requestOpen(undefined, \"platform\");\n });\n useLayoutEffect(() => {\n reconcileOnMount();\n }, []);\n\n // The apply effect (design 2.5): after every commit that re-renders the Popup, once nothing is\n // in flight, the DOM follows committed state. Since the Root's value is memoised (#48) an\n // unrelated Root render does not re-render the Popup, so this does not run for it; every path\n // that makes the DOM diverge from state goes through a dispatch, whose `pending` flips re-render\n // the Popup (review of #48, finding 4). Do not rely on an unrelated render to re-apply state.\n // One effect covers the normal open, `defaultOpen`, a controlled open\n // or close, a library-driven close after `openChangeAction(false)` settled, the re-open of a\n // refused dismissal, and the close after a refused or failed pre-hydration open.\n useLayoutEffect(() => {\n if (!isLibrary) return;\n const element = popupRef.current;\n if (element === null || !element.isConnected) return;\n if (!ctx.pending && ctx.latestInFlight() === null) {\n const shown = isPopoverOpen(element);\n if (ctx.committed && !shown) {\n if (!awaitingClose.current) showPopup(element);\n } else if (!ctx.committed && shown) {\n // An echo: the `toggle` listener finds `false` intended and skips the dispatch.\n ctx.recordCloseIntent(undefined);\n element.hidePopover();\n // The close commit may have unmounted the focused content before the hide (H1): the\n // platform restores focus only from inside the popover, so do it now.\n if (focusWasInside.current) returnFocus(element);\n }\n }\n // Content that unmounted while the popup stays open (not a close): forget it.\n if (ctx.committed && isPopoverOpen(element)) contentProbe.forget();\n // A hold the close commit set (or a dismissal's notification commit) that no hide animation\n // is going to release: the popup is already hidden and its `toggle` has been handled.\n if (\n exit.held &&\n !ctx.committed &&\n !exit.waiting() &&\n !awaitingClose.current &&\n !isPopoverOpen(element)\n ) {\n exit.release();\n }\n });\n\n // --- Native listeners (conventions 7.4; filtered on target: React 19.3.1 emulates bubbling\n // of `toggle`, and a nested popover's events reach this element through the real DOM too) ---\n\n const onBeforeToggle = (event: Event): void => {\n const element = popupRef.current;\n if (element === null || event.target !== element) return;\n const toggle = event as ToggleEvent;\n if (toggle.newState === \"open\") {\n // Every open the library did not start itself is routed through the Root (design 2.2):\n // a foreign `show-popover` / `toggle-popover` invoker, a script `showPopover()`.\n if (isLibrary && !opening.current) {\n event.preventDefault();\n ctx.requestOpen(event, \"activate\");\n } else {\n // A Close action that resolves after this open must not close it (design 3.4).\n ctx.nextOpenGeneration();\n }\n contentProbe.forget();\n } else {\n awaitingClose.current = true;\n exit.recordBaseline(element);\n const active = element.ownerDocument.activeElement;\n focusWasInside.current =\n (active !== null && element.contains(active)) || contentProbe.hadFocus();\n contentProbe.forget();\n }\n onBeforeToggleOption?.(toggle);\n };\n\n const onToggle = (event: Event): void => {\n const element = popupRef.current;\n if (element === null || event.target !== element) return;\n awaitingClose.current = false;\n if (!isLibrary) syncExpanded(element);\n\n if (!isPopoverOpen(element)) {\n // Focus return for what the platform leaves out (design 6.2): a nested popover restores\n // nothing, and an opener that is gone leaves focus inside the hidden popup.\n const wasInside = focusWasInside.current;\n focusWasInside.current = false;\n if (wasInside) returnFocus(element);\n\n const intent = ctx.takeCloseIntent();\n if (isLibrary) {\n // `false` already intended: the library's own hide (or a dismissal during a pending\n // part-initiated close). With `transition` such a close needs no hold (4.4).\n const echo = !ctx.intended();\n if (!echo || transition === undefined) {\n const animations = exit.exitAnimations(element);\n if (animations.length > 0) {\n // Before the notification, so its commit keeps the children (urgent, internal).\n exit.holdUntil(animations);\n } else {\n // Nothing animates (Firefox, or no CSS exit): release a hold now; the commit is\n // invisible, the popup is already `display: none`.\n exit.release();\n }\n }\n ctx.notifyClosed(intent?.event ?? event, intent?.reason ?? \"dismiss\");\n }\n }\n onToggleOption?.(event as ToggleEvent);\n };\n\n const onCommand = (event: Event): void => {\n const element = popupRef.current;\n if (element === null || event.target !== element || event.defaultPrevented) return;\n const command = String((event as Event & { command?: string }).command ?? \"\").toLowerCase();\n // A foreign invoker closing the popup (design 2.3). Opens are caught at `beforetoggle`.\n if (command === \"hide-popover\" || (command === \"toggle-popover\" && isPopoverOpen(element))) {\n ctx.recordCloseIntent({ reason: \"activate\", event });\n }\n };\n\n // The listeners the callback ref attaches call the latest render's handlers. An insertion\n // effect publishes them before refs attach and before any layout effect (whose\n // `hidePopover()` dispatches `beforetoggle` synchronously).\n const handlers = useRef({ onBeforeToggle, onToggle, onCommand });\n useInsertionEffect(() => {\n handlers.current = { onBeforeToggle, onToggle, onCommand };\n });\n\n // --- Element ------------------------------------------------------------------------------\n\n // The listeners attach in the callback ref, so they are in place from the commit that mounts\n // the element (no passive-effect window, no extra render). JS-free mode: there is no \"right\n // before showPopover()\" (the browser runs the command), so the `autofocus` attribute stays on\n // the `initialFocus` target while it is mounted (as Dialog).\n const setElement = useCallback(\n (element: HTMLElement) => {\n if (isDev && element.tagName !== \"DIALOG\" && element.tagName !== \"DIV\") {\n console.error(\n `${part}: expected to render a <dialog> or a <div> but got ` +\n `<${element.tagName.toLowerCase()}>.`,\n );\n }\n popupRef.current = element;\n const listeners: [string, EventListener][] = [\n [\"beforetoggle\", (event) => handlers.current.onBeforeToggle(event)],\n [\"toggle\", (event) => handlers.current.onToggle(event)],\n [\"command\", (event) => handlers.current.onCommand(event)],\n ];\n for (const [type, listener] of listeners) element.addEventListener(type, listener);\n\n let autofocused: HTMLElement | null = null;\n if (!isLibrary && initialFocus !== undefined) {\n const target = resolveInitialFocus(element, initialFocus);\n if (target !== null && element.contains(target) && !target.hasAttribute(\"autofocus\")) {\n target.setAttribute(\"autofocus\", \"\");\n autofocused = target;\n }\n }\n\n return () => {\n for (const [type, listener] of listeners) element.removeEventListener(type, listener);\n autofocused?.removeAttribute(\"autofocus\");\n if (popupRef.current === element) popupRef.current = null;\n };\n },\n [part, popupRef, isLibrary, initialFocus],\n );\n\n // Development check (design 9): `role=\"dialog\"` needs a name once the popup is (or can be)\n // shown.\n const shown = !isLibrary || ctx.committed;\n const warnedName = useRef(false);\n useEffect(() => {\n const element = popupRef.current;\n if (!isDev || element === null || !shown || warnedName.current) return;\n if (element.hasAttribute(\"aria-label\")) return;\n const labelledBy = (element.getAttribute(\"aria-labelledby\") ?? \"\").split(/\\s+/);\n const doc = element.ownerDocument;\n if (labelledBy.some((id) => id !== \"\" && doc.getElementById(id) !== null)) return;\n const role = element.getAttribute(\"role\");\n if (role !== \"dialog\" && role !== \"alertdialog\" && role !== \"menu\") return;\n warnedName.current = true;\n console.error(\n `${part}: the popup has no accessible name. Render a Title, or give the Popup an ` +\n \"`aria-label`.\",\n );\n }, [popupRef, shown, part]);\n\n return { setElement, visible: ctx.committed || exit.held, wrapContent };\n}\n"],"mappings":";;;;;AAgEA,MAAM,QAAQ,QAAQ,IAAI,aAAa;AAEvC,SAAS,cAAc,SAA2B;CAChD,IAAI;EACF,OAAO,QAAQ,QAAQ,eAAe;CACxC,QAAQ;EACN,OAAO;CACT;AACF;AAEA,IAAI;;;;;;AAOJ,SAAS,2BAA2B,KAAwB;CAC1D,IAAI,uBAAuB,KAAA,GAAW;EACpC,IAAI,OAAO;EACX,IAAI;GACF,IAAI,cAAc,KAAK,CAAC,CAAC,MAAM,EAC7B,IAAI,eAAe;IACjB,OAAO;IACP,OAAO;GACT,EACF,CAAiB;EACnB,QAAQ;GACN,OAAO;EACT;EACA,qBAAqB;CACvB;CACA,OAAO;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AC;CAE1B,OAAO;EAAE;EAAY,SAAS,IAAI,aAAa,KAAK;EAAM;CAAY;AACxE"}
|
|
1
|
+
{"version":3,"file":"use-popover-popup.mjs","names":[],"sources":["../../src/internal/use-popover-popup.ts"],"sourcesContent":["\"use client\";\nimport {\n useCallback,\n useEffect,\n useEffectEvent,\n useInsertionEffect,\n useLayoutEffect,\n useRef,\n useState,\n} from \"react\";\nimport type { ReactNode, RefObject, ViewTransitionClass } from \"react\";\nimport { useContentFocusProbe } from \"./content-focus-probe.js\";\nimport { useExitAnimation } from \"./use-exit-animation.js\";\nimport type { PopoverRootValue } from \"./use-popover-root.js\";\n\nexport interface UsePopoverPopupOptions {\n /** Public name of the part for development warnings (`\"Popover.Popup\"`). */\n readonly part: string;\n /** Element to focus on open: a ref, or a selector resolved inside the popup (design 6.1). */\n readonly initialFocus: RefObject<HTMLElement | null> | string | undefined;\n /**\n * Replaces `initialFocus` with a function called right before each library open (Menu: the\n * first or last item by open cause). `null` leaves the choice to the platform. A\n * `QuietFocusTarget` asks for focus without the focus ring (a pointer-opened menu).\n */\n readonly resolveInitialFocus?: (popup: HTMLElement) => HTMLElement | QuietFocusTarget | null;\n readonly keepMounted: boolean | undefined;\n /** The consumer's `transition`; decides how a library-driven close keeps its content (4.4). */\n readonly transition: ViewTransitionClass | undefined;\n /** Runs after the library's own `beforetoggle` handling (Menu: focus bookkeeping). */\n readonly onBeforeToggle?: (event: ToggleEvent) => void;\n /** Runs after the library's own `toggle` handling (Menu: the outside right-click listeners). */\n readonly onToggle?: (event: ToggleEvent) => void;\n /**\n * The `source` of each library `showPopover({ source })`, read right before the call. Default:\n * the registered Trigger. Menu passes the ContextTrigger region for a point open\n * (design/components/context-menu.md 14).\n */\n readonly source?: () => HTMLElement | null;\n}\n\n/**\n * An initial focus target opened by a pointer: where the engine reads `focus({ focusVisible })`,\n * the library focuses it with `focusVisible: false` right after `showPopover()` instead of\n * writing `autofocus` (WebKit shows the ring for an `autofocus` target after a click; review H1).\n * Elsewhere it is an ordinary target.\n */\nexport interface QuietFocusTarget {\n readonly element: HTMLElement;\n readonly focusVisible: false;\n}\n\nexport interface PopoverPopupHook {\n /** Callback ref for the popup element; pass as `useRender`'s `ref`. */\n readonly setElement: (element: HTMLElement) => () => void;\n /**\n * Whether the children are shown: committed open, or held while the hide animates (4.4).\n * Library mode only; JS-free mode always renders them.\n */\n readonly visible: boolean;\n /**\n * Wraps the library-mode content: call it only while `visible` (`visible ? wrapContent(x) :\n * null`), or inside the `<Activity>` for `keepMounted`. When a close commit unmounts the\n * content while it holds focus (a library-driven close with `transition`, where the focused\n * element is gone before `hidePopover()` runs and the platform restores nothing), the hide\n * that follows returns focus to the Trigger (design 6.2).\n */\n readonly wrapContent: (children: ReactNode) => ReactNode;\n /**\n * Re-shows a shown popup in place (`hidePopover()` + `showPopover({ source })` under the\n * opening flag, initial focus resolved again): Menu's move to a new point\n * (design/components/context-menu.md 4.3). The platform coalesces the two toggles into one\n * `toggle` (`open` to `open`), so nothing is dispatched. No-op (`false`) when the popup is not\n * shown.\n */\n readonly reshow: () => boolean;\n}\n\nconst isDev = process.env.NODE_ENV !== \"production\";\n\nfunction isPopoverOpen(element: Element): boolean {\n try {\n return element.matches(\":popover-open\");\n } catch {\n return false;\n }\n}\n\nlet focusVisibleOption: boolean | undefined;\n\n/**\n * The engine reads the `focusVisible` member of `focus()`'s options (Chromium 145, Firefox 104,\n * Safari 18.4). Detected once through a getter: the bindings convert the dictionary before the\n * method runs, so a detached element is enough and nothing is focused.\n */\nfunction supportsFocusVisibleOption(doc: Document): boolean {\n if (focusVisibleOption === undefined) {\n let read = false;\n try {\n doc.createElement(\"div\").focus({\n get focusVisible() {\n read = true;\n return false;\n },\n } as FocusOptions);\n } catch {\n read = false;\n }\n focusVisibleOption = read;\n }\n return focusVisibleOption;\n}\n\n/** Resolves `initialFocus` against the popup (a ref, or a selector inside the popup). */\nfunction resolveInitialFocus(\n popup: HTMLElement,\n initialFocus: RefObject<HTMLElement | null> | string,\n): HTMLElement | null {\n if (typeof initialFocus === \"string\") return popup.querySelector<HTMLElement>(initialFocus);\n return initialFocus.current;\n}\n\n/** Whether `element` sits inside a popover that is not shown (an outer popup that closed). */\nfunction insideHiddenPopover(element: Element): boolean {\n for (let node = element.parentElement; node !== null; node = node.parentElement) {\n if (node.hasAttribute(\"popover\") && !isPopoverOpen(node)) return true;\n }\n return false;\n}\n\n/**\n * Everything in a popover-like Popup except rendering (design/components/popover.md 14.2): mount\n * reconciliation, the apply effect, the native `beforetoggle` / `toggle` / `command` listeners,\n * close intents, the focus-return fallback, the JS-free `aria-expanded` sync, the deferred\n * unmount while a hide animates, and `initialFocus`. Shared by Popover and Menu.\n */\nexport function usePopoverPopup(\n ctx: PopoverRootValue,\n options: UsePopoverPopupOptions,\n): PopoverPopupHook {\n const { part, initialFocus, transition, onBeforeToggle: onBeforeToggleOption } = options;\n const onToggleOption = options.onToggle;\n const isLibrary = ctx.mode === \"library\";\n const { popupRef, triggerRef } = ctx;\n\n // The opening `beforetoggle` lets this one through: the library's own `showPopover()`.\n const opening = useRef(false);\n // Set across `reshow()`: the show is not a new open.\n const reshowing = useRef(false);\n // Set by the synchronous `beforetoggle` of a close, cleared by `toggle` a task later: the\n // popup is hidden but its `toggle` has not arrived, so the apply effect must not re-open it\n // from a commit that lands in between (design 2.5).\n const awaitingClose = useRef(false);\n // Whether focus was inside the popup when its last close began (design 6.2 rule 1).\n const focusWasInside = useRef(false);\n const warnedFocus = useRef(false);\n\n // --- `held`: children stay mounted while the hide animates (design 4.4) -------------------\n\n const exit = useExitAnimation();\n\n // A library-driven close commits `false` first and hides in the layout effect, so without\n // `transition` the children are held through the close commit (\"previous value in state\").\n // With `transition` the close commit unmounts them inside the view transition, whose old\n // snapshot carries the content out.\n const [prevCommitted, setPrevCommitted] = useState(ctx.committed);\n if (prevCommitted !== ctx.committed) {\n setPrevCommitted(ctx.committed);\n if (isLibrary && prevCommitted && transition === undefined) exit.holdDuringRender();\n }\n\n // Whether the content unmounted while holding focus since the popup was last shown (H1).\n const contentProbe = useContentFocusProbe(popupRef);\n const { wrapContent } = contentProbe;\n\n const syncExpanded = (element: HTMLElement): void => {\n // JS-free mode only (design 9): read the DOM, not `newState`, so a stale task cannot lie.\n triggerRef.current?.setAttribute(\"aria-expanded\", String(isPopoverOpen(element)));\n };\n\n /**\n * Design 6.2 rule 2, for a close that began with focus inside: when focus is now on `<body>`\n * or still inside the hidden popup, focus the Trigger (connected, not inside a hidden\n * popover), else blur what holds focus.\n */\n const returnFocus = (element: HTMLElement): void => {\n const doc = element.ownerDocument;\n const active = doc.activeElement;\n if (active !== null && active !== doc.body && !element.contains(active)) return;\n const trigger = triggerRef.current;\n if (trigger !== null && trigger.isConnected && !insideHiddenPopover(trigger)) {\n trigger.focus();\n } else if (active instanceof HTMLElement && active !== doc.body) {\n active.blur();\n }\n };\n\n // --- Opening ------------------------------------------------------------------------------\n\n const resolveFocusTarget = (element: HTMLElement): HTMLElement | QuietFocusTarget | null => {\n if (options.resolveInitialFocus !== undefined) {\n const resolved = options.resolveInitialFocus(element);\n if (resolved === null) return null;\n const target = resolved instanceof HTMLElement ? resolved : resolved.element;\n return element.contains(target) ? resolved : null;\n }\n if (initialFocus === undefined) return null;\n const target = resolveInitialFocus(element, initialFocus);\n if (target !== null && element.contains(target)) return target;\n if (isDev && !warnedFocus.current) {\n warnedFocus.current = true;\n console.error(\n target === null\n ? `${part}: \\`initialFocus\\` resolved to no element; the platform picks the focus target.`\n : `${part}: \\`initialFocus\\` is outside the popup; the platform picks the focus target.`,\n );\n }\n return null;\n };\n\n const showPopup = (element: HTMLElement): void => {\n const resolved = resolveFocusTarget(element);\n const target =\n resolved === null || resolved instanceof HTMLElement ? resolved : resolved.element;\n // A pointer open asks for no focus ring: `focus({ focusVisible: false })` after the show,\n // where the engine reads that option (QuietFocusTarget).\n const quiet =\n target !== null &&\n !(resolved instanceof HTMLElement) &&\n supportsFocusVisibleOption(element.ownerDocument);\n // Otherwise the popover focusing steps honour `autofocus` (and run the dialog focusing steps\n // for a <dialog>, in every engine); React drops `autoFocus` on client-rendered content.\n // Never `.focus()` (6.1) except for the quiet target above.\n const added = target !== null && !quiet && !target.hasAttribute(\"autofocus\");\n if (added) target.setAttribute(\"autofocus\", \"\");\n opening.current = true;\n try {\n const source = options.source !== undefined ? options.source() : triggerRef.current;\n element.showPopover(source != null && source.isConnected ? { source } : undefined);\n } finally {\n opening.current = false;\n if (added) target.removeAttribute(\"autofocus\");\n }\n if (quiet && isPopoverOpen(element) && element.contains(target)) {\n target.focus({ focusVisible: false } as FocusOptions);\n }\n };\n\n const reshow = (): boolean => {\n const element = popupRef.current;\n if (!isLibrary || element === null || !isPopoverOpen(element)) return false;\n // The hide half's `beforetoggle` records `awaitingClose` / `focusWasInside`; the coalesced\n // `toggle` finds the popup open again, so it clears `awaitingClose`, returns no focus and\n // notifies nothing. The opening flag covers both halves (conventions 3.5, the move).\n opening.current = true;\n reshowing.current = true;\n try {\n element.hidePopover();\n showPopup(element);\n } finally {\n opening.current = false;\n reshowing.current = false;\n }\n // Whether or not the engine fires the coalesced `toggle`, the close bookkeeping of the hide\n // half is void: the popup is open again.\n awaitingClose.current = false;\n focusWasInside.current = false;\n return isPopoverOpen(element);\n };\n\n // Mount reconciliation (design 2.4): a click before hydration opened the popover natively.\n // Runs before the apply effect; the dispatch makes the apply effect wait for it.\n const reconcileOnMount = useEffectEvent(() => {\n const element = popupRef.current;\n if (element === null) return;\n if (!isLibrary) {\n syncExpanded(element);\n return;\n }\n if (isPopoverOpen(element)) ctx.requestOpen(undefined, \"platform\");\n });\n useLayoutEffect(() => {\n reconcileOnMount();\n }, []);\n\n // The apply effect (design 2.5): after every commit that re-renders the Popup, once nothing is\n // in flight, the DOM follows committed state. Since the Root's value is memoised (#48) an\n // unrelated Root render does not re-render the Popup, so this does not run for it; every path\n // that makes the DOM diverge from state goes through a dispatch, whose `pending` flips re-render\n // the Popup (review of #48, finding 4). Do not rely on an unrelated render to re-apply state.\n // One effect covers the normal open, `defaultOpen`, a controlled open\n // or close, a library-driven close after `openChangeAction(false)` settled, the re-open of a\n // refused dismissal, and the close after a refused or failed pre-hydration open.\n useLayoutEffect(() => {\n if (!isLibrary) return;\n const element = popupRef.current;\n if (element === null || !element.isConnected) return;\n if (!ctx.pending && ctx.latestInFlight() === null) {\n const shown = isPopoverOpen(element);\n if (ctx.committed && !shown) {\n if (!awaitingClose.current) showPopup(element);\n } else if (!ctx.committed && shown) {\n // An echo: the `toggle` listener finds `false` intended and skips the dispatch.\n ctx.recordCloseIntent(undefined);\n element.hidePopover();\n // The close commit may have unmounted the focused content before the hide (H1): the\n // platform restores focus only from inside the popover, so do it now.\n if (focusWasInside.current) returnFocus(element);\n }\n }\n // Content that unmounted while the popup stays open (not a close): forget it.\n if (ctx.committed && isPopoverOpen(element)) contentProbe.forget();\n // A hold the close commit set (or a dismissal's notification commit) that no hide animation\n // is going to release: the popup is already hidden and its `toggle` has been handled.\n if (\n exit.held &&\n !ctx.committed &&\n !exit.waiting() &&\n !awaitingClose.current &&\n !isPopoverOpen(element)\n ) {\n exit.release();\n }\n });\n\n // --- Native listeners (conventions 7.4; filtered on target: React 19.3.1 emulates bubbling\n // of `toggle`, and a nested popover's events reach this element through the real DOM too) ---\n\n const onBeforeToggle = (event: Event): void => {\n const element = popupRef.current;\n if (element === null || event.target !== element) return;\n const toggle = event as ToggleEvent;\n if (toggle.newState === \"open\") {\n // Every open the library did not start itself is routed through the Root (design 2.2):\n // a foreign `show-popover` / `toggle-popover` invoker, a script `showPopover()`.\n if (isLibrary && !opening.current) {\n event.preventDefault();\n ctx.requestOpen(event, \"activate\");\n } else if (!reshowing.current) {\n // A Close action that resolves after this open must not close it (design 3.4). A re-show\n // (Menu's move) is the same open: an item action pending across it still closes.\n ctx.nextOpenGeneration();\n }\n contentProbe.forget();\n } else {\n awaitingClose.current = true;\n exit.recordBaseline(element);\n const active = element.ownerDocument.activeElement;\n focusWasInside.current =\n (active !== null && element.contains(active)) || contentProbe.hadFocus();\n contentProbe.forget();\n }\n onBeforeToggleOption?.(toggle);\n };\n\n const onToggle = (event: Event): void => {\n const element = popupRef.current;\n if (element === null || event.target !== element) return;\n awaitingClose.current = false;\n if (!isLibrary) syncExpanded(element);\n\n if (!isPopoverOpen(element)) {\n // Focus return for what the platform leaves out (design 6.2): a nested popover restores\n // nothing, and an opener that is gone leaves focus inside the hidden popup.\n const wasInside = focusWasInside.current;\n focusWasInside.current = false;\n if (wasInside) returnFocus(element);\n\n const intent = ctx.takeCloseIntent();\n if (isLibrary) {\n // `false` already intended: the library's own hide (or a dismissal during a pending\n // part-initiated close). With `transition` such a close needs no hold (4.4).\n const echo = !ctx.intended();\n if (!echo || transition === undefined) {\n const animations = exit.exitAnimations(element);\n if (animations.length > 0) {\n // Before the notification, so its commit keeps the children (urgent, internal).\n exit.holdUntil(animations);\n } else {\n // Nothing animates (Firefox, or no CSS exit): release a hold now; the commit is\n // invisible, the popup is already `display: none`.\n exit.release();\n }\n }\n ctx.notifyClosed(intent?.event ?? event, intent?.reason ?? \"dismiss\");\n }\n }\n onToggleOption?.(event as ToggleEvent);\n };\n\n const onCommand = (event: Event): void => {\n const element = popupRef.current;\n if (element === null || event.target !== element || event.defaultPrevented) return;\n const command = String((event as Event & { command?: string }).command ?? \"\").toLowerCase();\n // A foreign invoker closing the popup (design 2.3). Opens are caught at `beforetoggle`.\n if (command === \"hide-popover\" || (command === \"toggle-popover\" && isPopoverOpen(element))) {\n ctx.recordCloseIntent({ reason: \"activate\", event });\n }\n };\n\n // The listeners the callback ref attaches call the latest render's handlers. An insertion\n // effect publishes them before refs attach and before any layout effect (whose\n // `hidePopover()` dispatches `beforetoggle` synchronously).\n const handlers = useRef({ onBeforeToggle, onToggle, onCommand });\n useInsertionEffect(() => {\n handlers.current = { onBeforeToggle, onToggle, onCommand };\n });\n\n // --- Element ------------------------------------------------------------------------------\n\n // The listeners attach in the callback ref, so they are in place from the commit that mounts\n // the element (no passive-effect window, no extra render). JS-free mode: there is no \"right\n // before showPopover()\" (the browser runs the command), so the `autofocus` attribute stays on\n // the `initialFocus` target while it is mounted (as Dialog).\n const setElement = useCallback(\n (element: HTMLElement) => {\n if (isDev && element.tagName !== \"DIALOG\" && element.tagName !== \"DIV\") {\n console.error(\n `${part}: expected to render a <dialog> or a <div> but got ` +\n `<${element.tagName.toLowerCase()}>.`,\n );\n }\n popupRef.current = element;\n const listeners: [string, EventListener][] = [\n [\"beforetoggle\", (event) => handlers.current.onBeforeToggle(event)],\n [\"toggle\", (event) => handlers.current.onToggle(event)],\n [\"command\", (event) => handlers.current.onCommand(event)],\n ];\n for (const [type, listener] of listeners) element.addEventListener(type, listener);\n\n let autofocused: HTMLElement | null = null;\n if (!isLibrary && initialFocus !== undefined) {\n const target = resolveInitialFocus(element, initialFocus);\n if (target !== null && element.contains(target) && !target.hasAttribute(\"autofocus\")) {\n target.setAttribute(\"autofocus\", \"\");\n autofocused = target;\n }\n }\n\n return () => {\n for (const [type, listener] of listeners) element.removeEventListener(type, listener);\n autofocused?.removeAttribute(\"autofocus\");\n if (popupRef.current === element) popupRef.current = null;\n };\n },\n [part, popupRef, isLibrary, initialFocus],\n );\n\n // Development check (design 9): `role=\"dialog\"` needs a name once the popup is (or can be)\n // shown.\n const shown = !isLibrary || ctx.committed;\n const warnedName = useRef(false);\n useEffect(() => {\n const element = popupRef.current;\n if (!isDev || element === null || !shown || warnedName.current) return;\n if (element.hasAttribute(\"aria-label\")) return;\n const labelledBy = (element.getAttribute(\"aria-labelledby\") ?? \"\").split(/\\s+/);\n const doc = element.ownerDocument;\n if (labelledBy.some((id) => id !== \"\" && doc.getElementById(id) !== null)) return;\n const role = element.getAttribute(\"role\");\n if (role !== \"dialog\" && role !== \"alertdialog\" && role !== \"menu\") return;\n warnedName.current = true;\n console.error(\n `${part}: the popup has no accessible name. Render a Title, or give the Popup an ` +\n \"`aria-label`.\",\n );\n }, [popupRef, shown, part]);\n\n return { setElement, visible: ctx.committed || exit.held, wrapContent, reshow };\n}\n"],"m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AAO,aAAa,WAAW,GAAG;IACpF,OAAO,aAAa,aAAa,EAAE;IACnC,cAAc;GAChB;EACF;EAEA,aAAa;GACX,KAAK,MAAM,CAAC,MAAM,aAAa,WAAW,QAAQ,oBAAoB,MAAM,QAAQ;GACpF,aAAa,gBAAgB,WAAW;GACxC,IAAI,SAAS,YAAY,SAAS,SAAS,UAAU;EACvD;CACF,GACA;EAAC;EAAM;EAAU;EAAW;CAAY,CAC1C;CAIA,MAAM,QAAQ,CAAC,aAAa,IAAI;CAChC,MAAM,aAAa,OAAO,KAAK;CAC/B,gBAAgB;EACd,MAAM,UAAU,SAAS;EACzB,IAAI,CAAC,SAAS,YAAY,QAAQ,CAAC,SAAS,WAAW,SAAS;EAChE,IAAI,QAAQ,aAAa,YAAY,GAAG;EACxC,MAAM,cAAc,QAAQ,aAAa,iBAAiB,KAAK,GAAA,CAAI,MAAM,KAAK;EAC9E,MAAM,MAAM,QAAQ;EACpB,IAAI,WAAW,MAAM,OAAO,OAAO,MAAM,IAAI,eAAe,EAAE,MAAM,IAAI,GAAG;EAC3E,MAAM,OAAO,QAAQ,aAAa,MAAM;EACxC,IAAI,SAAS,YAAY,SAAS,iBAAiB,SAAS,QAAQ;EACpE,WAAW,UAAU;EACrB,QAAQ,MACN,GAAG,KAAK,yFAEV;CACF,GAAG;EAAC;EAAU;EAAO;CAAI,CAAC;CAE1B,OAAO;EAAE;EAAY,SAAS,IAAI,aAAa,KAAK;EAAM;EAAa;CAAO;AAChF"}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { parsePositionArea } from "./anchor-position.mjs";
|
|
3
|
+
import { useCallback, useLayoutEffect, useRef, useState } from "react";
|
|
4
|
+
import { flushSync } from "react-dom";
|
|
5
|
+
//#region src/internal/use-resolved-placement.ts
|
|
6
|
+
const toDirection = (value) => value === "rtl" ? "rtl" : "ltr";
|
|
7
|
+
/**
|
|
8
|
+
* Where the browser actually placed an anchored popup (design/conventions.md 13.2): the
|
|
9
|
+
* `position-try-fallbacks` option it applied, read from `getComputedStyle(popup).positionArea`
|
|
10
|
+
* while the popup is shown and parsed into a physical side and an alignment. Observation, not
|
|
11
|
+
* positioning: the browser keeps placing the popup; this only reports where.
|
|
12
|
+
*
|
|
13
|
+
* Sources: a `ResizeObserver` on the popup from mount (hidden to shown is a resize, delivered
|
|
14
|
+
* after layout and before paint, so the first painted frame carries the resolved side), `toggle`
|
|
15
|
+
* (and the jsdom shim's), and while shown `scroll` on the document (capture, passive: nested
|
|
16
|
+
* scrollers too) and `resize` on the window, each read at once and again in the next two
|
|
17
|
+
* animation frames (an engine applies the option a scroll calls for only in the layout after the
|
|
18
|
+
* event). The write is urgent (`flushSync`, so the attribute lands before paint; conventions 3.5)
|
|
19
|
+
* and happens only when the rendered pair would change: a popup with a physical `side` that opens
|
|
20
|
+
* where it was asked costs no render (the state holds `null` then). A logical side
|
|
21
|
+
* (`inline-start` / `inline-end`) costs one render on show and one on hide, because `data-side`
|
|
22
|
+
* turns physical while shown. Hidden again resets to the requested values, once the popup no
|
|
23
|
+
* longer renders (an exit animation keeps the resolved side to its end).
|
|
24
|
+
*
|
|
25
|
+
* Idle with `side: null` (a stylesheet's `@position-try` rules may set insets the parser cannot
|
|
26
|
+
* interpret): no reads, the result's `side` is `null`. Runs whatever opened the popup, the
|
|
27
|
+
* native `interestfor` and `commandfor` paths included.
|
|
28
|
+
*/
|
|
29
|
+
function useResolvedPlacement(request) {
|
|
30
|
+
const { side, align } = request;
|
|
31
|
+
const [resolved, setResolved] = useState(null);
|
|
32
|
+
const written = useRef(null);
|
|
33
|
+
const requested = useRef(request);
|
|
34
|
+
const sync = useRef(null);
|
|
35
|
+
const ref = useCallback((element) => {
|
|
36
|
+
if (element === null) return;
|
|
37
|
+
const doc = element.ownerDocument;
|
|
38
|
+
const win = doc.defaultView ?? window;
|
|
39
|
+
const write = (value, urgent) => {
|
|
40
|
+
const next = value !== null && value.side === requested.current.side && value.align === requested.current.align ? null : value;
|
|
41
|
+
const previous = written.current;
|
|
42
|
+
if (previous === next || previous !== null && next !== null && previous.side === next.side && previous.align === next.align) return;
|
|
43
|
+
written.current = next;
|
|
44
|
+
if (urgent) flushSync(() => setResolved(next));
|
|
45
|
+
else setResolved(next);
|
|
46
|
+
};
|
|
47
|
+
let listening = false;
|
|
48
|
+
const listen = (on) => {
|
|
49
|
+
if (on === listening) return;
|
|
50
|
+
listening = on;
|
|
51
|
+
if (on) {
|
|
52
|
+
doc.addEventListener("scroll", onMove, {
|
|
53
|
+
capture: true,
|
|
54
|
+
passive: true
|
|
55
|
+
});
|
|
56
|
+
win.addEventListener("resize", onMove);
|
|
57
|
+
} else {
|
|
58
|
+
doc.removeEventListener("scroll", onMove, { capture: true });
|
|
59
|
+
win.removeEventListener("resize", onMove);
|
|
60
|
+
cancelFrames();
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
let frame = 0;
|
|
64
|
+
let again = false;
|
|
65
|
+
const cancelFrames = () => {
|
|
66
|
+
if (frame !== 0) win.cancelAnimationFrame(frame);
|
|
67
|
+
frame = 0;
|
|
68
|
+
again = false;
|
|
69
|
+
};
|
|
70
|
+
const readLater = () => {
|
|
71
|
+
if (frame !== 0) {
|
|
72
|
+
again = true;
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
frame = win.requestAnimationFrame(() => {
|
|
76
|
+
read(true);
|
|
77
|
+
if (!listening) return;
|
|
78
|
+
frame = win.requestAnimationFrame(() => {
|
|
79
|
+
frame = 0;
|
|
80
|
+
read(true);
|
|
81
|
+
if (again && listening) {
|
|
82
|
+
again = false;
|
|
83
|
+
readLater();
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
const read = (urgent) => {
|
|
89
|
+
if (requested.current.side === null) {
|
|
90
|
+
listen(false);
|
|
91
|
+
write(null, urgent);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (element.isConnected && element.matches(":popover-open")) {
|
|
95
|
+
listen(true);
|
|
96
|
+
const style = getComputedStyle(element);
|
|
97
|
+
const placement = parsePositionArea(style.positionArea, toDirection(style.direction), () => toDirection(getComputedStyle(doc.documentElement).direction), style.writingMode || "horizontal-tb");
|
|
98
|
+
write(placement, urgent);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
listen(false);
|
|
102
|
+
if (written.current !== null && (!element.isConnected || element.getClientRects().length === 0)) write(null, urgent);
|
|
103
|
+
};
|
|
104
|
+
function onEvent() {
|
|
105
|
+
read(true);
|
|
106
|
+
}
|
|
107
|
+
function onMove() {
|
|
108
|
+
read(true);
|
|
109
|
+
if (listening) readLater();
|
|
110
|
+
}
|
|
111
|
+
sync.current = read;
|
|
112
|
+
const observer = typeof ResizeObserver === "function" ? new ResizeObserver(onEvent) : null;
|
|
113
|
+
observer?.observe(element);
|
|
114
|
+
element.addEventListener("toggle", onEvent);
|
|
115
|
+
return () => {
|
|
116
|
+
observer?.disconnect();
|
|
117
|
+
element.removeEventListener("toggle", onEvent);
|
|
118
|
+
listen(false);
|
|
119
|
+
if (sync.current === read) sync.current = null;
|
|
120
|
+
};
|
|
121
|
+
}, []);
|
|
122
|
+
useLayoutEffect(() => {
|
|
123
|
+
requested.current = {
|
|
124
|
+
side,
|
|
125
|
+
align
|
|
126
|
+
};
|
|
127
|
+
sync.current?.(false);
|
|
128
|
+
}, [side, align]);
|
|
129
|
+
if (side === null) return {
|
|
130
|
+
side: null,
|
|
131
|
+
align,
|
|
132
|
+
ref
|
|
133
|
+
};
|
|
134
|
+
return resolved === null ? {
|
|
135
|
+
side,
|
|
136
|
+
align,
|
|
137
|
+
ref
|
|
138
|
+
} : {
|
|
139
|
+
...resolved,
|
|
140
|
+
ref
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
//#endregion
|
|
144
|
+
export { useResolvedPlacement };
|
|
145
|
+
|
|
146
|
+
//# sourceMappingURL=use-resolved-placement.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-resolved-placement.mjs","names":[],"sources":["../../src/internal/use-resolved-placement.ts"],"sourcesContent":["\"use client\";\nimport { useCallback, useLayoutEffect, useRef, useState } from \"react\";\nimport type { RefCallback } from \"react\";\nimport { flushSync } from \"react-dom\";\nimport { parsePositionArea } from \"./anchor-position.js\";\nimport type {\n AnchorAlign,\n AnchorSide,\n Direction,\n PhysicalSide,\n ResolvedPlacement,\n} from \"./anchor-position.js\";\n\nexport interface PlacementRequest<Side extends AnchorSide> {\n /** The requested side; `null` when the stylesheet owns placement (the hook is idle). */\n readonly side: Side | null;\n readonly align: AnchorAlign;\n}\n\nexport interface PlacementResult<Side extends AnchorSide> {\n /** The resolved physical side while the popup is shown, else the requested side. */\n readonly side: Side | PhysicalSide | null;\n /** The resolved alignment while shown, else the requested one. */\n readonly align: AnchorAlign;\n /** Merge into the popup element's ref. Stable. */\n readonly ref: RefCallback<HTMLElement>;\n}\n\nconst toDirection = (value: string): Direction => (value === \"rtl\" ? \"rtl\" : \"ltr\");\n\n/**\n * Where the browser actually placed an anchored popup (design/conventions.md 13.2): the\n * `position-try-fallbacks` option it applied, read from `getComputedStyle(popup).positionArea`\n * while the popup is shown and parsed into a physical side and an alignment. Observation, not\n * positioning: the browser keeps placing the popup; this only reports where.\n *\n * Sources: a `ResizeObserver` on the popup from mount (hidden to shown is a resize, delivered\n * after layout and before paint, so the first painted frame carries the resolved side), `toggle`\n * (and the jsdom shim's), and while shown `scroll` on the document (capture, passive: nested\n * scrollers too) and `resize` on the window, each read at once and again in the next two\n * animation frames (an engine applies the option a scroll calls for only in the layout after the\n * event). The write is urgent (`flushSync`, so the attribute lands before paint; conventions 3.5)\n * and happens only when the rendered pair would change: a popup with a physical `side` that opens\n * where it was asked costs no render (the state holds `null` then). A logical side\n * (`inline-start` / `inline-end`) costs one render on show and one on hide, because `data-side`\n * turns physical while shown. Hidden again resets to the requested values, once the popup no\n * longer renders (an exit animation keeps the resolved side to its end).\n *\n * Idle with `side: null` (a stylesheet's `@position-try` rules may set insets the parser cannot\n * interpret): no reads, the result's `side` is `null`. Runs whatever opened the popup, the\n * native `interestfor` and `commandfor` paths included.\n */\nexport function useResolvedPlacement<Side extends AnchorSide>(\n request: PlacementRequest<Side>,\n): PlacementResult<Side> {\n const { side, align } = request;\n const [resolved, setResolved] = useState<ResolvedPlacement | null>(null);\n // What the state holds (or is about to hold), so an unchanged read schedules nothing.\n const written = useRef<ResolvedPlacement | null>(null);\n // The request the element is rendered with (kept by the layout effect below).\n const requested = useRef<PlacementRequest<Side>>(request);\n // The mounted element's read, for the layout effect below.\n const sync = useRef<((urgent: boolean) => void) | null>(null);\n\n const ref = useCallback((element: HTMLElement | null) => {\n if (element === null) return;\n const doc = element.ownerDocument;\n const win = doc.defaultView ?? window;\n\n const write = (value: ResolvedPlacement | null, urgent: boolean) => {\n // Where it was asked to be: the requested values say so already, no state needed.\n const next =\n value !== null &&\n value.side === requested.current.side &&\n value.align === requested.current.align\n ? null\n : value;\n const previous = written.current;\n if (\n previous === next ||\n (previous !== null &&\n next !== null &&\n previous.side === next.side &&\n previous.align === next.align)\n ) {\n return;\n }\n written.current = next;\n if (urgent) flushSync(() => setResolved(next));\n else setResolved(next);\n };\n\n let listening = false;\n const listen = (on: boolean) => {\n if (on === listening) return;\n listening = on;\n if (on) {\n doc.addEventListener(\"scroll\", onMove, { capture: true, passive: true });\n win.addEventListener(\"resize\", onMove);\n } else {\n doc.removeEventListener(\"scroll\", onMove, { capture: true });\n win.removeEventListener(\"resize\", onMove);\n cancelFrames();\n }\n };\n\n // Both engines (Chromium 156, Firefox 157) apply the scroll-dependent anchoring state, the\n // option and the scroll-compensated geometry alike, in the layout of the frame after the\n // `scroll` event: a read in the handler, or in a rAF registered from it (which runs before\n // that layout), sees the old option even after a forced layout. The timing is unspecified\n // (css-anchor-position-1 §3.3 issue), so read again in the next two frames; a scroll that\n // arrives while that is pending restarts it at the end, so the last scroll is always followed.\n // Separately, Chromium 156 keeps an overflowing option for a top-layer popover once the first\n // frames after the show have passed (a bug, §6.5; upstream item #92): then no frame reads a\n // new option, and `data-side` correctly keeps reporting the applied one. Do not drop the\n // frame reads \"because Chromium does not re-choose\": when it does, it lags like Firefox.\n let frame = 0;\n let again = false;\n const cancelFrames = () => {\n if (frame !== 0) win.cancelAnimationFrame(frame);\n frame = 0;\n again = false;\n };\n const readLater = () => {\n if (frame !== 0) {\n again = true;\n return;\n }\n frame = win.requestAnimationFrame(() => {\n read(true);\n // The popup was found hidden (or `side` turned null): `listen(false)` ended the pair.\n if (!listening) return;\n frame = win.requestAnimationFrame(() => {\n frame = 0;\n read(true);\n if (again && listening) {\n again = false;\n readLater();\n }\n });\n });\n };\n\n const read = (urgent: boolean) => {\n if (requested.current.side === null) {\n listen(false);\n write(null, urgent);\n return;\n }\n if (element.isConnected && element.matches(\":popover-open\")) {\n listen(true);\n const style = getComputedStyle(element);\n const placement = parsePositionArea(\n style.positionArea,\n toDirection(style.direction),\n () => toDirection(getComputedStyle(doc.documentElement).direction),\n style.writingMode || \"horizontal-tb\",\n );\n write(placement, urgent);\n return;\n }\n listen(false);\n // Hidden, but still rendered while an exit animation runs: keep the resolved side to the\n // end of it. The ResizeObserver sees the box go and resets then.\n // Nothing written, nothing to reset: skip the forced layout (every mount and request change\n // of a hidden popup comes through here).\n if (\n written.current !== null &&\n (!element.isConnected || element.getClientRects().length === 0)\n ) {\n write(null, urgent);\n }\n };\n\n function onEvent() {\n read(true);\n }\n function onMove() {\n // In every Chromium 156 / Firefox 157 probe this read still saw the old option (see the\n // frame reads above); kept as cover for WebKit, which was not probed, at the cost of one\n // style read per scroll event.\n read(true);\n if (listening) readLater();\n }\n\n sync.current = read;\n const observer = typeof ResizeObserver === \"function\" ? new ResizeObserver(onEvent) : null;\n observer?.observe(element);\n element.addEventListener(\"toggle\", onEvent);\n return () => {\n observer?.disconnect();\n element.removeEventListener(\"toggle\", onEvent);\n listen(false);\n if (sync.current === read) sync.current = null;\n };\n }, []);\n\n // A changed request while shown places the popup differently: read again (a layout effect, so\n // the corrected attribute is in the same frame). Also how `side={null}` turns the hook off.\n useLayoutEffect(() => {\n requested.current = { side, align };\n sync.current?.(false);\n }, [side, align]);\n\n if (side === null) return { side: null, align, ref };\n return resolved === null ? { side, align, ref } : { ...resolved, ref };\n}\n"],"mappings":";;;;;AA4BA,MAAM,eAAe,UAA8B,UAAU,QAAQ,QAAQ;;;;;;;;;;;;;;;;;;;;;;;AAwB7E,SAAgB,qBACd,SACuB;CACvB,MAAM,EAAE,MAAM,UAAU;CACxB,MAAM,CAAC,UAAU,eAAe,SAAmC,IAAI;CAEvE,MAAM,UAAU,OAAiC,IAAI;CAErD,MAAM,YAAY,OAA+B,OAAO;CAExD,MAAM,OAAO,OAA2C,IAAI;CAE5D,MAAM,MAAM,aAAa,YAAgC;EACvD,IAAI,YAAY,MAAM;EACtB,MAAM,MAAM,QAAQ;EACpB,MAAM,MAAM,IAAI,eAAe;EAE/B,MAAM,SAAS,OAAiC,WAAoB;GAElE,MAAM,OACJ,UAAU,QACV,MAAM,SAAS,UAAU,QAAQ,QACjC,MAAM,UAAU,UAAU,QAAQ,QAC9B,OACA;GACN,MAAM,WAAW,QAAQ;GACzB,IACE,aAAa,QACZ,aAAa,QACZ,SAAS,QACT,SAAS,SAAS,KAAK,QACvB,SAAS,UAAU,KAAK,OAE1B;GAEF,QAAQ,UAAU;GAClB,IAAI,QAAQ,gBAAgB,YAAY,IAAI,CAAC;QACxC,YAAY,IAAI;EACvB;EAEA,IAAI,YAAY;EAChB,MAAM,UAAU,OAAgB;GAC9B,IAAI,OAAO,WAAW;GACtB,YAAY;GACZ,IAAI,IAAI;IACN,IAAI,iBAAiB,UAAU,QAAQ;KAAE,SAAS;KAAM,SAAS;IAAK,CAAC;IACvE,IAAI,iBAAiB,UAAU,MAAM;GACvC,OAAO;IACL,IAAI,oBAAoB,UAAU,QAAQ,EAAE,SAAS,KAAK,CAAC;IAC3D,IAAI,oBAAoB,UAAU,MAAM;IACxC,aAAa;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AO;GAC7C,OAAO,KAAK;GACZ,IAAI,KAAK,YAAY,MAAM,KAAK,UAAU;EAC5C;CACF,GAAG,CAAC,CAAC;CAIL,sBAAsB;EACpB,UAAU,UAAU;GAAE;GAAM;EAAM;EAClC,KAAK,UAAU,KAAK;CACtB,GAAG,CAAC,MAAM,KAAK,CAAC;CAEhB,IAAI,SAAS,MAAM,OAAO;EAAE,MAAM;EAAM;EAAO;CAAI;CACnD,OAAO,aAAa,OAAO;EAAE;EAAM;EAAO;CAAI,IAAI;EAAE,GAAG;EAAU;CAAI;AACvE"}
|
package/dist/menu/index.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import "../internal/platform-attributes.mjs";
|
|
2
2
|
import { MenuItem, MenuItemDetails, MenuItemProps, MenuItemState } from "./menu-item.mjs";
|
|
3
3
|
import { MenuCheckboxItem, MenuCheckboxItemProps, MenuCheckboxItemState } from "./menu-checkbox-item.mjs";
|
|
4
|
+
import { MenuContextTrigger, MenuContextTriggerProps, MenuContextTriggerState } from "./menu-context-trigger.mjs";
|
|
4
5
|
import { MenuGroup, MenuGroupLabel, MenuGroupLabelProps, MenuGroupLabelState, MenuGroupProps, MenuGroupState, MenuSeparator, MenuSeparatorProps, MenuSeparatorState } from "./menu-group.mjs";
|
|
5
6
|
import { MenuAlign, MenuPopup, MenuPopupProps, MenuPopupState, MenuSide } from "./menu-popup.mjs";
|
|
6
7
|
import { MenuRadioGroup, MenuRadioGroupProps, MenuRadioGroupState } from "./menu-radio-group.mjs";
|
|
@@ -15,6 +16,8 @@ import { MenuSubmenuTrigger, MenuSubmenuTriggerProps, MenuSubmenuTriggerState }
|
|
|
15
16
|
* `CheckboxItem`, `RadioGroup` > `RadioItem`, `Group` > `GroupLabel`, `Separator`, and submenus
|
|
16
17
|
* (a `Root` inside a `Popup`, opened by its `SubmenuTrigger`). An item's `action` runs in its own
|
|
17
18
|
* transition with `data-pending` on the item, and the whole menu closes when it settles.
|
|
19
|
+
* `ContextTrigger` is a region whose right-click (or Shift+F10, or a touch long-press) opens the
|
|
20
|
+
* same `Popup` at the pointer; a Root may hold a `Trigger` and a `ContextTrigger`.
|
|
18
21
|
*/
|
|
19
22
|
export declare const Menu: {
|
|
20
23
|
readonly Root: typeof MenuRoot;
|
|
@@ -28,7 +31,8 @@ export declare const Menu: {
|
|
|
28
31
|
readonly GroupLabel: typeof MenuGroupLabel;
|
|
29
32
|
readonly Separator: typeof MenuSeparator;
|
|
30
33
|
readonly SubmenuTrigger: typeof MenuSubmenuTrigger;
|
|
34
|
+
readonly ContextTrigger: typeof MenuContextTrigger;
|
|
31
35
|
};
|
|
32
36
|
//#endregion
|
|
33
|
-
export type { MenuAlign, MenuCheckboxItemProps, MenuCheckboxItemState, MenuGroupLabelProps, MenuGroupLabelState, MenuGroupProps, MenuGroupState, MenuItemDetails, MenuItemProps, MenuItemState, MenuOpenChangeDetails, MenuPopupProps, MenuPopupState, MenuRadioGroupProps, MenuRadioGroupState, MenuRadioItemProps, MenuRadioItemState, MenuRootProps, MenuRootState, MenuSeparatorProps, MenuSeparatorState, MenuSide, MenuSubmenuTriggerProps, MenuSubmenuTriggerState, MenuTriggerProps, MenuTriggerState };
|
|
37
|
+
export type { MenuAlign, MenuCheckboxItemProps, MenuCheckboxItemState, MenuContextTriggerProps, MenuContextTriggerState, MenuGroupLabelProps, MenuGroupLabelState, MenuGroupProps, MenuGroupState, MenuItemDetails, MenuItemProps, MenuItemState, MenuOpenChangeDetails, MenuPopupProps, MenuPopupState, MenuRadioGroupProps, MenuRadioGroupState, MenuRadioItemProps, MenuRadioItemState, MenuRootProps, MenuRootState, MenuSeparatorProps, MenuSeparatorState, MenuSide, MenuSubmenuTriggerProps, MenuSubmenuTriggerState, MenuTriggerProps, MenuTriggerState };
|
|
34
38
|
//# sourceMappingURL=index.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/menu/index.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/menu/index.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;qBAuBa;WACX,aAAI;WACJ,gBAAO;WACP,cAAK;WACL,aAAI;WACJ,qBAAY;WACZ,mBAAU;WACV,kBAAS;WACT,cAAK;WACL,mBAAU;WACV,kBAAS;WACT,uBAAc;WACd,uBAAc"}
|
package/dist/menu/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { MenuCheckboxItem } from "./menu-checkbox-item.mjs";
|
|
2
|
+
import { MenuContextTrigger } from "./menu-context-trigger.mjs";
|
|
2
3
|
import { MenuGroup, MenuGroupLabel, MenuSeparator } from "./menu-group.mjs";
|
|
3
4
|
import { MenuItem } from "./menu-item.mjs";
|
|
4
5
|
import { MenuPopup } from "./menu-popup.mjs";
|
|
@@ -14,6 +15,8 @@ import { MenuSubmenuTrigger } from "./menu-submenu-trigger.mjs";
|
|
|
14
15
|
* `CheckboxItem`, `RadioGroup` > `RadioItem`, `Group` > `GroupLabel`, `Separator`, and submenus
|
|
15
16
|
* (a `Root` inside a `Popup`, opened by its `SubmenuTrigger`). An item's `action` runs in its own
|
|
16
17
|
* transition with `data-pending` on the item, and the whole menu closes when it settles.
|
|
18
|
+
* `ContextTrigger` is a region whose right-click (or Shift+F10, or a touch long-press) opens the
|
|
19
|
+
* same `Popup` at the pointer; a Root may hold a `Trigger` and a `ContextTrigger`.
|
|
17
20
|
*/
|
|
18
21
|
const Menu = {
|
|
19
22
|
Root: MenuRoot,
|
|
@@ -26,7 +29,8 @@ const Menu = {
|
|
|
26
29
|
Group: MenuGroup,
|
|
27
30
|
GroupLabel: MenuGroupLabel,
|
|
28
31
|
Separator: MenuSeparator,
|
|
29
|
-
SubmenuTrigger: MenuSubmenuTrigger
|
|
32
|
+
SubmenuTrigger: MenuSubmenuTrigger,
|
|
33
|
+
ContextTrigger: MenuContextTrigger
|
|
30
34
|
};
|
|
31
35
|
//#endregion
|
|
32
36
|
export { Menu };
|
package/dist/menu/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/menu/index.ts"],"sourcesContent":["// Ships the platform attribute typings (`command`, `commandfor`, `focusgroup`) with this subpath\n// (conventions 7.4). Type-only: the built JS has no import of it.\nimport \"../internal/platform-attributes.js\";\nimport { MenuCheckboxItem } from \"./menu-checkbox-item.js\";\nimport { MenuGroup, MenuGroupLabel, MenuSeparator } from \"./menu-group.js\";\nimport { MenuItem } from \"./menu-item.js\";\nimport { MenuPopup } from \"./menu-popup.js\";\nimport { MenuRadioGroup } from \"./menu-radio-group.js\";\nimport { MenuRadioItem } from \"./menu-radio-item.js\";\nimport { MenuRoot } from \"./menu-root.js\";\nimport { MenuSubmenuTrigger } from \"./menu-submenu-trigger.js\";\nimport { MenuTrigger } from \"./menu-trigger.js\";\n\n/**\n * Menu button (WAI-ARIA APG Menu Button + Menu) on `popover=\"auto\"`: `Root` (no DOM) > `Trigger`\n * (`button[aria-haspopup=menu]`) + `Popup` (`div[popover=auto][role=menu]`) > `Item`,\n * `CheckboxItem`, `RadioGroup` > `RadioItem`, `Group` > `GroupLabel`, `Separator`, and submenus\n * (a `Root` inside a `Popup`, opened by its `SubmenuTrigger`). An item's `action` runs in its own\n * transition with `data-pending` on the item, and the whole menu closes when it settles.\n */\nexport const Menu = {\n Root: MenuRoot,\n Trigger: MenuTrigger,\n Popup: MenuPopup,\n Item: MenuItem,\n CheckboxItem: MenuCheckboxItem,\n RadioGroup: MenuRadioGroup,\n RadioItem: MenuRadioItem,\n Group: MenuGroup,\n GroupLabel: MenuGroupLabel,\n Separator: MenuSeparator,\n SubmenuTrigger: MenuSubmenuTrigger,\n} as const;\n\nexport type { MenuRootProps, MenuRootState, MenuOpenChangeDetails } from \"./menu-root.js\";\nexport type { MenuTriggerProps, MenuTriggerState } from \"./menu-trigger.js\";\nexport type { MenuPopupProps, MenuPopupState, MenuSide, MenuAlign } from \"./menu-popup.js\";\nexport type { MenuItemProps, MenuItemState, MenuItemDetails } from \"./menu-item.js\";\nexport type { MenuCheckboxItemProps, MenuCheckboxItemState } from \"./menu-checkbox-item.js\";\nexport type { MenuRadioGroupProps, MenuRadioGroupState } from \"./menu-radio-group.js\";\nexport type { MenuRadioItemProps, MenuRadioItemState } from \"./menu-radio-item.js\";\nexport type {\n MenuGroupProps,\n MenuGroupState,\n MenuGroupLabelProps,\n MenuGroupLabelState,\n MenuSeparatorProps,\n MenuSeparatorState,\n} from \"./menu-group.js\";\nexport type { MenuSubmenuTriggerProps, MenuSubmenuTriggerState } from \"./menu-submenu-trigger.js\";\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/menu/index.ts"],"sourcesContent":["// Ships the platform attribute typings (`command`, `commandfor`, `focusgroup`) with this subpath\n// (conventions 7.4). Type-only: the built JS has no import of it.\nimport \"../internal/platform-attributes.js\";\nimport { MenuCheckboxItem } from \"./menu-checkbox-item.js\";\nimport { MenuContextTrigger } from \"./menu-context-trigger.js\";\nimport { MenuGroup, MenuGroupLabel, MenuSeparator } from \"./menu-group.js\";\nimport { MenuItem } from \"./menu-item.js\";\nimport { MenuPopup } from \"./menu-popup.js\";\nimport { MenuRadioGroup } from \"./menu-radio-group.js\";\nimport { MenuRadioItem } from \"./menu-radio-item.js\";\nimport { MenuRoot } from \"./menu-root.js\";\nimport { MenuSubmenuTrigger } from \"./menu-submenu-trigger.js\";\nimport { MenuTrigger } from \"./menu-trigger.js\";\n\n/**\n * Menu button (WAI-ARIA APG Menu Button + Menu) on `popover=\"auto\"`: `Root` (no DOM) > `Trigger`\n * (`button[aria-haspopup=menu]`) + `Popup` (`div[popover=auto][role=menu]`) > `Item`,\n * `CheckboxItem`, `RadioGroup` > `RadioItem`, `Group` > `GroupLabel`, `Separator`, and submenus\n * (a `Root` inside a `Popup`, opened by its `SubmenuTrigger`). An item's `action` runs in its own\n * transition with `data-pending` on the item, and the whole menu closes when it settles.\n * `ContextTrigger` is a region whose right-click (or Shift+F10, or a touch long-press) opens the\n * same `Popup` at the pointer; a Root may hold a `Trigger` and a `ContextTrigger`.\n */\nexport const Menu = {\n Root: MenuRoot,\n Trigger: MenuTrigger,\n Popup: MenuPopup,\n Item: MenuItem,\n CheckboxItem: MenuCheckboxItem,\n RadioGroup: MenuRadioGroup,\n RadioItem: MenuRadioItem,\n Group: MenuGroup,\n GroupLabel: MenuGroupLabel,\n Separator: MenuSeparator,\n SubmenuTrigger: MenuSubmenuTrigger,\n ContextTrigger: MenuContextTrigger,\n} as const;\n\nexport type { MenuRootProps, MenuRootState, MenuOpenChangeDetails } from \"./menu-root.js\";\nexport type { MenuTriggerProps, MenuTriggerState } from \"./menu-trigger.js\";\nexport type { MenuPopupProps, MenuPopupState, MenuSide, MenuAlign } from \"./menu-popup.js\";\nexport type { MenuItemProps, MenuItemState, MenuItemDetails } from \"./menu-item.js\";\nexport type { MenuCheckboxItemProps, MenuCheckboxItemState } from \"./menu-checkbox-item.js\";\nexport type { MenuRadioGroupProps, MenuRadioGroupState } from \"./menu-radio-group.js\";\nexport type { MenuRadioItemProps, MenuRadioItemState } from \"./menu-radio-item.js\";\nexport type {\n MenuGroupProps,\n MenuGroupState,\n MenuGroupLabelProps,\n MenuGroupLabelState,\n MenuSeparatorProps,\n MenuSeparatorState,\n} from \"./menu-group.js\";\nexport type { MenuSubmenuTriggerProps, MenuSubmenuTriggerState } from \"./menu-submenu-trigger.js\";\nexport type { MenuContextTriggerProps, MenuContextTriggerState } from \"./menu-context-trigger.js\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAuBA,MAAa,OAAO;CAClB,MAAM;CACN,SAAS;CACT,OAAO;CACP,MAAM;CACN,cAAc;CACd,YAAY;CACZ,WAAW;CACX,OAAO;CACP,YAAY;CACZ,WAAW;CACX,gBAAgB;CAChB,gBAAgB;AAClB"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { RenderProp, StateValue } from "../internal/types.mjs";
|
|
2
|
+
import { CSSProperties, ComponentPropsWithRef, ReactNode } from "react";
|
|
3
|
+
//#region src/menu/menu-context-trigger.d.ts
|
|
4
|
+
export interface MenuContextTriggerState {
|
|
5
|
+
/** Optimistic open state (the Root's). */
|
|
6
|
+
readonly open: boolean;
|
|
7
|
+
/** The Root's transition is pending (an open or a close in flight). */
|
|
8
|
+
readonly pending: boolean;
|
|
9
|
+
readonly disabled: boolean;
|
|
10
|
+
}
|
|
11
|
+
export interface MenuContextTriggerProps extends Omit<ComponentPropsWithRef<"div">, "onContextMenu" | "onPointerDown" | "onPointerMove" | "onPointerUp" | "onPointerCancel" | "className" | "style" | "children"> {
|
|
12
|
+
/** The region does nothing; a `contextmenu` bubbles on to an outer region. `data-disabled`. */
|
|
13
|
+
disabled?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* What a `contextmenu` (or long-press) on an editable target inside the region opens: the
|
|
16
|
+
* browser's own menu (`"native"`, default: copy and paste keep working) or this menu.
|
|
17
|
+
* Editable: a text-taking `<input>`, a `<textarea>`, an element with `isContentEditable`.
|
|
18
|
+
*/
|
|
19
|
+
editableTargets?: "native" | "menu";
|
|
20
|
+
/** Milliseconds a touch or pen pointer must stay down (within 10 px) to open. Default 500. */
|
|
21
|
+
longPressDelay?: number;
|
|
22
|
+
className?: StateValue<string | undefined, MenuContextTriggerState>;
|
|
23
|
+
style?: StateValue<CSSProperties | undefined, MenuContextTriggerState>;
|
|
24
|
+
children?: StateValue<ReactNode, MenuContextTriggerState>;
|
|
25
|
+
render?: RenderProp<ComponentPropsWithRef<"div">, MenuContextTriggerState>;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A region whose `contextmenu` (right-click, Ctrl+click, Shift+F10 / the ContextMenu key at a
|
|
29
|
+
* focused descendant) or touch / pen long-press opens the Root's `Menu.Popup` at the pointer
|
|
30
|
+
* (design/components/context-menu.md). A plain element (default `div`) with no role and no ARIA:
|
|
31
|
+
* the region keeps its own semantics (`render={<tr />}`), and keyboard users reach it through
|
|
32
|
+
* focusable content inside it. A second right-click inside the region while the menu is open moves
|
|
33
|
+
* the menu there. Render the `Menu.Popup` as a sibling of the region where the markup allows; in
|
|
34
|
+
* a table row (`render={<tr />}`), inside one of its cells: events from inside the popup are
|
|
35
|
+
* never the region's.
|
|
36
|
+
*
|
|
37
|
+
* Owned keys (conventions 1.2): `onContextMenu`, `onPointerDown`, `onPointerMove`,
|
|
38
|
+
* `onPointerUp`, `onPointerCancel`, `data-open`, `data-pending`, `data-disabled`, and the style
|
|
39
|
+
* key `WebkitTouchCallout` (`"none"`; the consumer's `style` wins).
|
|
40
|
+
*/
|
|
41
|
+
export declare function MenuContextTrigger({ disabled, editableTargets, longPressDelay, className, style, children, render, ...rest }: MenuContextTriggerProps): ReactNode;
|
|
42
|
+
export declare namespace MenuContextTrigger {
|
|
43
|
+
var displayName: string;
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
//# sourceMappingURL=menu-context-trigger.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"menu-context-trigger.d.mts","names":[],"sources":["../../src/menu/menu-context-trigger.tsx"],"mappings":";;;iBAwBiB;;WAEN;;WAEA;WACA;;iBAGM,gCAAgC,KAC/C;;EAWA;;;;;;EAMA;;EAEA;EACA,YAAY,+BAA+B;EAC3C,QAAQ,WAAW,2BAA2B;EAC9C,WAAW,WAAW,WAAW;EACjC,SAAS,WAAW,8BAA8B;;;;;;;;;;;;;;;;wBAiNpC,qBACd,UACA,iBACA,gBACA,WACA,OACA,UACA,WACG,QACF,0BAAuB;yBATV"}
|