@funstack/ui 0.1.0-alpha.21 → 0.1.0-alpha.22
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/docs/components/menu.md +1 -1
- package/dist/docs/components/menubar.md +309 -0
- package/dist/docs/components/meter.md +1 -1
- package/dist/docs/components/navigation-menu.md +501 -0
- package/dist/docs/components/number-field.md +2 -2
- package/dist/docs/components/otp-field.md +8 -1
- package/dist/docs/components/progress.md +1 -1
- package/dist/docs/components/radio-group.md +1 -1
- package/dist/docs/components/slider.md +1 -1
- package/dist/docs/concepts/direction-and-locales.md +106 -0
- package/dist/docs/index.md +4 -1
- package/dist/docs/roadmap.md +7 -0
- package/dist/index.d.mts +11 -2
- package/dist/index.mjs +3 -1
- package/dist/internal/menubar-context.mjs +12 -0
- package/dist/internal/menubar-context.mjs.map +1 -0
- package/dist/internal/overlay/trigger-ref.mjs +1 -1
- package/dist/internal/overlay/trigger-ref.mjs.map +1 -1
- package/dist/internal/overlay/use-overlay-root.mjs +3 -2
- package/dist/internal/overlay/use-overlay-root.mjs.map +1 -1
- package/dist/internal/overlay/use-popover-trigger.mjs +2 -2
- package/dist/internal/overlay/use-popover-trigger.mjs.map +1 -1
- package/dist/internal/range-value.mjs +5 -3
- package/dist/internal/range-value.mjs.map +1 -1
- package/dist/menu/menu-popup.d.mts.map +1 -1
- package/dist/menu/menu-popup.mjs +33 -6
- package/dist/menu/menu-popup.mjs.map +1 -1
- package/dist/menu/menu-trigger.d.mts +10 -3
- package/dist/menu/menu-trigger.d.mts.map +1 -1
- package/dist/menu/menu-trigger.mjs +53 -5
- package/dist/menu/menu-trigger.mjs.map +1 -1
- package/dist/menubar/index.d.mts +3 -0
- package/dist/menubar/index.mjs +1 -0
- package/dist/menubar/menubar.d.mts +49 -0
- package/dist/menubar/menubar.d.mts.map +1 -0
- package/dist/menubar/menubar.mjs +147 -0
- package/dist/menubar/menubar.mjs.map +1 -0
- package/dist/meter/meter-root.d.mts +3 -2
- package/dist/meter/meter-root.d.mts.map +1 -1
- package/dist/meter/meter-root.mjs.map +1 -1
- package/dist/multi-select/multi-select-root.mjs +1 -1
- package/dist/multi-select/multi-select-trigger.mjs +1 -1
- package/dist/navigation-menu/index.d.mts +32 -0
- package/dist/navigation-menu/index.d.mts.map +1 -0
- package/dist/navigation-menu/index.mjs +32 -0
- package/dist/navigation-menu/index.mjs.map +1 -0
- package/dist/navigation-menu/navigation-menu-context.mjs +19 -0
- package/dist/navigation-menu/navigation-menu-context.mjs.map +1 -0
- package/dist/navigation-menu/navigation-menu-item.d.mts +79 -0
- package/dist/navigation-menu/navigation-menu-item.d.mts.map +1 -0
- package/dist/navigation-menu/navigation-menu-item.mjs +80 -0
- package/dist/navigation-menu/navigation-menu-item.mjs.map +1 -0
- package/dist/navigation-menu/navigation-menu-link.d.mts +48 -0
- package/dist/navigation-menu/navigation-menu-link.d.mts.map +1 -0
- package/dist/navigation-menu/navigation-menu-link.mjs +66 -0
- package/dist/navigation-menu/navigation-menu-link.mjs.map +1 -0
- package/dist/navigation-menu/navigation-menu-list.d.mts +33 -0
- package/dist/navigation-menu/navigation-menu-list.d.mts.map +1 -0
- package/dist/navigation-menu/navigation-menu-list.mjs +30 -0
- package/dist/navigation-menu/navigation-menu-list.mjs.map +1 -0
- package/dist/navigation-menu/navigation-menu-popup.d.mts +102 -0
- package/dist/navigation-menu/navigation-menu-popup.d.mts.map +1 -0
- package/dist/navigation-menu/navigation-menu-popup.mjs +93 -0
- package/dist/navigation-menu/navigation-menu-popup.mjs.map +1 -0
- package/dist/navigation-menu/navigation-menu-root.d.mts +33 -0
- package/dist/navigation-menu/navigation-menu-root.d.mts.map +1 -0
- package/dist/navigation-menu/navigation-menu-root.mjs +27 -0
- package/dist/navigation-menu/navigation-menu-root.mjs.map +1 -0
- package/dist/navigation-menu/navigation-menu-trigger.d.mts +32 -0
- package/dist/navigation-menu/navigation-menu-trigger.d.mts.map +1 -0
- package/dist/navigation-menu/navigation-menu-trigger.mjs +42 -0
- package/dist/navigation-menu/navigation-menu-trigger.mjs.map +1 -0
- package/dist/otp-field/otp-field-root.d.mts +9 -0
- package/dist/otp-field/otp-field-root.d.mts.map +1 -1
- package/dist/otp-field/otp-field-root.mjs +5 -2
- package/dist/otp-field/otp-field-root.mjs.map +1 -1
- package/dist/popover/index.d.mts +1 -1
- package/dist/popover/popover-trigger.mjs +1 -1
- package/dist/progress/progress-root.d.mts +3 -2
- package/dist/progress/progress-root.d.mts.map +1 -1
- package/dist/progress/progress-root.mjs.map +1 -1
- package/package.json +4 -1
- package/skills/funstack-ui-knowledge/SKILL.md +3 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"range-value.mjs","names":[],"sources":["../../src/internal/range-value.ts"],"sourcesContent":["// No \"use client\": pure arithmetic and formatting shared by the directive-free `Progress.Root`\n// and `Meter.Root` (design/components/progress.md 2.1, 2.2 and implementation note 14.4).\n\n/** The platform's clamp (`<progress>` 150 of 100 is 100). A degenerate range yields `min`. */\nexport function clamp(value: number, min: number, max: number): number {\n return Math.max(min, Math.min(max, value));\n}\n\n/** Whether `[min, max]` is empty or a point (`max <= min`): a bug at the call site. */\nexport function isDegenerate(min: number, max: number): boolean {\n return !(max > min);\n}\n\n/** `(value - min) / (max - min)`, `0` for a degenerate range. */\nexport function fraction(value: number, min: number, max: number): number {\n return isDegenerate(min, max) ? 0 : (value - min) / (max - min);\n}\n\n/** The fraction as a number 0–100. */\nexport function percentage(value: number, min: number, max: number): number {\n return fraction(value, min, max) * 100;\n}\n\nconst PERCENT: Intl.NumberFormatOptions = { style: \"percent\" };\nconst CACHE_LIMIT = 32;\nconst formatters = new Map<string, Intl.NumberFormat>();\n\n/**\n * One `Intl.NumberFormat` per `locale` + `format`, so an update loop does not construct one per\n * render. Bounded: past 32 entries the oldest is dropped.\n */\nexport function getNumberFormat(\n locale:
|
|
1
|
+
{"version":3,"file":"range-value.mjs","names":[],"sources":["../../src/internal/range-value.ts"],"sourcesContent":["// No \"use client\": pure arithmetic and formatting shared by the directive-free `Progress.Root`\n// and `Meter.Root` (design/components/progress.md 2.1, 2.2 and implementation note 14.4).\n\n/** The platform's clamp (`<progress>` 150 of 100 is 100). A degenerate range yields `min`. */\nexport function clamp(value: number, min: number, max: number): number {\n return Math.max(min, Math.min(max, value));\n}\n\n/** Whether `[min, max]` is empty or a point (`max <= min`): a bug at the call site. */\nexport function isDegenerate(min: number, max: number): boolean {\n return !(max > min);\n}\n\n/** `(value - min) / (max - min)`, `0` for a degenerate range. */\nexport function fraction(value: number, min: number, max: number): number {\n return isDegenerate(min, max) ? 0 : (value - min) / (max - min);\n}\n\n/** The fraction as a number 0–100. */\nexport function percentage(value: number, min: number, max: number): number {\n return fraction(value, min, max) * 100;\n}\n\nconst PERCENT: Intl.NumberFormatOptions = { style: \"percent\" };\nconst CACHE_LIMIT = 32;\nconst formatters = new Map<string, Intl.NumberFormat>();\n\n/** A tag or a preference list of tags, what the `Intl` constructors take (conventions 14.2). */\nexport type RangeLocale = string | readonly string[];\n\n/**\n * One `Intl.NumberFormat` per `locale` + `format`, so an update loop does not construct one per\n * render. Bounded: past 32 entries the oldest is dropped. The key is JSON, as in\n * `number-field/number-field-format.ts`, so a list and a string never collide.\n */\nexport function getNumberFormat(\n locale: RangeLocale | undefined,\n format: Intl.NumberFormatOptions | undefined,\n): Intl.NumberFormat {\n const options = format ?? PERCENT;\n const key = JSON.stringify([locale ?? null, options]);\n let formatter = formatters.get(key);\n if (formatter === undefined) {\n // `Intl` takes a mutable `string[]`: copy a readonly list rather than cast it.\n const locales = typeof locale === \"string\" || locale === undefined ? locale : [...locale];\n formatter = new Intl.NumberFormat(locales, options);\n if (formatters.size >= CACHE_LIMIT) {\n const oldest = formatters.keys().next().value;\n if (oldest !== undefined) formatters.delete(oldest);\n }\n formatters.set(key, formatter);\n }\n return formatter;\n}\n\n/**\n * The visible text of a (clamped) value: the fraction of the range for the `percent` style (the\n * default when `format` is absent), the value itself for any other style. A `format` without\n * `style` is `decimal` per `Intl` and formats the value.\n */\nexport function formatRangeValue(\n value: number,\n min: number,\n max: number,\n format: Intl.NumberFormatOptions | undefined,\n locale: RangeLocale | undefined,\n): string {\n const percentStyle = format === undefined || format.style === \"percent\";\n return getNumberFormat(locale, format).format(percentStyle ? fraction(value, min, max) : value);\n}\n"],"mappings":";;AAIA,SAAgB,MAAM,OAAe,KAAa,KAAqB;CACrE,OAAO,KAAK,IAAI,KAAK,KAAK,IAAI,KAAK,KAAK,CAAC;AAC3C;;AAGA,SAAgB,aAAa,KAAa,KAAsB;CAC9D,OAAO,EAAE,MAAM;AACjB;;AAGA,SAAgB,SAAS,OAAe,KAAa,KAAqB;CACxE,OAAO,aAAa,KAAK,GAAG,IAAI,KAAK,QAAQ,QAAQ,MAAM;AAC7D;;AAGA,SAAgB,WAAW,OAAe,KAAa,KAAqB;CAC1E,OAAO,SAAS,OAAO,KAAK,GAAG,IAAI;AACrC;AAEA,MAAM,UAAoC,EAAE,OAAO,UAAU;AAC7D,MAAM,cAAc;AACpB,MAAM,6BAAa,IAAI,IAA+B;;;;;;AAUtD,SAAgB,gBACd,QACA,QACmB;CACnB,MAAM,UAAU,UAAU;CAC1B,MAAM,MAAM,KAAK,UAAU,CAAC,UAAU,MAAM,OAAO,CAAC;CACpD,IAAI,YAAY,WAAW,IAAI,GAAG;CAClC,IAAI,cAAc,KAAA,GAAW;EAE3B,MAAM,UAAU,OAAO,WAAW,YAAY,WAAW,KAAA,IAAY,SAAS,CAAC,GAAG,MAAM;EACxF,YAAY,IAAI,KAAK,aAAa,SAAS,OAAO;EAClD,IAAI,WAAW,QAAQ,aAAa;GAClC,MAAM,SAAS,WAAW,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC;GACxC,IAAI,WAAW,KAAA,GAAW,WAAW,OAAO,MAAM;EACpD;EACA,WAAW,IAAI,KAAK,SAAS;CAC/B;CACA,OAAO;AACT;;;;;;AAOA,SAAgB,iBACd,OACA,KACA,KACA,QACA,QACQ;CACR,MAAM,eAAe,WAAW,KAAA,KAAa,OAAO,UAAU;CAC9D,OAAO,gBAAgB,QAAQ,MAAM,CAAC,CAAC,OAAO,eAAe,SAAS,OAAO,KAAK,GAAG,IAAI,KAAK;AAChG"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"menu-popup.d.mts","names":[],"sources":["../../src/menu/menu-popup.tsx"],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"menu-popup.d.mts","names":[],"sources":["../../src/menu/menu-popup.tsx"],"mappings":";;;;;YAwCY,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;;;;;;;;;;;;;;;;;;;;;;;;;;wBAmCpC,YACd,MAAM,UACN,OACA,aACA,yBACA,YACA,WACA,OACA,UACA,WACG,QACF,iBAAc;yBAXD"}
|
package/dist/menu/menu-popup.mjs
CHANGED
|
@@ -14,7 +14,8 @@ import { listenInvokerPresses } from "../internal/overlay/invokers.mjs";
|
|
|
14
14
|
import { usePopoverPopup } from "../internal/overlay/use-popover-popup.mjs";
|
|
15
15
|
import { capturePointer } from "../internal/pointer-capture.mjs";
|
|
16
16
|
import { MenuListContext, claimPress, useMenuContext } from "./menu-context.mjs";
|
|
17
|
-
import { edgeItem } from "./menu-dom.mjs";
|
|
17
|
+
import { edgeItem, hasModifier } from "./menu-dom.mjs";
|
|
18
|
+
import { MenubarContext, useMenubarContext } from "../internal/menubar-context.mjs";
|
|
18
19
|
import { scheduleHoverClose } from "./menu-hover.mjs";
|
|
19
20
|
import { listenOutside } from "./menu-outside.mjs";
|
|
20
21
|
import { MENU_POINT_HREF, MENU_POINT_RULES, pointStyle } from "./menu-point.mjs";
|
|
@@ -67,7 +68,27 @@ function MenuPopup({ side: sideProp, align = "start", keepMounted, experimental_
|
|
|
67
68
|
});
|
|
68
69
|
const { side, align: resolvedAlign, ref: placementRef } = placement;
|
|
69
70
|
const nativeFocusgroup = useNativeFocusgroup(experimental_focusgroup);
|
|
70
|
-
const { onKeyDown, typingAhead } = useMenuKeyboard(menu, nativeFocusgroup);
|
|
71
|
+
const { onKeyDown: onMenuKeyDown, typingAhead } = useMenuKeyboard(menu, nativeFocusgroup);
|
|
72
|
+
const bar = useMenubarContext();
|
|
73
|
+
const onKeyDown = (event) => {
|
|
74
|
+
onMenuKeyDown(event);
|
|
75
|
+
if (bar === null || event.defaultPrevented || hasModifier(event)) return;
|
|
76
|
+
const popup = event.currentTarget;
|
|
77
|
+
const target = event.target;
|
|
78
|
+
if (!target.matches("[role=\"menuitem\"], [role=\"menuitemcheckbox\"], [role=\"menuitemradio\"]") || !popup.contains(target)) return;
|
|
79
|
+
const rtl = bar.rtl();
|
|
80
|
+
let delta;
|
|
81
|
+
if (event.key === (rtl ? "ArrowLeft" : "ArrowRight")) {
|
|
82
|
+
if (target.hasAttribute("aria-haspopup")) return;
|
|
83
|
+
delta = 1;
|
|
84
|
+
} else if (event.key === (rtl ? "ArrowRight" : "ArrowLeft")) {
|
|
85
|
+
if (target.closest("[role=\"menu\"]") !== popup) return;
|
|
86
|
+
delta = -1;
|
|
87
|
+
} else return;
|
|
88
|
+
const trigger = popover.triggerRef.current;
|
|
89
|
+
if (trigger === null) return;
|
|
90
|
+
if (bar.step(trigger, delta, event.nativeEvent)) event.preventDefault();
|
|
91
|
+
};
|
|
71
92
|
const { setElement, visible, renderContent, reshow } = usePopoverPopup(popover, {
|
|
72
93
|
part: "Menu.Popup",
|
|
73
94
|
initialFocus: (popup) => {
|
|
@@ -170,10 +191,16 @@ function MenuPopup({ side: sideProp, align = "start", keepMounted, experimental_
|
|
|
170
191
|
nonce: csp?.nonce,
|
|
171
192
|
children: MENU_POINT_RULES
|
|
172
193
|
}) : null;
|
|
173
|
-
const content = (s) =>
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
194
|
+
const content = (s) => {
|
|
195
|
+
const list = /* @__PURE__ */ jsxs(MenuListContext, {
|
|
196
|
+
value: listContext,
|
|
197
|
+
children: [pointRules, renderContent(() => resolveStateValue(children, s))]
|
|
198
|
+
});
|
|
199
|
+
return bar === null ? list : /* @__PURE__ */ jsx(MenubarContext, {
|
|
200
|
+
value: null,
|
|
201
|
+
children: list
|
|
202
|
+
});
|
|
203
|
+
};
|
|
177
204
|
const element = useRender({
|
|
178
205
|
defaultElement: "div",
|
|
179
206
|
render,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"menu-popup.mjs","names":[],"sources":["../../src/menu/menu-popup.tsx"],"sourcesContent":["\"use client\";\nimport { use, useCallback, useLayoutEffect, useMemo, useRef } from \"react\";\nimport type {\n ComponentPropsWithRef,\n CSSProperties,\n FocusEvent,\n MouseEvent,\n PointerEvent,\n ReactNode,\n ViewTransitionClass,\n} from \"react\";\nimport type { AnchorAlign, AnchorSide } from \"../internal/overlay/anchor-position.js\";\nimport { CSPContext } from \"../internal/csp-context.js\";\nimport { listenInvokerPresses } from \"../internal/overlay/invokers.js\";\nimport { isPopoverOpen } from \"../internal/overlay/popover-open.js\";\nimport { capturePointer } from \"../internal/pointer-capture.js\";\nimport { FSUI_PRECEDENCE } from \"../internal/shipped-styles.js\";\nimport { useTransitionBoundary } from \"../internal/transition-boundary.js\";\nimport type { RenderProp, StateValue } from \"../internal/types.js\";\nimport { useElementCheck } from \"../internal/use-element-check.js\";\nimport { useLatest } from \"../internal/use-latest.js\";\nimport { useMergedRefs } from \"../internal/use-merged-refs.js\";\nimport { useNativeFocusgroup } from \"../internal/use-native-focusgroup.js\";\nimport { usePopoverPopup } from \"../internal/overlay/use-popover-popup.js\";\nimport { useAnchoredPlacement } from \"../internal/overlay/use-resolved-placement.js\";\nimport { resolveStateValue, useRender } from \"../internal/use-render.js\";\nimport type { DataAttributes } from \"../internal/use-render.js\";\nimport { claimPress, MenuListContext, useMenuContext } from \"./menu-context.js\";\nimport type { MenuListContextValue } from \"./menu-context.js\";\nimport { edgeItem } from \"./menu-dom.js\";\nimport { scheduleHoverClose } from \"./menu-hover.js\";\nimport { listenOutside } from \"./menu-outside.js\";\nimport { MENU_POINT_HREF, MENU_POINT_RULES, pointStyle } from \"./menu-point.js\";\nimport { useMenuKeyboard } from \"./use-menu-keyboard.js\";\n\nconst isDev = process.env.NODE_ENV !== \"production\";\n\n/** `inline-start` / `inline-end` follow the popup's `direction` (conventions 13.1). */\nexport type MenuSide = AnchorSide;\n/**\n * Alignment of the Popup along its side: inline-relative on a top / bottom side, the top / bottom\n * edges on the others.\n */\nexport type MenuAlign = AnchorAlign;\n\n/**\n * State of `Menu.Popup`, passed to the function forms of `className`, `style`, `children` and\n * `render`.\n */\nexport interface MenuPopupState {\n /** Committed open state. Read `:popover-open` for the DOM truth. */\n readonly open: boolean;\n /**\n * While shown: the physical side the browser placed the popup on (after any fallback flip;\n * conventions 13.2). Otherwise the `side` prop. `null` with `side={null}`, and while the menu\n * is placed at a point (`context`).\n */\n readonly side: MenuSide | null;\n /** While shown: the resolved alignment; otherwise the `align` prop. */\n readonly align: MenuAlign;\n /** This popup belongs to a submenu. */\n readonly submenu: boolean;\n /**\n * The current open came from a `Menu.ContextTrigger`: the popup is placed at the pointer, with\n * `--menu-point-x` / `--menu-point-y` set inline (`data-context`).\n */\n readonly context: boolean;\n}\n\n/**\n * Props of `Menu.Popup`.\n *\n * @see https://ui.funstack.work/components/menu\n */\nexport interface MenuPopupProps extends Omit<\n ComponentPropsWithRef<\"div\">,\n | \"id\"\n | \"popover\"\n | \"role\"\n | \"aria-labelledby\"\n | \"onKeyDown\"\n | \"onFocus\"\n | \"onBlur\"\n | \"onMouseDown\"\n | \"onContextMenu\"\n | \"onPointerEnter\"\n | \"onPointerLeave\"\n | \"onToggle\"\n | \"onBeforeToggle\"\n | \"className\"\n | \"style\"\n | \"children\"\n> {\n /**\n * Placement against the Trigger through inline `position-area`, with `position-try-fallbacks`\n * flipping it when it does not fit. A submenu's `\"inline-end\"` is the right of the item in LTR,\n * its left in RTL; its fallback is `flip-inline` only. `\"left\"` / `\"right\"` stay physical.\n * `null` emits no inline positioning (and no `data-side` / `data-align`): the stylesheet owns\n * placement. Ignored for an open from a\n * `Menu.ContextTrigger`, which is placed at the pointer; there `null` also opts out of the\n * inline point placement and the library's `@position-try` rules, leaving only\n * `--menu-point-x` / `--menu-point-y`.\n *\n * @defaultValue `\"bottom\"` for a top-level menu, `\"inline-end\"` for a submenu\n */\n side?: MenuSide | null;\n /**\n * Alignment along the side: inline-relative on a top / bottom side (mirrored under\n * `direction: rtl`), the top edges on the others. Ignored when `side` is `null`.\n *\n * @defaultValue `\"start\"` (both levels)\n */\n align?: MenuAlign;\n /**\n * `false` (default): the items are not rendered while the menu is closed; mounting them is part\n * of the opening transition, so items that suspend hold the open (and the Trigger pending)\n * until they are ready. `true`: they stay mounted inside `<Activity mode=\"hidden\">`.\n *\n * @defaultValue `false`\n */\n keepMounted?: boolean;\n /**\n * Experimental (Chromium 150+). Renders `focusgroup=\"menu wrap|nowrap\"` and, where the engine\n * implements `focusGroup`, lets the browser handle ArrowUp / ArrowDown / Home / End: items then\n * render no `tabindex` and the library's arrow-key handling steps aside. Typeahead, activation,\n * submenu keys and Tab stay in JS. No effect in other engines.\n *\n * @defaultValue `false`\n */\n experimental_focusgroup?: boolean;\n /**\n * Forwarded to the `<ViewTransition default>` around the popup element. Animates the opening\n * commit and every library-driven close (an item's activation, the Trigger, ArrowLeft, hover\n * leaving a submenu, controlled `open={false}`) in all engines; dismissals (Escape, a click\n * outside, Tab) animate with CSS only. Without it the part renders no `<ViewTransition>`.\n * While a view transition runs, the popup's CSS transitions are turned off for the moment of the\n * library's own show or hide (an inline `transition: none !important`, restored right after),\n * so that show or hide runs no CSS transition (a `@keyframes` animation is not cut).\n */\n transition?: ViewTransitionClass;\n /** Class name, or a function of the part's state that returns one. */\n className?: StateValue<string | undefined, MenuPopupState>;\n /** Inline style, or a function of the part's state that returns one. */\n style?: StateValue<CSSProperties | undefined, MenuPopupState>;\n /** Content, or a function of the part's state. */\n children?: StateValue<ReactNode, MenuPopupState>;\n /** Should render a `<div>`; a `<dialog>` would run the dialog focusing steps. */\n render?: RenderProp<ComponentPropsWithRef<\"div\">, MenuPopupState>;\n}\n\n/**\n * What a press may focus inside a popup: items, the popup itself, the consumer's controls.\n * Differs from the roving and Tooltip selectors on purpose: it asks \"does a press focus\n * something\", so any `input` counts (a hidden one is never pressed).\n */\nconst FOCUSABLE_SELECTOR =\n 'a[href], button, input, select, textarea, summary, [tabindex], [contenteditable]:not([contenteditable=\"false\"])';\n\n/**\n * The menu itself: `<div popover=\"auto\" role=\"menu\" aria-labelledby={trigger} tabindex=\"-1\">`,\n * placed against its Trigger with CSS Anchor Positioning. Owns the keyboard model (design 5):\n * ArrowUp / ArrowDown / Home / End move focus between items (or the browser does, with\n * `experimental_focusgroup` where supported), printable characters run typeahead, ArrowLeft\n * closes a submenu. The top-level popup closes the whole menu at once when focus leaves it (Tab);\n * a press on a button of the consumer's that names it (`popovertarget`, `commandfor`) moves no\n * focus, so that button's click closes the menu as `\"activate\"`.\n * Style it with `:popover-open`; it emits `data-side` / `data-align` but no `data-open`. While\n * shown, those are where the browser placed it (conventions 13.2), otherwise the requested\n * values.\n *\n * Owned keys (conventions 1.2): `id`, `popover`, `role`, `aria-labelledby` (token-merged),\n * `tabIndex`, `focusgroup` (with the flag), `onKeyDown`, `onBlur`, `onMouseDown` (a press on\n * non-item space keeps focus on the current item), `onContextMenu` (no native menu over this\n * one), `onPointerEnter` / `onPointerLeave`, `data-side` / `data-align` unless `side` is `null`\n * or the menu is placed at a point, and `data-context` for an open from a `Menu.ContextTrigger`.\n * Such an open is placed at the pointer: `--menu-point-x` / `--menu-point-y` inline and, unless\n * `side` is `null`, `top` and `left` (`right` for an RTL popup) at the point with the library's\n * `@position-try` fallbacks, whose rules it renders as a hoisted `<style>`. A top-level menu\n * closes at once on a right- or middle-click outside it.\n *\n * @see https://ui.funstack.work/components/menu\n */\nexport function MenuPopup({\n side: sideProp,\n align = \"start\",\n keepMounted,\n experimental_focusgroup = false,\n transition,\n className,\n style,\n children,\n render,\n ...rest\n}: MenuPopupProps) {\n const menu = useMenuContext(\"Menu.Popup\");\n const { popover, submenu, loop } = menu;\n const csp = use(CSPContext);\n // The listeners installed at the open read the placement of the latest commit (4.8).\n const latestPlacement = useLatest(menu.placement);\n // A submenu opens on the inline-end side of its item: the side ArrowRight (ArrowLeft in RTL)\n // points to.\n const sideOption = sideProp === undefined ? (submenu ? \"inline-end\" : \"bottom\") : sideProp;\n // A ContextTrigger's open is placed at the point; nothing is anchored (context-menu.md 3.3).\n const point = menu.placement.kind === \"point\" ? menu.placement : null;\n const requestedSide = point !== null ? null : sideOption;\n // Resolved while shown (conventions 13.2); idle for a point open, which has no side.\n const placement = useAnchoredPlacement({\n side: requestedSide,\n align,\n fallbacks: submenu ? \"flip-inline\" : undefined,\n });\n const { side, align: resolvedAlign, ref: placementRef } = placement;\n\n const nativeFocusgroup = useNativeFocusgroup(experimental_focusgroup);\n const { onKeyDown, typingAhead } = useMenuKeyboard(menu, nativeFocusgroup);\n\n const { setElement, visible, renderContent, reshow } = usePopoverPopup(popover, {\n part: \"Menu.Popup\",\n // Design 6.1: the first (or last) enabled item by open cause, nothing for a hover open, the\n // popup itself when there are no items.\n // A pointer open focuses without the focus ring where the engine can be told so.\n initialFocus: (popup) => {\n const { cause, pointer } = menu.takeOpenCause();\n if (cause === \"none\") return null;\n const element = edgeItem(popup, cause) ?? popup;\n return pointer ? { element, focusVisible: false } : element;\n },\n keepMounted,\n transition,\n // A point open nests through the region that opened it (context-menu.md 3.1).\n source: () => (point !== null ? point.source : popover.triggerRef.current),\n // The outside right- / middle-click listeners and the foreign invoker press guard of a\n // top-level menu.\n whileShown: submenu\n ? undefined\n : (element) => {\n const stopListening = listenOutside(element, menu, () => latestPlacement.current);\n const stopPresses = listenInvokerPresses(element);\n return () => {\n stopListening();\n stopPresses();\n };\n },\n });\n\n // A move (context-menu.md 4.3): the popup is shown and the placement changed since it was\n // shown (a new point, or a right-click in the region while a Trigger open is shown). Re-show\n // it so the respelled fallbacks place it afresh, the source is the region and the first item\n // is focused again; then capture the still-pressed pointer again, against the Firefox / WebKit\n // `pointerup` light dismiss. Runs after the hook's apply effect (declared after it).\n // Once the popup is not visible (hidden, exit animation over) and no open is intended or in\n // flight, a point placement goes back to anchored for the next open that is not a\n // ContextTrigger's: after a point open that was shown and closed, and after one that was\n // rejected or refused.\n const shownKey = useRef<number | \"anchor\" | null>(null);\n useLayoutEffect(() => {\n const element = popover.popupRef.current;\n if (element === null) return;\n const key = point !== null ? point.generation : \"anchor\";\n if (isPopoverOpen(element)) {\n if (shownKey.current !== null && shownKey.current !== key && reshow()) {\n capturePointer(element, point?.pointerId);\n }\n shownKey.current = key;\n } else {\n shownKey.current = null;\n if (!visible && point !== null && !popover.pending && !popover.intended()) {\n menu.resetPlacement(point.generation);\n }\n }\n });\n\n const checkElement = useElementCheck(\n isDev &&\n ((element) =>\n element.tagName === \"DIALOG\"\n ? \"Menu.Popup: rendered a <dialog>. Render a <div>: a <dialog popover> runs the dialog \" +\n \"focusing steps, which fight the menu's item focus.\"\n : null),\n );\n const pressRef = useCallback(\n (element: HTMLElement) => {\n checkElement?.(element);\n // A press inside a menu is never a long-press of a Menu.ContextTrigger around it.\n // Native, so it runs before React's root listener dispatches to the region.\n const onPointerDown = (event: Event) => claimPress(event);\n element.addEventListener(\"pointerdown\", onPointerDown);\n return () => element.removeEventListener(\"pointerdown\", onPointerDown);\n },\n [checkElement],\n );\n const ref = useMergedRefs(setElement, pressRef, placementRef);\n\n const listContext = useMemo<MenuListContextValue>(\n () => ({ menu, nativeFocusgroup, typingAhead }),\n [menu, nativeFocusgroup, typingAhead],\n );\n\n const state: MenuPopupState = {\n open: popover.committed,\n side,\n align: resolvedAlign,\n submenu,\n context: point !== null,\n };\n\n // Tab / Shift+Tab or any focus move out of the tree closes the whole stack at once (design\n // 2.2): submenus are DOM descendants of the top-level popup, the Trigger is outside on purpose.\n // A null `relatedTarget` (window blur, a removed element) closes nothing.\n const onBlur = (event: FocusEvent<HTMLElement>) => {\n if (submenu) return;\n const popup = event.currentTarget;\n const next = event.relatedTarget;\n if (!(next instanceof Node) || popup.contains(next)) return;\n if (isPopoverOpen(popup)) popup.hidePopover();\n };\n\n // A press on non-item space (padding, a Separator, a GroupLabel) would focus the popup itself\n // (`tabindex=\"-1\"`) and take focus off the current item; keep it where it is (the\n // pointer-focus model of design 6.2). Focusable content of the consumer's (a text field) still\n // takes focus.\n const onMouseDown = (event: MouseEvent<HTMLElement>) => {\n if (event.button !== 0) return;\n const target = event.target as Element;\n const focusable = target.closest(FOCUSABLE_SELECTOR);\n if (focusable === null || focusable.getAttribute(\"role\") === \"menu\") event.preventDefault();\n };\n\n const libraryProps: ComponentPropsWithRef<\"div\"> & DataAttributes = {\n id: popover.ids.popup,\n popover: \"auto\",\n role: \"menu\",\n \"aria-labelledby\": menu.triggerId,\n // The popup itself takes focus as the last resort when a menu has no items.\n tabIndex: -1,\n ...(experimental_focusgroup && { focusgroup: `menu ${loop ? \"wrap\" : \"nowrap\"}` }),\n onKeyDown,\n onBlur,\n onMouseDown,\n // No native menu over the custom one. The popup is a sibling of any ContextTrigger, so this\n // event never reaches a region as a move; an outer region sees it prevented and stays quiet.\n onContextMenu: (event: MouseEvent<HTMLElement>) => {\n event.preventDefault();\n },\n ...(submenu && {\n onPointerEnter: (event: PointerEvent<HTMLElement>) => {\n if (event.pointerType !== \"touch\") menu.timers.cancelClose();\n },\n onPointerLeave: (event: PointerEvent<HTMLElement>) => {\n if (event.pointerType !== \"touch\") scheduleHoverClose(menu, event.nativeEvent);\n },\n }),\n ...placement.dataAttributes,\n \"data-context\": point !== null ? \"\" : undefined,\n };\n\n // The shipped `@position-try` rules (conventions 12): hoisted into <head> and deduplicated by\n // React, rendered only by a point open that does not opt out with `side={null}`, with the\n // `<CSPProvider>` nonce for a nonce-based `style-src`.\n const pointRules =\n point !== null && sideOption !== null ? (\n <style href={MENU_POINT_HREF} precedence={FSUI_PRECEDENCE} nonce={csp?.nonce}>\n {MENU_POINT_RULES}\n </style>\n ) : null;\n\n const content = (s: MenuPopupState): ReactNode => (\n <MenuListContext value={listContext}>\n {pointRules}\n {renderContent(() => resolveStateValue(children, s))}\n </MenuListContext>\n );\n\n const element = useRender<ComponentPropsWithRef<\"div\">, MenuPopupState>({\n defaultElement: \"div\",\n render,\n state,\n className,\n // The inline placement sits under the consumer's style: the consumer wins.\n style:\n point !== null\n ? (s) => ({ ...pointStyle(point, sideOption === null), ...resolveStateValue(style, s) })\n : placement.style(style),\n children: content,\n props: rest,\n libraryProps,\n ref,\n });\n\n return useTransitionBoundary(\"Menu.Popup\", transition, element);\n}\n\nMenuPopup.displayName = \"MenuPopup\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAmCA,MAAM,QAAQ,QAAQ,IAAI,aAAa;;;;;;AAwHvC,MAAM,qBACJ;;;;;;;;;;;;;;;;;;;;;;;;;AA0BF,SAAgB,UAAU,EACxB,MAAM,UACN,QAAQ,SACR,aACA,0BAA0B,OAC1B,YACA,WACA,OACA,UACA,QACA,GAAG,QACc;CACjB,MAAM,OAAO,eAAe,YAAY;CACxC,MAAM,EAAE,SAAS,SAAS,SAAS;CACnC,MAAM,MAAM,IAAI,UAAU;CAE1B,MAAM,kBAAkB,UAAU,KAAK,SAAS;CAGhD,MAAM,aAAa,aAAa,KAAA,IAAa,UAAU,eAAe,WAAY;CAElF,MAAM,QAAQ,KAAK,UAAU,SAAS,UAAU,KAAK,YAAY;CAGjE,MAAM,YAAY,qBAAqB;EACrC,MAHoB,UAAU,OAAO,OAAO;EAI5C;EACA,WAAW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O,MAAM;EAAiB,YAAY;EAAiB,OAAO,KAAK;EACpE,UAAA;CACI,CAAA,IACL;CAEN,MAAM,WAAW,MACf,qBAAC,iBAAD;EAAiB,OAAO;EAAxB,UAAA,CACG,YACA,oBAAoB,kBAAkB,UAAU,CAAC,CAAC,CACpC;;CAGnB,MAAM,UAAU,UAAwD;EACtE,gBAAgB;EAChB;EACA;EACA;EAEA,OACE,UAAU,QACL,OAAO;GAAE,GAAG,WAAW,OAAO,eAAe,IAAI;GAAG,GAAG,kBAAkB,OAAO,CAAC;EAAE,KACpF,UAAU,MAAM,KAAK;EAC3B,UAAU;EACV,OAAO;EACP;EACA;CACF,CAAC;CAED,OAAO,sBAAsB,cAAc,YAAY,OAAO;AAChE;AAEA,UAAU,cAAc"}
|
|
1
|
+
{"version":3,"file":"menu-popup.mjs","names":[],"sources":["../../src/menu/menu-popup.tsx"],"sourcesContent":["\"use client\";\nimport { use, useCallback, useLayoutEffect, useMemo, useRef } from \"react\";\nimport type {\n ComponentPropsWithRef,\n CSSProperties,\n FocusEvent,\n KeyboardEvent,\n MouseEvent,\n PointerEvent,\n ReactNode,\n ViewTransitionClass,\n} from \"react\";\nimport type { AnchorAlign, AnchorSide } from \"../internal/overlay/anchor-position.js\";\nimport { CSPContext } from \"../internal/csp-context.js\";\nimport { MenubarContext, useMenubarContext } from \"../internal/menubar-context.js\";\nimport { listenInvokerPresses } from \"../internal/overlay/invokers.js\";\nimport { isPopoverOpen } from \"../internal/overlay/popover-open.js\";\nimport { capturePointer } from \"../internal/pointer-capture.js\";\nimport { FSUI_PRECEDENCE } from \"../internal/shipped-styles.js\";\nimport { useTransitionBoundary } from \"../internal/transition-boundary.js\";\nimport type { RenderProp, StateValue } from \"../internal/types.js\";\nimport { useElementCheck } from \"../internal/use-element-check.js\";\nimport { useLatest } from \"../internal/use-latest.js\";\nimport { useMergedRefs } from \"../internal/use-merged-refs.js\";\nimport { useNativeFocusgroup } from \"../internal/use-native-focusgroup.js\";\nimport { usePopoverPopup } from \"../internal/overlay/use-popover-popup.js\";\nimport { useAnchoredPlacement } from \"../internal/overlay/use-resolved-placement.js\";\nimport { resolveStateValue, useRender } from \"../internal/use-render.js\";\nimport type { DataAttributes } from \"../internal/use-render.js\";\nimport { claimPress, MenuListContext, useMenuContext } from \"./menu-context.js\";\nimport type { MenuListContextValue } from \"./menu-context.js\";\nimport { edgeItem, hasModifier, ITEM_SELECTOR } from \"./menu-dom.js\";\nimport { scheduleHoverClose } from \"./menu-hover.js\";\nimport { listenOutside } from \"./menu-outside.js\";\nimport { MENU_POINT_HREF, MENU_POINT_RULES, pointStyle } from \"./menu-point.js\";\nimport { useMenuKeyboard } from \"./use-menu-keyboard.js\";\n\nconst isDev = process.env.NODE_ENV !== \"production\";\n\n/** `inline-start` / `inline-end` follow the popup's `direction` (conventions 13.1). */\nexport type MenuSide = AnchorSide;\n/**\n * Alignment of the Popup along its side: inline-relative on a top / bottom side, the top / bottom\n * edges on the others.\n */\nexport type MenuAlign = AnchorAlign;\n\n/**\n * State of `Menu.Popup`, passed to the function forms of `className`, `style`, `children` and\n * `render`.\n */\nexport interface MenuPopupState {\n /** Committed open state. Read `:popover-open` for the DOM truth. */\n readonly open: boolean;\n /**\n * While shown: the physical side the browser placed the popup on (after any fallback flip;\n * conventions 13.2). Otherwise the `side` prop. `null` with `side={null}`, and while the menu\n * is placed at a point (`context`).\n */\n readonly side: MenuSide | null;\n /** While shown: the resolved alignment; otherwise the `align` prop. */\n readonly align: MenuAlign;\n /** This popup belongs to a submenu. */\n readonly submenu: boolean;\n /**\n * The current open came from a `Menu.ContextTrigger`: the popup is placed at the pointer, with\n * `--menu-point-x` / `--menu-point-y` set inline (`data-context`).\n */\n readonly context: boolean;\n}\n\n/**\n * Props of `Menu.Popup`.\n *\n * @see https://ui.funstack.work/components/menu\n */\nexport interface MenuPopupProps extends Omit<\n ComponentPropsWithRef<\"div\">,\n | \"id\"\n | \"popover\"\n | \"role\"\n | \"aria-labelledby\"\n | \"onKeyDown\"\n | \"onFocus\"\n | \"onBlur\"\n | \"onMouseDown\"\n | \"onContextMenu\"\n | \"onPointerEnter\"\n | \"onPointerLeave\"\n | \"onToggle\"\n | \"onBeforeToggle\"\n | \"className\"\n | \"style\"\n | \"children\"\n> {\n /**\n * Placement against the Trigger through inline `position-area`, with `position-try-fallbacks`\n * flipping it when it does not fit. A submenu's `\"inline-end\"` is the right of the item in LTR,\n * its left in RTL; its fallback is `flip-inline` only. `\"left\"` / `\"right\"` stay physical.\n * `null` emits no inline positioning (and no `data-side` / `data-align`): the stylesheet owns\n * placement. Ignored for an open from a\n * `Menu.ContextTrigger`, which is placed at the pointer; there `null` also opts out of the\n * inline point placement and the library's `@position-try` rules, leaving only\n * `--menu-point-x` / `--menu-point-y`.\n *\n * @defaultValue `\"bottom\"` for a top-level menu, `\"inline-end\"` for a submenu\n */\n side?: MenuSide | null;\n /**\n * Alignment along the side: inline-relative on a top / bottom side (mirrored under\n * `direction: rtl`), the top edges on the others. Ignored when `side` is `null`.\n *\n * @defaultValue `\"start\"` (both levels)\n */\n align?: MenuAlign;\n /**\n * `false` (default): the items are not rendered while the menu is closed; mounting them is part\n * of the opening transition, so items that suspend hold the open (and the Trigger pending)\n * until they are ready. `true`: they stay mounted inside `<Activity mode=\"hidden\">`.\n *\n * @defaultValue `false`\n */\n keepMounted?: boolean;\n /**\n * Experimental (Chromium 150+). Renders `focusgroup=\"menu wrap|nowrap\"` and, where the engine\n * implements `focusGroup`, lets the browser handle ArrowUp / ArrowDown / Home / End: items then\n * render no `tabindex` and the library's arrow-key handling steps aside. Typeahead, activation,\n * submenu keys and Tab stay in JS. No effect in other engines.\n *\n * @defaultValue `false`\n */\n experimental_focusgroup?: boolean;\n /**\n * Forwarded to the `<ViewTransition default>` around the popup element. Animates the opening\n * commit and every library-driven close (an item's activation, the Trigger, ArrowLeft, hover\n * leaving a submenu, controlled `open={false}`) in all engines; dismissals (Escape, a click\n * outside, Tab) animate with CSS only. Without it the part renders no `<ViewTransition>`.\n * While a view transition runs, the popup's CSS transitions are turned off for the moment of the\n * library's own show or hide (an inline `transition: none !important`, restored right after),\n * so that show or hide runs no CSS transition (a `@keyframes` animation is not cut).\n */\n transition?: ViewTransitionClass;\n /** Class name, or a function of the part's state that returns one. */\n className?: StateValue<string | undefined, MenuPopupState>;\n /** Inline style, or a function of the part's state that returns one. */\n style?: StateValue<CSSProperties | undefined, MenuPopupState>;\n /** Content, or a function of the part's state. */\n children?: StateValue<ReactNode, MenuPopupState>;\n /** Should render a `<div>`; a `<dialog>` would run the dialog focusing steps. */\n render?: RenderProp<ComponentPropsWithRef<\"div\">, MenuPopupState>;\n}\n\n/**\n * What a press may focus inside a popup: items, the popup itself, the consumer's controls.\n * Differs from the roving and Tooltip selectors on purpose: it asks \"does a press focus\n * something\", so any `input` counts (a hidden one is never pressed).\n */\nconst FOCUSABLE_SELECTOR =\n 'a[href], button, input, select, textarea, summary, [tabindex], [contenteditable]:not([contenteditable=\"false\"])';\n\n/**\n * The menu itself: `<div popover=\"auto\" role=\"menu\" aria-labelledby={trigger} tabindex=\"-1\">`,\n * placed against its Trigger with CSS Anchor Positioning. Owns the keyboard model (design 5):\n * ArrowUp / ArrowDown / Home / End move focus between items (or the browser does, with\n * `experimental_focusgroup` where supported), printable characters run typeahead, ArrowLeft\n * closes a submenu. The top-level popup closes the whole menu at once when focus leaves it (Tab);\n * a press on a button of the consumer's that names it (`popovertarget`, `commandfor`) moves no\n * focus, so that button's click closes the menu as `\"activate\"`.\n * Style it with `:popover-open`; it emits `data-side` / `data-align` but no `data-open`. While\n * shown, those are where the browser placed it (conventions 13.2), otherwise the requested\n * values.\n *\n * Owned keys (conventions 1.2): `id`, `popover`, `role`, `aria-labelledby` (token-merged),\n * `tabIndex`, `focusgroup` (with the flag), `onKeyDown`, `onBlur`, `onMouseDown` (a press on\n * non-item space keeps focus on the current item), `onContextMenu` (no native menu over this\n * one), `onPointerEnter` / `onPointerLeave`, `data-side` / `data-align` unless `side` is `null`\n * or the menu is placed at a point, and `data-context` for an open from a `Menu.ContextTrigger`.\n * Such an open is placed at the pointer: `--menu-point-x` / `--menu-point-y` inline and, unless\n * `side` is `null`, `top` and `left` (`right` for an RTL popup) at the point with the library's\n * `@position-try` fallbacks, whose rules it renders as a hoisted `<style>`. A top-level menu\n * closes at once on a right- or middle-click outside it.\n *\n * @see https://ui.funstack.work/components/menu\n */\nexport function MenuPopup({\n side: sideProp,\n align = \"start\",\n keepMounted,\n experimental_focusgroup = false,\n transition,\n className,\n style,\n children,\n render,\n ...rest\n}: MenuPopupProps) {\n const menu = useMenuContext(\"Menu.Popup\");\n const { popover, submenu, loop } = menu;\n const csp = use(CSPContext);\n // The listeners installed at the open read the placement of the latest commit (4.8).\n const latestPlacement = useLatest(menu.placement);\n // A submenu opens on the inline-end side of its item: the side ArrowRight (ArrowLeft in RTL)\n // points to.\n const sideOption = sideProp === undefined ? (submenu ? \"inline-end\" : \"bottom\") : sideProp;\n // A ContextTrigger's open is placed at the point; nothing is anchored (context-menu.md 3.3).\n const point = menu.placement.kind === \"point\" ? menu.placement : null;\n const requestedSide = point !== null ? null : sideOption;\n // Resolved while shown (conventions 13.2); idle for a point open, which has no side.\n const placement = useAnchoredPlacement({\n side: requestedSide,\n align,\n fallbacks: submenu ? \"flip-inline\" : undefined,\n });\n const { side, align: resolvedAlign, ref: placementRef } = placement;\n\n const nativeFocusgroup = useNativeFocusgroup(experimental_focusgroup);\n const { onKeyDown: onMenuKeyDown, typingAhead } = useMenuKeyboard(menu, nativeFocusgroup);\n // A menu of a Menubar (design/components/menubar.md 2.3). `null` in a submenu: this Popup ends\n // the bar's context for its content.\n const bar = useMenubarContext();\n const onKeyDown = (event: KeyboardEvent<HTMLElement>) => {\n onMenuKeyDown(event);\n if (bar === null || event.defaultPrevented || hasModifier(event)) return;\n const popup = event.currentTarget;\n const target = event.target as Element;\n // An item at any depth: submenus are DOM descendants of this popup. A SubmenuTrigger keeps\n // its forward arrow (it opens the submenu; a disabled one does nothing).\n if (!target.matches(ITEM_SELECTOR) || !popup.contains(target)) return;\n const rtl = bar.rtl();\n let delta: 1 | -1;\n if (event.key === (rtl ? \"ArrowLeft\" : \"ArrowRight\")) {\n if (target.hasAttribute(\"aria-haspopup\")) return;\n delta = 1;\n } else if (event.key === (rtl ? \"ArrowRight\" : \"ArrowLeft\")) {\n // Only from the top level: in a submenu the backward arrow closes the submenu.\n if (target.closest('[role=\"menu\"]') !== popup) return;\n delta = -1;\n } else {\n return;\n }\n const trigger = popover.triggerRef.current;\n if (trigger === null) return;\n if (bar.step(trigger, delta, event.nativeEvent)) event.preventDefault();\n };\n\n const { setElement, visible, renderContent, reshow } = usePopoverPopup(popover, {\n part: \"Menu.Popup\",\n // Design 6.1: the first (or last) enabled item by open cause, nothing for a hover open, the\n // popup itself when there are no items.\n // A pointer open focuses without the focus ring where the engine can be told so.\n initialFocus: (popup) => {\n const { cause, pointer } = menu.takeOpenCause();\n if (cause === \"none\") return null;\n const element = edgeItem(popup, cause) ?? popup;\n return pointer ? { element, focusVisible: false } : element;\n },\n keepMounted,\n transition,\n // A point open nests through the region that opened it (context-menu.md 3.1).\n source: () => (point !== null ? point.source : popover.triggerRef.current),\n // The outside right- / middle-click listeners and the foreign invoker press guard of a\n // top-level menu.\n whileShown: submenu\n ? undefined\n : (element) => {\n const stopListening = listenOutside(element, menu, () => latestPlacement.current);\n const stopPresses = listenInvokerPresses(element);\n return () => {\n stopListening();\n stopPresses();\n };\n },\n });\n\n // A move (context-menu.md 4.3): the popup is shown and the placement changed since it was\n // shown (a new point, or a right-click in the region while a Trigger open is shown). Re-show\n // it so the respelled fallbacks place it afresh, the source is the region and the first item\n // is focused again; then capture the still-pressed pointer again, against the Firefox / WebKit\n // `pointerup` light dismiss. Runs after the hook's apply effect (declared after it).\n // Once the popup is not visible (hidden, exit animation over) and no open is intended or in\n // flight, a point placement goes back to anchored for the next open that is not a\n // ContextTrigger's: after a point open that was shown and closed, and after one that was\n // rejected or refused.\n const shownKey = useRef<number | \"anchor\" | null>(null);\n useLayoutEffect(() => {\n const element = popover.popupRef.current;\n if (element === null) return;\n const key = point !== null ? point.generation : \"anchor\";\n if (isPopoverOpen(element)) {\n if (shownKey.current !== null && shownKey.current !== key && reshow()) {\n capturePointer(element, point?.pointerId);\n }\n shownKey.current = key;\n } else {\n shownKey.current = null;\n if (!visible && point !== null && !popover.pending && !popover.intended()) {\n menu.resetPlacement(point.generation);\n }\n }\n });\n\n const checkElement = useElementCheck(\n isDev &&\n ((element) =>\n element.tagName === \"DIALOG\"\n ? \"Menu.Popup: rendered a <dialog>. Render a <div>: a <dialog popover> runs the dialog \" +\n \"focusing steps, which fight the menu's item focus.\"\n : null),\n );\n const pressRef = useCallback(\n (element: HTMLElement) => {\n checkElement?.(element);\n // A press inside a menu is never a long-press of a Menu.ContextTrigger around it.\n // Native, so it runs before React's root listener dispatches to the region.\n const onPointerDown = (event: Event) => claimPress(event);\n element.addEventListener(\"pointerdown\", onPointerDown);\n return () => element.removeEventListener(\"pointerdown\", onPointerDown);\n },\n [checkElement],\n );\n const ref = useMergedRefs(setElement, pressRef, placementRef);\n\n const listContext = useMemo<MenuListContextValue>(\n () => ({ menu, nativeFocusgroup, typingAhead }),\n [menu, nativeFocusgroup, typingAhead],\n );\n\n const state: MenuPopupState = {\n open: popover.committed,\n side,\n align: resolvedAlign,\n submenu,\n context: point !== null,\n };\n\n // Tab / Shift+Tab or any focus move out of the tree closes the whole stack at once (design\n // 2.2): submenus are DOM descendants of the top-level popup, the Trigger is outside on purpose.\n // A null `relatedTarget` (window blur, a removed element) closes nothing.\n const onBlur = (event: FocusEvent<HTMLElement>) => {\n if (submenu) return;\n const popup = event.currentTarget;\n const next = event.relatedTarget;\n if (!(next instanceof Node) || popup.contains(next)) return;\n if (isPopoverOpen(popup)) popup.hidePopover();\n };\n\n // A press on non-item space (padding, a Separator, a GroupLabel) would focus the popup itself\n // (`tabindex=\"-1\"`) and take focus off the current item; keep it where it is (the\n // pointer-focus model of design 6.2). Focusable content of the consumer's (a text field) still\n // takes focus.\n const onMouseDown = (event: MouseEvent<HTMLElement>) => {\n if (event.button !== 0) return;\n const target = event.target as Element;\n const focusable = target.closest(FOCUSABLE_SELECTOR);\n if (focusable === null || focusable.getAttribute(\"role\") === \"menu\") event.preventDefault();\n };\n\n const libraryProps: ComponentPropsWithRef<\"div\"> & DataAttributes = {\n id: popover.ids.popup,\n popover: \"auto\",\n role: \"menu\",\n \"aria-labelledby\": menu.triggerId,\n // The popup itself takes focus as the last resort when a menu has no items.\n tabIndex: -1,\n ...(experimental_focusgroup && { focusgroup: `menu ${loop ? \"wrap\" : \"nowrap\"}` }),\n onKeyDown,\n onBlur,\n onMouseDown,\n // No native menu over the custom one. The popup is a sibling of any ContextTrigger, so this\n // event never reaches a region as a move; an outer region sees it prevented and stays quiet.\n onContextMenu: (event: MouseEvent<HTMLElement>) => {\n event.preventDefault();\n },\n ...(submenu && {\n onPointerEnter: (event: PointerEvent<HTMLElement>) => {\n if (event.pointerType !== \"touch\") menu.timers.cancelClose();\n },\n onPointerLeave: (event: PointerEvent<HTMLElement>) => {\n if (event.pointerType !== \"touch\") scheduleHoverClose(menu, event.nativeEvent);\n },\n }),\n ...placement.dataAttributes,\n \"data-context\": point !== null ? \"\" : undefined,\n };\n\n // The shipped `@position-try` rules (conventions 12): hoisted into <head> and deduplicated by\n // React, rendered only by a point open that does not opt out with `side={null}`, with the\n // `<CSPProvider>` nonce for a nonce-based `style-src`.\n const pointRules =\n point !== null && sideOption !== null ? (\n <style href={MENU_POINT_HREF} precedence={FSUI_PRECEDENCE} nonce={csp?.nonce}>\n {MENU_POINT_RULES}\n </style>\n ) : null;\n\n const content = (s: MenuPopupState): ReactNode => {\n const list = (\n <MenuListContext value={listContext}>\n {pointRules}\n {renderContent(() => resolveStateValue(children, s))}\n </MenuListContext>\n );\n // The bar's context ends here: a Root inside is a submenu, not a menu of the bar.\n return bar === null ? list : <MenubarContext value={null}>{list}</MenubarContext>;\n };\n\n const element = useRender<ComponentPropsWithRef<\"div\">, MenuPopupState>({\n defaultElement: \"div\",\n render,\n state,\n className,\n // The inline placement sits under the consumer's style: the consumer wins.\n style:\n point !== null\n ? (s) => ({ ...pointStyle(point, sideOption === null), ...resolveStateValue(style, s) })\n : placement.style(style),\n children: content,\n props: rest,\n libraryProps,\n ref,\n });\n\n return useTransitionBoundary(\"Menu.Popup\", transition, element);\n}\n\nMenuPopup.displayName = \"MenuPopup\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAqCA,MAAM,QAAQ,QAAQ,IAAI,aAAa;;;;;;AAwHvC,MAAM,qBACJ;;;;;;;;;;;;;;;;;;;;;;;;;AA0BF,SAAgB,UAAU,EACxB,MAAM,UACN,QAAQ,SACR,aACA,0BAA0B,OAC1B,YACA,WACA,OACA,UACA,QACA,GAAG,QACc;CACjB,MAAM,OAAO,eAAe,YAAY;CACxC,MAAM,EAAE,SAAS,SAAS,SAAS;CACnC,MAAM,MAAM,IAAI,UAAU;CAE1B,MAAM,kBAAkB,UAAU,KAAK,SAAS;CAGhD,MAAM,aAAa,aAAa,KAAA,IAAa,UAAU,eAAe,WAAY;CAElF,MAAM,QAAQ,KAAK,UAAU,SAAS,UAAU,KAAK,YAAY;CAGjE,MAAM,YAAY,qBAAqB;EACrC,MAHoB,UAAU,OAAO,OAAO;EAI5C;EACA,WAAW,UAAU,gBAAgB,KAAA;CACvC,CAAC;CACD,MAAM,EAAE,MAAM,OAAO,eAAe,KAAK,iBAAiB;CAE1D,MAAM,mBAAmB,oBAAoB,uBAAuB;CACpE,MAAM,EAAE,WAAW,eAAe,gBAAgB,gBAAgB,MAAM,gBAAgB;CAGxF,MAAM,MAAM,kBAAkB;CAC9B,MAAM,aAAa,UAAsC;EACvD,cAAc,KAAK;EACnB,IAAI,QAAQ,QAAQ,MAAM,oBAAoB,YAAY,KAAK,GAAG;EAClE,MAAM,QAAQ,MAAM;EACpB,MAAM,SAAS,MAAM;EAGrB,IAAI,CAAC,OAAO,QAAA,4EAAqB,KAAK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,OAAO,YAAY;GAC7D;GACA,iBAAiB,UAAqC;IACpD,IAAI,MAAM,gBAAgB,SAAS,mBAAmB,MAAM,MAAM,WAAW;GAC/E;EACF;EACA,GAAG,UAAU;EACb,gBAAgB,UAAU,OAAO,KAAK,KAAA;CACxC;CAKA,MAAM,aACJ,UAAU,QAAQ,eAAe,OAC/B,oBAAC,SAAD;EAAO,MAAM;EAAiB,YAAY;EAAiB,OAAO,KAAK;EACpE,UAAA;CACI,CAAA,IACL;CAEN,MAAM,WAAW,MAAiC;EAChD,MAAM,OACJ,qBAAC,iBAAD;GAAiB,OAAO;GAAxB,UAAA,CACG,YACA,oBAAoB,kBAAkB,UAAU,CAAC,CAAC,CACpC;;EAGnB,OAAO,QAAQ,OAAO,OAAO,oBAAC,gBAAD;GAAgB,OAAO;GAAO,UAAA;EAAqB,CAAA;CAClF;CAEA,MAAM,UAAU,UAAwD;EACtE,gBAAgB;EAChB;EACA;EACA;EAEA,OACE,UAAU,QACL,OAAO;GAAE,GAAG,WAAW,OAAO,eAAe,IAAI;GAAG,GAAG,kBAAkB,OAAO,CAAC;EAAE,KACpF,UAAU,MAAM,KAAK;EAC3B,UAAU;EACV,OAAO;EACP;EACA;CACF,CAAC;CAED,OAAO,sBAAsB,cAAc,YAAY,OAAO;AAChE;AAEA,UAAU,cAAc"}
|
|
@@ -42,10 +42,17 @@ export interface MenuTriggerProps extends Omit<ComponentPropsWithRef<"button">,
|
|
|
42
42
|
* first / last item focused (inside a vertical `Toolbar.Root`: the inline-end arrow, with the
|
|
43
43
|
* first item focused).
|
|
44
44
|
*
|
|
45
|
+
* Inside a `Menubar` it is an item of the bar (design/components/menubar.md): `role="menuitem"`,
|
|
46
|
+
* no rendered `tabindex` (the bar writes it), and `disabled` as `aria-disabled="true"` (it stays
|
|
47
|
+
* focusable and never opens). Moving the pointer onto it while another menu of the bar is open
|
|
48
|
+
* switches to its menu.
|
|
49
|
+
*
|
|
45
50
|
* Owned keys (conventions 1.2): `type` (on a `<button>` only), `id`, `disabled`, `aria-disabled`
|
|
46
|
-
* (link or component `render
|
|
47
|
-
* `onKeyDown`, `onMouseDown`, `data-open`, `data-pending`,
|
|
48
|
-
* `
|
|
51
|
+
* (link or component `render`, and in a Menubar), `popoverTarget`, `aria-haspopup`,
|
|
52
|
+
* `aria-expanded`, `onClick`, `onKeyDown`, `onMouseDown`, `data-open`, `data-pending`,
|
|
53
|
+
* `data-disabled`, and in a Menubar `role`; `commandfor`, `command` and `popoverTargetAction`
|
|
54
|
+
* (never written; omitted from the props type). In a Menubar the hover switch is an
|
|
55
|
+
* `onPointerMove` handler merged with the consumer's (theirs runs first).
|
|
49
56
|
*
|
|
50
57
|
* @see https://ui.funstack.work/components/menu
|
|
51
58
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"menu-trigger.d.mts","names":[],"sources":["../../src/menu/menu-trigger.tsx"],"mappings":";;;;;;;;;
|
|
1
|
+
{"version":3,"file":"menu-trigger.d.mts","names":[],"sources":["../../src/menu/menu-trigger.tsx"],"mappings":";;;;;;;;;YA+BY,mBAAmB;;;;;;iBAOd,yBAAyB,KACxC;;;;;;;EAoBA;;EAEA,YAAY,+BAA+B;;EAE3C,QAAQ,WAAW,2BAA2B;;EAE9C,WAAW,WAAW,WAAW;;;;;;EAMjC,SAAS,WAAW,iCAAiC;;;;;;;;;;;;;;;;;;;;;;;;wBAkEvC,cACd,UACA,WACA,OACA,UACA,WACG,QACF,mBAAgB;yBAPH"}
|
|
@@ -2,10 +2,14 @@
|
|
|
2
2
|
import { useToolbarContext } from "../internal/toolbar-context.mjs";
|
|
3
3
|
import { useDevWarning } from "../internal/use-dev-warning.mjs";
|
|
4
4
|
import { useRender } from "../internal/use-render.mjs";
|
|
5
|
+
import { useLatest } from "../internal/use-latest.mjs";
|
|
6
|
+
import { useMergedRefs } from "../internal/use-merged-refs.mjs";
|
|
5
7
|
import { isPopoverOpen } from "../internal/overlay/popover-open.mjs";
|
|
6
8
|
import { toggleFromTrigger, usePopoverTrigger } from "../internal/overlay/use-popover-trigger.mjs";
|
|
7
9
|
import { useMenuContext } from "./menu-context.mjs";
|
|
8
10
|
import { edgeItem, focusItem, hasModifier, isRtl } from "./menu-dom.mjs";
|
|
11
|
+
import { useMenubarContext } from "../internal/menubar-context.mjs";
|
|
12
|
+
import { useCallback } from "react";
|
|
9
13
|
//#region src/menu/menu-trigger.tsx
|
|
10
14
|
const isDev = process.env.NODE_ENV !== "production";
|
|
11
15
|
/**
|
|
@@ -30,6 +34,19 @@ function isPointerClick(event) {
|
|
|
30
34
|
return event.type === "click" && event.detail > 0;
|
|
31
35
|
}
|
|
32
36
|
/**
|
|
37
|
+
* A switch's open (design/components/menubar.md 2.2 step 3): the first item focused when the menu
|
|
38
|
+
* shows (or at once, when it is already shown), or focus left on the Trigger.
|
|
39
|
+
*/
|
|
40
|
+
function openFromBar(menu, event, focus) {
|
|
41
|
+
if (focus === "first") {
|
|
42
|
+
openMenuWithFocus(menu, event, "first");
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (menu.popover.intended()) return;
|
|
46
|
+
menu.setOpenCause("none");
|
|
47
|
+
menu.popover.requestOpen(event, "activate");
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
33
50
|
* The menu button: `<button type="button" popovertarget aria-haspopup="menu">`, with no
|
|
34
51
|
* `aria-expanded` (the browser exposes the menu's state on its invoker; a link or other non-button
|
|
35
52
|
* element, or a disabled component `render`, renders it from the shown menu). A click (Enter,
|
|
@@ -37,21 +54,30 @@ function isPointerClick(event) {
|
|
|
37
54
|
* first / last item focused (inside a vertical `Toolbar.Root`: the inline-end arrow, with the
|
|
38
55
|
* first item focused).
|
|
39
56
|
*
|
|
57
|
+
* Inside a `Menubar` it is an item of the bar (design/components/menubar.md): `role="menuitem"`,
|
|
58
|
+
* no rendered `tabindex` (the bar writes it), and `disabled` as `aria-disabled="true"` (it stays
|
|
59
|
+
* focusable and never opens). Moving the pointer onto it while another menu of the bar is open
|
|
60
|
+
* switches to its menu.
|
|
61
|
+
*
|
|
40
62
|
* Owned keys (conventions 1.2): `type` (on a `<button>` only), `id`, `disabled`, `aria-disabled`
|
|
41
|
-
* (link or component `render
|
|
42
|
-
* `onKeyDown`, `onMouseDown`, `data-open`, `data-pending`,
|
|
43
|
-
* `
|
|
63
|
+
* (link or component `render`, and in a Menubar), `popoverTarget`, `aria-haspopup`,
|
|
64
|
+
* `aria-expanded`, `onClick`, `onKeyDown`, `onMouseDown`, `data-open`, `data-pending`,
|
|
65
|
+
* `data-disabled`, and in a Menubar `role`; `commandfor`, `command` and `popoverTargetAction`
|
|
66
|
+
* (never written; omitted from the props type). In a Menubar the hover switch is an
|
|
67
|
+
* `onPointerMove` handler merged with the consumer's (theirs runs first).
|
|
44
68
|
*
|
|
45
69
|
* @see https://ui.funstack.work/components/menu
|
|
46
70
|
*/
|
|
47
71
|
function MenuTrigger({ disabled = false, className, style, children, render, ...rest }) {
|
|
48
72
|
const menu = useMenuContext("Menu.Trigger");
|
|
49
73
|
useDevWarning(menu.submenu, isDev && "Menu.Trigger: rendered in a submenu. Use Menu.SubmenuTrigger, which is also an item of the parent menu.");
|
|
74
|
+
const bar = useMenubarContext();
|
|
75
|
+
const inBar = bar !== null;
|
|
50
76
|
const toolbar = useToolbarContext();
|
|
51
77
|
const onKeyDown = (event) => {
|
|
52
78
|
if (disabled || hasModifier(event)) return;
|
|
53
79
|
let which;
|
|
54
|
-
if (toolbar?.orientation === "vertical") {
|
|
80
|
+
if (!inBar && toolbar?.orientation === "vertical") {
|
|
55
81
|
const openKey = isRtl(event.currentTarget) ? "ArrowLeft" : "ArrowRight";
|
|
56
82
|
if (event.key !== openKey) return;
|
|
57
83
|
which = "first";
|
|
@@ -62,18 +88,39 @@ function MenuTrigger({ disabled = false, className, style, children, render, ...
|
|
|
62
88
|
event.preventDefault();
|
|
63
89
|
openMenuWithFocus(menu, event.nativeEvent, which);
|
|
64
90
|
};
|
|
65
|
-
const
|
|
91
|
+
const latest = useLatest({
|
|
92
|
+
menu,
|
|
93
|
+
disabled
|
|
94
|
+
});
|
|
95
|
+
const barRef = useCallback((element) => bar?.register(element, {
|
|
96
|
+
disabled: () => latest.current.disabled,
|
|
97
|
+
intended: () => latest.current.menu.popover.intended(),
|
|
98
|
+
hiddenBeforeToggle: () => latest.current.menu.popover.hiddenBeforeToggle(),
|
|
99
|
+
popup: () => latest.current.menu.popover.popupRef.current,
|
|
100
|
+
open: (event, focus) => openFromBar(latest.current.menu, event, focus),
|
|
101
|
+
supersede: (event) => latest.current.menu.popover.requestClose(event, "dismiss")
|
|
102
|
+
}), [bar, latest]);
|
|
103
|
+
const onPointerMove = bar === null ? void 0 : (event) => {
|
|
104
|
+
if (event.pointerType === "touch" || disabled || menu.popover.intended()) return;
|
|
105
|
+
const element = event.currentTarget;
|
|
106
|
+
if (!bar.otherIntended(element)) return;
|
|
107
|
+
bar.switchTo(element, "none", event.nativeEvent, true);
|
|
108
|
+
};
|
|
109
|
+
const { state, libraryProps, ref: triggerRef } = usePopoverTrigger(menu.popover, {
|
|
66
110
|
part: "Menu.Trigger",
|
|
67
111
|
disabled,
|
|
68
112
|
render,
|
|
113
|
+
alwaysFocusable: inBar,
|
|
69
114
|
handlers: {
|
|
70
115
|
onKeyDown,
|
|
116
|
+
...onPointerMove !== void 0 && { onPointerMove },
|
|
71
117
|
onMouseDown: (event) => {
|
|
72
118
|
if (event.button === 0 && menu.popover.intended()) event.preventDefault();
|
|
73
119
|
}
|
|
74
120
|
},
|
|
75
121
|
onActivate: (event) => toggleFromTrigger(menu.popover, event.nativeEvent, () => menu.setOpenCause("first", isPointerClick(event.nativeEvent)))
|
|
76
122
|
});
|
|
123
|
+
const ref = useMergedRefs(triggerRef, barRef);
|
|
77
124
|
return useRender({
|
|
78
125
|
defaultElement: "button",
|
|
79
126
|
render,
|
|
@@ -84,6 +131,7 @@ function MenuTrigger({ disabled = false, className, style, children, render, ...
|
|
|
84
131
|
props: rest,
|
|
85
132
|
libraryProps: {
|
|
86
133
|
...libraryProps,
|
|
134
|
+
...inBar && { role: "menuitem" },
|
|
87
135
|
id: menu.triggerId,
|
|
88
136
|
"aria-haspopup": "menu"
|
|
89
137
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"menu-trigger.mjs","names":[],"sources":["../../src/menu/menu-trigger.tsx"],"sourcesContent":["\"use client\";\nimport type {\n ComponentPropsWithRef,\n CSSProperties,\n KeyboardEvent,\n MouseEvent as ReactMouseEvent,\n ReactNode,\n} from \"react\";\nimport { isPopoverOpen } from \"../internal/overlay/popover-open.js\";\nimport { useToolbarContext } from \"../internal/toolbar-context.js\";\nimport type { RenderProp, StateValue } from \"../internal/types.js\";\nimport { useDevWarning } from \"../internal/use-dev-warning.js\";\nimport { toggleFromTrigger, usePopoverTrigger } from \"../internal/overlay/use-popover-trigger.js\";\nimport type { PopoverTriggerHookState } from \"../internal/overlay/use-popover-trigger.js\";\nimport { useRender } from \"../internal/use-render.js\";\nimport { useMenuContext } from \"./menu-context.js\";\nimport type { MenuContextValue } from \"./menu-context.js\";\nimport { edgeItem, focusItem, hasModifier, isRtl } from \"./menu-dom.js\";\n\nconst isDev = process.env.NODE_ENV !== \"production\";\n\n/**\n * State of `Menu.Trigger`, passed to the function forms of `className`, `style`, `children` and\n * `render`.\n */\nexport type MenuTriggerState = PopoverTriggerHookState;\n\n/**\n * Props of `Menu.Trigger`.\n *\n * @see https://ui.funstack.work/components/menu\n */\nexport interface MenuTriggerProps extends Omit<\n ComponentPropsWithRef<\"button\">,\n | \"onClick\"\n | \"onKeyDown\"\n | \"onMouseDown\"\n | \"aria-expanded\"\n | \"aria-haspopup\"\n | \"commandfor\"\n | \"command\"\n | \"popoverTarget\"\n | \"popoverTargetAction\"\n | \"className\"\n | \"style\"\n | \"children\"\n> {\n /**\n * The menu cannot be opened from this button. Native `disabled` on a `<button>`; with a link or\n * component `render`, `aria-disabled=\"true\"` instead (the element stays focusable).\n *\n * @defaultValue `false`\n */\n disabled?: boolean;\n /** Class name, or a function of the part's state that returns one. */\n className?: StateValue<string | undefined, MenuTriggerState>;\n /** Inline style, or a function of the part's state that returns one. */\n style?: StateValue<CSSProperties | undefined, MenuTriggerState>;\n /** Rendered from optimistic state; must not suspend. */\n children?: StateValue<ReactNode, MenuTriggerState>;\n /**\n * Should render a `<button>` (the `popovertarget` path before hydration). Another element works\n * through the click handler, but a press on it while the menu is open is a light dismiss rather\n * than the awaited close.\n */\n render?: RenderProp<ComponentPropsWithRef<\"button\">, MenuTriggerState>;\n}\n\n/**\n * Opens with the first (`\"first\"`) or last (`\"last\"`) item focused, or focuses that item when the\n * menu is already open (a pre-hydration open, design walkthrough 10). Shared with SubmenuTrigger.\n */\nexport function openMenuWithFocus(\n menu: MenuContextValue,\n event: Event,\n which: \"first\" | \"last\",\n): void {\n const { popover } = menu;\n if (popover.intended()) {\n const popup = popover.popupRef.current;\n if (popup !== null && isPopoverOpen(popup)) {\n const item = edgeItem(popup, which);\n if (item !== null) focusItem(item);\n }\n return;\n }\n menu.setOpenCause(which, isPointerClick(event));\n popover.requestOpen(event, \"activate\");\n}\n\n/** A click from a pointing device (`detail` counts clicks; keyboard and `click()` give 0). */\nfunction isPointerClick(event: Event): boolean {\n return event.type === \"click\" && (event as MouseEvent).detail > 0;\n}\n\n/**\n * The menu button: `<button type=\"button\" popovertarget aria-haspopup=\"menu\">`, with no\n * `aria-expanded` (the browser exposes the menu's state on its invoker; a link or other non-button\n * element, or a disabled component `render`, renders it from the shown menu). A click (Enter,\n * Space) toggles the menu through the Root's transition; ArrowDown / ArrowUp open it with the\n * first / last item focused (inside a vertical `Toolbar.Root`: the inline-end arrow, with the\n * first item focused).\n *\n * Owned keys (conventions 1.2): `type` (on a `<button>` only), `id`, `disabled`, `aria-disabled`\n * (link or component `render`), `popoverTarget`, `aria-haspopup`, `aria-expanded`, `onClick`,\n * `onKeyDown`, `onMouseDown`, `data-open`, `data-pending`, `data-disabled`; `commandfor`,\n * `command` and `popoverTargetAction` (never written; omitted from the props type).\n *\n * @see https://ui.funstack.work/components/menu\n */\nexport function MenuTrigger({\n disabled = false,\n className,\n style,\n children,\n render,\n ...rest\n}: MenuTriggerProps) {\n const menu = useMenuContext(\"Menu.Trigger\");\n useDevWarning(\n menu.submenu,\n isDev &&\n \"Menu.Trigger: rendered in a submenu. Use Menu.SubmenuTrigger, which is also an item of the \" +\n \"parent menu.\",\n );\n\n // Inside a vertical Toolbar, ArrowDown / ArrowUp are the Toolbar's: the trigger opens on the\n // inline-end arrow instead, as a submenu trigger does (design/components/toggle-group.md 7.4).\n const toolbar = useToolbarContext();\n const onKeyDown = (event: KeyboardEvent<HTMLButtonElement>) => {\n if (disabled || hasModifier(event)) return;\n let which: \"first\" | \"last\";\n if (toolbar?.orientation === \"vertical\") {\n const openKey = isRtl(event.currentTarget) ? \"ArrowLeft\" : \"ArrowRight\";\n if (event.key !== openKey) return;\n which = \"first\";\n } else {\n if (event.key !== \"ArrowDown\" && event.key !== \"ArrowUp\") return;\n which = event.key === \"ArrowDown\" ? \"first\" : \"last\";\n }\n // No page scroll, and an enclosing Toolbar sees `defaultPrevented`; Enter and Space stay the\n // native click.\n event.preventDefault();\n openMenuWithFocus(menu, event.nativeEvent, which);\n };\n\n const { state, libraryProps, ref } = usePopoverTrigger(menu.popover, {\n part: \"Menu.Trigger\",\n disabled,\n render,\n handlers: {\n onKeyDown,\n onMouseDown: (event: ReactMouseEvent<HTMLButtonElement>) => {\n // A press on the Trigger of an open menu must not move focus out of the popup: the Popup's\n // focusout would hide the menu before the click's library-owned close (design 2.2).\n if (event.button === 0 && menu.popover.intended()) event.preventDefault();\n },\n },\n // The default toggle, with the open cause recorded right before an open.\n onActivate: (event) =>\n toggleFromTrigger(menu.popover, event.nativeEvent, () =>\n menu.setOpenCause(\"first\", isPointerClick(event.nativeEvent)),\n ),\n });\n\n return useRender<ComponentPropsWithRef<\"button\">, MenuTriggerState>({\n defaultElement: \"button\",\n render,\n state,\n className,\n style,\n children,\n props: rest,\n libraryProps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AAc,UAA8C;IAG1D,IAAI,MAAM,WAAW,KAAK,KAAK,QAAQ,SAAS,GAAG,MAAM,eAAe;GAC1E;EACF;EAEA,aAAa,UACX,kBAAkB,KAAK,SAAS,MAAM,mBACpC,KAAK,aAAa,SAAS,eAAe,MAAM,WAAW,CAAC,CAC9D;CACJ,CAAC;CAED,OAAO,UAA6D;EAClE,gBAAgB;EAChB;EACA;EACA;EACA;EACA;EACA,OAAO;EACP,cAAc;GACZ,GAAG;GACH,IAAI,KAAK;GACT,iBAAiB;EACnB;EACA;CACF,CAAC;AACH;AAEA,YAAY,cAAc"}
|
|
1
|
+
{"version":3,"file":"menu-trigger.mjs","names":[],"sources":["../../src/menu/menu-trigger.tsx"],"sourcesContent":["\"use client\";\nimport { useCallback } from \"react\";\nimport type {\n ComponentPropsWithRef,\n CSSProperties,\n KeyboardEvent,\n MouseEvent as ReactMouseEvent,\n PointerEvent,\n ReactNode,\n} from \"react\";\nimport { useMenubarContext } from \"../internal/menubar-context.js\";\nimport type { MenubarOpenFocus } from \"../internal/menubar-context.js\";\nimport { isPopoverOpen } from \"../internal/overlay/popover-open.js\";\nimport { useToolbarContext } from \"../internal/toolbar-context.js\";\nimport type { RenderProp, StateValue } from \"../internal/types.js\";\nimport { useDevWarning } from \"../internal/use-dev-warning.js\";\nimport { useLatest } from \"../internal/use-latest.js\";\nimport { useMergedRefs } from \"../internal/use-merged-refs.js\";\nimport { toggleFromTrigger, usePopoverTrigger } from \"../internal/overlay/use-popover-trigger.js\";\nimport type { PopoverTriggerHookState } from \"../internal/overlay/use-popover-trigger.js\";\nimport { useRender } from \"../internal/use-render.js\";\nimport { useMenuContext } from \"./menu-context.js\";\nimport type { MenuContextValue } from \"./menu-context.js\";\nimport { edgeItem, focusItem, hasModifier, isRtl } from \"./menu-dom.js\";\n\nconst isDev = process.env.NODE_ENV !== \"production\";\n\n/**\n * State of `Menu.Trigger`, passed to the function forms of `className`, `style`, `children` and\n * `render`.\n */\nexport type MenuTriggerState = PopoverTriggerHookState;\n\n/**\n * Props of `Menu.Trigger`.\n *\n * @see https://ui.funstack.work/components/menu\n */\nexport interface MenuTriggerProps extends Omit<\n ComponentPropsWithRef<\"button\">,\n | \"onClick\"\n | \"onKeyDown\"\n | \"onMouseDown\"\n | \"aria-expanded\"\n | \"aria-haspopup\"\n | \"commandfor\"\n | \"command\"\n | \"popoverTarget\"\n | \"popoverTargetAction\"\n | \"className\"\n | \"style\"\n | \"children\"\n> {\n /**\n * The menu cannot be opened from this button. Native `disabled` on a `<button>`; with a link or\n * component `render`, `aria-disabled=\"true\"` instead (the element stays focusable).\n *\n * @defaultValue `false`\n */\n disabled?: boolean;\n /** Class name, or a function of the part's state that returns one. */\n className?: StateValue<string | undefined, MenuTriggerState>;\n /** Inline style, or a function of the part's state that returns one. */\n style?: StateValue<CSSProperties | undefined, MenuTriggerState>;\n /** Rendered from optimistic state; must not suspend. */\n children?: StateValue<ReactNode, MenuTriggerState>;\n /**\n * Should render a `<button>` (the `popovertarget` path before hydration). Another element works\n * through the click handler, but a press on it while the menu is open is a light dismiss rather\n * than the awaited close.\n */\n render?: RenderProp<ComponentPropsWithRef<\"button\">, MenuTriggerState>;\n}\n\n/**\n * Opens with the first (`\"first\"`) or last (`\"last\"`) item focused, or focuses that item when the\n * menu is already open (a pre-hydration open, design walkthrough 10). Shared with SubmenuTrigger.\n */\nexport function openMenuWithFocus(\n menu: MenuContextValue,\n event: Event,\n which: \"first\" | \"last\",\n): void {\n const { popover } = menu;\n if (popover.intended()) {\n const popup = popover.popupRef.current;\n if (popup !== null && isPopoverOpen(popup)) {\n const item = edgeItem(popup, which);\n if (item !== null) focusItem(item);\n }\n return;\n }\n menu.setOpenCause(which, isPointerClick(event));\n popover.requestOpen(event, \"activate\");\n}\n\n/** A click from a pointing device (`detail` counts clicks; keyboard and `click()` give 0). */\nfunction isPointerClick(event: Event): boolean {\n return event.type === \"click\" && (event as MouseEvent).detail > 0;\n}\n\n/**\n * A switch's open (design/components/menubar.md 2.2 step 3): the first item focused when the menu\n * shows (or at once, when it is already shown), or focus left on the Trigger.\n */\nfunction openFromBar(menu: MenuContextValue, event: Event, focus: MenubarOpenFocus): void {\n if (focus === \"first\") {\n openMenuWithFocus(menu, event, \"first\");\n return;\n }\n if (menu.popover.intended()) return;\n menu.setOpenCause(\"none\");\n menu.popover.requestOpen(event, \"activate\");\n}\n\n/**\n * The menu button: `<button type=\"button\" popovertarget aria-haspopup=\"menu\">`, with no\n * `aria-expanded` (the browser exposes the menu's state on its invoker; a link or other non-button\n * element, or a disabled component `render`, renders it from the shown menu). A click (Enter,\n * Space) toggles the menu through the Root's transition; ArrowDown / ArrowUp open it with the\n * first / last item focused (inside a vertical `Toolbar.Root`: the inline-end arrow, with the\n * first item focused).\n *\n * Inside a `Menubar` it is an item of the bar (design/components/menubar.md): `role=\"menuitem\"`,\n * no rendered `tabindex` (the bar writes it), and `disabled` as `aria-disabled=\"true\"` (it stays\n * focusable and never opens). Moving the pointer onto it while another menu of the bar is open\n * switches to its menu.\n *\n * Owned keys (conventions 1.2): `type` (on a `<button>` only), `id`, `disabled`, `aria-disabled`\n * (link or component `render`, and in a Menubar), `popoverTarget`, `aria-haspopup`,\n * `aria-expanded`, `onClick`, `onKeyDown`, `onMouseDown`, `data-open`, `data-pending`,\n * `data-disabled`, and in a Menubar `role`; `commandfor`, `command` and `popoverTargetAction`\n * (never written; omitted from the props type). In a Menubar the hover switch is an\n * `onPointerMove` handler merged with the consumer's (theirs runs first).\n *\n * @see https://ui.funstack.work/components/menu\n */\nexport function MenuTrigger({\n disabled = false,\n className,\n style,\n children,\n render,\n ...rest\n}: MenuTriggerProps) {\n const menu = useMenuContext(\"Menu.Trigger\");\n useDevWarning(\n menu.submenu,\n isDev &&\n \"Menu.Trigger: rendered in a submenu. Use Menu.SubmenuTrigger, which is also an item of the \" +\n \"parent menu.\",\n );\n\n // An item of an enclosing Menubar (design/components/menubar.md 1); the bar's context ends at\n // each Menu.Popup, so a Trigger in a menu never sees it.\n const bar = useMenubarContext();\n const inBar = bar !== null;\n // Inside a vertical Toolbar, ArrowDown / ArrowUp are the Toolbar's: the trigger opens on the\n // inline-end arrow instead, as a submenu trigger does (design/components/toggle-group.md 7.4).\n const toolbar = useToolbarContext();\n const onKeyDown = (event: KeyboardEvent<HTMLButtonElement>) => {\n if (disabled || hasModifier(event)) return;\n let which: \"first\" | \"last\";\n if (!inBar && toolbar?.orientation === \"vertical\") {\n const openKey = isRtl(event.currentTarget) ? \"ArrowLeft\" : \"ArrowRight\";\n if (event.key !== openKey) return;\n which = \"first\";\n } else {\n if (event.key !== \"ArrowDown\" && event.key !== \"ArrowUp\") return;\n which = event.key === \"ArrowDown\" ? \"first\" : \"last\";\n }\n // No page scroll, and an enclosing Toolbar sees `defaultPrevented`; Enter and Space stay the\n // native click.\n event.preventDefault();\n openMenuWithFocus(menu, event.nativeEvent, which);\n };\n\n // The menu as the bar sees it (menubar.md 2.1), read through the latest render.\n const latest = useLatest({ menu, disabled });\n const barRef = useCallback(\n (element: HTMLElement) =>\n bar?.register(element, {\n disabled: () => latest.current.disabled,\n intended: () => latest.current.menu.popover.intended(),\n hiddenBeforeToggle: () => latest.current.menu.popover.hiddenBeforeToggle(),\n popup: () => latest.current.menu.popover.popupRef.current,\n open: (event, focus) => openFromBar(latest.current.menu, event, focus),\n supersede: (event) => latest.current.menu.popover.requestClose(event, \"dismiss\"),\n }),\n [bar, latest],\n );\n\n // Menu mode (menubar.md 2.3, 4): the pointer moving onto this Trigger while another menu of the\n // bar is intended open switches to this menu, focus left on the Trigger. `pointermove`, not\n // `pointerenter`: a menu appearing under a resting pointer is no move.\n const onPointerMove =\n bar === null\n ? undefined\n : (event: PointerEvent<HTMLButtonElement>) => {\n if (event.pointerType === \"touch\" || disabled || menu.popover.intended()) return;\n const element = event.currentTarget;\n if (!bar.otherIntended(element)) return;\n bar.switchTo(element, \"none\", event.nativeEvent, true);\n };\n\n const {\n state,\n libraryProps,\n ref: triggerRef,\n } = usePopoverTrigger(menu.popover, {\n part: \"Menu.Trigger\",\n disabled,\n render,\n alwaysFocusable: inBar,\n handlers: {\n onKeyDown,\n // Only in a bar: an `undefined` key in the owned layer would remove the consumer's handler.\n ...(onPointerMove !== undefined && { onPointerMove }),\n onMouseDown: (event: ReactMouseEvent<HTMLButtonElement>) => {\n // A press on the Trigger of an open menu must not move focus out of the popup: the Popup's\n // focusout would hide the menu before the click's library-owned close (design 2.2).\n if (event.button === 0 && menu.popover.intended()) event.preventDefault();\n },\n },\n // The default toggle, with the open cause recorded right before an open.\n onActivate: (event) =>\n toggleFromTrigger(menu.popover, event.nativeEvent, () =>\n menu.setOpenCause(\"first\", isPointerClick(event.nativeEvent)),\n ),\n });\n\n const ref = useMergedRefs(triggerRef, barRef);\n\n return useRender<ComponentPropsWithRef<\"button\">, MenuTriggerState>({\n defaultElement: \"button\",\n render,\n state,\n className,\n style,\n children,\n props: rest,\n libraryProps: {\n ...libraryProps,\n ...(inBar && { role: \"menuitem\" }),\n id: menu.triggerId,\n \"aria-haspopup\": \"menu\",\n },\n ref,\n });\n}\n\nMenuTrigger.displayName = \"MenuTrigger\";\n"],"mappings":";;;;;;;;;;;;;AAyBA,MAAM,QAAQ,QAAQ,IAAI,aAAa;;;;;AAqDvC,SAAgB,kBACd,MACA,OACA,OACM;CACN,MAAM,EAAE,YAAY;CACpB,IAAI,QAAQ,SAAS,GAAG;EACtB,MAAM,QAAQ,QAAQ,SAAS;EAC/B,IAAI,UAAU,QAAQ,cAAc,KAAK,GAAG;GAC1C,MAAM,OAAO,SAAS,OAAO,KAAK;GAClC,IAAI,SAAS,MAAM,UAAU,IAAI;EACnC;EACA;CACF;CACA,KAAK,aAAa,OAAO,eAAe,KAAK,CAAC;CAC9C,QAAQ,YAAY,OAAO,UAAU;AACvC;;AAGA,SAAS,eAAe,OAAuB;CAC7C,OAAO,MAAM,SAAS,WAAY,MAAqB,SAAS;AAClE;;;;;AAMA,SAAS,YAAY,MAAwB,OAAc,OAA+B;CACxF,IAAI,UAAU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QAAQ,OACJ,KAAA,KACC,UAA2C;EAC1C,IAAI,MAAM,gBAAgB,WAAW,YAAY,KAAK,QAAQ,SAAS,GAAG;EAC1E,MAAM,UAAU,MAAM;EACtB,IAAI,CAAC,IAAI,cAAc,OAAO,GAAG;EACjC,IAAI,SAAS,SAAS,QAAQ,MAAM,aAAa,IAAI;CACvD;CAEN,MAAM,EACJ,OACA,cACA,KAAK,eACH,kBAAkB,KAAK,SAAS;EAClC,MAAM;EACN;EACA;EACA,iBAAiB;EACjB,UAAU;GACR;GAEA,GAAI,kBAAkB,KAAA,KAAa,EAAE,cAAc;GACnD,cAAc,UAA8C;IAG1D,IAAI,MAAM,WAAW,KAAK,KAAK,QAAQ,SAAS,GAAG,MAAM,eAAe;GAC1E;EACF;EAEA,aAAa,UACX,kBAAkB,KAAK,SAAS,MAAM,mBACpC,KAAK,aAAa,SAAS,eAAe,MAAM,WAAW,CAAC,CAC9D;CACJ,CAAC;CAED,MAAM,MAAM,cAAc,YAAY,MAAM;CAE5C,OAAO,UAA6D;EAClE,gBAAgB;EAChB;EACA;EACA;EACA;EACA;EACA,OAAO;EACP,cAAc;GACZ,GAAG;GACH,GAAI,SAAS,EAAE,MAAM,WAAW;GAChC,IAAI,KAAK;GACT,iBAAiB;EACnB;EACA;CACF,CAAC;AACH;AAEA,YAAY,cAAc"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Menubar } from "./menubar.mjs";
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { RenderProp, StateValue } from "../internal/types.mjs";
|
|
2
|
+
import { CSSProperties, ComponentPropsWithRef, ReactNode } from "react";
|
|
3
|
+
//#region src/menubar/menubar.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* State of `Menubar`, passed to the function forms of `className`, `style`, `children` and
|
|
6
|
+
* `render`. Empty: the bar holds no open state (each menu is its own state owner).
|
|
7
|
+
*/
|
|
8
|
+
export interface MenubarState {}
|
|
9
|
+
/**
|
|
10
|
+
* Props of `Menubar`.
|
|
11
|
+
*
|
|
12
|
+
* @see https://ui.funstack.work/components/menubar
|
|
13
|
+
*/
|
|
14
|
+
export interface MenubarProps extends Omit<ComponentPropsWithRef<"div">, "role" | "onKeyDown" | "onFocus" | "onBlur" | "onPointerDown" | "className" | "style" | "children"> {
|
|
15
|
+
/**
|
|
16
|
+
* Arrow keys (on the bar and from inside its menus) wrap from the last menu to the first and
|
|
17
|
+
* back.
|
|
18
|
+
*
|
|
19
|
+
* @defaultValue `true`
|
|
20
|
+
*/
|
|
21
|
+
loop?: boolean;
|
|
22
|
+
/** Class name, or a function of the part's state that returns one. */
|
|
23
|
+
className?: StateValue<string | undefined, MenubarState>;
|
|
24
|
+
/** Inline style, or a function of the part's state that returns one. */
|
|
25
|
+
style?: StateValue<CSSProperties | undefined, MenubarState>;
|
|
26
|
+
/** The menus of the bar (`Menu.Root`s), or a function of the part's state. */
|
|
27
|
+
children?: StateValue<ReactNode, MenubarState>;
|
|
28
|
+
/** Replace the default `<div>` (element form) or take over rendering (function form). */
|
|
29
|
+
render?: RenderProp<ComponentPropsWithRef<"div">, MenubarState>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* `<div role="menubar">` (WAI-ARIA APG Menubar): a row of top-level `Menu.Root`s whose
|
|
33
|
+
* `Menu.Trigger`s are the items of the bar (`role="menuitem"`). One Tab stop with
|
|
34
|
+
* ArrowLeft / ArrowRight / Home / End between the Triggers (the Toolbar's roving engine);
|
|
35
|
+
* ArrowRight / ArrowLeft inside a menu, the same keys on the Trigger of an open menu and the
|
|
36
|
+
* pointer moving onto another Trigger while a menu is open switch menus. The bar holds no open
|
|
37
|
+
* state: each `Menu.Root` keeps its own (design/components/menubar.md). Name it with
|
|
38
|
+
* `aria-label` or `aria-labelledby`.
|
|
39
|
+
*
|
|
40
|
+
* Owned keys (conventions 1.2): `role`, `onKeyDown`, `onFocus`, `onBlur`, `onPointerDown`.
|
|
41
|
+
*
|
|
42
|
+
* @see https://ui.funstack.work/components/menubar
|
|
43
|
+
*/
|
|
44
|
+
export declare function Menubar({ loop, className, style, children, render, ...rest }: MenubarProps): import("react").JSX.Element;
|
|
45
|
+
export declare namespace Menubar {
|
|
46
|
+
var displayName: string;
|
|
47
|
+
}
|
|
48
|
+
//#endregion
|
|
49
|
+
//# sourceMappingURL=menubar.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"menubar.d.mts","names":[],"sources":["../../src/menubar/menubar.tsx"],"mappings":";;;;;;;iBA2BiB;;;;;;iBAOA,qBAAqB,KACpC;;;;;;;EASA;;EAEA,YAAY,+BAA+B;;EAE3C,QAAQ,WAAW,2BAA2B;;EAE9C,WAAW,WAAW,WAAW;;EAEjC,SAAS,WAAW,8BAA8B;;;;;;;;;;;;;;;wBAoCpC,UACd,MACA,WACA,OACA,UACA,WACG,QACF,+BAAY,IAAA;yBAPC"}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useRender } from "../internal/use-render.mjs";
|
|
3
|
+
import { useLatest } from "../internal/use-latest.mjs";
|
|
4
|
+
import { sortByDocumentOrder } from "../internal/dom-order.mjs";
|
|
5
|
+
import { focusWithRing } from "../internal/arrow-keys.mjs";
|
|
6
|
+
import { useRovingFocus } from "../internal/use-roving-focus.mjs";
|
|
7
|
+
import { isPopoverOpen } from "../internal/overlay/popover-open.mjs";
|
|
8
|
+
import { MenubarContext } from "../internal/menubar-context.mjs";
|
|
9
|
+
import { useMemo, useRef } from "react";
|
|
10
|
+
import { jsx } from "react/jsx-runtime";
|
|
11
|
+
//#region src/menubar/menubar.tsx
|
|
12
|
+
const STATE = {};
|
|
13
|
+
/** The roving engine's entry: the first Trigger before any was focused. */
|
|
14
|
+
function firstItem(items) {
|
|
15
|
+
return items[0];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The shown popup of a menu of the bar is hidden at once (a dismissal, reported by its `toggle`);
|
|
19
|
+
* an open still pending is superseded (design/components/menubar.md 2.2 step 2, 4).
|
|
20
|
+
*/
|
|
21
|
+
function closeMenu(menu, event) {
|
|
22
|
+
const popup = menu.popup();
|
|
23
|
+
if (popup !== null && isPopoverOpen(popup)) popup.hidePopover();
|
|
24
|
+
else if (menu.intended() && !menu.hiddenBeforeToggle()) menu.supersede(event);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* `<div role="menubar">` (WAI-ARIA APG Menubar): a row of top-level `Menu.Root`s whose
|
|
28
|
+
* `Menu.Trigger`s are the items of the bar (`role="menuitem"`). One Tab stop with
|
|
29
|
+
* ArrowLeft / ArrowRight / Home / End between the Triggers (the Toolbar's roving engine);
|
|
30
|
+
* ArrowRight / ArrowLeft inside a menu, the same keys on the Trigger of an open menu and the
|
|
31
|
+
* pointer moving onto another Trigger while a menu is open switch menus. The bar holds no open
|
|
32
|
+
* state: each `Menu.Root` keeps its own (design/components/menubar.md). Name it with
|
|
33
|
+
* `aria-label` or `aria-labelledby`.
|
|
34
|
+
*
|
|
35
|
+
* Owned keys (conventions 1.2): `role`, `onKeyDown`, `onFocus`, `onBlur`, `onPointerDown`.
|
|
36
|
+
*
|
|
37
|
+
* @see https://ui.funstack.work/components/menubar
|
|
38
|
+
*/
|
|
39
|
+
function Menubar({ loop = true, className, style, children, render, ...rest }) {
|
|
40
|
+
const rootRef = useRef(null);
|
|
41
|
+
const roving = useRovingFocus(rootRef, {
|
|
42
|
+
orientation: "horizontal",
|
|
43
|
+
loop,
|
|
44
|
+
native: false,
|
|
45
|
+
entry: firstItem,
|
|
46
|
+
memory: true
|
|
47
|
+
});
|
|
48
|
+
const latestLoop = useLatest(loop);
|
|
49
|
+
const registry = useRef(/* @__PURE__ */ new Map());
|
|
50
|
+
const held = useRef(null);
|
|
51
|
+
const context = useMemo(() => {
|
|
52
|
+
const menus = registry.current;
|
|
53
|
+
const switchTo = (target, focus, event, pointer) => {
|
|
54
|
+
const menu = menus.get(target);
|
|
55
|
+
if (menu === void 0) return;
|
|
56
|
+
held.current = null;
|
|
57
|
+
if (target.ownerDocument.activeElement !== target) {
|
|
58
|
+
if (pointer) target.focus({ focusVisible: false });
|
|
59
|
+
else focusWithRing(target);
|
|
60
|
+
}
|
|
61
|
+
for (const [trigger, other] of menus) if (trigger !== target) closeMenu(other, event);
|
|
62
|
+
if (menu.disabled()) held.current = {
|
|
63
|
+
trigger: target,
|
|
64
|
+
focus
|
|
65
|
+
};
|
|
66
|
+
else menu.open(event, focus);
|
|
67
|
+
};
|
|
68
|
+
return {
|
|
69
|
+
register: (trigger, menu) => {
|
|
70
|
+
menus.set(trigger, menu);
|
|
71
|
+
return () => {
|
|
72
|
+
if (menus.get(trigger) === menu) menus.delete(trigger);
|
|
73
|
+
};
|
|
74
|
+
},
|
|
75
|
+
switchTo,
|
|
76
|
+
step: (from, delta, event) => {
|
|
77
|
+
const triggers = sortByDocumentOrder([...menus.keys()], (el) => el);
|
|
78
|
+
const index = triggers.indexOf(from);
|
|
79
|
+
if (index < 0) return false;
|
|
80
|
+
const last = triggers.length - 1;
|
|
81
|
+
let next = index + delta;
|
|
82
|
+
if (next > last) next = latestLoop.current ? 0 : last;
|
|
83
|
+
else if (next < 0) next = latestLoop.current ? last : 0;
|
|
84
|
+
const target = triggers[next];
|
|
85
|
+
if (target === void 0 || target === from) return false;
|
|
86
|
+
switchTo(target, "first", event, false);
|
|
87
|
+
return true;
|
|
88
|
+
},
|
|
89
|
+
otherIntended: (trigger) => {
|
|
90
|
+
if (held.current !== null && held.current.trigger !== trigger) return true;
|
|
91
|
+
for (const [other, menu] of menus) if (other !== trigger && menu.intended() && !menu.hiddenBeforeToggle()) return true;
|
|
92
|
+
return false;
|
|
93
|
+
},
|
|
94
|
+
rtl: () => {
|
|
95
|
+
const root = rootRef.current;
|
|
96
|
+
return root !== null && getComputedStyle(root).direction === "rtl";
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}, [latestLoop]);
|
|
100
|
+
const onKeyDown = (event) => {
|
|
101
|
+
const target = event.target;
|
|
102
|
+
const menu = registry.current.get(target);
|
|
103
|
+
const hold = held.current !== null && held.current.trigger === target ? held.current : null;
|
|
104
|
+
if (hold !== null && event.key === "Escape") {
|
|
105
|
+
held.current = null;
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const menuMode = menu !== void 0 && !event.defaultPrevented && (hold !== null || menu.intended());
|
|
109
|
+
roving.onKeyDown(event);
|
|
110
|
+
if (!menuMode || !event.defaultPrevented) return;
|
|
111
|
+
const focused = target.ownerDocument.activeElement;
|
|
112
|
+
if (focused instanceof HTMLElement && focused !== target && registry.current.has(focused)) context.switchTo(focused, hold?.focus ?? "none", event.nativeEvent, false);
|
|
113
|
+
};
|
|
114
|
+
const onBlur = (event) => {
|
|
115
|
+
roving.onBlur(event);
|
|
116
|
+
if (held.current !== null && event.target === held.current.trigger) held.current = null;
|
|
117
|
+
const next = event.relatedTarget;
|
|
118
|
+
if (!(next instanceof Node) || event.currentTarget.contains(next)) return;
|
|
119
|
+
for (const menu of registry.current.values()) closeMenu(menu, event.nativeEvent);
|
|
120
|
+
};
|
|
121
|
+
const element = useRender({
|
|
122
|
+
defaultElement: "div",
|
|
123
|
+
render,
|
|
124
|
+
state: STATE,
|
|
125
|
+
className,
|
|
126
|
+
style,
|
|
127
|
+
children,
|
|
128
|
+
props: rest,
|
|
129
|
+
libraryProps: {
|
|
130
|
+
role: "menubar",
|
|
131
|
+
onKeyDown,
|
|
132
|
+
onFocus: roving.onFocus,
|
|
133
|
+
onBlur,
|
|
134
|
+
onPointerDown: roving.onPointerDown
|
|
135
|
+
},
|
|
136
|
+
ref: rootRef
|
|
137
|
+
});
|
|
138
|
+
return /* @__PURE__ */ jsx(MenubarContext, {
|
|
139
|
+
value: context,
|
|
140
|
+
children: element
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
Menubar.displayName = "Menubar";
|
|
144
|
+
//#endregion
|
|
145
|
+
export { Menubar };
|
|
146
|
+
|
|
147
|
+
//# sourceMappingURL=menubar.mjs.map
|