@funstack/ui 0.1.0-alpha.16 → 0.1.0-alpha.17
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/accordion/accordion-root.mjs +1 -1
- package/dist/accordion/accordion-root.mjs.map +1 -1
- package/dist/button/button.d.mts +21 -8
- package/dist/button/button.d.mts.map +1 -1
- package/dist/button/button.mjs +8 -6
- package/dist/button/button.mjs.map +1 -1
- package/dist/checkbox-group/checkbox-group-root.mjs +5 -1
- package/dist/checkbox-group/checkbox-group-root.mjs.map +1 -1
- package/dist/combobox/combobox-popup.d.mts +4 -1
- package/dist/combobox/combobox-popup.d.mts.map +1 -1
- package/dist/combobox/combobox-popup.mjs.map +1 -1
- package/dist/combobox/combobox-root.d.mts +15 -5
- package/dist/combobox/combobox-root.d.mts.map +1 -1
- package/dist/combobox/combobox-root.mjs +9 -11
- package/dist/combobox/combobox-root.mjs.map +1 -1
- package/dist/combobox/combobox-trigger.mjs +7 -2
- package/dist/combobox/combobox-trigger.mjs.map +1 -1
- package/dist/dialog/dialog-close.d.mts +13 -3
- package/dist/dialog/dialog-close.d.mts.map +1 -1
- package/dist/dialog/dialog-close.mjs +3 -2
- package/dist/dialog/dialog-close.mjs.map +1 -1
- package/dist/dialog/dialog-context.mjs.map +1 -1
- package/dist/dialog/dialog-form.d.mts +2 -1
- package/dist/dialog/dialog-form.d.mts.map +1 -1
- package/dist/dialog/dialog-form.mjs.map +1 -1
- package/dist/dialog/dialog-popup.d.mts +9 -4
- package/dist/dialog/dialog-popup.d.mts.map +1 -1
- package/dist/dialog/dialog-popup.mjs +12 -20
- package/dist/dialog/dialog-popup.mjs.map +1 -1
- package/dist/dialog/dialog-root.d.mts +38 -24
- package/dist/dialog/dialog-root.d.mts.map +1 -1
- package/dist/dialog/dialog-root.mjs +10 -8
- package/dist/dialog/dialog-root.mjs.map +1 -1
- package/dist/dialog/dialog-trigger.d.mts +3 -3
- package/dist/dialog/dialog-trigger.mjs.map +1 -1
- package/dist/dialog/index.d.mts +5 -5
- package/dist/dialog/index.mjs +5 -5
- package/dist/dialog/index.mjs.map +1 -1
- package/dist/docs/components/button.md +65 -21
- package/dist/docs/components/checkbox-group.md +12 -3
- package/dist/docs/components/combobox.md +85 -22
- package/dist/docs/components/context-menu.md +8 -8
- package/dist/docs/components/dialog.md +42 -36
- package/dist/docs/components/menu.md +106 -25
- package/dist/docs/components/popover.md +101 -36
- package/dist/docs/components/progress.md +1 -1
- package/dist/docs/components/slider.md +1 -1
- package/dist/docs/components/toast.md +37 -14
- package/dist/docs/components/toolbar.md +2 -1
- package/dist/docs/components/tooltip.md +1 -1
- package/dist/docs/concepts/action-props.md +55 -25
- package/dist/docs/concepts/browser-owned-state.md +27 -12
- package/dist/docs/concepts/pending-and-optimistic.md +15 -2
- package/dist/docs/concepts/render.md +23 -0
- package/dist/docs/concepts/server-components.md +15 -1
- package/dist/docs/concepts/suspense.md +8 -3
- package/dist/docs/concepts/transitions.md +3 -9
- package/dist/docs/getting-started.md +9 -22
- package/dist/docs/index.md +5 -4
- package/dist/docs/roadmap.md +54 -0
- package/dist/docs/why.md +7 -3
- package/dist/index.d.mts +2 -2
- package/dist/internal/call-urgent-handler.mjs +37 -0
- package/dist/internal/call-urgent-handler.mjs.map +1 -0
- package/dist/internal/css-transition-cut.mjs +66 -0
- package/dist/internal/css-transition-cut.mjs.map +1 -0
- package/dist/internal/overlay-popup.mjs.map +1 -1
- package/dist/internal/report-dropped.mjs +45 -0
- package/dist/internal/report-dropped.mjs.map +1 -0
- package/dist/internal/types.d.mts +33 -7
- package/dist/internal/types.d.mts.map +1 -1
- package/dist/internal/use-action-trigger.mjs +32 -17
- package/dist/internal/use-action-trigger.mjs.map +1 -1
- package/dist/internal/use-controllable-action.mjs +52 -16
- package/dist/internal/use-controllable-action.mjs.map +1 -1
- package/dist/internal/use-exit-animation.mjs +1 -1
- package/dist/internal/use-exit-animation.mjs.map +1 -1
- package/dist/internal/use-native-group-reconciliation.mjs +4 -2
- package/dist/internal/use-native-group-reconciliation.mjs.map +1 -1
- package/dist/internal/use-overlay-close.mjs +10 -4
- package/dist/internal/use-overlay-close.mjs.map +1 -1
- package/dist/internal/use-overlay-root.mjs +40 -7
- package/dist/internal/use-overlay-root.mjs.map +1 -1
- package/dist/internal/use-popover-popup.mjs +22 -7
- package/dist/internal/use-popover-popup.mjs.map +1 -1
- package/dist/internal/use-popover-trigger.d.mts +1 -1
- package/dist/internal/use-popover-trigger.d.mts.map +1 -1
- package/dist/internal/use-popover-trigger.mjs +11 -3
- package/dist/internal/use-popover-trigger.mjs.map +1 -1
- package/dist/menu/menu-checkbox-item.d.mts +1 -1
- package/dist/menu/menu-checkbox-item.mjs.map +1 -1
- package/dist/menu/menu-item.d.mts +15 -4
- package/dist/menu/menu-item.d.mts.map +1 -1
- package/dist/menu/menu-item.mjs +10 -5
- package/dist/menu/menu-item.mjs.map +1 -1
- package/dist/menu/menu-outside.mjs +2 -2
- package/dist/menu/menu-outside.mjs.map +1 -1
- package/dist/menu/menu-popup.d.mts +3 -0
- package/dist/menu/menu-popup.d.mts.map +1 -1
- package/dist/menu/menu-popup.mjs.map +1 -1
- package/dist/menu/menu-root.d.mts +27 -12
- package/dist/menu/menu-root.d.mts.map +1 -1
- package/dist/menu/menu-root.mjs +5 -4
- package/dist/menu/menu-root.mjs.map +1 -1
- package/dist/number-field/number-field-root.d.mts.map +1 -1
- package/dist/number-field/number-field-root.mjs +2 -8
- package/dist/number-field/number-field-root.mjs.map +1 -1
- package/dist/popover/index.d.mts +3 -3
- package/dist/popover/index.mjs +3 -3
- package/dist/popover/index.mjs.map +1 -1
- package/dist/popover/popover-close.d.mts +14 -3
- package/dist/popover/popover-close.d.mts.map +1 -1
- package/dist/popover/popover-close.mjs +4 -2
- package/dist/popover/popover-close.mjs.map +1 -1
- package/dist/popover/popover-popup.d.mts +6 -3
- package/dist/popover/popover-popup.d.mts.map +1 -1
- package/dist/popover/popover-popup.mjs +3 -2
- package/dist/popover/popover-popup.mjs.map +1 -1
- package/dist/popover/popover-root.d.mts +31 -18
- package/dist/popover/popover-root.d.mts.map +1 -1
- package/dist/popover/popover-root.mjs +9 -7
- package/dist/popover/popover-root.mjs.map +1 -1
- package/dist/slider/slider-root.d.mts.map +1 -1
- package/dist/slider/slider-root.mjs +2 -8
- package/dist/slider/slider-root.mjs.map +1 -1
- package/dist/toast/toast-action.d.mts +14 -3
- package/dist/toast/toast-action.d.mts.map +1 -1
- package/dist/toast/toast-action.mjs +6 -4
- package/dist/toast/toast-action.mjs.map +1 -1
- package/dist/toast/toast-context.mjs.map +1 -1
- package/dist/toast/toast-queue.mjs.map +1 -1
- package/dist/toast/toast-viewport.d.mts.map +1 -1
- package/dist/toast/toast-viewport.mjs +15 -15
- package/dist/toast/toast-viewport.mjs.map +1 -1
- package/dist/toast/toast.d.mts +10 -7
- package/dist/toast/toast.d.mts.map +1 -1
- package/dist/toast/toast.mjs.map +1 -1
- package/dist/toggle-group/toggle-group-root.mjs +1 -1
- package/dist/toggle-group/toggle-group-root.mjs.map +1 -1
- package/package.json +1 -1
- package/skills/funstack-ui-knowledge/SKILL.md +6 -6
- package/dist/internal/use-superseded-rejection.mjs +0 -47
- package/dist/internal/use-superseded-rejection.mjs.map +0 -1
|
@@ -3,6 +3,7 @@ import { useLatest } from "./use-latest.mjs";
|
|
|
3
3
|
import { useElementCheck } from "./use-element-check.mjs";
|
|
4
4
|
import { isPopoverOpen } from "./popover-open.mjs";
|
|
5
5
|
import { useContentFocusProbe } from "./content-focus-probe.mjs";
|
|
6
|
+
import { withCssTransitionCut } from "./css-transition-cut.mjs";
|
|
6
7
|
import { commandOf, renderOverlayContent, resolveInitialFocus, returnFocusToTrigger, useCloseHold, useInitialFocusTarget } from "./overlay-popup.mjs";
|
|
7
8
|
import { closedByPopoverTarget } from "./invokers.mjs";
|
|
8
9
|
import { useShownNameWarning } from "./use-name-warning.mjs";
|
|
@@ -31,11 +32,14 @@ function insideHiddenPopover(element) {
|
|
|
31
32
|
function usePopoverPopup(ctx, options) {
|
|
32
33
|
const { part, initialFocus, transition } = options;
|
|
33
34
|
const isLibrary = ctx.mode === "library";
|
|
35
|
+
const hasTransition = transition !== void 0;
|
|
34
36
|
const { popupRef, triggerRef, setShown } = ctx;
|
|
35
37
|
const opening = useRef(false);
|
|
36
38
|
const reshowing = useRef(false);
|
|
37
39
|
const awaitingClose = useRef(false);
|
|
40
|
+
const libraryHide = useRef(false);
|
|
38
41
|
const focusWasInside = useRef(false);
|
|
42
|
+
const refusedFact = useRef(false);
|
|
39
43
|
const initialFocusTarget = useInitialFocusTarget(part, "popup");
|
|
40
44
|
const interest = useInterestDismiss(ctx);
|
|
41
45
|
const stopWhileShown = useRef(null);
|
|
@@ -68,13 +72,15 @@ function usePopoverPopup(ctx, options) {
|
|
|
68
72
|
};
|
|
69
73
|
};
|
|
70
74
|
const showPopup = (element) => {
|
|
71
|
-
const
|
|
75
|
+
const refused = refusedFact.current && element.tagName !== "DIALOG";
|
|
76
|
+
refusedFact.current = false;
|
|
77
|
+
const focus = refused ? null : resolveFocusTarget(element);
|
|
72
78
|
const autofocused = focus !== null && !focus.quiet && !focus.target.hasAttribute("autofocus") ? focus.target : null;
|
|
73
79
|
autofocused?.setAttribute("autofocus", "");
|
|
74
80
|
opening.current = true;
|
|
75
81
|
try {
|
|
76
82
|
const source = options.source !== void 0 ? options.source() : triggerRef.current;
|
|
77
|
-
element.showPopover(source != null && source.isConnected ? { source } : void 0);
|
|
83
|
+
withCssTransitionCut(element, hasTransition, () => element.showPopover(source != null && source.isConnected ? { source } : void 0));
|
|
78
84
|
} finally {
|
|
79
85
|
opening.current = false;
|
|
80
86
|
autofocused?.removeAttribute("autofocus");
|
|
@@ -86,8 +92,10 @@ function usePopoverPopup(ctx, options) {
|
|
|
86
92
|
if (!isLibrary || element === null || !isPopoverOpen(element)) return false;
|
|
87
93
|
reshowing.current = true;
|
|
88
94
|
try {
|
|
89
|
-
element
|
|
90
|
-
|
|
95
|
+
withCssTransitionCut(element, hasTransition, () => {
|
|
96
|
+
element.hidePopover();
|
|
97
|
+
showPopup(element);
|
|
98
|
+
});
|
|
91
99
|
} finally {
|
|
92
100
|
reshowing.current = false;
|
|
93
101
|
}
|
|
@@ -111,13 +119,15 @@ function usePopoverPopup(ctx, options) {
|
|
|
111
119
|
if (!isLibrary) return;
|
|
112
120
|
const element = popupRef.current;
|
|
113
121
|
if (element === null || !element.isConnected) return;
|
|
122
|
+
if (!ctx.committed) refusedFact.current = false;
|
|
114
123
|
if (!ctx.pending && ctx.latestInFlight() === null) {
|
|
115
124
|
const shown = isPopoverOpen(element);
|
|
116
125
|
if (ctx.committed && !shown) {
|
|
117
126
|
if (!awaitingClose.current) showPopup(element);
|
|
118
127
|
} else if (!ctx.committed && shown) {
|
|
119
128
|
ctx.recordCloseIntent(void 0);
|
|
120
|
-
|
|
129
|
+
libraryHide.current = true;
|
|
130
|
+
withCssTransitionCut(element, hasTransition, () => element.hidePopover());
|
|
121
131
|
if (focusWasInside.current) returnFocus(element);
|
|
122
132
|
}
|
|
123
133
|
}
|
|
@@ -151,6 +161,8 @@ function usePopoverPopup(ctx, options) {
|
|
|
151
161
|
const element = popupRef.current;
|
|
152
162
|
if (element === null || event.target !== element) return;
|
|
153
163
|
awaitingClose.current = false;
|
|
164
|
+
const echo = libraryHide.current;
|
|
165
|
+
libraryHide.current = false;
|
|
154
166
|
const lostInterest = interest.takeLostInterest();
|
|
155
167
|
if (isLibrary) setShown(isPopoverOpen(element));
|
|
156
168
|
else syncOpen(element);
|
|
@@ -161,8 +173,11 @@ function usePopoverPopup(ctx, options) {
|
|
|
161
173
|
if (wasInside && !lostInterest) returnFocus(element);
|
|
162
174
|
const intent = ctx.takeCloseIntent();
|
|
163
175
|
if (isLibrary) {
|
|
164
|
-
exit.afterClose(element,
|
|
165
|
-
|
|
176
|
+
exit.afterClose(element, echo);
|
|
177
|
+
if (!echo) {
|
|
178
|
+
refusedFact.current = true;
|
|
179
|
+
ctx.notifyClosed(intent?.event ?? event, intent?.reason ?? "dismiss");
|
|
180
|
+
}
|
|
166
181
|
}
|
|
167
182
|
}
|
|
168
183
|
const { newState } = event;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-popover-popup.mjs","names":[],"sources":["../../src/internal/use-popover-popup.ts"],"sourcesContent":["\"use client\";\nimport { useCallback, useEffectEvent, useLayoutEffect, useRef } from \"react\";\nimport type { ReactNode, RefObject, ViewTransitionClass } from \"react\";\nimport { useContentFocusProbe } from \"./content-focus-probe.js\";\nimport { closedByPopoverTarget } from \"./invokers.js\";\nimport { supportsFocusVisibleOption } from \"./focus-visible.js\";\nimport type { QuietFocusTarget } from \"./focus-visible.js\";\nimport {\n commandOf,\n renderOverlayContent,\n resolveInitialFocus,\n returnFocusToTrigger,\n useCloseHold,\n useInitialFocusTarget,\n} from \"./overlay-popup.js\";\nimport { isPopoverOpen } from \"./popover-open.js\";\nimport { useElementCheck } from \"./use-element-check.js\";\nimport { useInterestDismiss } from \"./use-interest-dismiss.js\";\nimport { useLatest } from \"./use-latest.js\";\nimport { useShownNameWarning } from \"./use-name-warning.js\";\nimport type { PopoverRootValue } from \"./use-overlay-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 /** The consumer's `keepMounted`: `renderContent` keeps the children in an `<Activity>`. */\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 /**\n * Installs listeners that live while the popup is shown (Menu: the outside right-click\n * listeners; Menu and Combobox: the foreign invoker press guard) and returns their cleanup.\n * Called at the `toggle` that finds the popup shown, and again when the ref attaches to a\n * shown popup; the cleanup runs at the `toggle` of the close and when the element detaches.\n */\n readonly whileShown?: ((popup: HTMLElement) => () => void) | undefined;\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\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 * The popup's children by mode, `visible` and `keepMounted` (`renderOverlayContent`), inside\n * the focus probe: when a close commit unmounts the content while it holds focus (a\n * library-driven close with `transition`), the hide that follows returns focus to the Trigger\n * (design 6.2).\n */\n readonly renderContent: (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\n/** The popup roles that require a name (a listbox's name is checked on the List). */\nfunction needsName(element: Element): boolean {\n const role = element.getAttribute(\"role\");\n return role === \"dialog\" || role === \"alertdialog\" || role === \"menu\";\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 * (and `interest` / `loseinterest`), close intents, the focus-return fallback, the JS-free\n * `data-open` sync, the deferred unmount while a hide animates, and `initialFocus`. Shared by\n * Popover, Menu and Combobox.\n */\nexport function usePopoverPopup(\n ctx: PopoverRootValue,\n options: UsePopoverPopupOptions,\n): PopoverPopupHook {\n const { part, initialFocus, transition } = options;\n const isLibrary = ctx.mode === \"library\";\n const { popupRef, triggerRef, setShown } = 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 initialFocusTarget = useInitialFocusTarget(part, \"popup\");\n // `interestfor` (library mode, popover.md lost-interest amendment).\n const interest = useInterestDismiss(ctx);\n // The cleanup of `whileShown` while the popup is shown.\n const stopWhileShown = useRef<(() => void) | null>(null);\n const stopListeningWhileShown = (): void => {\n stopWhileShown.current?.();\n stopWhileShown.current = null;\n };\n\n // `held`: children stay mounted while the hide animates (design 4.4).\n const exit = useCloseHold(ctx.committed, isLibrary, transition);\n\n // Whether the content unmounted while holding focus since the popup was last shown.\n const contentProbe = useContentFocusProbe(popupRef);\n const { wrapContent } = contentProbe;\n\n const syncOpen = (element: HTMLElement): void => {\n // JS-free mode only (design 9): read the DOM, not `newState`, so a stale task cannot\n // lie.\n triggerRef.current?.toggleAttribute(\"data-open\", isPopoverOpen(element));\n };\n\n // Design 6.2 rule 2, for a close that began with focus inside; not to a Trigger inside a\n // hidden popover.\n const returnFocus = (element: HTMLElement): void =>\n returnFocusToTrigger(element, triggerRef.current, (trigger) => !insideHiddenPopover(trigger));\n\n // --- Opening ------------------------------------------------------------------------------\n\n // The initial focus target inside the popup, and whether it is focused without the focus ring.\n const resolveFocusTarget = (\n element: HTMLElement,\n ): { readonly target: HTMLElement; readonly quiet: boolean } | 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 if (!element.contains(target)) return null;\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 !(resolved instanceof HTMLElement) && supportsFocusVisibleOption(element.ownerDocument);\n return { target, quiet };\n }\n const target = initialFocusTarget(element, initialFocus);\n return target === null ? null : { target, quiet: false };\n };\n\n const showPopup = (element: HTMLElement): void => {\n const focus = resolveFocusTarget(element);\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 autofocused =\n focus !== null && !focus.quiet && !focus.target.hasAttribute(\"autofocus\")\n ? focus.target\n : null;\n autofocused?.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 autofocused?.removeAttribute(\"autofocus\");\n }\n if (focus?.quiet === true && isPopoverOpen(element) && element.contains(focus.target)) {\n focus.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 show half runs under `showPopup`'s opening flag (conventions 3.5, the\n // move); the hide half needs none, only an opening `beforetoggle` reads it.\n reshowing.current = true;\n try {\n element.hidePopover();\n showPopup(element);\n } finally {\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 syncOpen(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. One effect covers the normal open, `defaultOpen`,\n // a controlled open or close, a library-driven close after `openChangeAction(false)` settled,\n // the re-open of a refused dismissal, and the close after a refused or failed pre-hydration\n // open. The Root's value is memoised, so an unrelated Root render does not re-render the Popup\n // and does not run this; every path that makes the DOM diverge from state goes through a\n // dispatch, whose `pending` flips do. Do not rely on an unrelated render to re-apply state.\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: 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 exit.releaseIfIdle(ctx.committed, awaitingClose.current, isPopoverOpen(element));\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 interest.noteOpen(toggle);\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 // A foreign `popovertarget` invoker closing the popup (design 2.3): `source` is the\n // button, and `null` for Escape, light dismiss and script. The library's own parts never\n // get here with a source (their clicks are cancelled; their closes are `hidePopover()`). A\n // button with `commandfor` as well runs its command, whose intent comes first and stays.\n if (isLibrary && closedByPopoverTarget(toggle, element)) {\n ctx.recordCloseIntent({ reason: \"activate\", event }, { unlessRecorded: true });\n }\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 };\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 const lostInterest = interest.takeLostInterest();\n // JS-free mode writes the Trigger's `data-open`; library mode mirrors into the Root, which\n // non-button Triggers and the Combobox Input render as `aria-expanded`.\n if (isLibrary) setShown(isPopoverOpen(element));\n else syncOpen(element);\n\n if (!isPopoverOpen(element)) {\n interest.noteClosed();\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. Not after a\n // lost-interest hide: the platform never focuses the invoker there, and focusing a Trigger\n // with `interestfor` would give it interest again and re-open the popup.\n const wasInside = focusWasInside.current;\n focusWasInside.current = false;\n if (wasInside && !lostInterest) returnFocus(element);\n\n const intent = ctx.takeCloseIntent();\n if (isLibrary) {\n // An echo: `false` already intended (the library's own hide, or a dismissal during a\n // pending part-initiated close).\n exit.afterClose(element, !ctx.intended());\n ctx.notifyClosed(intent?.event ?? event, intent?.reason ?? \"dismiss\");\n }\n }\n const { newState } = event as ToggleEvent;\n if (newState === \"open\" && isPopoverOpen(element)) {\n stopWhileShown.current ??= options.whileShown?.(element) ?? null;\n } else if (newState === \"closed\") {\n stopListeningWhileShown();\n }\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 = commandOf(event);\n // A foreign `commandfor` invoker closing the popup (design 2.3). Opens are caught at\n // `beforetoggle`.\n // The `command` event fires whatever the popover's state (HTML): a `hide-popover` at a\n // hidden popup closes nothing and must leave no intent for a later close.\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 = useLatest({\n onBeforeToggle,\n onToggle,\n onCommand,\n onInterest: interest.onInterest,\n onLoseInterest: interest.onLoseInterest,\n });\n const latestWhileShown = useLatest(options.whileShown);\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 checkElement = useElementCheck(\n isDev &&\n ((element) =>\n element.tagName !== \"DIALOG\" && element.tagName !== \"DIV\"\n ? `${part}: expected to render a <dialog> or a <div> but got ` +\n `<${element.tagName.toLowerCase()}>.`\n : null),\n );\n const resetInterest = interest.reset;\n const setElement = useCallback(\n (element: HTMLElement) => {\n checkElement?.(element);\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 // Fired only where `interestfor` exists (Chromium).\n [\"interest\", (event) => handlers.current.onInterest(event)],\n [\"loseinterest\", (event) => handlers.current.onLoseInterest(event)],\n ];\n for (const [type, listener] of listeners) element.addEventListener(type, listener);\n // A pre-hydration open is shown from the first commit; a popup removed while open fires no\n // `toggle`, so the detach clears the mirror.\n if (isLibrary) setShown(isPopoverOpen(element));\n // The ref re-runs whenever the merged ref changes (an inline consumer `ref`), and its detach\n // stopped the listeners: a popup that is still shown gets them back, as no `toggle` comes.\n if (isPopoverOpen(element)) {\n stopWhileShown.current ??= latestWhileShown.current?.(element) ?? null;\n }\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 resetInterest();\n stopListeningWhileShown();\n if (isLibrary) setShown(false);\n autofocused?.removeAttribute(\"autofocus\");\n if (popupRef.current === element) popupRef.current = null;\n };\n },\n [\n checkElement,\n popupRef,\n setShown,\n handlers,\n latestWhileShown,\n isLibrary,\n initialFocus,\n resetInterest,\n ],\n );\n\n // Development check (design 9): a `dialog`, `alertdialog` or `menu` role needs a name once the\n // popup is (or can be) shown.\n useShownNameWarning(\n popupRef,\n !isLibrary || ctx.committed,\n isDev &&\n `${part}: the popup has no accessible name. Render a Title, or give the Popup an ` +\n \"`aria-label`.\",\n needsName,\n );\n\n const visible = ctx.committed || exit.held;\n const renderContent = (children: () => ReactNode): ReactNode =>\n renderOverlayContent(\n { isLibrary, keepMounted: options.keepMounted, visible, wrapContent },\n children,\n );\n return { setElement, visible, renderContent, reshow };\n}\n"],"mappings":";;;;;;;;;;;;AA6EA,MAAM,QAAQ,QAAQ,IAAI,aAAa;;AAGvC,SAAS,UAAU,SAA2B;CAC5C,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+
{"version":3,"file":"use-popover-popup.mjs","names":[],"sources":["../../src/internal/use-popover-popup.ts"],"sourcesContent":["\"use client\";\nimport { useCallback, useEffectEvent, useLayoutEffect, useRef } from \"react\";\nimport type { ReactNode, RefObject, ViewTransitionClass } from \"react\";\nimport { useContentFocusProbe } from \"./content-focus-probe.js\";\nimport { withCssTransitionCut } from \"./css-transition-cut.js\";\nimport { closedByPopoverTarget } from \"./invokers.js\";\nimport { supportsFocusVisibleOption } from \"./focus-visible.js\";\nimport type { QuietFocusTarget } from \"./focus-visible.js\";\nimport {\n commandOf,\n renderOverlayContent,\n resolveInitialFocus,\n returnFocusToTrigger,\n useCloseHold,\n useInitialFocusTarget,\n} from \"./overlay-popup.js\";\nimport { isPopoverOpen } from \"./popover-open.js\";\nimport { useElementCheck } from \"./use-element-check.js\";\nimport { useInterestDismiss } from \"./use-interest-dismiss.js\";\nimport { useLatest } from \"./use-latest.js\";\nimport { useShownNameWarning } from \"./use-name-warning.js\";\nimport type { PopoverRootValue } from \"./use-overlay-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 /** The consumer's `keepMounted`: `renderContent` keeps the children in an `<Activity>`. */\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 /**\n * Installs listeners that live while the popup is shown (Menu: the outside right-click\n * listeners; Menu and Combobox: the foreign invoker press guard) and returns their cleanup.\n * Called at the `toggle` that finds the popup shown, and again when the ref attaches to a\n * shown popup; the cleanup runs at the `toggle` of the close and when the element detaches.\n */\n readonly whileShown?: ((popup: HTMLElement) => () => void) | undefined;\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\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 * The popup's children by mode, `visible` and `keepMounted` (`renderOverlayContent`), inside\n * the focus probe: when a close commit unmounts the content while it holds focus (a\n * library-driven close with `transition`), the hide that follows returns focus to the Trigger\n * (design 6.2).\n */\n readonly renderContent: (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\n/** The popup roles that require a name (a listbox's name is checked on the List). */\nfunction needsName(element: Element): boolean {\n const role = element.getAttribute(\"role\");\n return role === \"dialog\" || role === \"alertdialog\" || role === \"menu\";\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 * (and `interest` / `loseinterest`), close intents, the focus-return fallback, the JS-free\n * `data-open` sync, the deferred unmount while a hide animates, and `initialFocus`. Shared by\n * Popover, Menu and Combobox.\n */\nexport function usePopoverPopup(\n ctx: PopoverRootValue,\n options: UsePopoverPopupOptions,\n): PopoverPopupHook {\n const { part, initialFocus, transition } = options;\n const isLibrary = ctx.mode === \"library\";\n // The library's own shows and hides cut the popup's CSS transitions while a view transition\n // runs (conventions 5.1).\n const hasTransition = transition !== undefined;\n const { popupRef, triggerRef, setShown } = 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 // Set right before the library's own `hidePopover()` (the apply effect), consumed by the next\n // `toggle`: that hide is the library's, every other is a fact (overlay-open-change.md 2.2). The\n // echo is recognised by its source, as Dialog's `libraryClose`, not by what is intended: a\n // dismissal during a pending part close is a fact and must be reported.\n const libraryHide = 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 // Set when a fact hid the popup, cleared once a commit has `committed` false (the fact was\n // adopted): a show while it is set is the re-open of a dismissal a controlled parent refused,\n // which leaves focus where the dismissal put it (no initial focus; #155: otherwise Tab and\n // Escape could never leave a menu whose parent keeps `open={true}`).\n const refusedFact = useRef(false);\n const initialFocusTarget = useInitialFocusTarget(part, \"popup\");\n // `interestfor` (library mode, popover.md lost-interest amendment).\n const interest = useInterestDismiss(ctx);\n // The cleanup of `whileShown` while the popup is shown.\n const stopWhileShown = useRef<(() => void) | null>(null);\n const stopListeningWhileShown = (): void => {\n stopWhileShown.current?.();\n stopWhileShown.current = null;\n };\n\n // `held`: children stay mounted while the hide animates (design 4.4).\n const exit = useCloseHold(ctx.committed, isLibrary, transition);\n\n // Whether the content unmounted while holding focus since the popup was last shown.\n const contentProbe = useContentFocusProbe(popupRef);\n const { wrapContent } = contentProbe;\n\n const syncOpen = (element: HTMLElement): void => {\n // JS-free mode only (design 9): read the DOM, not `newState`, so a stale task cannot\n // lie.\n triggerRef.current?.toggleAttribute(\"data-open\", isPopoverOpen(element));\n };\n\n // Design 6.2 rule 2, for a close that began with focus inside; not to a Trigger inside a\n // hidden popover.\n const returnFocus = (element: HTMLElement): void =>\n returnFocusToTrigger(element, triggerRef.current, (trigger) => !insideHiddenPopover(trigger));\n\n // --- Opening ------------------------------------------------------------------------------\n\n // The initial focus target inside the popup, and whether it is focused without the focus ring.\n const resolveFocusTarget = (\n element: HTMLElement,\n ): { readonly target: HTMLElement; readonly quiet: boolean } | 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 if (!element.contains(target)) return null;\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 !(resolved instanceof HTMLElement) && supportsFocusVisibleOption(element.ownerDocument);\n return { target, quiet };\n }\n const target = initialFocusTarget(element, initialFocus);\n return target === null ? null : { target, quiet: false };\n };\n\n const showPopup = (element: HTMLElement): void => {\n // A `<dialog>` (Popover's default) runs the dialog focusing steps on every show whatever the\n // library does, so the skip applies where the platform focuses nothing itself (Menu, a\n // `<div>` Popover; Combobox never moves focus).\n const refused = refusedFact.current && element.tagName !== \"DIALOG\";\n refusedFact.current = false;\n const focus = refused ? null : resolveFocusTarget(element);\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 autofocused =\n focus !== null && !focus.quiet && !focus.target.hasAttribute(\"autofocus\")\n ? focus.target\n : null;\n autofocused?.setAttribute(\"autofocus\", \"\");\n opening.current = true;\n try {\n const source = options.source !== undefined ? options.source() : triggerRef.current;\n withCssTransitionCut(element, hasTransition, () =>\n element.showPopover(source != null && source.isConnected ? { source } : undefined),\n );\n } finally {\n opening.current = false;\n autofocused?.removeAttribute(\"autofocus\");\n }\n if (focus?.quiet === true && isPopoverOpen(element) && element.contains(focus.target)) {\n focus.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 show half runs under `showPopup`'s opening flag (conventions 3.5, the\n // move); the hide half needs none, only an opening `beforetoggle` reads it.\n reshowing.current = true;\n try {\n // One cut around both halves (the show's own cut, nested, does nothing).\n withCssTransitionCut(element, hasTransition, () => {\n element.hidePopover();\n showPopup(element);\n });\n } finally {\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 syncOpen(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. One effect covers the normal open, `defaultOpen`,\n // a controlled open or close, a library-driven close after `openChangeAction(false)` settled,\n // the re-open of a dismissal a controlled parent refused (in the fact's own commit, or once the\n // requests it superseded settled: overlay-open-change.md 2.4), and the close after a refused or\n // failed pre-hydration open. The Root's value is memoised, so an unrelated Root render does not\n // re-render the Popup and does not run this; every path that makes the DOM diverge from state\n // goes through a dispatch, whose `pending` flips do, or a fact, whose `epoch` does. Do not rely\n // on an unrelated render to re-apply state.\n useLayoutEffect(() => {\n if (!isLibrary) return;\n const element = popupRef.current;\n if (element === null || !element.isConnected) return;\n // The fact was adopted (or anything else closed the popup in state): a later show is a new\n // open, with initial focus.\n if (!ctx.committed) refusedFact.current = false;\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 // The library's own hide: the `toggle` listener recognises it by `libraryHide` and\n // reports nothing.\n ctx.recordCloseIntent(undefined);\n libraryHide.current = true;\n withCssTransitionCut(element, hasTransition, () => element.hidePopover());\n // The close commit may have unmounted the focused content before the hide: 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 exit.releaseIfIdle(ctx.committed, awaitingClose.current, isPopoverOpen(element));\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 interest.noteOpen(toggle);\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 // A foreign `popovertarget` invoker closing the popup (design 2.3): `source` is the\n // button, and `null` for Escape, light dismiss and script. The library's own parts never\n // get here with a source (their clicks are cancelled; their closes are `hidePopover()`). A\n // button with `commandfor` as well runs its command, whose intent comes first and stays.\n if (isLibrary && closedByPopoverTarget(toggle, element)) {\n ctx.recordCloseIntent({ reason: \"activate\", event }, { unlessRecorded: true });\n }\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 };\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 // Consumed by every `toggle`, whatever it reports: a hide the engine coalesced with a show\n // must not mark a later dismissal as the library's.\n const echo = libraryHide.current;\n libraryHide.current = false;\n const lostInterest = interest.takeLostInterest();\n // JS-free mode writes the Trigger's `data-open`; library mode mirrors into the Root, which\n // non-button Triggers and the Combobox Input render as `aria-expanded`.\n if (isLibrary) setShown(isPopoverOpen(element));\n else syncOpen(element);\n\n if (!isPopoverOpen(element)) {\n interest.noteClosed();\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. Not after a\n // lost-interest hide: the platform never focuses the invoker there, and focusing a Trigger\n // with `interestfor` would give it interest again and re-open the popup.\n const wasInside = focusWasInside.current;\n focusWasInside.current = false;\n if (wasInside && !lostInterest) returnFocus(element);\n\n const intent = ctx.takeCloseIntent();\n if (isLibrary) {\n exit.afterClose(element, echo);\n // Anything but the library's own hide is a fact: committed `false` now (urgent, this\n // event's commit) and `onOpenChange(false)` (overlay-open-change.md 2.3).\n if (!echo) {\n refusedFact.current = true;\n ctx.notifyClosed(intent?.event ?? event, intent?.reason ?? \"dismiss\");\n }\n }\n }\n const { newState } = event as ToggleEvent;\n if (newState === \"open\" && isPopoverOpen(element)) {\n stopWhileShown.current ??= options.whileShown?.(element) ?? null;\n } else if (newState === \"closed\") {\n stopListeningWhileShown();\n }\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 = commandOf(event);\n // A foreign `commandfor` invoker closing the popup (design 2.3). Opens are caught at\n // `beforetoggle`.\n // The `command` event fires whatever the popover's state (HTML): a `hide-popover` at a\n // hidden popup closes nothing and must leave no intent for a later close.\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 = useLatest({\n onBeforeToggle,\n onToggle,\n onCommand,\n onInterest: interest.onInterest,\n onLoseInterest: interest.onLoseInterest,\n });\n const latestWhileShown = useLatest(options.whileShown);\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 checkElement = useElementCheck(\n isDev &&\n ((element) =>\n element.tagName !== \"DIALOG\" && element.tagName !== \"DIV\"\n ? `${part}: expected to render a <dialog> or a <div> but got ` +\n `<${element.tagName.toLowerCase()}>.`\n : null),\n );\n const resetInterest = interest.reset;\n const setElement = useCallback(\n (element: HTMLElement) => {\n checkElement?.(element);\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 // Fired only where `interestfor` exists (Chromium).\n [\"interest\", (event) => handlers.current.onInterest(event)],\n [\"loseinterest\", (event) => handlers.current.onLoseInterest(event)],\n ];\n for (const [type, listener] of listeners) element.addEventListener(type, listener);\n // A pre-hydration open is shown from the first commit; a popup removed while open fires no\n // `toggle`, so the detach clears the mirror.\n if (isLibrary) setShown(isPopoverOpen(element));\n // The ref re-runs whenever the merged ref changes (an inline consumer `ref`), and its detach\n // stopped the listeners: a popup that is still shown gets them back, as no `toggle` comes.\n if (isPopoverOpen(element)) {\n stopWhileShown.current ??= latestWhileShown.current?.(element) ?? null;\n }\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 resetInterest();\n stopListeningWhileShown();\n if (isLibrary) setShown(false);\n autofocused?.removeAttribute(\"autofocus\");\n if (popupRef.current === element) popupRef.current = null;\n };\n },\n [\n checkElement,\n popupRef,\n setShown,\n handlers,\n latestWhileShown,\n isLibrary,\n initialFocus,\n resetInterest,\n ],\n );\n\n // Development check (design 9): a `dialog`, `alertdialog` or `menu` role needs a name once the\n // popup is (or can be) shown.\n useShownNameWarning(\n popupRef,\n !isLibrary || ctx.committed,\n isDev &&\n `${part}: the popup has no accessible name. Render a Title, or give the Popup an ` +\n \"`aria-label`.\",\n needsName,\n );\n\n const visible = ctx.committed || exit.held;\n const renderContent = (children: () => ReactNode): ReactNode =>\n renderOverlayContent(\n { isLibrary, keepMounted: options.keepMounted, visible, wrapContent },\n children,\n );\n return { setElement, visible, renderContent, reshow };\n}\n"],"mappings":";;;;;;;;;;;;;AA8EA,MAAM,QAAQ,QAAQ,IAAI,aAAa;;AAGvC,SAAS,UAAU,SAA2B;CAC5C,MAAM,OAAO,QAAQ,aAAa,MAAM;CACxC,OAAO,SAAS,YAAY,SAAS,iBAAiB,SAAS;AACjE;;AAGA,SAAS,oBAAoB,SAA2B;CACtD,KAAK,IAAI,OAAO,QAAQ,eAAe,SAAS,MAAM,OAAO,KAAK,eAChE,IAAI,KAAK,aAAa,SAAS,KAAK,CAAC,cAAc,IAAI,GAAG,OAAO;CAEnE,OAAO;AACT;;;;;;;;AASA,SAAgB,gBACd,KACA,SACkB;CAClB,MAAM,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Aa,QAAQ;EAAa;EAAS;CAAY,GACpE,QACF;CACF,OAAO;EAAE;EAAY;EAAS;EAAe;CAAO;AACtD"}
|
|
@@ -7,7 +7,7 @@ import { ComponentPropsWithRef } from "react";
|
|
|
7
7
|
export interface PopoverTriggerHookState {
|
|
8
8
|
/** Optimistic open state. A Popover in JS-free mode (the browser owns it) reports `false`. */
|
|
9
9
|
readonly open: boolean;
|
|
10
|
-
/** The Root's transition is pending
|
|
10
|
+
/** The Root's transition is pending: an open or a part-initiated close (never a dismissal). */
|
|
11
11
|
readonly pending: boolean;
|
|
12
12
|
/** The `disabled` prop. */
|
|
13
13
|
readonly disabled: boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-popover-trigger.d.mts","names":[],"sources":["../../src/internal/use-popover-trigger.ts"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"use-popover-trigger.d.mts","names":[],"sources":["../../src/internal/use-popover-trigger.ts"],"mappings":";;;;;;iBA8CiB;;WAEN;;WAEA;;WAEA"}
|
|
@@ -23,7 +23,7 @@ const RESERVED_INVOKER_KEYS = {
|
|
|
23
23
|
* hides it before the click. `popover` and `popup` are the nouns the text uses for it.
|
|
24
24
|
*/
|
|
25
25
|
function nonInvokerMessage(part, tag, popover, popup) {
|
|
26
|
-
return `${part}: rendered a <${tag}>. Only a <button> is the ${popover}'s invoker: a press on this element while the ${popup} is open closes it through light dismiss before the click, so the close is
|
|
26
|
+
return `${part}: rendered a <${tag}>. Only a <button> is the ${popover}'s invoker: a press on this element while the ${popup} is open closes it through light dismiss before the click, so the close is a dismissal (reported to onOpenChange) rather than a request: openChangeAction(false) is not called, cannot veto it and there is no view transition. Render a <button>.`;
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
29
29
|
* The Trigger of a popover-like component (design/components/popover.md 14.3):
|
|
@@ -72,8 +72,16 @@ function usePopoverTrigger(ctx, options) {
|
|
|
72
72
|
return;
|
|
73
73
|
}
|
|
74
74
|
const native = event.nativeEvent;
|
|
75
|
-
if (ctx.intended())
|
|
76
|
-
|
|
75
|
+
if (ctx.intended()) {
|
|
76
|
+
if (ctx.hiddenBeforeToggle()) {
|
|
77
|
+
ctx.recordCloseIntent({
|
|
78
|
+
reason: "activate",
|
|
79
|
+
event: native
|
|
80
|
+
});
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
ctx.requestClose(native, "activate");
|
|
84
|
+
} else {
|
|
77
85
|
beforeOpen?.(event);
|
|
78
86
|
ctx.requestOpen(native, "activate");
|
|
79
87
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-popover-trigger.mjs","names":[],"sources":["../../src/internal/use-popover-trigger.ts"],"sourcesContent":["\"use client\";\nimport { useCallback } from \"react\";\nimport type { ComponentPropsWithRef, DOMAttributes, MouseEvent } from \"react\";\nimport { dataAttr } from \"./data-attr.js\";\nimport { isPopoverOpen } from \"./popover-open.js\";\nimport { registerTrigger } from \"./trigger-ref.js\";\nimport type { RenderProp } from \"./types.js\";\nimport { useElementCheck } from \"./use-element-check.js\";\nimport { disabledButton, rendersButton } from \"./use-render.js\";\nimport type { DataAttributes } from \"./use-render.js\";\nimport type { PopoverRootValue } from \"./use-overlay-root.js\";\n\nconst isDev = process.env.NODE_ENV !== \"production\";\n\n/**\n * Owned and never written on an overlay Trigger (Popover, Menu, Combobox): the default\n * `popovertarget` action, which a render element must not change, and an Invoker Command, which\n * would take precedence over `popovertarget` and act on another element.\n */\nexport const RESERVED_INVOKER_KEYS = {\n popoverTargetAction: undefined,\n commandfor: undefined,\n command: undefined,\n} as const;\n\n/**\n * The library-mode dev warning for a Trigger rendered as another element than a `<button>`: only\n * an invoker button is spared by the popover's light dismiss, so a press while the popup is open\n * hides it before the click. `popover` and `popup` are the nouns the text uses for it.\n */\nexport function nonInvokerMessage(\n part: string,\n tag: string,\n popover: string,\n popup: string,\n): string {\n return (\n `${part}: rendered a <${tag}>. Only a <button> is the ${popover}'s invoker: a press on ` +\n `this element while the ${popup} is open closes it through light dismiss before the ` +\n \"click, so the close is not awaited, openChangeAction(false) cannot veto it and \" +\n \"there is no exit transition. Render a <button>.\"\n );\n}\n\n/** The state of an overlay Trigger (Popover.Trigger, Menu.Trigger and its siblings). */\nexport interface PopoverTriggerHookState {\n /** Optimistic open state. A Popover in JS-free mode (the browser owns it) reports `false`. */\n readonly open: boolean;\n /** The Root's transition is pending (an open, a part-initiated close or a dismissal notification). */\n readonly pending: boolean;\n /** The `disabled` prop. */\n readonly disabled: boolean;\n}\n\n/** Event handlers a component layers onto the Trigger's own (Menu's keys and hover intent). */\nexport type PopoverTriggerHandlers = Pick<\n DOMAttributes<HTMLButtonElement>,\n \"onClick\" | \"onKeyDown\" | \"onMouseDown\" | \"onPointerMove\" | \"onPointerEnter\" | \"onPointerLeave\"\n>;\n\nexport interface UsePopoverTriggerOptions<State> {\n /** Public name of the part for development warnings (`\"Popover.Trigger\"`). */\n readonly part: string;\n readonly disabled: boolean;\n readonly render: RenderProp<ComponentPropsWithRef<\"button\">, State> | undefined;\n /**\n * Extra handlers, merged in front of the Trigger's own: for a key both define, the extra one\n * runs first (the consumer's pass-through handler still runs before both, conventions 1.2).\n */\n readonly handlers?: PopoverTriggerHandlers;\n /**\n * What a click does in library mode, after the command has been cancelled and `disabled`\n * checked, replacing the default toggle against `intended()` (`requestOpen` / `requestClose`).\n * Menu's SubmenuTrigger opens, or moves focus into an already open submenu, and never closes;\n * so with `activate` the library-mode non-button warning says a press while open dismisses the\n * popup instead of moving focus into it.\n */\n readonly activate?: (event: MouseEvent<HTMLElement>) => void;\n /** Runs right before the default toggle's `requestOpen` (Menu.Trigger: the open cause). */\n readonly beforeOpen?: (event: MouseEvent<HTMLElement>) => void;\n /**\n * How `disabled` is rendered. `\"native\"` (default): the `disabled` attribute, so the button is\n * no invoker at all; in library mode a link or component `render` still gets the `\"aria\"`\n * branch, because native `disabled` means nothing on a link and the installed click handler is\n * what refuses. `\"aria\"`: `aria-disabled=\"true\"` and no `disabled` (a menu item stays\n * focusable); the click handler still refuses.\n */\n readonly disabledAs?: \"native\" | \"aria\";\n}\n\nexport interface PopoverTriggerHook {\n readonly state: PopoverTriggerHookState;\n /** Owned props (conventions 1.2) for `useRender`'s `libraryProps`. */\n readonly libraryProps: ComponentPropsWithRef<\"button\"> & DataAttributes;\n /** Registers the element as the Root's Trigger; pass as `useRender`'s `ref`. */\n readonly ref: (element: HTMLElement) => () => void;\n}\n\n/**\n * The Trigger of a popover-like component (design/components/popover.md 14.3):\n * `button[popoverTarget]`, with no `aria-expanded` on an invoker: every engine exposes the\n * popover's shown state from the invoker relationship. In JS-free mode the browser toggles\n * the popover and the Popup keeps `data-open` current on the element. In library mode the click\n * is cancelled (so the activation does not run) and toggles against what the user already\n * intends: an open through `requestOpen`, a library-owned close through `requestClose`. There\n * `data-open` and `state.open` are optimistic; a non-button `render` (no invoker) renders\n * `aria-expanded` from whether the popup is shown (in the top layer).\n *\n * Menu adds `aria-haspopup=\"menu\"` and its own key handling on top.\n */\nexport function usePopoverTrigger<State>(\n ctx: PopoverRootValue,\n options: UsePopoverTriggerOptions<State>,\n): PopoverTriggerHook {\n const { part, disabled, render, handlers, activate, beforeOpen, disabledAs = \"native\" } = options;\n const isLibrary = ctx.mode === \"library\";\n const { ids, triggerRef, popupRef } = ctx;\n\n const state: PopoverTriggerHookState = {\n open: ctx.optimistic,\n // Opens, part-initiated closes and dismissal notifications all belong to the Trigger (4.2).\n pending: ctx.pending,\n disabled,\n };\n\n const checkElement = useElementCheck(\n isDev &&\n ((element) => {\n if (element.tagName === \"BUTTON\") return null;\n const tag = element.tagName.toLowerCase();\n // Library mode: the click handler works, but only an invoker button is spared by the\n // popover's light dismiss, so a press while open hides the popup before the click.\n if (isLibrary && activate !== undefined) {\n return (\n `${part}: rendered a <${tag}>. Only a <button> is the popover's invoker: a press on ` +\n \"this element while its popup is open dismisses the popup through light dismiss \" +\n \"instead of moving focus into it. Render a <button>.\"\n );\n }\n return isLibrary\n ? nonInvokerMessage(part, tag, \"popover\", \"popup\")\n : `${part}: rendered a <${tag}> in JS-free mode. Only a <button> is a ` +\n \"`popovertarget` invoker; this trigger will not open the popover. Render a \" +\n \"<button>, or put the component in library mode (`defaultOpen={false}`).\";\n }),\n );\n const ref = useCallback(\n (element: HTMLElement) => {\n checkElement?.(element);\n const unregister = registerTrigger(triggerRef, element, part);\n // JS-free mode: the DOM owns `data-open` (design 9). A Trigger that mounts after the\n // Popup (or is re-created) picks up the current state here.\n if (!isLibrary && popupRef.current !== null) {\n element.toggleAttribute(\"data-open\", isPopoverOpen(popupRef.current));\n }\n return unregister;\n },\n [isLibrary, checkElement, triggerRef, popupRef, part],\n );\n\n const onClick = (event: MouseEvent<HTMLElement>) => {\n // Cancelling the click cancels the popover toggle (it runs as the button's activation\n // behaviour, only if the click was not cancelled). Enter and Space synthesise the same click.\n event.preventDefault();\n if (disabled) return;\n if (activate !== undefined) {\n activate(event);\n return;\n }\n const native = event.nativeEvent;\n if (ctx.intended()) {\n ctx.requestClose(native, \"activate\");\n } else {\n // First: what it records (Menu's open cause) must be in place before anything can show the\n // popup. Today the show waits for the apply effect, so the order is not observable.\n beforeOpen?.(event);\n ctx.requestOpen(native, \"activate\");\n }\n };\n\n // The component's handlers first, then the Trigger's (design/components/menu.md 14). The click\n // handler is only owned when installed, so a render element keeps its own `onClick` in JS-free\n // mode.\n const merged: PopoverTriggerHandlers = { ...handlers };\n if (isLibrary) {\n const extra = handlers?.onClick;\n merged.onClick =\n extra === undefined\n ? onClick\n : (event) => {\n extra(event);\n onClick(event);\n };\n }\n // The click handler is installed in library mode only; in JS-free mode the activation needs\n // native `disabled` to stop it.\n const disabledProps = disabledButton(render, disabled, {\n handlesClick: isLibrary,\n alwaysFocusable: disabledAs === \"aria\",\n });\n\n const invoker = disabledProps.invoker && rendersButton(render);\n\n const libraryProps: ComponentPropsWithRef<\"button\"> & DataAttributes = {\n // `type` only applies to a <button>; on any other element the key is not owned.\n ...(rendersButton(render) && { type: \"button\" as const }),\n // Owned: a render element must not re-enable a disabled Trigger (in JS-free mode the browser\n // would toggle the popover).\n ...disabledProps.props,\n // Only on a button (or a component, assumed to render one): another element is no invoker,\n // and the attribute would claim otherwise.\n popoverTarget: invoker ? ids.popup : undefined,\n ...RESERVED_INVOKER_KEYS,\n // No `aria-expanded` on an invoker: every engine exposes the popover's shown state from\n // `popovertarget`, and ARIA in HTML forbids the attribute there. Otherwise nothing else\n // exposes the state: library mode renders shown (the popup is in the top layer; `data-open`\n // is the optimistic one), JS-free mode cannot know it and renders \"false\".\n \"aria-expanded\": invoker ? undefined : isLibrary ? ctx.shown : false,\n ...merged,\n \"data-open\": dataAttr(state.open),\n \"data-pending\": dataAttr(state.pending),\n // Every mode: the styling hook agrees with the rendered `disabled` / `aria-disabled`\n // (conventions 1.2).\n \"data-disabled\": dataAttr(disabled),\n };\n\n return { state, libraryProps, ref };\n}\n"],"mappings":";;;;;;;;AAYA,MAAM,QAAQ,QAAQ,IAAI,aAAa;;;;;;AAOvC,MAAa,wBAAwB;CACnC,qBAAqB,KAAA;CACrB,YAAY,KAAA;CACZ,SAAS,KAAA;AACX;;;;;;AAOA,SAAgB,kBACd,MACA,KACA,SACA,OACQ;CACR,OACE,GAAG,KAAK,gBAAgB,IAAI,4BAA4B,QAAQ,gDACtC,MAAM;AAIpC;;;;;;;;;;;;;AAoEA,SAAgB,kBACd,KACA,SACoB;CACpB,MAAM,EAAE,MAAM,UAAU,QAAQ,UAAU,UAAU,YAAY,aAAa,aAAa;CAC1F,MAAM,YAAY,IAAI,SAAS;CAC/B,MAAM,EAAE,KAAK,YAAY,aAAa;CAEtC,MAAM,QAAiC;EACrC,MAAM,IAAI;EAEV,SAAS,IAAI;EACb;CACF;CAEA,MAAM,eAAe,gBACnB,WACI,YAAY;EACZ,IAAI,QAAQ,YAAY,UAAU,OAAO;EACzC,MAAM,MAAM,QAAQ,QAAQ,YAAY;EAGxC,IAAI,aAAa,aAAa,KAAA,GAC5B,OACE,GAAG,KAAK,gBAA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I;AACpC"}
|
|
1
|
+
{"version":3,"file":"use-popover-trigger.mjs","names":[],"sources":["../../src/internal/use-popover-trigger.ts"],"sourcesContent":["\"use client\";\nimport { useCallback } from \"react\";\nimport type { ComponentPropsWithRef, DOMAttributes, MouseEvent } from \"react\";\nimport { dataAttr } from \"./data-attr.js\";\nimport { isPopoverOpen } from \"./popover-open.js\";\nimport { registerTrigger } from \"./trigger-ref.js\";\nimport type { RenderProp } from \"./types.js\";\nimport { useElementCheck } from \"./use-element-check.js\";\nimport { disabledButton, rendersButton } from \"./use-render.js\";\nimport type { DataAttributes } from \"./use-render.js\";\nimport type { PopoverRootValue } from \"./use-overlay-root.js\";\n\nconst isDev = process.env.NODE_ENV !== \"production\";\n\n/**\n * Owned and never written on an overlay Trigger (Popover, Menu, Combobox): the default\n * `popovertarget` action, which a render element must not change, and an Invoker Command, which\n * would take precedence over `popovertarget` and act on another element.\n */\nexport const RESERVED_INVOKER_KEYS = {\n popoverTargetAction: undefined,\n commandfor: undefined,\n command: undefined,\n} as const;\n\n/**\n * The library-mode dev warning for a Trigger rendered as another element than a `<button>`: only\n * an invoker button is spared by the popover's light dismiss, so a press while the popup is open\n * hides it before the click. `popover` and `popup` are the nouns the text uses for it.\n */\nexport function nonInvokerMessage(\n part: string,\n tag: string,\n popover: string,\n popup: string,\n): string {\n return (\n `${part}: rendered a <${tag}>. Only a <button> is the ${popover}'s invoker: a press on ` +\n `this element while the ${popup} is open closes it through light dismiss before the ` +\n \"click, so the close is a dismissal (reported to onOpenChange) rather than a request: \" +\n \"openChangeAction(false) is not called, cannot veto it and there is no view transition. \" +\n \"Render a <button>.\"\n );\n}\n\n/** The state of an overlay Trigger (Popover.Trigger, Menu.Trigger and its siblings). */\nexport interface PopoverTriggerHookState {\n /** Optimistic open state. A Popover in JS-free mode (the browser owns it) reports `false`. */\n readonly open: boolean;\n /** The Root's transition is pending: an open or a part-initiated close (never a dismissal). */\n readonly pending: boolean;\n /** The `disabled` prop. */\n readonly disabled: boolean;\n}\n\n/** Event handlers a component layers onto the Trigger's own (Menu's keys and hover intent). */\nexport type PopoverTriggerHandlers = Pick<\n DOMAttributes<HTMLButtonElement>,\n \"onClick\" | \"onKeyDown\" | \"onMouseDown\" | \"onPointerMove\" | \"onPointerEnter\" | \"onPointerLeave\"\n>;\n\nexport interface UsePopoverTriggerOptions<State> {\n /** Public name of the part for development warnings (`\"Popover.Trigger\"`). */\n readonly part: string;\n readonly disabled: boolean;\n readonly render: RenderProp<ComponentPropsWithRef<\"button\">, State> | undefined;\n /**\n * Extra handlers, merged in front of the Trigger's own: for a key both define, the extra one\n * runs first (the consumer's pass-through handler still runs before both, conventions 1.2).\n */\n readonly handlers?: PopoverTriggerHandlers;\n /**\n * What a click does in library mode, after the command has been cancelled and `disabled`\n * checked, replacing the default toggle against `intended()` (`requestOpen` / `requestClose`).\n * Menu's SubmenuTrigger opens, or moves focus into an already open submenu, and never closes;\n * so with `activate` the library-mode non-button warning says a press while open dismisses the\n * popup instead of moving focus into it.\n */\n readonly activate?: (event: MouseEvent<HTMLElement>) => void;\n /** Runs right before the default toggle's `requestOpen` (Menu.Trigger: the open cause). */\n readonly beforeOpen?: (event: MouseEvent<HTMLElement>) => void;\n /**\n * How `disabled` is rendered. `\"native\"` (default): the `disabled` attribute, so the button is\n * no invoker at all; in library mode a link or component `render` still gets the `\"aria\"`\n * branch, because native `disabled` means nothing on a link and the installed click handler is\n * what refuses. `\"aria\"`: `aria-disabled=\"true\"` and no `disabled` (a menu item stays\n * focusable); the click handler still refuses.\n */\n readonly disabledAs?: \"native\" | \"aria\";\n}\n\nexport interface PopoverTriggerHook {\n readonly state: PopoverTriggerHookState;\n /** Owned props (conventions 1.2) for `useRender`'s `libraryProps`. */\n readonly libraryProps: ComponentPropsWithRef<\"button\"> & DataAttributes;\n /** Registers the element as the Root's Trigger; pass as `useRender`'s `ref`. */\n readonly ref: (element: HTMLElement) => () => void;\n}\n\n/**\n * The Trigger of a popover-like component (design/components/popover.md 14.3):\n * `button[popoverTarget]`, with no `aria-expanded` on an invoker: every engine exposes the\n * popover's shown state from the invoker relationship. In JS-free mode the browser toggles\n * the popover and the Popup keeps `data-open` current on the element. In library mode the click\n * is cancelled (so the activation does not run) and toggles against what the user already\n * intends: an open through `requestOpen`, a library-owned close through `requestClose`. There\n * `data-open` and `state.open` are optimistic; a non-button `render` (no invoker) renders\n * `aria-expanded` from whether the popup is shown (in the top layer).\n *\n * Menu adds `aria-haspopup=\"menu\"` and its own key handling on top.\n */\nexport function usePopoverTrigger<State>(\n ctx: PopoverRootValue,\n options: UsePopoverTriggerOptions<State>,\n): PopoverTriggerHook {\n const { part, disabled, render, handlers, activate, beforeOpen, disabledAs = \"native\" } = options;\n const isLibrary = ctx.mode === \"library\";\n const { ids, triggerRef, popupRef } = ctx;\n\n const state: PopoverTriggerHookState = {\n open: ctx.optimistic,\n // Opens and part-initiated closes belong to the Trigger (4.2); a dismissal is a fact and\n // pends nothing (overlay-open-change.md 2.7).\n pending: ctx.pending,\n disabled,\n };\n\n const checkElement = useElementCheck(\n isDev &&\n ((element) => {\n if (element.tagName === \"BUTTON\") return null;\n const tag = element.tagName.toLowerCase();\n // Library mode: the click handler works, but only an invoker button is spared by the\n // popover's light dismiss, so a press while open hides the popup before the click.\n if (isLibrary && activate !== undefined) {\n return (\n `${part}: rendered a <${tag}>. Only a <button> is the popover's invoker: a press on ` +\n \"this element while its popup is open dismisses the popup through light dismiss \" +\n \"instead of moving focus into it. Render a <button>.\"\n );\n }\n return isLibrary\n ? nonInvokerMessage(part, tag, \"popover\", \"popup\")\n : `${part}: rendered a <${tag}> in JS-free mode. Only a <button> is a ` +\n \"`popovertarget` invoker; this trigger will not open the popover. Render a \" +\n \"<button>, or put the component in library mode (`defaultOpen={false}`).\";\n }),\n );\n const ref = useCallback(\n (element: HTMLElement) => {\n checkElement?.(element);\n const unregister = registerTrigger(triggerRef, element, part);\n // JS-free mode: the DOM owns `data-open` (design 9). A Trigger that mounts after the\n // Popup (or is re-created) picks up the current state here.\n if (!isLibrary && popupRef.current !== null) {\n element.toggleAttribute(\"data-open\", isPopoverOpen(popupRef.current));\n }\n return unregister;\n },\n [isLibrary, checkElement, triggerRef, popupRef, part],\n );\n\n const onClick = (event: MouseEvent<HTMLElement>) => {\n // Cancelling the click cancels the popover toggle (it runs as the button's activation\n // behaviour, only if the click was not cancelled). Enter and Space synthesise the same click.\n event.preventDefault();\n if (disabled) return;\n if (activate !== undefined) {\n activate(event);\n return;\n }\n const native = event.nativeEvent;\n if (ctx.intended()) {\n if (ctx.hiddenBeforeToggle()) {\n // A non-button Trigger (#115): light dismiss hid the popup at `pointerup`, before this\n // click. The hide is a fact, reported by its pending `toggle`; this click only names its\n // cause (overlay-open-change.md 3.2).\n ctx.recordCloseIntent({ reason: \"activate\", event: native });\n return;\n }\n ctx.requestClose(native, \"activate\");\n } else {\n // First: what it records (Menu's open cause) must be in place before anything can show the\n // popup. Today the show waits for the apply effect, so the order is not observable.\n beforeOpen?.(event);\n ctx.requestOpen(native, \"activate\");\n }\n };\n\n // The component's handlers first, then the Trigger's (design/components/menu.md 14). The click\n // handler is only owned when installed, so a render element keeps its own `onClick` in JS-free\n // mode.\n const merged: PopoverTriggerHandlers = { ...handlers };\n if (isLibrary) {\n const extra = handlers?.onClick;\n merged.onClick =\n extra === undefined\n ? onClick\n : (event) => {\n extra(event);\n onClick(event);\n };\n }\n // The click handler is installed in library mode only; in JS-free mode the activation needs\n // native `disabled` to stop it.\n const disabledProps = disabledButton(render, disabled, {\n handlesClick: isLibrary,\n alwaysFocusable: disabledAs === \"aria\",\n });\n\n const invoker = disabledProps.invoker && rendersButton(render);\n\n const libraryProps: ComponentPropsWithRef<\"button\"> & DataAttributes = {\n // `type` only applies to a <button>; on any other element the key is not owned.\n ...(rendersButton(render) && { type: \"button\" as const }),\n // Owned: a render element must not re-enable a disabled Trigger (in JS-free mode the browser\n // would toggle the popover).\n ...disabledProps.props,\n // Only on a button (or a component, assumed to render one): another element is no invoker,\n // and the attribute would claim otherwise.\n popoverTarget: invoker ? ids.popup : undefined,\n ...RESERVED_INVOKER_KEYS,\n // No `aria-expanded` on an invoker: every engine exposes the popover's shown state from\n // `popovertarget`, and ARIA in HTML forbids the attribute there. Otherwise nothing else\n // exposes the state: library mode renders shown (the popup is in the top layer; `data-open`\n // is the optimistic one), JS-free mode cannot know it and renders \"false\".\n \"aria-expanded\": invoker ? undefined : isLibrary ? ctx.shown : false,\n ...merged,\n \"data-open\": dataAttr(state.open),\n \"data-pending\": dataAttr(state.pending),\n // Every mode: the styling hook agrees with the rendered `disabled` / `aria-disabled`\n // (conventions 1.2).\n \"data-disabled\": dataAttr(disabled),\n };\n\n return { state, libraryProps, ref };\n}\n"],"mappings":";;;;;;;;AAYA,MAAM,QAAQ,QAAQ,IAAI,aAAa;;;;;;AAOvC,MAAa,wBAAwB;CACnC,qBAAqB,KAAA;CACrB,YAAY,KAAA;CACZ,SAAS,KAAA;AACX;;;;;;AAOA,SAAgB,kBACd,MACA,KACA,SACA,OACQ;CACR,OACE,GAAG,KAAK,gBAAgB,IAAI,4BAA4B,QAAQ,gDACtC,MAAM;AAKpC;;;;;;;;;;;;;AAoEA,SAAgB,kBACd,KACA,SACoB;CACpB,MAAM,EAAE,MAAM,UAAU,QAAQ,UAAU,UAAU,YAAY,aAAa,aAAa;CAC1F,MAAM,YAAY,IAAI,SAAS;CAC/B,MAAM,EAAE,KAAK,YAAY,aAAa;CAEtC,MAAM,QAAiC;EACrC,MAAM,IAAI;EAGV,SAAS,IAAI;EACb;CACF;CAEA,MAAM,eAAe,gBACnB,WACI,YAAY;EACZ,IAAI,QAAQ,YAAY,UAAU,OAAO;EACzC,MAAM,MAAM,QAAQ,QAAQ,YAAY;EAGxC,IAAI,aAAa,aAAa,KAAA,GAC5B,OACE,GAAG,KAAK,gBAAgB,IAAI;EAKhC,OAAO,YACH,kBAAkB,MAAM,KAAK,WAAW,OAAO,IAC/C,GAAG,KAAK,gBAAgB,IAAI;CAGlC,EACJ;CACA,MAAM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AACpC"}
|
|
@@ -19,7 +19,7 @@ export interface MenuCheckboxItemState {
|
|
|
19
19
|
*
|
|
20
20
|
* @see https://ui.funstack.work/components/menu
|
|
21
21
|
*/
|
|
22
|
-
export interface MenuCheckboxItemProps extends Omit<MenuItemProps, "action" | "closeOnActivate" | "className" | "style" | "children" | "render"> {
|
|
22
|
+
export interface MenuCheckboxItemProps extends Omit<MenuItemProps, "action" | "closeOnActivate" | "whilePending" | "className" | "style" | "children" | "render"> {
|
|
23
23
|
/** Controlled checked state. `undefined` means uncontrolled. */
|
|
24
24
|
checked?: boolean;
|
|
25
25
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"menu-checkbox-item.mjs","names":[],"sources":["../../src/menu/menu-checkbox-item.tsx"],"sourcesContent":["\"use client\";\nimport { useRef } from \"react\";\nimport type { ComponentPropsWithRef, CSSProperties, ReactNode } from \"react\";\nimport { createActionDetails } from \"../internal/action-details.js\";\nimport { dataAttr } from \"../internal/data-attr.js\";\nimport { transitionTypes } from \"../internal/transition-types.js\";\nimport type { ChangeAction, RenderProp, StateValue } from \"../internal/types.js\";\nimport { useControllableAction } from \"../internal/use-controllable-action.js\";\nimport { rendersButton, useRender } from \"../internal/use-render.js\";\nimport { closeStackLater, useMenuContext } from \"./menu-context.js\";\nimport type { MenuItemProps } from \"./menu-item.js\";\nimport { useMenuItem } from \"./use-menu-item.js\";\n\n/**\n * State of `Menu.CheckboxItem`, passed to the function forms of `className`, `style`, `children`\n * and `render`.\n */\nexport interface MenuCheckboxItemState {\n /** Optimistic checked state (`aria-checked`). */\n readonly checked: boolean;\n /** `checkedChangeAction` is in flight. */\n readonly pending: boolean;\n /** The `disabled` prop. */\n readonly disabled: boolean;\n}\n\n/**\n * Props of `Menu.CheckboxItem`.\n *\n * @see https://ui.funstack.work/components/menu\n */\nexport interface MenuCheckboxItemProps extends Omit<\n MenuItemProps,\n \"action\" | \"closeOnActivate\" | \"className\" | \"style\" | \"children\" | \"render\"\n> {\n /** Controlled checked state. `undefined` means uncontrolled. */\n checked?: boolean;\n /**\n * Initial checked state when uncontrolled.\n *\n * @defaultValue `false`\n */\n defaultChecked?: boolean;\n /**\n * Awaited inside the item's transition; `aria-checked` flips at once and rolls back on\n * rejection (which reaches the nearest Error Boundary above the item). May be a Server\n * Function.\n */\n checkedChangeAction?: ChangeAction<boolean>;\n /**\n * Close the whole menu once the change settles. When `false` the menu stays open.\n *\n * @defaultValue `false`\n */\n closeOnActivate?: boolean;\n /** Class name, or a function of the part's state that returns one. */\n className?: StateValue<string | undefined, MenuCheckboxItemState>;\n /** Inline style, or a function of the part's state that returns one. */\n style?: StateValue<CSSProperties | undefined, MenuCheckboxItemState>;\n /** Content, or a function of the part's state. */\n children?: StateValue<ReactNode, MenuCheckboxItemState>;\n /** Replace the default `<button>` (element form) or take over rendering (function form). */\n render?: RenderProp<ComponentPropsWithRef<\"button\">, MenuCheckboxItemState>;\n}\n\n/**\n * A checkable menu item: `<button type=\"button\" role=\"menuitemcheckbox\" aria-checked>`. Click,\n * Enter or Space toggles it through its own transition with optimistic `aria-checked` /\n * `data-checked` and `data-pending`; the menu stays open unless `closeOnActivate`.\n *\n * Owned keys (conventions 1.2): those of `Menu.Item`, plus `aria-checked` and `data-checked`.\n *\n * @see https://ui.funstack.work/components/menu\n */\nexport function MenuCheckboxItem({\n checked,\n defaultChecked,\n checkedChangeAction,\n closeOnActivate = false,\n disabled = false,\n label,\n className,\n style,\n children,\n render,\n ...rest\n}: MenuCheckboxItemProps) {\n const menu = useMenuContext(\"Menu.CheckboxItem\");\n // The close to run after a dispatch's action resolved, keyed by the dispatch's details.\n const after = useRef(new WeakMap<object, () => void>());\n const { optimistic, pending, dispatch } = useControllableAction<boolean>({\n controlled: checked,\n defaultValue: defaultChecked,\n initialValue: false,\n action: async (next, details) => {\n await checkedChangeAction?.(next, details);\n after.current.get(details)?.();\n },\n transitionTypes: (next) => transitionTypes(\"menu\", next ? \"check\" : \"uncheck\"),\n warn: { component: \"Menu.CheckboxItem\", prop: \"checked\" },\n });\n\n const { libraryProps } = useMenuItem({\n part: \"Menu.CheckboxItem\",\n role: \"menuitemcheckbox\",\n disabled,\n label,\n activate: (event) => {\n const details = createActionDetails(event, {});\n if (closeOnActivate) after.current.set(details, closeStackLater(menu, event));\n dispatch(!optimistic, details);\n },\n });\n\n const state: MenuCheckboxItemState = { checked: optimistic, pending, disabled };\n\n return useRender<ComponentPropsWithRef<\"button\">, MenuCheckboxItemState>({\n defaultElement: \"button\",\n render,\n state,\n className,\n style,\n children,\n props: rest,\n libraryProps: {\n ...(rendersButton(render) && { type: \"button\" as const }),\n ...libraryProps,\n \"aria-checked\": optimistic,\n \"data-checked\": dataAttr(optimistic),\n \"data-pending\": dataAttr(pending),\n },\n });\n}\n\nMenuCheckboxItem.displayName = \"MenuCheckboxItem\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AA0EA,SAAgB,iBAAiB,EAC/B,SACA,gBACA,qBACA,kBAAkB,OAClB,WAAW,OACX,OACA,WACA,OACA,UACA,QACA,GAAG,QACqB;CACxB,MAAM,OAAO,eAAe,mBAAmB;CAE/C,MAAM,QAAQ,uBAAO,IAAI,QAA4B,CAAC;CACtD,MAAM,EAAE,YAAY,SAAS,aAAa,sBAA+B;EACvE,YAAY;EACZ,cAAc;EACd,cAAc;EACd,QAAQ,OAAO,MAAM,YAAY;GAC/B,MAAM,sBAAsB,MAAM,OAAO;GACzC,MAAM,QAAQ,IAAI,OAAO,CAAC,GAAG;EAC/B;EACA,kBAAkB,SAAS,gBAAgB,QAAQ,OAAO,UAAU,SAAS;EAC7E,MAAM;GAAE,WAAW;GAAqB,MAAM;EAAU;CAC1D,CAAC;CAED,MAAM,EAAE,iBAAiB,YAAY;EACnC,MAAM;EACN,MAAM;EACN;EACA;EACA,WAAW,UAAU;GACnB,MAAM,UAAU,oBAAoB,OAAO,CAAC,CAAC;GAC7C,IAAI,iBAAiB,MAAM,QAAQ,IAAI,SAAS,gBAAgB,MAAM,KAAK,CAAC;GAC5E,SAAS,CAAC,YAAY,OAAO;EAC/B;CACF,CAAC;CAID,OAAO,UAAkE;EACvE,gBAAgB;EAChB;EACA,OAAA;GALqC,SAAS;GAAY;GAAS;EAK/D;EACJ;EACA;EACA;EACA,OAAO;EACP,cAAc;GACZ,GAAI,cAAc,MAAM,KAAK,EAAE,MAAM,SAAkB;GACvD,GAAG;GACH,gBAAgB;GAChB,gBAAgB,SAAS,UAAU;GACnC,gBAAgB,SAAS,OAAO;EAClC;CACF,CAAC;AACH;AAEA,iBAAiB,cAAc"}
|
|
1
|
+
{"version":3,"file":"menu-checkbox-item.mjs","names":[],"sources":["../../src/menu/menu-checkbox-item.tsx"],"sourcesContent":["\"use client\";\nimport { useRef } from \"react\";\nimport type { ComponentPropsWithRef, CSSProperties, ReactNode } from \"react\";\nimport { createActionDetails } from \"../internal/action-details.js\";\nimport { dataAttr } from \"../internal/data-attr.js\";\nimport { transitionTypes } from \"../internal/transition-types.js\";\nimport type { ChangeAction, RenderProp, StateValue } from \"../internal/types.js\";\nimport { useControllableAction } from \"../internal/use-controllable-action.js\";\nimport { rendersButton, useRender } from \"../internal/use-render.js\";\nimport { closeStackLater, useMenuContext } from \"./menu-context.js\";\nimport type { MenuItemProps } from \"./menu-item.js\";\nimport { useMenuItem } from \"./use-menu-item.js\";\n\n/**\n * State of `Menu.CheckboxItem`, passed to the function forms of `className`, `style`, `children`\n * and `render`.\n */\nexport interface MenuCheckboxItemState {\n /** Optimistic checked state (`aria-checked`). */\n readonly checked: boolean;\n /** `checkedChangeAction` is in flight. */\n readonly pending: boolean;\n /** The `disabled` prop. */\n readonly disabled: boolean;\n}\n\n/**\n * Props of `Menu.CheckboxItem`.\n *\n * @see https://ui.funstack.work/components/menu\n */\nexport interface MenuCheckboxItemProps extends Omit<\n MenuItemProps,\n \"action\" | \"closeOnActivate\" | \"whilePending\" | \"className\" | \"style\" | \"children\" | \"render\"\n> {\n /** Controlled checked state. `undefined` means uncontrolled. */\n checked?: boolean;\n /**\n * Initial checked state when uncontrolled.\n *\n * @defaultValue `false`\n */\n defaultChecked?: boolean;\n /**\n * Awaited inside the item's transition; `aria-checked` flips at once and rolls back on\n * rejection (which reaches the nearest Error Boundary above the item). May be a Server\n * Function.\n */\n checkedChangeAction?: ChangeAction<boolean>;\n /**\n * Close the whole menu once the change settles. When `false` the menu stays open.\n *\n * @defaultValue `false`\n */\n closeOnActivate?: boolean;\n /** Class name, or a function of the part's state that returns one. */\n className?: StateValue<string | undefined, MenuCheckboxItemState>;\n /** Inline style, or a function of the part's state that returns one. */\n style?: StateValue<CSSProperties | undefined, MenuCheckboxItemState>;\n /** Content, or a function of the part's state. */\n children?: StateValue<ReactNode, MenuCheckboxItemState>;\n /** Replace the default `<button>` (element form) or take over rendering (function form). */\n render?: RenderProp<ComponentPropsWithRef<\"button\">, MenuCheckboxItemState>;\n}\n\n/**\n * A checkable menu item: `<button type=\"button\" role=\"menuitemcheckbox\" aria-checked>`. Click,\n * Enter or Space toggles it through its own transition with optimistic `aria-checked` /\n * `data-checked` and `data-pending`; the menu stays open unless `closeOnActivate`.\n *\n * Owned keys (conventions 1.2): those of `Menu.Item`, plus `aria-checked` and `data-checked`.\n *\n * @see https://ui.funstack.work/components/menu\n */\nexport function MenuCheckboxItem({\n checked,\n defaultChecked,\n checkedChangeAction,\n closeOnActivate = false,\n disabled = false,\n label,\n className,\n style,\n children,\n render,\n ...rest\n}: MenuCheckboxItemProps) {\n const menu = useMenuContext(\"Menu.CheckboxItem\");\n // The close to run after a dispatch's action resolved, keyed by the dispatch's details.\n const after = useRef(new WeakMap<object, () => void>());\n const { optimistic, pending, dispatch } = useControllableAction<boolean>({\n controlled: checked,\n defaultValue: defaultChecked,\n initialValue: false,\n action: async (next, details) => {\n await checkedChangeAction?.(next, details);\n after.current.get(details)?.();\n },\n transitionTypes: (next) => transitionTypes(\"menu\", next ? \"check\" : \"uncheck\"),\n warn: { component: \"Menu.CheckboxItem\", prop: \"checked\" },\n });\n\n const { libraryProps } = useMenuItem({\n part: \"Menu.CheckboxItem\",\n role: \"menuitemcheckbox\",\n disabled,\n label,\n activate: (event) => {\n const details = createActionDetails(event, {});\n if (closeOnActivate) after.current.set(details, closeStackLater(menu, event));\n dispatch(!optimistic, details);\n },\n });\n\n const state: MenuCheckboxItemState = { checked: optimistic, pending, disabled };\n\n return useRender<ComponentPropsWithRef<\"button\">, MenuCheckboxItemState>({\n defaultElement: \"button\",\n render,\n state,\n className,\n style,\n children,\n props: rest,\n libraryProps: {\n ...(rendersButton(render) && { type: \"button\" as const }),\n ...libraryProps,\n \"aria-checked\": optimistic,\n \"data-checked\": dataAttr(optimistic),\n \"data-pending\": dataAttr(pending),\n },\n });\n}\n\nMenuCheckboxItem.displayName = \"MenuCheckboxItem\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AA0EA,SAAgB,iBAAiB,EAC/B,SACA,gBACA,qBACA,kBAAkB,OAClB,WAAW,OACX,OACA,WACA,OACA,UACA,QACA,GAAG,QACqB;CACxB,MAAM,OAAO,eAAe,mBAAmB;CAE/C,MAAM,QAAQ,uBAAO,IAAI,QAA4B,CAAC;CACtD,MAAM,EAAE,YAAY,SAAS,aAAa,sBAA+B;EACvE,YAAY;EACZ,cAAc;EACd,cAAc;EACd,QAAQ,OAAO,MAAM,YAAY;GAC/B,MAAM,sBAAsB,MAAM,OAAO;GACzC,MAAM,QAAQ,IAAI,OAAO,CAAC,GAAG;EAC/B;EACA,kBAAkB,SAAS,gBAAgB,QAAQ,OAAO,UAAU,SAAS;EAC7E,MAAM;GAAE,WAAW;GAAqB,MAAM;EAAU;CAC1D,CAAC;CAED,MAAM,EAAE,iBAAiB,YAAY;EACnC,MAAM;EACN,MAAM;EACN;EACA;EACA,WAAW,UAAU;GACnB,MAAM,UAAU,oBAAoB,OAAO,CAAC,CAAC;GAC7C,IAAI,iBAAiB,MAAM,QAAQ,IAAI,SAAS,gBAAgB,MAAM,KAAK,CAAC;GAC5E,SAAS,CAAC,YAAY,OAAO;EAC/B;CACF,CAAC;CAID,OAAO,UAAkE;EACvE,gBAAgB;EAChB;EACA,OAAA;GALqC,SAAS;GAAY;GAAS;EAK/D;EACJ;EACA;EACA;EACA,OAAO;EACP,cAAc;GACZ,GAAI,cAAc,MAAM,KAAK,EAAE,MAAM,SAAkB;GACvD,GAAG;GACH,gBAAgB;GAChB,gBAAgB,SAAS,UAAU;GACnC,gBAAgB,SAAS,OAAO;EAClC;CACF,CAAC;AACH;AAEA,iBAAiB,cAAc"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Action, ActionDetails, RenderProp, StateValue } from "../internal/types.mjs";
|
|
1
|
+
import { Action, ActionDetails, RenderProp, StateValue, WhilePending } from "../internal/types.mjs";
|
|
2
2
|
import { CSSProperties, ComponentPropsWithRef, ReactNode } from "react";
|
|
3
3
|
//#region src/menu/menu-item.d.ts
|
|
4
4
|
/** Details passed to `Menu.Item`'s `action`. */
|
|
@@ -32,6 +32,16 @@ export interface MenuItemProps extends Omit<ComponentPropsWithRef<"button">, "on
|
|
|
32
32
|
* @defaultValue `true`
|
|
33
33
|
*/
|
|
34
34
|
closeOnActivate?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* What a click, Enter or Space does while this item's `action` is pending. `"ignore"`:
|
|
37
|
+
* nothing (the event is cancelled, `aria-disabled="true"` meanwhile). `"run"`: the action runs
|
|
38
|
+
* again (`data-pending` until every run has settled); a closing item closes the menu once,
|
|
39
|
+
* when the last run settles and only if the latest run resolved (a rejection keeps the menu
|
|
40
|
+
* open, as in `"ignore"` mode). No effect without `action`.
|
|
41
|
+
*
|
|
42
|
+
* @defaultValue `"ignore"`, or `"run"` with `closeOnActivate={false}`
|
|
43
|
+
*/
|
|
44
|
+
whilePending?: WhilePending;
|
|
35
45
|
/**
|
|
36
46
|
* Renders `aria-disabled` and `data-disabled`: the item stays focusable and never activates.
|
|
37
47
|
* It is an ordinary item for navigation; only the open focus skips items with `disabled`.
|
|
@@ -52,8 +62,9 @@ export interface MenuItemProps extends Omit<ComponentPropsWithRef<"button">, "on
|
|
|
52
62
|
}
|
|
53
63
|
/**
|
|
54
64
|
* A menu command: `<button type="button" role="menuitem" tabindex="-1">`. Click, Enter or Space
|
|
55
|
-
* runs `action` in the item's own transition (`data-pending`
|
|
56
|
-
*
|
|
65
|
+
* runs `action` in the item's own transition (`data-pending` on the item; a second activation
|
|
66
|
+
* meanwhile is ignored and the item `aria-disabled`, unless `whilePending` is `"run"`, the
|
|
67
|
+
* default of a `closeOnActivate={false}` item); once it resolves the whole menu stack closes
|
|
57
68
|
* through the top-level Root (`openChangeAction(false, { reason: "activate" })` awaited, view
|
|
58
69
|
* transition if set). Hovering the item focuses it.
|
|
59
70
|
*
|
|
@@ -63,7 +74,7 @@ export interface MenuItemProps extends Omit<ComponentPropsWithRef<"button">, "on
|
|
|
63
74
|
*
|
|
64
75
|
* @see https://ui.funstack.work/components/menu
|
|
65
76
|
*/
|
|
66
|
-
export declare function MenuItem({ action, closeOnActivate, disabled, label, className, style, children, render, ...rest }: MenuItemProps): ReactNode;
|
|
77
|
+
export declare function MenuItem({ action, closeOnActivate, whilePending, disabled, label, className, style, children, render, ...rest }: MenuItemProps): ReactNode;
|
|
67
78
|
export declare namespace MenuItem {
|
|
68
79
|
var displayName: string;
|
|
69
80
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"menu-item.d.mts","names":[],"sources":["../../src/menu/menu-item.tsx"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"menu-item.d.mts","names":[],"sources":["../../src/menu/menu-item.tsx"],"mappings":";;;;iBAiBiB,wBAAwB;;;;;iBAMxB;;WAEN;;WAEA;;;;;;;iBAQM,sBAAsB,KACrC;;;;;;;EASA,SAAS,QAAQ,SAAS;;;;;;EAM1B;;;;;;;;;;EAUA,eAAe;;;;;;;EAOf;;EAEA;;EAEA,YAAY,+BAA+B;;EAE3C,QAAQ,WAAW,2BAA2B;;EAE9C,WAAW,WAAW,WAAW;;EAEjC,SAAS,WAAW,iCAAiC;;;;;;;;;;;;;;;;wBAiBvC,WACd,QACA,iBACA,cACA,UACA,OACA,WACA,OACA,UACA,WACG,QACF,gBAAa;yBAXA"}
|
package/dist/menu/menu-item.mjs
CHANGED
|
@@ -9,8 +9,9 @@ import { useMenuItem } from "./use-menu-item.mjs";
|
|
|
9
9
|
//#region src/menu/menu-item.tsx
|
|
10
10
|
/**
|
|
11
11
|
* A menu command: `<button type="button" role="menuitem" tabindex="-1">`. Click, Enter or Space
|
|
12
|
-
* runs `action` in the item's own transition (`data-pending`
|
|
13
|
-
*
|
|
12
|
+
* runs `action` in the item's own transition (`data-pending` on the item; a second activation
|
|
13
|
+
* meanwhile is ignored and the item `aria-disabled`, unless `whilePending` is `"run"`, the
|
|
14
|
+
* default of a `closeOnActivate={false}` item); once it resolves the whole menu stack closes
|
|
14
15
|
* through the top-level Root (`openChangeAction(false, { reason: "activate" })` awaited, view
|
|
15
16
|
* transition if set). Hovering the item focuses it.
|
|
16
17
|
*
|
|
@@ -20,9 +21,13 @@ import { useMenuItem } from "./use-menu-item.mjs";
|
|
|
20
21
|
*
|
|
21
22
|
* @see https://ui.funstack.work/components/menu
|
|
22
23
|
*/
|
|
23
|
-
function MenuItem({ action, closeOnActivate = true, disabled = false, label, className, style, children, render, ...rest }) {
|
|
24
|
+
function MenuItem({ action, closeOnActivate = true, whilePending, disabled = false, label, className, style, children, render, ...rest }) {
|
|
24
25
|
const menu = useMenuContext("Menu.Item");
|
|
25
|
-
const
|
|
26
|
+
const ignoring = (whilePending ?? (closeOnActivate ? "ignore" : "run")) === "ignore";
|
|
27
|
+
const { pending, run } = useActionTrigger({
|
|
28
|
+
openGeneration: menu.root.popover.openGeneration,
|
|
29
|
+
ignoreWhilePending: ignoring
|
|
30
|
+
});
|
|
26
31
|
const { libraryProps } = useMenuItem({
|
|
27
32
|
part: "Menu.Item",
|
|
28
33
|
role: "menuitem",
|
|
@@ -47,7 +52,7 @@ function MenuItem({ action, closeOnActivate = true, disabled = false, label, cla
|
|
|
47
52
|
libraryProps: {
|
|
48
53
|
...rendersButton(render) && { type: "button" },
|
|
49
54
|
...libraryProps,
|
|
50
|
-
"aria-disabled": disabled || pending ? true : void 0,
|
|
55
|
+
"aria-disabled": disabled || pending && ignoring ? true : void 0,
|
|
51
56
|
"data-pending": dataAttr(pending)
|
|
52
57
|
}
|
|
53
58
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"menu-item.mjs","names":[],"sources":["../../src/menu/menu-item.tsx"],"sourcesContent":["\"use client\";\nimport type { ComponentPropsWithRef, CSSProperties, ReactNode } from \"react\";\nimport { createActionDetails } from \"../internal/action-details.js\";\nimport { dataAttr } from \"../internal/data-attr.js\";\nimport type {
|
|
1
|
+
{"version":3,"file":"menu-item.mjs","names":[],"sources":["../../src/menu/menu-item.tsx"],"sourcesContent":["\"use client\";\nimport type { ComponentPropsWithRef, CSSProperties, ReactNode } from \"react\";\nimport { createActionDetails } from \"../internal/action-details.js\";\nimport { dataAttr } from \"../internal/data-attr.js\";\nimport type {\n Action,\n ActionDetails,\n RenderProp,\n StateValue,\n WhilePending,\n} from \"../internal/types.js\";\nimport { useActionTrigger } from \"../internal/use-action-trigger.js\";\nimport { rendersButton, useRender } from \"../internal/use-render.js\";\nimport { useMenuContext } from \"./menu-context.js\";\nimport { useMenuItem } from \"./use-menu-item.js\";\n\n/** Details passed to `Menu.Item`'s `action`. */\nexport interface MenuItemDetails extends ActionDetails {}\n\n/**\n * State of `Menu.Item`, passed to the function forms of `className`, `style`, `children` and\n * `render`.\n */\nexport interface MenuItemState {\n /** This item's `action` is in flight. */\n readonly pending: boolean;\n /** The `disabled` prop. */\n readonly disabled: boolean;\n}\n\n/**\n * Props of `Menu.Item`.\n *\n * @see https://ui.funstack.work/components/menu\n */\nexport interface MenuItemProps extends Omit<\n ComponentPropsWithRef<\"button\">,\n \"onClick\" | \"onKeyDown\" | \"onPointerMove\" | \"disabled\" | \"className\" | \"style\" | \"children\"\n> {\n /**\n * Runs inside this item's own transition (`data-pending` on the item); the menu stays open\n * until it settles, then the whole menu closes (`closeOnActivate`). May be a Server Function\n * (`details.event` is virtual). A rejection leaves the menu open and reaches the nearest Error\n * Boundary above the item. Without `action`, activation only closes.\n */\n action?: Action<[details: MenuItemDetails]>;\n /**\n * Close the whole menu once `action` settles.\n *\n * @defaultValue `true`\n */\n closeOnActivate?: boolean;\n /**\n * What a click, Enter or Space does while this item's `action` is pending. `\"ignore\"`:\n * nothing (the event is cancelled, `aria-disabled=\"true\"` meanwhile). `\"run\"`: the action runs\n * again (`data-pending` until every run has settled); a closing item closes the menu once,\n * when the last run settles and only if the latest run resolved (a rejection keeps the menu\n * open, as in `\"ignore\"` mode). No effect without `action`.\n *\n * @defaultValue `\"ignore\"`, or `\"run\"` with `closeOnActivate={false}`\n */\n whilePending?: WhilePending;\n /**\n * Renders `aria-disabled` and `data-disabled`: the item stays focusable and never activates.\n * It is an ordinary item for navigation; only the open focus skips items with `disabled`.\n *\n * @defaultValue `false`\n */\n disabled?: boolean;\n /** Text typeahead matches when the item's text content is not representative (an icon). */\n label?: string;\n /** Class name, or a function of the part's state that returns one. */\n className?: StateValue<string | undefined, MenuItemState>;\n /** Inline style, or a function of the part's state that returns one. */\n style?: StateValue<CSSProperties | undefined, MenuItemState>;\n /** Content, or a function of the part's state. */\n children?: StateValue<ReactNode, MenuItemState>;\n /** Replace the default `<button>` (element form) or take over rendering (function form). */\n render?: RenderProp<ComponentPropsWithRef<\"button\">, MenuItemState>;\n}\n\n/**\n * A menu command: `<button type=\"button\" role=\"menuitem\" tabindex=\"-1\">`. Click, Enter or Space\n * runs `action` in the item's own transition (`data-pending` on the item; a second activation\n * meanwhile is ignored and the item `aria-disabled`, unless `whilePending` is `\"run\"`, the\n * default of a `closeOnActivate={false}` item); once it resolves the whole menu stack closes\n * through the top-level Root (`openChangeAction(false, { reason: \"activate\" })` awaited, view\n * transition if set). Hovering the item focuses it.\n *\n * Owned keys (conventions 1.2): `type` (on a `<button>`), `role`, `tabIndex`, `disabled`\n * (always absent), `aria-disabled`, `onClick`, `onKeyDown`, `onPointerMove`, `data-pending`,\n * `data-disabled`, `data-typeahead`.\n *\n * @see https://ui.funstack.work/components/menu\n */\nexport function MenuItem({\n action,\n closeOnActivate = true,\n whilePending,\n disabled = false,\n label,\n className,\n style,\n children,\n render,\n ...rest\n}: MenuItemProps) {\n const menu = useMenuContext(\"Menu.Item\");\n // The close belongs to the top-level menu, so its open generation guards it.\n // Mixed ownership (conventions 2.5.1): an item the library closes the menu after ignores by\n // default; a keep-open item is a user-owned trigger and runs every activation.\n const ignoring = (whilePending ?? (closeOnActivate ? \"ignore\" : \"run\")) === \"ignore\";\n const { pending, run } = useActionTrigger({\n openGeneration: menu.root.popover.openGeneration,\n ignoreWhilePending: ignoring,\n });\n\n const { libraryProps } = useMenuItem({\n part: \"Menu.Item\",\n role: \"menuitem\",\n disabled,\n label,\n activate: (event) => {\n const details: MenuItemDetails = createActionDetails(event, {});\n // When ignoring, a second activation while this item's own run is in flight starts\n // nothing and is cancelled (the hook's guard): a link does not navigate, a composed\n // command does not run. Enter / Space reach here through the element's click, their keydown is cancelled.\n const started = run(\n () => action?.(details),\n closeOnActivate ? () => menu.root.popover.requestClose(event, \"activate\") : undefined,\n );\n if (!started) event.preventDefault();\n },\n });\n\n const state: MenuItemState = { pending, disabled };\n\n return useRender<ComponentPropsWithRef<\"button\">, MenuItemState>({\n defaultElement: \"button\",\n render,\n state,\n className,\n style,\n children,\n props: rest,\n libraryProps: {\n ...(rendersButton(render) && { type: \"button\" as const }),\n ...libraryProps,\n // Also while pending when ignoring: an item that will not act is unavailable to AT.\n // Navigation keys on `data-disabled` (the `disabled` prop), so a pending item is still an\n // enabled item.\n \"aria-disabled\": disabled || (pending && ignoring) ? true : undefined,\n \"data-pending\": dataAttr(pending),\n },\n });\n}\n\nMenuItem.displayName = \"MenuItem\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA+FA,SAAgB,SAAS,EACvB,QACA,kBAAkB,MAClB,cACA,WAAW,OACX,OACA,WACA,OACA,UACA,QACA,GAAG,QACa;CAChB,MAAM,OAAO,eAAe,WAAW;CAIvC,MAAM,YAAY,iBAAiB,kBAAkB,WAAW,YAAY;CAC5E,MAAM,EAAE,SAAS,QAAQ,iBAAiB;EACxC,gBAAgB,KAAK,KAAK,QAAQ;EAClC,oBAAoB;CACtB,CAAC;CAED,MAAM,EAAE,iBAAiB,YAAY;EACnC,MAAM;EACN,MAAM;EACN;EACA;EACA,WAAW,UAAU;GACnB,MAAM,UAA2B,oBAAoB,OAAO,CAAC,CAAC;GAQ9D,IAAI,CAJY,UACR,SAAS,OAAO,GACtB,wBAAwB,KAAK,KAAK,QAAQ,aAAa,OAAO,UAAU,IAAI,KAAA,CAEnE,GAAG,MAAM,eAAe;EACrC;CACF,CAAC;CAID,OAAO,UAA0D;EAC/D,gBAAgB;EAChB;EACA,OAAA;GAL6B;GAAS;EAKlC;EACJ;EACA;EACA;EACA,OAAO;EACP,cAAc;GACZ,GAAI,cAAc,MAAM,KAAK,EAAE,MAAM,SAAkB;GACvD,GAAG;GAIH,iBAAiB,YAAa,WAAW,WAAY,OAAO,KAAA;GAC5D,gBAAgB,SAAS,OAAO;EAClC;CACF,CAAC;AACH;AAEA,SAAS,cAAc"}
|
|
@@ -26,8 +26,8 @@ function scrollsWithin(popup, target, event) {
|
|
|
26
26
|
/**
|
|
27
27
|
* The outside right- and middle-click close of a top-level menu (context-menu.md 5): a
|
|
28
28
|
* `contextmenu` outside the menu tree that this menu's ContextTrigger did not handle, or a
|
|
29
|
-
* middle-button `pointerdown` outside it, hides the root popup at once (`"dismiss"`
|
|
30
|
-
* `toggle`
|
|
29
|
+
* middle-button `pointerdown` outside it, hides the root popup at once (a fact: `"dismiss"` to
|
|
30
|
+
* `onOpenChange` from the `toggle`). Chromium's click-based light dismiss ignores those buttons.
|
|
31
31
|
*
|
|
32
32
|
* While the menu is placed at a point (4.8; read at each event: a right-click may
|
|
33
33
|
* re-place a Trigger-opened menu), a `scroll` of the document or of an element that contains the
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"menu-outside.mjs","names":[],"sources":["../../src/menu/menu-outside.ts"],"sourcesContent":["// The outside right- and middle-click, scroll and wheel handling of an open top-level menu\n// (design/components/context-menu.md 5). No \"use client\" directive: DOM listeners only\n// (conventions 7.1).\nimport { isPopoverOpen } from \"../internal/popover-open.js\";\nimport { isHandledBy } from \"./menu-context.js\";\nimport type { MenuContextValue, MenuPlacement } from \"./menu-context.js\";\n\n/**\n * The element under a wheel event that can scroll the way the wheel turns, up to `popup`: then\n * the wheel is the menu's own and the page under it is not involved.\n */\nfunction scrollsWithin(popup: HTMLElement, target: EventTarget | null, event: WheelEvent) {\n const vertical = Math.abs(event.deltaY) >= Math.abs(event.deltaX);\n const delta = vertical ? event.deltaY : event.deltaX;\n if (delta === 0) return true;\n let element = target instanceof Element ? target : null;\n while (element !== null && popup.contains(element)) {\n const computed = element.ownerDocument.defaultView?.getComputedStyle(element);\n const overflow = computed?.[vertical ? \"overflowY\" : \"overflowX\"] ?? \"\";\n if (overflow === \"auto\" || overflow === \"scroll\" || overflow === \"overlay\") {\n // Scroll offsets run 0..room (down, or right in LTR) or -room..0 (right to left).\n const room = vertical\n ? element.scrollHeight - element.clientHeight\n : element.scrollWidth - element.clientWidth;\n const offset = vertical ? element.scrollTop : element.scrollLeft;\n const min = !vertical && computed?.direction === \"rtl\" ? -room : 0;\n if (room > 0 && (delta > 0 ? offset < min + room - 0.5 : offset > min + 0.5)) return true;\n }\n element = element.parentElement;\n }\n return false;\n}\n\n/**\n * The outside right- and middle-click close of a top-level menu (context-menu.md 5): a\n * `contextmenu` outside the menu tree that this menu's ContextTrigger did not handle, or a\n * middle-button `pointerdown` outside it, hides the root popup at once (`\"dismiss\"`
|
|
1
|
+
{"version":3,"file":"menu-outside.mjs","names":[],"sources":["../../src/menu/menu-outside.ts"],"sourcesContent":["// The outside right- and middle-click, scroll and wheel handling of an open top-level menu\n// (design/components/context-menu.md 5). No \"use client\" directive: DOM listeners only\n// (conventions 7.1).\nimport { isPopoverOpen } from \"../internal/popover-open.js\";\nimport { isHandledBy } from \"./menu-context.js\";\nimport type { MenuContextValue, MenuPlacement } from \"./menu-context.js\";\n\n/**\n * The element under a wheel event that can scroll the way the wheel turns, up to `popup`: then\n * the wheel is the menu's own and the page under it is not involved.\n */\nfunction scrollsWithin(popup: HTMLElement, target: EventTarget | null, event: WheelEvent) {\n const vertical = Math.abs(event.deltaY) >= Math.abs(event.deltaX);\n const delta = vertical ? event.deltaY : event.deltaX;\n if (delta === 0) return true;\n let element = target instanceof Element ? target : null;\n while (element !== null && popup.contains(element)) {\n const computed = element.ownerDocument.defaultView?.getComputedStyle(element);\n const overflow = computed?.[vertical ? \"overflowY\" : \"overflowX\"] ?? \"\";\n if (overflow === \"auto\" || overflow === \"scroll\" || overflow === \"overlay\") {\n // Scroll offsets run 0..room (down, or right in LTR) or -room..0 (right to left).\n const room = vertical\n ? element.scrollHeight - element.clientHeight\n : element.scrollWidth - element.clientWidth;\n const offset = vertical ? element.scrollTop : element.scrollLeft;\n const min = !vertical && computed?.direction === \"rtl\" ? -room : 0;\n if (room > 0 && (delta > 0 ? offset < min + room - 0.5 : offset > min + 0.5)) return true;\n }\n element = element.parentElement;\n }\n return false;\n}\n\n/**\n * The outside right- and middle-click close of a top-level menu (context-menu.md 5): a\n * `contextmenu` outside the menu tree that this menu's ContextTrigger did not handle, or a\n * middle-button `pointerdown` outside it, hides the root popup at once (a fact: `\"dismiss\"` to\n * `onOpenChange` from the `toggle`). Chromium's click-based light dismiss ignores those buttons.\n *\n * While the menu is placed at a point (4.8; read at each event: a right-click may\n * re-place a Trigger-opened menu), a `scroll` of the document or of an element that contains the\n * region closes it the same way; scrolls elsewhere and inside the menu are ignored. Scrolling\n * already in flight at the open (a smooth scroll, trackpad momentum) does not count: scroll\n * events are ignored until the first animation frame without one. And a `wheel`\n * over any menu never scrolls the page: it is prevented unless something in the menu under the\n * pointer can scroll that way, or it is a zoom (`ctrlKey`: Ctrl+wheel, trackpad pinch). Returns\n * the cleanup.\n */\nexport function listenOutside(\n popup: HTMLElement,\n menu: MenuContextValue,\n placement: () => MenuPlacement,\n): () => void {\n const doc = popup.ownerDocument;\n const inside = (target: EventTarget | null) => target instanceof Node && popup.contains(target);\n const close = () => {\n if (isPopoverOpen(popup)) popup.hidePopover();\n };\n // Bubble phase: the region's handler ran first and marked a move.\n const onContextMenu = (event: Event) => {\n if (inside(event.target) || isHandledBy(event, menu)) return;\n close();\n };\n const onPointerDown = (event: Event) => {\n if ((event as globalThis.PointerEvent).button !== 1 || inside(event.target)) return;\n close();\n };\n // Scroll events run in a frame's rendering steps, before its animation frame callbacks: the\n // first callback that saw none since the last one ends the in-flight scroll window.\n const view = doc.defaultView;\n let settling = typeof view?.requestAnimationFrame === \"function\";\n let scrolledThisFrame = false;\n let frame = 0;\n const tick = () => {\n if (scrolledThisFrame) {\n scrolledThisFrame = false;\n frame = view!.requestAnimationFrame(tick);\n } else settling = false;\n };\n if (settling) frame = view!.requestAnimationFrame(tick);\n const onScroll = (event: Event) => {\n if (settling) {\n scrolledThisFrame = true;\n return;\n }\n const current = placement();\n if (current.kind !== \"point\") return;\n const target = event.target;\n // The point is meaningless once the content under it moved: the document, or a scroller\n // around the region (React Aria's rule). A scroller elsewhere or inside the menu is not.\n if (target instanceof Node && target.nodeType !== Node.DOCUMENT_NODE) {\n if (inside(target) || !target.contains(current.source)) return;\n }\n close();\n };\n // Native menus never scroll the page from inside the menu; a non-overflowing popup would chain\n // the wheel to the page (Firefox ignores `overscroll-behavior` there).\n const onWheel = (event: WheelEvent) => {\n // A zoom, not a scroll: Ctrl+wheel and a trackpad pinch (`wheel` with `ctrlKey`) stay the\n // user's.\n if (event.ctrlKey) return;\n if (!scrollsWithin(popup, event.target, event)) event.preventDefault();\n };\n doc.addEventListener(\"contextmenu\", onContextMenu);\n doc.addEventListener(\"pointerdown\", onPointerDown, true);\n doc.addEventListener(\"scroll\", onScroll, { capture: true, passive: true });\n popup.addEventListener(\"wheel\", onWheel, { passive: false });\n return () => {\n if (settling) view!.cancelAnimationFrame(frame);\n doc.removeEventListener(\"contextmenu\", onContextMenu);\n doc.removeEventListener(\"pointerdown\", onPointerDown, true);\n doc.removeEventListener(\"scroll\", onScroll, { capture: true });\n popup.removeEventListener(\"wheel\", onWheel);\n };\n}\n"],"mappings":";;;;;;;AAWA,SAAS,cAAc,OAAoB,QAA4B,OAAmB;CACxF,MAAM,WAAW,KAAK,IAAI,MAAM,MAAM,KAAK,KAAK,IAAI,MAAM,MAAM;CAChE,MAAM,QAAQ,WAAW,MAAM,SAAS,MAAM;CAC9C,IAAI,UAAU,GAAG,OAAO;CACxB,IAAI,UAAU,kBAAkB,UAAU,SAAS;CACnD,OAAO,YAAY,QAAQ,MAAM,SAAS,OAAO,GAAG;EAClD,MAAM,WAAW,QAAQ,cAAc,aAAa,iBAAiB,OAAO;EAC5E,MAAM,WAAW,WAAW,WAAW,cAAc,gBAAgB;EACrE,IAAI,aAAa,UAAU,aAAa,YAAY,aAAa,WAAW;GAE1E,MAAM,OAAO,WACT,QAAQ,eAAe,QAAQ,eAC/B,QAAQ,cAAc,QAAQ;GAClC,MAAM,SAAS,WAAW,QAAQ,YAAY,QAAQ;GACtD,MAAM,MAAM,CAAC,YAAY,UAAU,cAAc,QAAQ,CAAC,OAAO;GACjE,IAAI,OAAO,MAAM,QAAQ,IAAI,SAAS,MAAM,OAAO,KAAM,SAAS,MAAM,KAAM,OAAO;EACvF;EACA,UAAU,QAAQ;CACpB;CACA,OAAO;AACT;;;;;;;;;;;;;;;;AAiBA,SAAgB,cACd,OACA,MACA,WACY;CACZ,MAAM,MAAM,MAAM;CAClB,MAAM,UAAU,WAA+B,kBAAkB,QAAQ,MAAM,SAAS,MAAM;CAC9F,MAAM,cAAc;EAClB,IAAI,cAAc,KAAK,GAAG,MAAM,YAAY;CAC9C;CAEA,MAAM,iBAAiB,UAA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,MAAM,oBAAoB,SAAS,OAAO;CAC5C;AACF"}
|
|
@@ -80,6 +80,9 @@ export interface MenuPopupProps extends Omit<ComponentPropsWithRef<"div">, "id"
|
|
|
80
80
|
* commit and every library-driven close (an item's activation, the Trigger, ArrowLeft, hover
|
|
81
81
|
* leaving a submenu, controlled `open={false}`) in all engines; dismissals (Escape, a click
|
|
82
82
|
* outside, Tab) animate with CSS only. Without it the part renders no `<ViewTransition>`.
|
|
83
|
+
* While a view transition runs, the popup's CSS transitions are turned off for the moment of the
|
|
84
|
+
* library's own show or hide (an inline `transition: none !important`, restored right after),
|
|
85
|
+
* so that show or hide runs no CSS transition (a `@keyframes` animation is not cut).
|
|
83
86
|
*/
|
|
84
87
|
transition?: ViewTransitionClass;
|
|
85
88
|
/** Class name, or a function of the part's state that returns one. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"menu-popup.d.mts","names":[],"sources":["../../src/menu/menu-popup.tsx"],"mappings":";;;;;YAsCY,WAAW;;;;;YAKX,YAAY;;;;;iBAMP;;WAEN;;;;;;WAMA,MAAM;;WAEN,OAAO;;WAEP;;;;;WAKA;;;;;;;iBAQM,uBAAuB,KACtC;;;;;;;;;;;;;EA8BA,OAAO;;;;;;;EAOP,QAAQ;;;;;;;;EAQR;;;;;;;;;EASA
|
|
1
|
+
{"version":3,"file":"menu-popup.d.mts","names":[],"sources":["../../src/menu/menu-popup.tsx"],"mappings":";;;;;YAsCY,WAAW;;;;;YAKX,YAAY;;;;;iBAMP;;WAEN;;;;;;WAMA,MAAM;;WAEN,OAAO;;WAEP;;;;;WAKA;;;;;;;iBAQM,uBAAuB,KACtC;;;;;;;;;;;;;EA8BA,OAAO;;;;;;;EAOP,QAAQ;;;;;;;;EAQR;;;;;;;;;EASA;;;;;;;;;;EAUA,aAAa;;EAEb,YAAY,+BAA+B;;EAE3C,QAAQ,WAAW,2BAA2B;;EAE9C,WAAW,WAAW,WAAW;;EAEjC,SAAS,WAAW,8BAA8B;;;;;;;;;;;;;;;;;;;;;;;;;;wBA+BpC,YACd,MAAM,UACN,OACA,aACA,yBACA,YACA,WACA,OACA,UACA,WACG,QACF,iBAAc;yBAXD"}
|