@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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"navigation-menu-list.mjs","names":[],"sources":["../../src/navigation-menu/navigation-menu-list.tsx"],"sourcesContent":["// No \"use client\": `NavigationMenu.List` reads no context and calls no hook, so a Server\n// Component may render it (design/components/navigation-menu.md 8). It must not import from any\n// \"use client\" module (checked by src/__tests__/directives.test.ts).\nimport type { ComponentPropsWithRef, CSSProperties, ReactNode } from \"react\";\nimport { mergeRenderRef, renderElement } from \"../internal/render-element.js\";\nimport type { RenderProp, StateValue } from \"../internal/types.js\";\n\n/** `NavigationMenu.List` has no state. */\nexport type NavigationMenuListState = Record<string, never>;\n\nconst EMPTY: Record<string, never> = {};\n\n/**\n * Props of `NavigationMenu.List`.\n *\n * @see https://ui.funstack.work/components/navigation-menu\n */\nexport interface NavigationMenuListProps extends Omit<\n ComponentPropsWithRef<\"ul\">,\n \"className\" | \"style\" | \"children\"\n> {\n /** Class name, or a function of the part's state that returns one. */\n className?: StateValue<string | undefined, NavigationMenuListState>;\n /** Inline style, or a function of the part's state that returns one. */\n style?: StateValue<CSSProperties | undefined, NavigationMenuListState>;\n /** Content: `NavigationMenu.Item`s. */\n children?: StateValue<ReactNode, NavigationMenuListState>;\n /** Replace the default `<ul>` (element form) or take over rendering (function form). */\n render?: RenderProp<ComponentPropsWithRef<\"ul\">, NavigationMenuListState>;\n}\n\n/**\n * The top-level entries: a `<ul role=\"list\">`. The explicit role keeps the list semantics that\n * Safari drops under `list-style: none` (design 1); it is a default the consumer may override.\n * Directive-free, so a Server Component may render it.\n *\n * @see https://ui.funstack.work/components/navigation-menu\n */\nexport function NavigationMenuList({\n className,\n style,\n children,\n render,\n ref,\n ...rest\n}: NavigationMenuListProps) {\n return renderElement<ComponentPropsWithRef<\"ul\">, NavigationMenuListState>({\n defaultElement: \"ul\",\n render,\n state: EMPTY,\n className,\n style,\n children,\n props: { role: \"list\", ...rest },\n // Passed through to the element, never read during render.\n // oxlint-disable-next-line react/refs\n ref: mergeRenderRef(ref, render),\n });\n}\n\nNavigationMenuList.displayName = \"NavigationMenuList\";\n"],"mappings":";;AAUA,MAAM,QAA+B,CAAC;;;;;;;;AA4BtC,SAAgB,mBAAmB,EACjC,WACA,OACA,UACA,QACA,KACA,GAAG,QACuB;CAC1B,OAAO,cAAoE;EACzE,gBAAgB;EAChB;EACA,OAAO;EACP;EACA;EACA;EACA,OAAO;GAAE,MAAM;GAAQ,GAAG;EAAK;EAG/B,KAAK,eAAe,KAAK,MAAM;CACjC,CAAC;AACH;AAEA,mBAAmB,cAAc"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { RenderProp, StateValue } from "../internal/types.mjs";
|
|
2
|
+
import { AnchorAlign, AnchorSide } from "../internal/overlay/anchor-position.mjs";
|
|
3
|
+
import { CSSProperties, ComponentPropsWithRef, ReactNode, ViewTransitionClass } from "react";
|
|
4
|
+
//#region src/navigation-menu/navigation-menu-popup.d.ts
|
|
5
|
+
/** `inline-start` / `inline-end` follow the panel's `direction` (conventions 13.1). */
|
|
6
|
+
export type NavigationMenuSide = AnchorSide;
|
|
7
|
+
/**
|
|
8
|
+
* Alignment of the panel along its side: inline-relative on a top / bottom side, the top /
|
|
9
|
+
* bottom edges on the others.
|
|
10
|
+
*/
|
|
11
|
+
export type NavigationMenuAlign = AnchorAlign;
|
|
12
|
+
/**
|
|
13
|
+
* State of `NavigationMenu.Popup`, passed to the function forms of `className`, `style`,
|
|
14
|
+
* `children` and `render`.
|
|
15
|
+
*/
|
|
16
|
+
export interface NavigationMenuPopupState {
|
|
17
|
+
/**
|
|
18
|
+
* Committed open state (library mode); `false` in JS-free mode. Read `:popover-open` for the
|
|
19
|
+
* DOM truth.
|
|
20
|
+
*/
|
|
21
|
+
readonly open: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* While shown: the physical side the browser placed the panel on (after any fallback flip;
|
|
24
|
+
* conventions 13.2). Otherwise the `side` prop. `null` with `side={null}`.
|
|
25
|
+
*/
|
|
26
|
+
readonly side: NavigationMenuSide | null;
|
|
27
|
+
/** While shown: the resolved alignment; otherwise the `align` prop. */
|
|
28
|
+
readonly align: NavigationMenuAlign;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Props of `NavigationMenu.Popup`.
|
|
32
|
+
*
|
|
33
|
+
* @see https://ui.funstack.work/components/navigation-menu
|
|
34
|
+
*/
|
|
35
|
+
export interface NavigationMenuPopupProps extends Omit<ComponentPropsWithRef<"div">, "id" | "popover" | "onToggle" | "onBeforeToggle" | "className" | "style" | "children"> {
|
|
36
|
+
/**
|
|
37
|
+
* Placement relative to the Trigger through inline `position-area`, with
|
|
38
|
+
* `position-try-fallbacks` flipping it when it does not fit. `null` emits no inline positioning
|
|
39
|
+
* (and no `data-side` / `data-align`), so a stylesheet owns placement (a full-width panel).
|
|
40
|
+
*
|
|
41
|
+
* @defaultValue `"bottom"`
|
|
42
|
+
*/
|
|
43
|
+
side?: NavigationMenuSide | null;
|
|
44
|
+
/**
|
|
45
|
+
* Alignment along the side. On a top / bottom side `start` / `end` are inline-relative
|
|
46
|
+
* (mirrored under `direction: rtl`). Ignored when `side` is `null`.
|
|
47
|
+
*
|
|
48
|
+
* @defaultValue `"start"`
|
|
49
|
+
*/
|
|
50
|
+
align?: NavigationMenuAlign;
|
|
51
|
+
/**
|
|
52
|
+
* Library mode: what happens to the children while the panel is closed.
|
|
53
|
+
* - `false` (default): not rendered. Mounting them is part of the opening transition, so
|
|
54
|
+
* content that suspends holds the open (the Trigger is `data-pending`) until it is ready.
|
|
55
|
+
* - `true`: rendered inside `<Activity mode="hidden">`: DOM and state preserved, effects
|
|
56
|
+
* stopped. Not in the server HTML.
|
|
57
|
+
*
|
|
58
|
+
* Ignored in JS-free mode (always mounted, in the server HTML).
|
|
59
|
+
*
|
|
60
|
+
* @defaultValue `false`
|
|
61
|
+
*/
|
|
62
|
+
keepMounted?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Library mode only. Forwarded to the `<ViewTransition default>` around the popover element.
|
|
65
|
+
* Animates library-driven opens and closes (the Trigger, a followed Link, controlled changes);
|
|
66
|
+
* dismissals animate with CSS only. Ignored (dev warning) in JS-free mode.
|
|
67
|
+
*
|
|
68
|
+
* Static: setting or unsetting it remounts the element.
|
|
69
|
+
*/
|
|
70
|
+
transition?: ViewTransitionClass;
|
|
71
|
+
/** Class name, or a function of the part's state that returns one. */
|
|
72
|
+
className?: StateValue<string | undefined, NavigationMenuPopupState>;
|
|
73
|
+
/** Inline style, or a function of the part's state that returns one. */
|
|
74
|
+
style?: StateValue<CSSProperties | undefined, NavigationMenuPopupState>;
|
|
75
|
+
/** The panel's links, or a function of the part's state. Library mode: may suspend. */
|
|
76
|
+
children?: StateValue<ReactNode, NavigationMenuPopupState>;
|
|
77
|
+
/** Replace the default `<div>` (element form) or take over rendering (function form). */
|
|
78
|
+
render?: RenderProp<ComponentPropsWithRef<"div">, NavigationMenuPopupState>;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The panel: a `<div popover="auto">` with no role (the disclosure pattern), placed against the
|
|
82
|
+
* Trigger with CSS Anchor Positioning. Render it **immediately after** the Trigger in the same
|
|
83
|
+
* Item: every engine then puts the panel's links right after the Trigger in the Tab order
|
|
84
|
+
* (development warning otherwise). Sibling `auto` popovers close each other, so one panel is
|
|
85
|
+
* open at a time.
|
|
86
|
+
*
|
|
87
|
+
* JS-free mode: the children are always mounted (in the server HTML). Library mode: they render
|
|
88
|
+
* from committed state, so content that suspends holds the open with `data-pending` on the
|
|
89
|
+
* Trigger; `showPopover({ source: trigger })` runs in the commit that mounted them. Style it with
|
|
90
|
+
* `:popover-open`; it emits `data-side` / `data-align` but no `data-open`.
|
|
91
|
+
*
|
|
92
|
+
* Owned keys (conventions 1.2): `id`, `popover`, and `data-side` / `data-align` unless `side` is
|
|
93
|
+
* `null`.
|
|
94
|
+
*
|
|
95
|
+
* @see https://ui.funstack.work/components/navigation-menu
|
|
96
|
+
*/
|
|
97
|
+
export declare function NavigationMenuPopup({ side: sideProp, align: alignProp, keepMounted, transition, className, style, children, render, ...rest }: NavigationMenuPopupProps): ReactNode;
|
|
98
|
+
export declare namespace NavigationMenuPopup {
|
|
99
|
+
var displayName: string;
|
|
100
|
+
}
|
|
101
|
+
//#endregion
|
|
102
|
+
//# sourceMappingURL=navigation-menu-popup.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"navigation-menu-popup.d.mts","names":[],"sources":["../../src/navigation-menu/navigation-menu-popup.tsx"],"mappings":";;;;;YAsBY,qBAAqB;;;;;YAKrB,sBAAsB;;;;;iBAMjB;;;;;WAKN;;;;;WAKA,MAAM;;WAEN,OAAO;;;;;;;iBAQD,iCAAiC,KAChD;;;;;;;;EAUA,OAAO;;;;;;;EAOP,QAAQ;;;;;;;;;;;;EAYR;;;;;;;;EAQA,aAAa;;EAEb,YAAY,+BAA+B;;EAE3C,QAAQ,WAAW,2BAA2B;;EAE9C,WAAW,WAAW,WAAW;;EAEjC,SAAS,WAAW,8BAA8B;;;;;;;;;;;;;;;;;;;wBAuBpC,sBACd,MAAM,UACN,OAAO,WACP,aACA,YACA,WACA,OACA,UACA,WACG,QACF,2BAAwB;yBAVX"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useTransitionBoundary } from "../internal/transition-boundary.mjs";
|
|
3
|
+
import { resolveStateValue } from "../internal/render-element.mjs";
|
|
4
|
+
import { useRender } from "../internal/use-render.mjs";
|
|
5
|
+
import { useElementCheck } from "../internal/use-element-check.mjs";
|
|
6
|
+
import { useMergedRefs } from "../internal/use-merged-refs.mjs";
|
|
7
|
+
import { useAnchoredPlacement } from "../internal/overlay/use-resolved-placement.mjs";
|
|
8
|
+
import { listenInvokerPresses } from "../internal/overlay/invokers.mjs";
|
|
9
|
+
import { useJsFreePropWarnings } from "../internal/overlay/overlay-popup.mjs";
|
|
10
|
+
import { usePopoverPopup } from "../internal/overlay/use-popover-popup.mjs";
|
|
11
|
+
import { NavigationMenuPopupContext, useNavigationMenuItemContext } from "./navigation-menu-context.mjs";
|
|
12
|
+
import { useEffect, useRef } from "react";
|
|
13
|
+
import { jsx } from "react/jsx-runtime";
|
|
14
|
+
//#region src/navigation-menu/navigation-menu-popup.tsx
|
|
15
|
+
const isDev = process.env.NODE_ENV !== "production";
|
|
16
|
+
/** The panel is a `<div>` (design 1). */
|
|
17
|
+
const POPUP_ELEMENTS = ["DIV"];
|
|
18
|
+
/**
|
|
19
|
+
* The panel: a `<div popover="auto">` with no role (the disclosure pattern), placed against the
|
|
20
|
+
* Trigger with CSS Anchor Positioning. Render it **immediately after** the Trigger in the same
|
|
21
|
+
* Item: every engine then puts the panel's links right after the Trigger in the Tab order
|
|
22
|
+
* (development warning otherwise). Sibling `auto` popovers close each other, so one panel is
|
|
23
|
+
* open at a time.
|
|
24
|
+
*
|
|
25
|
+
* JS-free mode: the children are always mounted (in the server HTML). Library mode: they render
|
|
26
|
+
* from committed state, so content that suspends holds the open with `data-pending` on the
|
|
27
|
+
* Trigger; `showPopover({ source: trigger })` runs in the commit that mounted them. Style it with
|
|
28
|
+
* `:popover-open`; it emits `data-side` / `data-align` but no `data-open`.
|
|
29
|
+
*
|
|
30
|
+
* Owned keys (conventions 1.2): `id`, `popover`, and `data-side` / `data-align` unless `side` is
|
|
31
|
+
* `null`.
|
|
32
|
+
*
|
|
33
|
+
* @see https://ui.funstack.work/components/navigation-menu
|
|
34
|
+
*/
|
|
35
|
+
function NavigationMenuPopup({ side: sideProp = "bottom", align: alignProp = "start", keepMounted, transition, className, style, children, render, ...rest }) {
|
|
36
|
+
const ctx = useNavigationMenuItemContext("NavigationMenu.Popup");
|
|
37
|
+
const isLibrary = ctx.mode === "library";
|
|
38
|
+
const { ids, popupRef, triggerRef } = ctx;
|
|
39
|
+
useJsFreePropWarnings("NavigationMenu.Popup", "panel", isLibrary, transition, keepMounted);
|
|
40
|
+
const { setElement, renderContent } = usePopoverPopup(ctx, {
|
|
41
|
+
part: "NavigationMenu.Popup",
|
|
42
|
+
keepMounted,
|
|
43
|
+
transition: isLibrary ? transition : void 0,
|
|
44
|
+
elements: POPUP_ELEMENTS,
|
|
45
|
+
whileShown: listenInvokerPresses
|
|
46
|
+
});
|
|
47
|
+
const checkSecond = useElementCheck(isDev && ((element) => popupRef.current !== null && popupRef.current !== element ? "NavigationMenu.Popup: a second NavigationMenu.Popup mounted in the same Item. An Item has one Trigger and one Popup; give each panel its own NavigationMenu.Item." : null));
|
|
48
|
+
const self = useRef(null);
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if (!isDev) return;
|
|
51
|
+
const element = self.current;
|
|
52
|
+
const trigger = triggerRef.current;
|
|
53
|
+
if (element === null || trigger === null || element.previousElementSibling === trigger) return;
|
|
54
|
+
console.error("NavigationMenu.Popup: the Popup does not directly follow its NavigationMenu.Trigger. Render the Popup immediately after the Trigger: Safari puts a popover's content after its invoker in the Tab order only when the two are adjacent.");
|
|
55
|
+
}, [triggerRef]);
|
|
56
|
+
const placement = useAnchoredPlacement({
|
|
57
|
+
side: sideProp,
|
|
58
|
+
align: alignProp
|
|
59
|
+
});
|
|
60
|
+
const { side, align } = placement;
|
|
61
|
+
const ref = useMergedRefs(checkSecond, setElement, placement.ref, self);
|
|
62
|
+
const state = {
|
|
63
|
+
open: ctx.committed,
|
|
64
|
+
side,
|
|
65
|
+
align
|
|
66
|
+
};
|
|
67
|
+
const content = (s) => /* @__PURE__ */ jsx(NavigationMenuPopupContext, {
|
|
68
|
+
value: ctx,
|
|
69
|
+
children: renderContent(() => resolveStateValue(children, s))
|
|
70
|
+
});
|
|
71
|
+
const libraryProps = {
|
|
72
|
+
id: ids.popup,
|
|
73
|
+
popover: "auto",
|
|
74
|
+
...placement.dataAttributes
|
|
75
|
+
};
|
|
76
|
+
const element = useRender({
|
|
77
|
+
defaultElement: "div",
|
|
78
|
+
render,
|
|
79
|
+
state,
|
|
80
|
+
className,
|
|
81
|
+
style: placement.style(style),
|
|
82
|
+
children: content,
|
|
83
|
+
props: rest,
|
|
84
|
+
libraryProps,
|
|
85
|
+
ref
|
|
86
|
+
});
|
|
87
|
+
return useTransitionBoundary("NavigationMenu.Popup", isLibrary ? transition : void 0, element);
|
|
88
|
+
}
|
|
89
|
+
NavigationMenuPopup.displayName = "NavigationMenuPopup";
|
|
90
|
+
//#endregion
|
|
91
|
+
export { NavigationMenuPopup };
|
|
92
|
+
|
|
93
|
+
//# sourceMappingURL=navigation-menu-popup.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"navigation-menu-popup.mjs","names":[],"sources":["../../src/navigation-menu/navigation-menu-popup.tsx"],"sourcesContent":["\"use client\";\nimport { useEffect, useRef } from \"react\";\nimport type { ComponentPropsWithRef, CSSProperties, ReactNode, ViewTransitionClass } from \"react\";\nimport type { AnchorAlign, AnchorSide } from \"../internal/overlay/anchor-position.js\";\nimport { listenInvokerPresses } from \"../internal/overlay/invokers.js\";\nimport { useJsFreePropWarnings } from \"../internal/overlay/overlay-popup.js\";\nimport { usePopoverPopup } from \"../internal/overlay/use-popover-popup.js\";\nimport { useAnchoredPlacement } from \"../internal/overlay/use-resolved-placement.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 { useMergedRefs } from \"../internal/use-merged-refs.js\";\nimport { resolveStateValue, useRender } from \"../internal/use-render.js\";\nimport type { DataAttributes } from \"../internal/use-render.js\";\nimport {\n NavigationMenuPopupContext,\n useNavigationMenuItemContext,\n} from \"./navigation-menu-context.js\";\n\nconst isDev = process.env.NODE_ENV !== \"production\";\n\n/** `inline-start` / `inline-end` follow the panel's `direction` (conventions 13.1). */\nexport type NavigationMenuSide = AnchorSide;\n/**\n * Alignment of the panel along its side: inline-relative on a top / bottom side, the top /\n * bottom edges on the others.\n */\nexport type NavigationMenuAlign = AnchorAlign;\n\n/**\n * State of `NavigationMenu.Popup`, passed to the function forms of `className`, `style`,\n * `children` and `render`.\n */\nexport interface NavigationMenuPopupState {\n /**\n * Committed open state (library mode); `false` in JS-free mode. Read `:popover-open` for the\n * DOM truth.\n */\n readonly open: boolean;\n /**\n * While shown: the physical side the browser placed the panel on (after any fallback flip;\n * conventions 13.2). Otherwise the `side` prop. `null` with `side={null}`.\n */\n readonly side: NavigationMenuSide | null;\n /** While shown: the resolved alignment; otherwise the `align` prop. */\n readonly align: NavigationMenuAlign;\n}\n\n/**\n * Props of `NavigationMenu.Popup`.\n *\n * @see https://ui.funstack.work/components/navigation-menu\n */\nexport interface NavigationMenuPopupProps extends Omit<\n ComponentPropsWithRef<\"div\">,\n \"id\" | \"popover\" | \"onToggle\" | \"onBeforeToggle\" | \"className\" | \"style\" | \"children\"\n> {\n /**\n * Placement relative to the Trigger through inline `position-area`, with\n * `position-try-fallbacks` flipping it when it does not fit. `null` emits no inline positioning\n * (and no `data-side` / `data-align`), so a stylesheet owns placement (a full-width panel).\n *\n * @defaultValue `\"bottom\"`\n */\n side?: NavigationMenuSide | null;\n /**\n * Alignment along the side. On a top / bottom side `start` / `end` are inline-relative\n * (mirrored under `direction: rtl`). Ignored when `side` is `null`.\n *\n * @defaultValue `\"start\"`\n */\n align?: NavigationMenuAlign;\n /**\n * Library mode: what happens to the children while the panel is closed.\n * - `false` (default): not rendered. Mounting them is part of the opening transition, so\n * content that suspends holds the open (the Trigger is `data-pending`) until it is ready.\n * - `true`: rendered inside `<Activity mode=\"hidden\">`: DOM and state preserved, effects\n * stopped. Not in the server HTML.\n *\n * Ignored in JS-free mode (always mounted, in the server HTML).\n *\n * @defaultValue `false`\n */\n keepMounted?: boolean;\n /**\n * Library mode only. Forwarded to the `<ViewTransition default>` around the popover element.\n * Animates library-driven opens and closes (the Trigger, a followed Link, controlled changes);\n * dismissals animate with CSS only. Ignored (dev warning) in JS-free mode.\n *\n * Static: setting or unsetting it remounts the element.\n */\n transition?: ViewTransitionClass;\n /** Class name, or a function of the part's state that returns one. */\n className?: StateValue<string | undefined, NavigationMenuPopupState>;\n /** Inline style, or a function of the part's state that returns one. */\n style?: StateValue<CSSProperties | undefined, NavigationMenuPopupState>;\n /** The panel's links, or a function of the part's state. Library mode: may suspend. */\n children?: StateValue<ReactNode, NavigationMenuPopupState>;\n /** Replace the default `<div>` (element form) or take over rendering (function form). */\n render?: RenderProp<ComponentPropsWithRef<\"div\">, NavigationMenuPopupState>;\n}\n\n/** The panel is a `<div>` (design 1). */\nconst POPUP_ELEMENTS: readonly string[] = [\"DIV\"];\n\n/**\n * The panel: a `<div popover=\"auto\">` with no role (the disclosure pattern), placed against the\n * Trigger with CSS Anchor Positioning. Render it **immediately after** the Trigger in the same\n * Item: every engine then puts the panel's links right after the Trigger in the Tab order\n * (development warning otherwise). Sibling `auto` popovers close each other, so one panel is\n * open at a time.\n *\n * JS-free mode: the children are always mounted (in the server HTML). Library mode: they render\n * from committed state, so content that suspends holds the open with `data-pending` on the\n * Trigger; `showPopover({ source: trigger })` runs in the commit that mounted them. Style it with\n * `:popover-open`; it emits `data-side` / `data-align` but no `data-open`.\n *\n * Owned keys (conventions 1.2): `id`, `popover`, and `data-side` / `data-align` unless `side` is\n * `null`.\n *\n * @see https://ui.funstack.work/components/navigation-menu\n */\nexport function NavigationMenuPopup({\n side: sideProp = \"bottom\",\n align: alignProp = \"start\",\n keepMounted,\n transition,\n className,\n style,\n children,\n render,\n ...rest\n}: NavigationMenuPopupProps) {\n const ctx = useNavigationMenuItemContext(\"NavigationMenu.Popup\");\n const isLibrary = ctx.mode === \"library\";\n const { ids, popupRef, triggerRef } = ctx;\n\n useJsFreePropWarnings(\"NavigationMenu.Popup\", \"panel\", isLibrary, transition, keepMounted);\n\n const { setElement, renderContent } = usePopoverPopup(ctx, {\n part: \"NavigationMenu.Popup\",\n keepMounted,\n transition: isLibrary ? transition : undefined,\n elements: POPUP_ELEMENTS,\n // While shown, a primary press on any `popovertarget` / `commandfor` invoker of the panel\n // moves no focus: a foreign one outside the Item (the focus-leave close would otherwise hide\n // the panel before the invoker's own toggle), and the panel's own Trigger as well. The latter\n // is harmless and intended: focus is already inside the Item, and the hide returns it.\n whileShown: listenInvokerPresses,\n });\n\n // Development checks (design 7): one Popup per Item, directly after its Trigger. Runs before\n // `setElement`, which registers this element.\n const checkSecond = useElementCheck(\n isDev &&\n ((element) =>\n popupRef.current !== null && popupRef.current !== element\n ? \"NavigationMenu.Popup: a second NavigationMenu.Popup mounted in the same Item. An \" +\n \"Item has one Trigger and one Popup; give each panel its own NavigationMenu.Item.\"\n : null),\n );\n const self = useRef<HTMLElement | null>(null);\n useEffect(() => {\n if (!isDev) return;\n const element = self.current;\n const trigger = triggerRef.current;\n if (element === null || trigger === null || element.previousElementSibling === trigger) return;\n console.error(\n \"NavigationMenu.Popup: the Popup does not directly follow its NavigationMenu.Trigger. \" +\n \"Render the Popup immediately after the Trigger: Safari puts a popover's content after \" +\n \"its invoker in the Tab order only when the two are adjacent.\",\n );\n }, [triggerRef]);\n\n const placement = useAnchoredPlacement({ side: sideProp, align: alignProp });\n const { side, align } = placement;\n const ref = useMergedRefs<HTMLElement>(checkSecond, setElement, placement.ref, self);\n\n const state: NavigationMenuPopupState = { open: ctx.committed, side, align };\n\n const content = (s: NavigationMenuPopupState): ReactNode => (\n <NavigationMenuPopupContext value={ctx}>\n {renderContent(() => resolveStateValue(children, s))}\n </NavigationMenuPopupContext>\n );\n\n const libraryProps: ComponentPropsWithRef<\"div\"> & DataAttributes = {\n id: ids.popup,\n popover: \"auto\",\n // Not owned with `side={null}`: the stylesheet (or a render element) may set them.\n ...placement.dataAttributes,\n };\n\n const element = useRender<ComponentPropsWithRef<\"div\">, NavigationMenuPopupState>({\n defaultElement: \"div\",\n render,\n state,\n className,\n // The consumer's style wins over the inline placement.\n style: placement.style(style),\n children: content,\n props: rest,\n libraryProps,\n ref,\n });\n\n return useTransitionBoundary(\"NavigationMenu.Popup\", isLibrary ? transition : undefined, element);\n}\n\nNavigationMenuPopup.displayName = \"NavigationMenuPopup\";\n"],"mappings":";;;;;;;;;;;;;;AAmBA,MAAM,QAAQ,QAAQ,IAAI,aAAa;;AAoFvC,MAAM,iBAAoC,CAAC,KAAK;;;;;;;;;;;;;;;;;;AAmBhD,SAAgB,oBAAoB,EAClC,MAAM,WAAW,UACjB,OAAO,YAAY,SACnB,aACA,YACA,WACA,OACA,UACA,QACA,GAAG,QACwB;CAC3B,MAAM,MAAM,6BAA6B,sBAAsB;CAC/D,MAAM,YAAY,IAAI,SAAS;CAC/B,MAAM,EAAE,KAAK,UAAU,eAAe;CAEtC,sBAAsB,wBAAwB,SAAS,WAAW,YAAY,WAAW;CAEzF,MAAM,EAAE,YAAY,kBAAkB,gBAAgB,KAAK;EACzD,MAAM;EACN;EACA,YAAY,YAAY,aAAa,KAAA;EACrC,UAAU;EAKV,YAAY;CACd,CAAC;CAID,MAAM,cAAc,gBAClB,WACI,YACA,SAAS,YAAY,QAAQ,SAAS,YAAY,UAC9C,sKAEA,KACV;CACA,MAAM,OAAO,OAA2B,IAAI;CAC5C,gBAAgB;EACd,IAAI,CAAC,OAAO;EACZ,MAAM,UAAU,KAAK;EACrB,MAAM,UAAU,WAAW;EAC3B,IAAI,YAAY,QAAQ,YAAY,QAAQ,QAAQ,2BAA2B,SAAS;EACxF,QAAQ,MACN,yOAGF;CACF,GAAG,CAAC,UAAU,CAAC;CAEf,MAAM,YAAY,qBAAqB;EAAE,MAAM;EAAU,OAAO;CAAU,CAAC;CAC3E,MAAM,EAAE,MAAM,UAAU;CACxB,MAAM,MAAM,cAA2B,aAAa,YAAY,UAAU,KAAK,IAAI;CAEnF,MAAM,QAAkC;EAAE,MAAM,IAAI;EAAW;EAAM;CAAM;CAE3E,MAAM,WAAW,MACf,oBAAC,4BAAD;EAA4B,OAAO;EAChC,UAAA,oBAAoB,kBAAkB,UAAU,CAAC,CAAC;CACzB,CAAA;CAG9B,MAAM,eAA8D;EAClE,IAAI,IAAI;EACR,SAAS;EAET,GAAG,UAAU;CACf;CAEA,MAAM,UAAU,UAAkE;EAChF,gBAAgB;EAChB;EACA;EACA;EAEA,OAAO,UAAU,MAAM,KAAK;EAC5B,UAAU;EACV,OAAO;EACP;EACA;CACF,CAAC;CAED,OAAO,sBAAsB,wBAAwB,YAAY,aAAa,KAAA,GAAW,OAAO;AAClG;AAEA,oBAAoB,cAAc"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { RenderProp, StateValue } from "../internal/types.mjs";
|
|
2
|
+
import { CSSProperties, ComponentPropsWithRef, ReactNode } from "react";
|
|
3
|
+
//#region src/navigation-menu/navigation-menu-root.d.ts
|
|
4
|
+
/** `NavigationMenu.Root` has no state. */
|
|
5
|
+
export type NavigationMenuRootState = Record<string, never>;
|
|
6
|
+
/**
|
|
7
|
+
* Props of `NavigationMenu.Root`.
|
|
8
|
+
*
|
|
9
|
+
* @see https://ui.funstack.work/components/navigation-menu
|
|
10
|
+
*/
|
|
11
|
+
export interface NavigationMenuRootProps extends Omit<ComponentPropsWithRef<"nav">, "className" | "style" | "children"> {
|
|
12
|
+
/** Class name, or a function of the part's state that returns one. */
|
|
13
|
+
className?: StateValue<string | undefined, NavigationMenuRootState>;
|
|
14
|
+
/** Inline style, or a function of the part's state that returns one. */
|
|
15
|
+
style?: StateValue<CSSProperties | undefined, NavigationMenuRootState>;
|
|
16
|
+
/** Content: a `NavigationMenu.List`. */
|
|
17
|
+
children?: StateValue<ReactNode, NavigationMenuRootState>;
|
|
18
|
+
/** Replace the default `<nav>` (element form) or take over rendering (function form). */
|
|
19
|
+
render?: RenderProp<ComponentPropsWithRef<"nav">, NavigationMenuRootState>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The navigation landmark: a `<nav>`. Structure only, with no state and no owned keys. Name it
|
|
23
|
+
* with `aria-label` (or `aria-labelledby`) when the page has more than one `nav`.
|
|
24
|
+
* Directive-free, so a Server Component may render it.
|
|
25
|
+
*
|
|
26
|
+
* @see https://ui.funstack.work/components/navigation-menu
|
|
27
|
+
*/
|
|
28
|
+
export declare function NavigationMenuRoot({ className, style, children, render, ref, ...rest }: NavigationMenuRootProps): ReactNode;
|
|
29
|
+
export declare namespace NavigationMenuRoot {
|
|
30
|
+
var displayName: string;
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
//# sourceMappingURL=navigation-menu-root.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"navigation-menu-root.d.mts","names":[],"sources":["../../src/navigation-menu/navigation-menu-root.tsx"],"mappings":";;;;YAQY,0BAA0B;;;;;;iBASrB,gCAAgC,KAC/C;;EAIA,YAAY,+BAA+B;;EAE3C,QAAQ,WAAW,2BAA2B;;EAE9C,WAAW,WAAW,WAAW;;EAEjC,SAAS,WAAW,8BAA8B;;;;;;;;;wBAUpC,qBACd,WACA,OACA,UACA,QACA,QACG,QACF,0BAAuB;yBAPV"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { mergeRenderRef, renderElement } from "../internal/render-element.mjs";
|
|
2
|
+
//#region src/navigation-menu/navigation-menu-root.tsx
|
|
3
|
+
const EMPTY = {};
|
|
4
|
+
/**
|
|
5
|
+
* The navigation landmark: a `<nav>`. Structure only, with no state and no owned keys. Name it
|
|
6
|
+
* with `aria-label` (or `aria-labelledby`) when the page has more than one `nav`.
|
|
7
|
+
* Directive-free, so a Server Component may render it.
|
|
8
|
+
*
|
|
9
|
+
* @see https://ui.funstack.work/components/navigation-menu
|
|
10
|
+
*/
|
|
11
|
+
function NavigationMenuRoot({ className, style, children, render, ref, ...rest }) {
|
|
12
|
+
return renderElement({
|
|
13
|
+
defaultElement: "nav",
|
|
14
|
+
render,
|
|
15
|
+
state: EMPTY,
|
|
16
|
+
className,
|
|
17
|
+
style,
|
|
18
|
+
children,
|
|
19
|
+
props: rest,
|
|
20
|
+
ref: mergeRenderRef(ref, render)
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
NavigationMenuRoot.displayName = "NavigationMenuRoot";
|
|
24
|
+
//#endregion
|
|
25
|
+
export { NavigationMenuRoot };
|
|
26
|
+
|
|
27
|
+
//# sourceMappingURL=navigation-menu-root.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"navigation-menu-root.mjs","names":[],"sources":["../../src/navigation-menu/navigation-menu-root.tsx"],"sourcesContent":["// No \"use client\": `NavigationMenu.Root` reads no context and calls no hook, so a Server\n// Component may render it and it runs once, on the server (design/components/navigation-menu.md\n// 8). It must not import from any \"use client\" module (checked by src/__tests__/directives.test.ts).\nimport type { ComponentPropsWithRef, CSSProperties, ReactNode } from \"react\";\nimport { mergeRenderRef, renderElement } from \"../internal/render-element.js\";\nimport type { RenderProp, StateValue } from \"../internal/types.js\";\n\n/** `NavigationMenu.Root` has no state. */\nexport type NavigationMenuRootState = Record<string, never>;\n\nconst EMPTY: Record<string, never> = {};\n\n/**\n * Props of `NavigationMenu.Root`.\n *\n * @see https://ui.funstack.work/components/navigation-menu\n */\nexport interface NavigationMenuRootProps extends Omit<\n ComponentPropsWithRef<\"nav\">,\n \"className\" | \"style\" | \"children\"\n> {\n /** Class name, or a function of the part's state that returns one. */\n className?: StateValue<string | undefined, NavigationMenuRootState>;\n /** Inline style, or a function of the part's state that returns one. */\n style?: StateValue<CSSProperties | undefined, NavigationMenuRootState>;\n /** Content: a `NavigationMenu.List`. */\n children?: StateValue<ReactNode, NavigationMenuRootState>;\n /** Replace the default `<nav>` (element form) or take over rendering (function form). */\n render?: RenderProp<ComponentPropsWithRef<\"nav\">, NavigationMenuRootState>;\n}\n\n/**\n * The navigation landmark: a `<nav>`. Structure only, with no state and no owned keys. Name it\n * with `aria-label` (or `aria-labelledby`) when the page has more than one `nav`.\n * Directive-free, so a Server Component may render it.\n *\n * @see https://ui.funstack.work/components/navigation-menu\n */\nexport function NavigationMenuRoot({\n className,\n style,\n children,\n render,\n ref,\n ...rest\n}: NavigationMenuRootProps) {\n return renderElement<ComponentPropsWithRef<\"nav\">, NavigationMenuRootState>({\n defaultElement: \"nav\",\n render,\n state: EMPTY,\n className,\n style,\n children,\n props: rest,\n // Passed through to the element, never read during render.\n // oxlint-disable-next-line react/refs\n ref: mergeRenderRef(ref, render),\n });\n}\n\nNavigationMenuRoot.displayName = \"NavigationMenuRoot\";\n"],"mappings":";;AAUA,MAAM,QAA+B,CAAC;;;;;;;;AA4BtC,SAAgB,mBAAmB,EACjC,WACA,OACA,UACA,QACA,KACA,GAAG,QACuB;CAC1B,OAAO,cAAqE;EAC1E,gBAAgB;EAChB;EACA,OAAO;EACP;EACA;EACA;EACA,OAAO;EAGP,KAAK,eAAe,KAAK,MAAM;CACjC,CAAC;AACH;AAEA,mBAAmB,cAAc"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { PopoverTriggerProps, PopoverTriggerState } from "../popover/popover-trigger.mjs";
|
|
2
|
+
//#region src/navigation-menu/navigation-menu-trigger.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* State of `NavigationMenu.Trigger`: Popover.Trigger's (design 6), passed to the function forms
|
|
5
|
+
* of `className`, `style`, `children` and `render`.
|
|
6
|
+
*/
|
|
7
|
+
export type NavigationMenuTriggerState = PopoverTriggerState;
|
|
8
|
+
/**
|
|
9
|
+
* Props of `NavigationMenu.Trigger`: Popover.Trigger's, unchanged (design 6).
|
|
10
|
+
*
|
|
11
|
+
* @see https://ui.funstack.work/components/navigation-menu
|
|
12
|
+
*/
|
|
13
|
+
export type NavigationMenuTriggerProps = PopoverTriggerProps;
|
|
14
|
+
/**
|
|
15
|
+
* The toggle of one panel: `<button type="button" popovertarget>` with no `aria-expanded` (the
|
|
16
|
+
* browser exposes the popover's state on its invoker). Opens on a click (Enter, Space); never on
|
|
17
|
+
* hover. In JS-free mode the browser toggles the panel and the Popup keeps `data-open` current;
|
|
18
|
+
* in library mode the click is cancelled and dispatched through the Item's transition, with
|
|
19
|
+
* optimistic `data-open` and `data-pending` until the change has landed.
|
|
20
|
+
*
|
|
21
|
+
* Owned keys (conventions 1.2): Popover.Trigger's: `type`, `disabled`, `popoverTarget`,
|
|
22
|
+
* `aria-expanded`, `onClick` (library mode), `data-open`, `data-pending`, `data-disabled`;
|
|
23
|
+
* `commandfor`, `command` and `popoverTargetAction` reserved.
|
|
24
|
+
*
|
|
25
|
+
* @see https://ui.funstack.work/components/navigation-menu
|
|
26
|
+
*/
|
|
27
|
+
export declare function NavigationMenuTrigger({ disabled, className, style, children, render, ...rest }: NavigationMenuTriggerProps): import("react").ReactNode;
|
|
28
|
+
export declare namespace NavigationMenuTrigger {
|
|
29
|
+
var displayName: string;
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
//# sourceMappingURL=navigation-menu-trigger.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"navigation-menu-trigger.d.mts","names":[],"sources":["../../src/navigation-menu/navigation-menu-trigger.tsx"],"mappings":";;;;;;YAWY,6BAA6B;;;;;;YAO7B,6BAA6B;;;;;;;;;;;;;;wBAezB,wBACd,UACA,WACA,OACA,UACA,WACG,QACF,6CAA0B;yBAPb"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useRender } from "../internal/use-render.mjs";
|
|
3
|
+
import { useNavigationMenuItemContext } from "./navigation-menu-context.mjs";
|
|
4
|
+
import { usePopoverTrigger } from "../internal/overlay/use-popover-trigger.mjs";
|
|
5
|
+
//#region src/navigation-menu/navigation-menu-trigger.tsx
|
|
6
|
+
/**
|
|
7
|
+
* The toggle of one panel: `<button type="button" popovertarget>` with no `aria-expanded` (the
|
|
8
|
+
* browser exposes the popover's state on its invoker). Opens on a click (Enter, Space); never on
|
|
9
|
+
* hover. In JS-free mode the browser toggles the panel and the Popup keeps `data-open` current;
|
|
10
|
+
* in library mode the click is cancelled and dispatched through the Item's transition, with
|
|
11
|
+
* optimistic `data-open` and `data-pending` until the change has landed.
|
|
12
|
+
*
|
|
13
|
+
* Owned keys (conventions 1.2): Popover.Trigger's: `type`, `disabled`, `popoverTarget`,
|
|
14
|
+
* `aria-expanded`, `onClick` (library mode), `data-open`, `data-pending`, `data-disabled`;
|
|
15
|
+
* `commandfor`, `command` and `popoverTargetAction` reserved.
|
|
16
|
+
*
|
|
17
|
+
* @see https://ui.funstack.work/components/navigation-menu
|
|
18
|
+
*/
|
|
19
|
+
function NavigationMenuTrigger({ disabled = false, className, style, children, render, ...rest }) {
|
|
20
|
+
const ctx = useNavigationMenuItemContext("NavigationMenu.Trigger");
|
|
21
|
+
const { state, libraryProps, ref } = usePopoverTrigger(ctx, {
|
|
22
|
+
part: "NavigationMenu.Trigger",
|
|
23
|
+
disabled,
|
|
24
|
+
render
|
|
25
|
+
});
|
|
26
|
+
return useRender({
|
|
27
|
+
defaultElement: "button",
|
|
28
|
+
render,
|
|
29
|
+
state,
|
|
30
|
+
className,
|
|
31
|
+
style,
|
|
32
|
+
children,
|
|
33
|
+
props: rest,
|
|
34
|
+
libraryProps,
|
|
35
|
+
ref
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
NavigationMenuTrigger.displayName = "NavigationMenuTrigger";
|
|
39
|
+
//#endregion
|
|
40
|
+
export { NavigationMenuTrigger };
|
|
41
|
+
|
|
42
|
+
//# sourceMappingURL=navigation-menu-trigger.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"navigation-menu-trigger.mjs","names":[],"sources":["../../src/navigation-menu/navigation-menu-trigger.tsx"],"sourcesContent":["\"use client\";\nimport type { ComponentPropsWithRef } from \"react\";\nimport { usePopoverTrigger } from \"../internal/overlay/use-popover-trigger.js\";\nimport { useRender } from \"../internal/use-render.js\";\nimport type { PopoverTriggerProps, PopoverTriggerState } from \"../popover/popover-trigger.js\";\nimport { useNavigationMenuItemContext } from \"./navigation-menu-context.js\";\n\n/**\n * State of `NavigationMenu.Trigger`: Popover.Trigger's (design 6), passed to the function forms\n * of `className`, `style`, `children` and `render`.\n */\nexport type NavigationMenuTriggerState = PopoverTriggerState;\n\n/**\n * Props of `NavigationMenu.Trigger`: Popover.Trigger's, unchanged (design 6).\n *\n * @see https://ui.funstack.work/components/navigation-menu\n */\nexport type NavigationMenuTriggerProps = PopoverTriggerProps;\n\n/**\n * The toggle of one panel: `<button type=\"button\" popovertarget>` with no `aria-expanded` (the\n * browser exposes the popover's state on its invoker). Opens on a click (Enter, Space); never on\n * hover. In JS-free mode the browser toggles the panel and the Popup keeps `data-open` current;\n * in library mode the click is cancelled and dispatched through the Item's transition, with\n * optimistic `data-open` and `data-pending` until the change has landed.\n *\n * Owned keys (conventions 1.2): Popover.Trigger's: `type`, `disabled`, `popoverTarget`,\n * `aria-expanded`, `onClick` (library mode), `data-open`, `data-pending`, `data-disabled`;\n * `commandfor`, `command` and `popoverTargetAction` reserved.\n *\n * @see https://ui.funstack.work/components/navigation-menu\n */\nexport function NavigationMenuTrigger({\n disabled = false,\n className,\n style,\n children,\n render,\n ...rest\n}: NavigationMenuTriggerProps) {\n const ctx = useNavigationMenuItemContext(\"NavigationMenu.Trigger\");\n const { state, libraryProps, ref } = usePopoverTrigger(ctx, {\n part: \"NavigationMenu.Trigger\",\n disabled,\n render,\n });\n\n return useRender<ComponentPropsWithRef<\"button\">, NavigationMenuTriggerState>({\n defaultElement: \"button\",\n render,\n state,\n className,\n style,\n children,\n props: rest,\n libraryProps,\n ref,\n });\n}\n\nNavigationMenuTrigger.displayName = \"NavigationMenuTrigger\";\n"],"mappings":";;;;;;;;;;;;;;;;;;AAiCA,SAAgB,sBAAsB,EACpC,WAAW,OACX,WACA,OACA,UACA,QACA,GAAG,QAC0B;CAC7B,MAAM,MAAM,6BAA6B,wBAAwB;CACjE,MAAM,EAAE,OAAO,cAAc,QAAQ,kBAAkB,KAAK;EAC1D,MAAM;EACN;EACA;CACF,CAAC;CAED,OAAO,UAAuE;EAC5E,gBAAgB;EAChB;EACA;EACA;EACA;EACA;EACA,OAAO;EACP;EACA;CACF,CAAC;AACH;AAEA,sBAAsB,cAAc"}
|
|
@@ -106,6 +106,15 @@ export interface OTPFieldRootProps extends Omit<ComponentPropsWithRef<"div">, "c
|
|
|
106
106
|
* @defaultValue `false`
|
|
107
107
|
*/
|
|
108
108
|
readOnly?: boolean;
|
|
109
|
+
/**
|
|
110
|
+
* The native attribute, with a library default: a code is read left to right in every locale,
|
|
111
|
+
* so the Slots, the Input's text and its caret keys stay left to right inside an RTL page.
|
|
112
|
+
* Pass `"rtl"` for a code that is read right to left (the attribute has no value meaning
|
|
113
|
+
* "inherit"). A `dir` on a `render` element wins as well.
|
|
114
|
+
*
|
|
115
|
+
* @defaultValue `"ltr"`
|
|
116
|
+
*/
|
|
117
|
+
dir?: ComponentPropsWithRef<"div">["dir"];
|
|
109
118
|
/**
|
|
110
119
|
* Ask the browser for the code of an origin-bound SMS (WebOTP, `navigator.credentials.get({
|
|
111
120
|
* otp })`) while the Input is mounted. Chromium and Safari; nothing happens where the API is
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"otp-field-root.d.mts","names":[],"sources":["../../src/otp-field/otp-field-root.tsx"],"mappings":";;;;;iBA+CiB,mCAAmC;;;;;;WAMzC,QAAQ;;;iBAIF,gCAAgC;;;;;WAKtC,QAAQ;;;iBAIF;;WAEN;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;;;;;;iBAQM,0BAA0B,KACzC;;EAIA;;;;;;EAMA,UAAU;;EAEV;;;;;;EAMA;;;;;;EAMA,gBAAgB,4BAA4B;;;;;;;;EAQ5C,iBAAiB,QAAQ,eAAe,SAAS;;;;;;;;EAQjD;;;;;;EAMA;;EAEA;;;;;;EAMA;;;;;;EAMA;;;;;;EAMA;;;;;;;;
|
|
1
|
+
{"version":3,"file":"otp-field-root.d.mts","names":[],"sources":["../../src/otp-field/otp-field-root.tsx"],"mappings":";;;;;iBA+CiB,mCAAmC;;;;;;WAMzC,QAAQ;;;iBAIF,gCAAgC;;;;;WAKtC,QAAQ;;;iBAIF;;WAEN;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;;;;;;iBAQM,0BAA0B,KACzC;;EAIA;;;;;;EAMA,UAAU;;EAEV;;;;;;EAMA;;;;;;EAMA,gBAAgB,4BAA4B;;;;;;;;EAQ5C,iBAAiB,QAAQ,eAAe,SAAS;;;;;;;;EAQjD;;;;;;EAMA;;EAEA;;;;;;EAMA;;;;;;EAMA;;;;;;EAMA;;;;;;;;;EASA,MAAM;;;;;;;;EAQN;;EAEA,YAAY,+BAA+B;;EAE3C,QAAQ,WAAW,2BAA2B;;EAE9C,WAAW,WAAW,WAAW;;EAEjC,SAAS,WAAW,8BAA8B;;;;;;;;;;;;;;wBAiEpC,aAAa,OAAO,oCAAiB,IAAA;yBAArC"}
|
|
@@ -57,7 +57,7 @@ function slotIndexProblem(slots, length) {
|
|
|
57
57
|
* @see https://ui.funstack.work/components/otp-field
|
|
58
58
|
*/
|
|
59
59
|
function OTPFieldRoot(props) {
|
|
60
|
-
const { length: lengthProp, charset: charsetProp = "numeric", value: valueProp, defaultValue, onValueChange, completeAction, submitOnComplete = false, name: nameProp, form, required: requiredProp = false, disabled: disabledProp = false, readOnly = false, experimental_webOTP: experimentalWebOTP = false, className, style, children, render, ...rest } = props;
|
|
60
|
+
const { length: lengthProp, charset: charsetProp = "numeric", value: valueProp, defaultValue, onValueChange, completeAction, submitOnComplete = false, name: nameProp, form, required: requiredProp = false, disabled: disabledProp = false, readOnly = false, dir = "ltr", experimental_webOTP: experimentalWebOTP = false, className, style, children, render, ...rest } = props;
|
|
61
61
|
const lengthValid = Number.isInteger(lengthProp) && lengthProp > 0;
|
|
62
62
|
const length = lengthValid ? lengthProp : Math.max(1, Math.floor(lengthProp) || 1);
|
|
63
63
|
useDevWarning(!lengthValid, isDev && `OTPField.Root: \`length\` must be a positive integer; got ${String(lengthProp)}.`);
|
|
@@ -382,7 +382,10 @@ function OTPFieldRoot(props) {
|
|
|
382
382
|
...resolveStateValue(style, s)
|
|
383
383
|
}),
|
|
384
384
|
children,
|
|
385
|
-
props:
|
|
385
|
+
props: {
|
|
386
|
+
...rest,
|
|
387
|
+
dir
|
|
388
|
+
},
|
|
386
389
|
libraryProps
|
|
387
390
|
});
|
|
388
391
|
return /* @__PURE__ */ jsx(OTPFieldContext, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"otp-field-root.mjs","names":[],"sources":["../../src/otp-field/otp-field-root.tsx"],"sourcesContent":["\"use client\";\nimport {\n addTransitionType,\n use,\n useCallback,\n useLayoutEffect,\n useMemo,\n useRef,\n useState,\n} from \"react\";\nimport type {\n ChangeEvent,\n ComponentPropsWithRef,\n CSSProperties,\n ReactNode,\n RefObject,\n} from \"react\";\nimport { createActionDetails } from \"../internal/action/action-details.js\";\nimport { callUrgentHandler } from \"../internal/action/call-urgent-handler.js\";\nimport { useActionTrigger } from \"../internal/action/use-action-trigger.js\";\nimport { dataAttr } from \"../internal/data-attr.js\";\nimport { FieldContext } from \"../internal/form/field-context.js\";\nimport { resolveFieldDefaults, useFieldValueChange } from \"../internal/form/use-field-control.js\";\nimport { useFormReset, useResetTarget } from \"../internal/form/use-form-reset.js\";\nimport type { FormAssociatedElement } from \"../internal/form/use-form-owner-event.js\";\nimport { resolveStateValue } from \"../internal/render-element.js\";\nimport { transitionTypes } from \"../internal/transition-types.js\";\nimport type {\n Action,\n ActionDetails,\n ChangeReason,\n RenderProp,\n StateValue,\n UrgentChangeHandler,\n} from \"../internal/types.js\";\nimport { useDevWarning } from \"../internal/use-dev-warning.js\";\nimport { useLatest } from \"../internal/use-latest.js\";\nimport { useRender } from \"../internal/use-render.js\";\nimport type { DataAttributes } from \"../internal/use-render.js\";\nimport { OTPFieldContext } from \"./otp-field-context.js\";\nimport type { OTPFieldContextValue } from \"./otp-field-context.js\";\nimport { activeRange, applyEdit, filterChars, normalizeCode } from \"./otp-field-filter.js\";\nimport type { EditBefore, OTPFieldCharset } from \"./otp-field-filter.js\";\n\nconst isDev = process.env.NODE_ENV !== \"production\";\n\n/** Details passed as the second argument of `onValueChange`. */\nexport interface OTPFieldValueChangeDetails extends ActionDetails {\n /**\n * `\"activate\"`: the user edited the text (`details.event` is the `input` event, or the\n * `compositionend` that ended a composition). `\"platform\"`: a form reset (the `reset` event),\n * text adopted at hydration or a WebOTP fill (`undefined`).\n */\n readonly reason: ChangeReason;\n}\n\n/** Details passed as the second argument of `completeAction`. */\nexport interface OTPFieldCompleteDetails extends ActionDetails {\n /**\n * `\"activate\"`: an edit completed the code (typing, paste, keyboard autofill; `details.event`\n * is the `input` event). `\"platform\"`: a WebOTP fill (`details.event` is `undefined`).\n */\n readonly reason: ChangeReason;\n}\n\n/** The state the Root's and the Input's function props receive. */\nexport interface OTPFieldState {\n /** The text shown, already filtered: at most `length` characters of `charset`. */\n readonly value: string;\n /** The number of characters in a complete code. */\n readonly length: number;\n /** `value.length === length`. */\n readonly complete: boolean;\n /** A `completeAction` is in flight. */\n readonly pending: boolean;\n /** The `disabled` prop, or the enclosing Field's. */\n readonly disabled: boolean;\n /** The `readOnly` prop. */\n readonly readOnly: boolean;\n /** Inside a `Field.Root`: an error is shown for the field. `false` outside. */\n readonly invalid: boolean;\n}\n\n/**\n * Props of `OTPField.Root`.\n *\n * @see https://ui.funstack.work/components/otp-field\n */\nexport interface OTPFieldRootProps extends Omit<\n ComponentPropsWithRef<\"div\">,\n \"className\" | \"style\" | \"children\" | \"defaultValue\"\n> {\n /** The number of characters in a complete code. */\n length: number;\n /**\n * Which characters a code may contain; also picks the Input's `inputmode` and `pattern`.\n *\n * @defaultValue `\"numeric\"`\n */\n charset?: OTPFieldCharset;\n /** Controlled text. Follows the input: it cannot hold an edit back (the urgent lane). */\n value?: string;\n /**\n * Initial text in uncontrolled mode (filtered), and the form-reset target.\n *\n * @defaultValue `\"\"`\n */\n defaultValue?: string;\n /**\n * The text changed: an edit (`\"activate\"`), a form reset, text adopted at hydration or a WebOTP\n * fill (`\"platform\"`). A fact: called synchronously, outside any transition, not awaited.\n * Called before `completeAction` for the same edit.\n */\n onValueChange?: UrgentChangeHandler<string, OTPFieldValueChangeDetails>;\n /**\n * An edit left the code complete: runs in the Root's transition and is awaited, with\n * `data-pending` on the Root and the Input. Runs for every such edit, also while an earlier run\n * is pending (a different code), and for the same code pasted again. Not at mount, on a reset,\n * for a controlled change, or while disabled or read-only. A rejection reaches the nearest Error\n * Boundary. May be a Server Function.\n */\n completeAction?: Action<[value: string, details: OTPFieldCompleteDetails]>;\n /**\n * Submit the Input's form (`requestSubmit()`) when an edit leaves the code complete, right\n * after dispatching `completeAction`. A change of context on input (WCAG 3.2.2): say so before\n * the field.\n *\n * @defaultValue `false`\n */\n submitOnComplete?: boolean;\n /**\n * Native `name` on the Input.\n *\n * @defaultValue the Field's `name` inside a `Field.Root`\n */\n name?: string;\n /** Native `form` on the Input. */\n form?: string;\n /**\n * Native `required` on the Input; or the Field's.\n *\n * @defaultValue `false`\n */\n required?: boolean;\n /**\n * Native `disabled` on the Input; or the Field's.\n *\n * @defaultValue `false`\n */\n disabled?: boolean;\n /**\n * Native `readonly` on the Input: no edits, no completion.\n *\n * @defaultValue `false`\n */\n readOnly?: boolean;\n /**\n * Ask the browser for the code of an origin-bound SMS (WebOTP, `navigator.credentials.get({\n * otp })`) while the Input is mounted. Chromium and Safari; nothing happens where the API is\n * missing (Firefox).\n *\n * @defaultValue `false`\n */\n experimental_webOTP?: boolean;\n /** Class name, or a function of the state that returns one. */\n className?: StateValue<string | undefined, OTPFieldState>;\n /** Inline style, or a function of the state that returns one. `position: relative` unless given. */\n style?: StateValue<CSSProperties | undefined, OTPFieldState>;\n /** The parts. The function form receives the state. */\n children?: StateValue<ReactNode, OTPFieldState>;\n /** Replace the default `<div>` (element form) or take over rendering (function form). */\n render?: RenderProp<ComponentPropsWithRef<\"div\">, OTPFieldState>;\n}\n\nconst COMPLETE_TYPES = transitionTypes(\"otp-field\", \"complete\");\n\ninterface Selection {\n readonly focused: boolean;\n readonly start: number | null;\n readonly end: number | null;\n}\n\nconst BLURRED: Selection = { focused: false, start: null, end: null };\n\nfunction isFocused(input: HTMLInputElement): boolean {\n return input.ownerDocument.activeElement === input || input.matches(\":focus\");\n}\n\n/** Writes `value` into the input when it differs and puts the selection at `[start, end]`. */\nfunction writeInput(\n input: HTMLInputElement,\n value: string,\n start: number | null,\n end: number | null = start,\n): void {\n if (input.value !== value) input.value = value;\n if (start === null || end === null || !isFocused(input)) return;\n if (input.selectionStart !== start || input.selectionEnd !== end) {\n input.setSelectionRange(start, end);\n }\n}\n\n/** The development check of the mounted Slots' indices (design 1). */\nfunction slotIndexProblem(slots: Map<HTMLElement, number>, length: number): string | null {\n if (slots.size === 0) return null;\n const seen = new Set<number>();\n for (const index of slots.values()) {\n if (!Number.isInteger(index) || index < 0 || index >= length) {\n return `OTPField.Slot: index ${index} is outside 0 to ${length - 1} (length ${length}).`;\n }\n if (seen.has(index)) return `OTPField.Slot: index ${index} is used by more than one Slot.`;\n seen.add(index);\n }\n for (let i = 0; i < length; i++) {\n if (!seen.has(i)) {\n return (\n `OTPField.Slot: no Slot has index ${i} (length ${length}). Render one Slot per ` +\n \"position, or the code's characters are not all shown.\"\n );\n }\n }\n return null;\n}\n\n/**\n * Owns the text of a one-time-code field (design/components/otp-field.md): the urgent value\n * (`value` / `defaultValue` / `onValueChange`), the filter that keeps it a code, the selection the\n * Slots draw, the completion transition (`completeAction`, `submitOnComplete`), form reset,\n * hydration adoption and WebOTP. Renders a `div` with `position: relative`: the box the Input\n * covers.\n *\n * Owned keys (conventions 1.2): `data-complete`, `data-pending`, `data-disabled`,\n * `data-invalid` (inside a Field); `position` in `style` unless the consumer's style sets it.\n *\n * @see https://ui.funstack.work/components/otp-field\n */\nexport function OTPFieldRoot(props: OTPFieldRootProps) {\n const {\n length: lengthProp,\n charset: charsetProp = \"numeric\",\n value: valueProp,\n defaultValue,\n onValueChange,\n completeAction,\n submitOnComplete = false,\n name: nameProp,\n form,\n required: requiredProp = false,\n disabled: disabledProp = false,\n readOnly = false,\n experimental_webOTP: experimentalWebOTP = false,\n className,\n style,\n children,\n render,\n ...rest\n } = props;\n\n const lengthValid = Number.isInteger(lengthProp) && lengthProp > 0;\n const length = lengthValid ? lengthProp : Math.max(1, Math.floor(lengthProp) || 1);\n useDevWarning(\n !lengthValid,\n isDev && `OTPField.Root: \\`length\\` must be a positive integer; got ${String(lengthProp)}.`,\n );\n const charset: OTPFieldCharset =\n charsetProp === \"alphanumeric\" || charsetProp === \"alphabetic\" ? charsetProp : \"numeric\";\n\n // --- Field ----------------------------------------------------------------------------------\n\n // OTPField.Input registers the control (design 7); the Root resolves the defaults.\n const fieldContext = use(FieldContext);\n const { disabled, required, name } = resolveFieldDefaults(fieldContext, {\n name: nameProp,\n disabled: disabledProp,\n required: requiredProp,\n });\n const fieldInvalid = fieldContext === null ? undefined : fieldContext.state.invalid;\n\n // --- The text (design 3.2) ------------------------------------------------------------------\n\n const controlled = valueProp !== undefined;\n const controlledValue = controlled ? normalizeCode(valueProp, charset, length) : undefined;\n useDevWarning(\n controlled && controlledValue !== valueProp,\n isDev &&\n `OTPField.Root: the controlled \\`value\\` (\"${valueProp ?? \"\"}\") holds characters outside ` +\n `\\`charset\\` (\"${charset}\") or more than \\`length\\` (${length}); it is rendered as ` +\n `\"${controlledValue ?? \"\"}\".`,\n );\n const [internalValue, setInternalValue] = useState(() =>\n normalizeCode(defaultValue ?? \"\", charset, length),\n );\n const value = controlledValue ?? internalValue;\n const valueRef = useRef(value);\n // The latest value for handlers between two renders (two edits in one task).\n useLayoutEffect(() => {\n valueRef.current = value;\n });\n\n /** The raw text while a composition is open (design 4.1, Composition): rendered as is. */\n const [composingText, setComposingText] = useState<string | null>(null);\n const composingRef = useRef(false);\n const [selection, setSelection] = useState<Selection>(BLURRED);\n\n const inputRef = useRef<HTMLInputElement | null>(null);\n const beforeRef = useRef<EditBefore | null>(null);\n /** The value change the Field is told about next was an edit (design 7: `takeEdit`). */\n const editRef = useRef(false);\n const webOTPAbort = useRef<AbortController | null>(null);\n /** A completion happened: no WebOTP request starts any more, and a late code is ignored. */\n const webOTPDone = useRef(false);\n /** `submitOnComplete` was held back by an open composition. */\n const submitAfterComposition = useRef(false);\n\n const { pending, run } = useActionTrigger();\n\n // The latest committed props and `run`, for the stable callbacks below. Named `*Ref` so that\n // React Compiler's lint treats `.current` as a ref read, not a missing memo dependency.\n // `run` is a new function on every render: reading it through the ref keeps every handler,\n // the context and the Input's listeners and WebOTP request stable across renders.\n const propsRef = useLatest({\n charset,\n length,\n disabled,\n readOnly,\n controlled,\n onValueChange,\n completeAction,\n submitOnComplete,\n run,\n notifyField: fieldContext?.notifyChange,\n });\n\n // --- Writes ---------------------------------------------------------------------------------\n\n /** Writes the value (urgent) and reports it to `onValueChange` (design 3.2). */\n const commitValue = useCallback(\n (next: string, reason: ChangeReason, event: Event | undefined, edit: boolean) => {\n const props = propsRef.current;\n editRef.current = edit;\n // Controlled: the value is the parent's until it renders it (it may not adopt the edit).\n if (!props.controlled) {\n valueRef.current = next;\n setInternalValue(next);\n }\n callUrgentHandler(\n props.onValueChange,\n next,\n createActionDetails(event, { reason }) as OTPFieldValueChangeDetails,\n );\n },\n [propsRef],\n );\n\n /** Submits the Input's form, after the Field re-validated the text just written (design 3.1). */\n const submit = useCallback(() => {\n const form = inputRef.current?.form;\n if (form == null) return;\n // The Field validated the browser's pre-filter text in its `input` listener; the commit\n // that re-validates the written text comes after this event. Refresh it now.\n propsRef.current.notifyField?.(true);\n form.requestSubmit();\n }, [propsRef]);\n\n /** The code is complete after an edit or a WebOTP fill (design 3.1). */\n const complete = useCallback(\n (code: string, reason: ChangeReason, event: Event | undefined, composing = false) => {\n const {\n disabled: off,\n readOnly: ro,\n completeAction: action,\n submitOnComplete: submitting,\n run: runAction,\n } = propsRef.current;\n if (off || ro) return;\n // A code completed by hand makes a waiting WebOTP request moot (design 8), and no new one\n // starts after it.\n webOTPDone.current = true;\n webOTPAbort.current?.abort();\n webOTPAbort.current = null;\n if (action !== undefined) {\n const details = createActionDetails(event, { reason }) as OTPFieldCompleteDetails;\n runAction(() => {\n for (const type of COMPLETE_TYPES) addTransitionType(type);\n return action(code, details);\n });\n }\n if (!submitting) return;\n // The IME has not committed the text yet: submit at `compositionend` (design 4.1).\n if (composing) submitAfterComposition.current = true;\n else submit();\n },\n [propsRef, submit],\n );\n\n const readSelection = useCallback(() => {\n const input = inputRef.current;\n const next: Selection =\n input === null || !isFocused(input)\n ? BLURRED\n : { focused: true, start: input.selectionStart, end: input.selectionEnd };\n setSelection((prev) =>\n prev.focused === next.focused && prev.start === next.start && prev.end === next.end\n ? prev\n : next,\n );\n }, []);\n\n // --- Editing (design 4.1) -------------------------------------------------------------------\n\n const beforeInput = useCallback((event: InputEvent) => {\n const input = event.target as HTMLInputElement;\n beforeRef.current = {\n text: input.value,\n selectionStart: input.selectionStart ?? input.value.length,\n selectionEnd: input.selectionEnd ?? input.value.length,\n };\n }, []);\n\n const change = useCallback(\n (event: ChangeEvent<HTMLInputElement>) => {\n const input = event.currentTarget;\n const native = event.nativeEvent as Partial<InputEvent> & Event;\n const before = beforeRef.current;\n beforeRef.current = null;\n const { charset: cs, length: len, disabled: off, readOnly: ro } = propsRef.current;\n if (off || ro) return;\n\n if (composingRef.current || native.isComposing === true) {\n // Derive always, write when allowed: the composition keeps its text in the input.\n const next = normalizeCode(input.value, cs, len);\n setComposingText(input.value);\n readSelection();\n if (next === valueRef.current) return;\n commitValue(next, \"activate\", native, true);\n if (next.length === len) complete(next, \"activate\", native, true);\n return;\n }\n\n const result = applyEdit({\n before,\n dom: input.value,\n caret: input.selectionStart,\n inputType: native.inputType ?? \"\",\n charset: cs,\n length: len,\n });\n setComposingText(null);\n if (result.selection !== null) {\n writeInput(input, result.value, result.selection[0], result.selection[1]);\n } else {\n writeInput(input, result.value, result.caret);\n }\n readSelection();\n const changed = result.value !== valueRef.current;\n if (changed) commitValue(result.value, \"activate\", native, true);\n if (result.value.length === len && (changed || result.kept > 0)) {\n complete(result.value, \"activate\", native);\n }\n },\n [commitValue, complete, propsRef, readSelection],\n );\n\n const compositionStart = useCallback(() => {\n composingRef.current = true;\n }, []);\n\n const compositionEnd = useCallback(\n (event: CompositionEvent) => {\n composingRef.current = false;\n const input = inputRef.current;\n setComposingText(null);\n if (input === null) return;\n const { charset: cs, length: len } = propsRef.current;\n const dom = input.value;\n const next = normalizeCode(dom, cs, len);\n const caret = Math.min(\n filterChars(dom.slice(0, input.selectionStart ?? dom.length), cs).length,\n next.length,\n );\n writeInput(input, next, caret);\n readSelection();\n const held = submitAfterComposition.current;\n submitAfterComposition.current = false;\n if (next !== valueRef.current) {\n commitValue(next, \"activate\", event, true);\n if (next.length === len) complete(next, \"activate\", event);\n return;\n }\n // The code was completed while composing: the submission held back then goes now.\n if (held && next.length === len && propsRef.current.submitOnComplete) submit();\n },\n [commitValue, complete, propsRef, readSelection, submit],\n );\n\n const blurred = useCallback(() => {\n setSelection(BLURRED);\n }, []);\n\n // --- The pointer (design 4.3) ---------------------------------------------------------------\n\n const slots = useRef(new Map<HTMLElement, number>());\n const registerSlot = useCallback((index: number, element: HTMLElement) => {\n slots.current.set(element, index);\n return () => {\n if (slots.current.get(element) === index) slots.current.delete(element);\n };\n }, []);\n\n const click = useCallback((event: MouseEvent) => {\n const input = inputRef.current;\n // A double click selects; a drag made a range: the browser's selection stands.\n if (input === null || event.detail > 1 || input.selectionStart !== input.selectionEnd) return;\n let target: number | null = null;\n let distance = Infinity;\n for (const [element, index] of slots.current) {\n const box = element.getBoundingClientRect();\n if (box.width === 0 && box.height === 0) continue;\n const dx = Math.max(box.left - event.clientX, 0, event.clientX - box.right);\n const dy = Math.max(box.top - event.clientY, 0, event.clientY - box.bottom);\n const d = dx * dx + dy * dy;\n if (d < distance) {\n distance = d;\n target = index;\n }\n }\n if (target === null) return;\n const caret = Math.min(target, input.value.length);\n if (input.selectionStart !== caret || input.selectionEnd !== caret) {\n input.setSelectionRange(caret, caret);\n }\n }, []);\n\n // --- Residual paths: hydration, reset, WebOTP (design 7, 8, 9) -----------------------------\n\n const inputMounted = useCallback(() => {\n const input = inputRef.current;\n if (input === null) return;\n const { charset: cs, length: len } = propsRef.current;\n const dom = input.value;\n const next = normalizeCode(dom, cs, len);\n if (dom !== next) input.value = next;\n // No completion for adopted text (design 9): a restored code is not a fresh one.\n if (next !== valueRef.current) commitValue(next, \"platform\", undefined, false);\n }, [commitValue, propsRef]);\n\n const resetTarget = normalizeCode(useResetTarget(valueProp, defaultValue) ?? \"\", charset, length);\n const [resync, setResync] = useState(0);\n const inputText = composingText ?? value;\n const inputTextRef = useLatest(inputText);\n useLayoutEffect(() => {\n // A controlled parent that did not adopt the reset gets the DOM put back.\n const input = inputRef.current;\n if (resync > 0 && input !== null && input.value !== inputTextRef.current) {\n input.value = inputTextRef.current;\n }\n }, [resync, inputTextRef]);\n useFormReset(\n inputRef as RefObject<FormAssociatedElement | null>,\n (event) => {\n composingRef.current = false;\n setComposingText(null);\n // The target is computed, not read from the input: after a click on a reset button this\n // microtask runs before the browser's reset algorithm (there is a microtask checkpoint\n // after each `reset` listener), so the input may still hold the old text here.\n if (resetTarget !== valueRef.current) commitValue(resetTarget, \"platform\", event, false);\n setResync((n) => n + 1);\n },\n () => {\n // react-dom rewrites a controlled input's `defaultValue` after every update; the reset\n // target is written right before the browser's reset algorithm reads it.\n const input = inputRef.current;\n if (input !== null && input.defaultValue !== resetTarget) input.defaultValue = resetTarget;\n },\n );\n\n const webOTPCode = useCallback(\n (code: string) => {\n const { charset: cs, length: len, disabled: off, readOnly: ro } = propsRef.current;\n if (off || ro || webOTPDone.current) return;\n const next = filterChars(code, cs);\n if (next.length !== len) return;\n // Written now, so that `submitOnComplete` posts it before React renders.\n const input = inputRef.current;\n if (input !== null) input.value = next;\n if (next !== valueRef.current) commitValue(next, \"platform\", undefined, true);\n complete(next, \"platform\", undefined);\n },\n [commitValue, complete, propsRef],\n );\n\n // A write-back or a WebOTP fill changed the value with no event the Field heard after it.\n useFieldValueChange(value, () => {\n const edit = editRef.current;\n editRef.current = false;\n return edit;\n });\n\n // --- Development warnings -------------------------------------------------------------------\n\n const slotWarned = useRef(false);\n useLayoutEffect(() => {\n if (!isDev || slotWarned.current) return;\n const problem = slotIndexProblem(slots.current, length);\n if (problem === null) return;\n slotWarned.current = true;\n console.error(problem);\n });\n\n // --- Render ---------------------------------------------------------------------------------\n\n const state = useMemo<OTPFieldState>(\n () => ({\n value,\n length,\n complete: value.length === length,\n pending,\n disabled,\n readOnly,\n invalid: fieldInvalid === true,\n }),\n [value, length, pending, disabled, readOnly, fieldInvalid],\n );\n\n const [activeFrom, activeTo] = activeRange(\n selection.focused,\n selection.start,\n selection.end,\n length,\n );\n\n const context = useMemo<OTPFieldContextValue>(\n () => ({\n state,\n charset,\n name,\n form,\n required,\n fieldInvalid,\n inputText,\n active: [activeFrom, activeTo],\n experimentalWebOTP,\n inputRef,\n change,\n beforeInput,\n compositionStart,\n compositionEnd,\n selectionChanged: readSelection,\n blurred,\n click,\n inputMounted,\n webOTPCode,\n registerSlot,\n webOTPAbort,\n webOTPDone,\n }),\n [\n state,\n charset,\n name,\n form,\n required,\n fieldInvalid,\n inputText,\n activeFrom,\n activeTo,\n experimentalWebOTP,\n change,\n beforeInput,\n compositionStart,\n compositionEnd,\n readSelection,\n blurred,\n click,\n inputMounted,\n webOTPCode,\n registerSlot,\n ],\n );\n\n const libraryProps: Partial<ComponentPropsWithRef<\"div\">> & DataAttributes = {\n ...(fieldContext !== null ? { \"data-invalid\": dataAttr(fieldInvalid === true) } : {}),\n \"data-complete\": dataAttr(state.complete),\n \"data-pending\": dataAttr(pending),\n \"data-disabled\": dataAttr(disabled),\n };\n\n const element = useRender<ComponentPropsWithRef<\"div\">, OTPFieldState>({\n defaultElement: \"div\",\n render,\n state,\n className,\n // `position: relative` is the Input's containing block (design 6), unless the consumer's\n // style places the Root itself.\n style: (s) => ({ position: \"relative\", ...resolveStateValue(style, s) }),\n children,\n props: rest,\n libraryProps,\n });\n\n return <OTPFieldContext value={context}>{element}</OTPFieldContext>;\n}\n\nOTPFieldRoot.displayName = \"OTPFieldRoot\";\n"],"mappings":";;;;;;;;;;;;;;;;;;AA4CA,MAAM,QAAQ,QAAQ,IAAI,aAAa;AAkIvC,MAAM,iBAAiB,gBAAgB,aAAa,UAAU;AAQ9D,MAAM,UAAqB;CAAE,SAAS;CAAO,OAAO;CAAM,KAAK;AAAK;AAEpE,SAAS,UAAU,OAAkC;CACnD,OAAO,MAAM,cAAc,kBAAkB,SAAS,MAAM,QAAQ,QAAQ;AAC9E;;AAGA,SAAS,WACP,OACA,OACA,OACA,MAAqB,OACf;CACN,IAAI,MAAM,UAAU,OAAO,MAAM,QAAQ;CACzC,IAAI,UAAU,QAAQ,QAAQ,QAAQ,CAAC,UAAU,KAAK,GAAG;CACzD,IAAI,MAAM,mBAAmB,SAAS,MAAM,iBAAiB,KAC3D,MAAM,kBAAkB,OAAO,GAAG;AAEtC;;AAGA,SAAS,iBAAiB,OAAiC,QAA+B;CACxF,IAAI,MAAM,SAAS,GAAG,OAAO;CAC7B,MAAM,uBAAO,IAAI,IAAY;CAC7B,KAAK,MAAM,SAAS,MAAM,OAAO,GAAG;EAClC,IAAI,CAAC,OAAO,UAAU,KAAK,KAAK,QAAQ,KAAK,SAAS,QACpD,OAAO,wBAAwB,MAAM,mBAAmB,SAAS,EAAE,WAAW,OAAO;EAEvF,IAAI,KAAK,IAAI,KAAK,GAAG,OAAO,wBAAwB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CACT,CAAC;CAID,MAAM,aAAa,OAAO,KAAK;CAC/B,sBAAsB;EACpB,IAAI,CAAC,SAAS,WAAW,SAAS;EAClC,MAAM,UAAU,iBAAiB,MAAM,SAAS,MAAM;EACtD,IAAI,YAAY,MAAM;EACtB,WAAW,UAAU;EACrB,QAAQ,MAAM,OAAO;CACvB,CAAC;CAID,MAAM,QAAQ,eACL;EACL;EACA;EACA,UAAU,MAAM,WAAW;EAC3B;EACA;EACA;EACA,SAAS,iBAAiB;CAC5B,IACA;EAAC;EAAO;EAAQ;EAAS;EAAU;EAAU;CAAY,CAC3D;CAEA,MAAM,CAAC,YAAY,YAAY,YAC7B,UAAU,SACV,UAAU,OACV,UAAU,KACV,MACF;CAEA,MAAM,UAAU,eACP;EACL;EACA;EACA;EACA;EACA;EACA;EACA;EACA,QAAQ,CAAC,YAAY,QAAQ;EAC7B;EACA;EACA;EACA;EACA;EACA;EACA,kBAAkB;EAClB;EACA;EACA;EACA;EACA;EACA;EACA;CACF,IACA;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACF,CACF;CAEA,MAAM,eAAuE;EAC3E,GAAI,iBAAiB,OAAO,EAAE,gBAAgB,SAAS,iBAAiB,IAAI,EAAE,IAAI,CAAC;EACnF,iBAAiB,SAAS,MAAM,QAAQ;EACxC,gBAAgB,SAAS,OAAO;EAChC,iBAAiB,SAAS,QAAQ;CACpC;CAEA,MAAM,UAAU,UAAuD;EACrE,gBAAgB;EAChB;EACA;EACA;EAGA,QAAQ,OAAO;GAAE,UAAU;GAAY,GAAG,kBAAkB,OAAO,CAAC;EAAE;EACtE;EACA,OAAO;EACP;CACF,CAAC;CAED,OAAO,oBAAC,iBAAD;EAAiB,OAAO;EAAU,UAAA;CAAyB,CAAA;AACpE;AAEA,aAAa,cAAc"}
|
|
1
|
+
{"version":3,"file":"otp-field-root.mjs","names":[],"sources":["../../src/otp-field/otp-field-root.tsx"],"sourcesContent":["\"use client\";\nimport {\n addTransitionType,\n use,\n useCallback,\n useLayoutEffect,\n useMemo,\n useRef,\n useState,\n} from \"react\";\nimport type {\n ChangeEvent,\n ComponentPropsWithRef,\n CSSProperties,\n ReactNode,\n RefObject,\n} from \"react\";\nimport { createActionDetails } from \"../internal/action/action-details.js\";\nimport { callUrgentHandler } from \"../internal/action/call-urgent-handler.js\";\nimport { useActionTrigger } from \"../internal/action/use-action-trigger.js\";\nimport { dataAttr } from \"../internal/data-attr.js\";\nimport { FieldContext } from \"../internal/form/field-context.js\";\nimport { resolveFieldDefaults, useFieldValueChange } from \"../internal/form/use-field-control.js\";\nimport { useFormReset, useResetTarget } from \"../internal/form/use-form-reset.js\";\nimport type { FormAssociatedElement } from \"../internal/form/use-form-owner-event.js\";\nimport { resolveStateValue } from \"../internal/render-element.js\";\nimport { transitionTypes } from \"../internal/transition-types.js\";\nimport type {\n Action,\n ActionDetails,\n ChangeReason,\n RenderProp,\n StateValue,\n UrgentChangeHandler,\n} from \"../internal/types.js\";\nimport { useDevWarning } from \"../internal/use-dev-warning.js\";\nimport { useLatest } from \"../internal/use-latest.js\";\nimport { useRender } from \"../internal/use-render.js\";\nimport type { DataAttributes } from \"../internal/use-render.js\";\nimport { OTPFieldContext } from \"./otp-field-context.js\";\nimport type { OTPFieldContextValue } from \"./otp-field-context.js\";\nimport { activeRange, applyEdit, filterChars, normalizeCode } from \"./otp-field-filter.js\";\nimport type { EditBefore, OTPFieldCharset } from \"./otp-field-filter.js\";\n\nconst isDev = process.env.NODE_ENV !== \"production\";\n\n/** Details passed as the second argument of `onValueChange`. */\nexport interface OTPFieldValueChangeDetails extends ActionDetails {\n /**\n * `\"activate\"`: the user edited the text (`details.event` is the `input` event, or the\n * `compositionend` that ended a composition). `\"platform\"`: a form reset (the `reset` event),\n * text adopted at hydration or a WebOTP fill (`undefined`).\n */\n readonly reason: ChangeReason;\n}\n\n/** Details passed as the second argument of `completeAction`. */\nexport interface OTPFieldCompleteDetails extends ActionDetails {\n /**\n * `\"activate\"`: an edit completed the code (typing, paste, keyboard autofill; `details.event`\n * is the `input` event). `\"platform\"`: a WebOTP fill (`details.event` is `undefined`).\n */\n readonly reason: ChangeReason;\n}\n\n/** The state the Root's and the Input's function props receive. */\nexport interface OTPFieldState {\n /** The text shown, already filtered: at most `length` characters of `charset`. */\n readonly value: string;\n /** The number of characters in a complete code. */\n readonly length: number;\n /** `value.length === length`. */\n readonly complete: boolean;\n /** A `completeAction` is in flight. */\n readonly pending: boolean;\n /** The `disabled` prop, or the enclosing Field's. */\n readonly disabled: boolean;\n /** The `readOnly` prop. */\n readonly readOnly: boolean;\n /** Inside a `Field.Root`: an error is shown for the field. `false` outside. */\n readonly invalid: boolean;\n}\n\n/**\n * Props of `OTPField.Root`.\n *\n * @see https://ui.funstack.work/components/otp-field\n */\nexport interface OTPFieldRootProps extends Omit<\n ComponentPropsWithRef<\"div\">,\n \"className\" | \"style\" | \"children\" | \"defaultValue\"\n> {\n /** The number of characters in a complete code. */\n length: number;\n /**\n * Which characters a code may contain; also picks the Input's `inputmode` and `pattern`.\n *\n * @defaultValue `\"numeric\"`\n */\n charset?: OTPFieldCharset;\n /** Controlled text. Follows the input: it cannot hold an edit back (the urgent lane). */\n value?: string;\n /**\n * Initial text in uncontrolled mode (filtered), and the form-reset target.\n *\n * @defaultValue `\"\"`\n */\n defaultValue?: string;\n /**\n * The text changed: an edit (`\"activate\"`), a form reset, text adopted at hydration or a WebOTP\n * fill (`\"platform\"`). A fact: called synchronously, outside any transition, not awaited.\n * Called before `completeAction` for the same edit.\n */\n onValueChange?: UrgentChangeHandler<string, OTPFieldValueChangeDetails>;\n /**\n * An edit left the code complete: runs in the Root's transition and is awaited, with\n * `data-pending` on the Root and the Input. Runs for every such edit, also while an earlier run\n * is pending (a different code), and for the same code pasted again. Not at mount, on a reset,\n * for a controlled change, or while disabled or read-only. A rejection reaches the nearest Error\n * Boundary. May be a Server Function.\n */\n completeAction?: Action<[value: string, details: OTPFieldCompleteDetails]>;\n /**\n * Submit the Input's form (`requestSubmit()`) when an edit leaves the code complete, right\n * after dispatching `completeAction`. A change of context on input (WCAG 3.2.2): say so before\n * the field.\n *\n * @defaultValue `false`\n */\n submitOnComplete?: boolean;\n /**\n * Native `name` on the Input.\n *\n * @defaultValue the Field's `name` inside a `Field.Root`\n */\n name?: string;\n /** Native `form` on the Input. */\n form?: string;\n /**\n * Native `required` on the Input; or the Field's.\n *\n * @defaultValue `false`\n */\n required?: boolean;\n /**\n * Native `disabled` on the Input; or the Field's.\n *\n * @defaultValue `false`\n */\n disabled?: boolean;\n /**\n * Native `readonly` on the Input: no edits, no completion.\n *\n * @defaultValue `false`\n */\n readOnly?: boolean;\n /**\n * The native attribute, with a library default: a code is read left to right in every locale,\n * so the Slots, the Input's text and its caret keys stay left to right inside an RTL page.\n * Pass `\"rtl\"` for a code that is read right to left (the attribute has no value meaning\n * \"inherit\"). A `dir` on a `render` element wins as well.\n *\n * @defaultValue `\"ltr\"`\n */\n dir?: ComponentPropsWithRef<\"div\">[\"dir\"];\n /**\n * Ask the browser for the code of an origin-bound SMS (WebOTP, `navigator.credentials.get({\n * otp })`) while the Input is mounted. Chromium and Safari; nothing happens where the API is\n * missing (Firefox).\n *\n * @defaultValue `false`\n */\n experimental_webOTP?: boolean;\n /** Class name, or a function of the state that returns one. */\n className?: StateValue<string | undefined, OTPFieldState>;\n /** Inline style, or a function of the state that returns one. `position: relative` unless given. */\n style?: StateValue<CSSProperties | undefined, OTPFieldState>;\n /** The parts. The function form receives the state. */\n children?: StateValue<ReactNode, OTPFieldState>;\n /** Replace the default `<div>` (element form) or take over rendering (function form). */\n render?: RenderProp<ComponentPropsWithRef<\"div\">, OTPFieldState>;\n}\n\nconst COMPLETE_TYPES = transitionTypes(\"otp-field\", \"complete\");\n\ninterface Selection {\n readonly focused: boolean;\n readonly start: number | null;\n readonly end: number | null;\n}\n\nconst BLURRED: Selection = { focused: false, start: null, end: null };\n\nfunction isFocused(input: HTMLInputElement): boolean {\n return input.ownerDocument.activeElement === input || input.matches(\":focus\");\n}\n\n/** Writes `value` into the input when it differs and puts the selection at `[start, end]`. */\nfunction writeInput(\n input: HTMLInputElement,\n value: string,\n start: number | null,\n end: number | null = start,\n): void {\n if (input.value !== value) input.value = value;\n if (start === null || end === null || !isFocused(input)) return;\n if (input.selectionStart !== start || input.selectionEnd !== end) {\n input.setSelectionRange(start, end);\n }\n}\n\n/** The development check of the mounted Slots' indices (design 1). */\nfunction slotIndexProblem(slots: Map<HTMLElement, number>, length: number): string | null {\n if (slots.size === 0) return null;\n const seen = new Set<number>();\n for (const index of slots.values()) {\n if (!Number.isInteger(index) || index < 0 || index >= length) {\n return `OTPField.Slot: index ${index} is outside 0 to ${length - 1} (length ${length}).`;\n }\n if (seen.has(index)) return `OTPField.Slot: index ${index} is used by more than one Slot.`;\n seen.add(index);\n }\n for (let i = 0; i < length; i++) {\n if (!seen.has(i)) {\n return (\n `OTPField.Slot: no Slot has index ${i} (length ${length}). Render one Slot per ` +\n \"position, or the code's characters are not all shown.\"\n );\n }\n }\n return null;\n}\n\n/**\n * Owns the text of a one-time-code field (design/components/otp-field.md): the urgent value\n * (`value` / `defaultValue` / `onValueChange`), the filter that keeps it a code, the selection the\n * Slots draw, the completion transition (`completeAction`, `submitOnComplete`), form reset,\n * hydration adoption and WebOTP. Renders a `div` with `position: relative`: the box the Input\n * covers.\n *\n * Owned keys (conventions 1.2): `data-complete`, `data-pending`, `data-disabled`,\n * `data-invalid` (inside a Field); `position` in `style` unless the consumer's style sets it.\n *\n * @see https://ui.funstack.work/components/otp-field\n */\nexport function OTPFieldRoot(props: OTPFieldRootProps) {\n const {\n length: lengthProp,\n charset: charsetProp = \"numeric\",\n value: valueProp,\n defaultValue,\n onValueChange,\n completeAction,\n submitOnComplete = false,\n name: nameProp,\n form,\n required: requiredProp = false,\n disabled: disabledProp = false,\n readOnly = false,\n // design/components/otp-field.md 4.4, conventions 14.1 rule 8: a pass-through prop with a\n // default, so a `render` element's own `dir` wins over it (conventions 1.2).\n dir = \"ltr\",\n experimental_webOTP: experimentalWebOTP = false,\n className,\n style,\n children,\n render,\n ...rest\n } = props;\n\n const lengthValid = Number.isInteger(lengthProp) && lengthProp > 0;\n const length = lengthValid ? lengthProp : Math.max(1, Math.floor(lengthProp) || 1);\n useDevWarning(\n !lengthValid,\n isDev && `OTPField.Root: \\`length\\` must be a positive integer; got ${String(lengthProp)}.`,\n );\n const charset: OTPFieldCharset =\n charsetProp === \"alphanumeric\" || charsetProp === \"alphabetic\" ? charsetProp : \"numeric\";\n\n // --- Field ----------------------------------------------------------------------------------\n\n // OTPField.Input registers the control (design 7); the Root resolves the defaults.\n const fieldContext = use(FieldContext);\n const { disabled, required, name } = resolveFieldDefaults(fieldContext, {\n name: nameProp,\n disabled: disabledProp,\n required: requiredProp,\n });\n const fieldInvalid = fieldContext === null ? undefined : fieldContext.state.invalid;\n\n // --- The text (design 3.2) ------------------------------------------------------------------\n\n const controlled = valueProp !== undefined;\n const controlledValue = controlled ? normalizeCode(valueProp, charset, length) : undefined;\n useDevWarning(\n controlled && controlledValue !== valueProp,\n isDev &&\n `OTPField.Root: the controlled \\`value\\` (\"${valueProp ?? \"\"}\") holds characters outside ` +\n `\\`charset\\` (\"${charset}\") or more than \\`length\\` (${length}); it is rendered as ` +\n `\"${controlledValue ?? \"\"}\".`,\n );\n const [internalValue, setInternalValue] = useState(() =>\n normalizeCode(defaultValue ?? \"\", charset, length),\n );\n const value = controlledValue ?? internalValue;\n const valueRef = useRef(value);\n // The latest value for handlers between two renders (two edits in one task).\n useLayoutEffect(() => {\n valueRef.current = value;\n });\n\n /** The raw text while a composition is open (design 4.1, Composition): rendered as is. */\n const [composingText, setComposingText] = useState<string | null>(null);\n const composingRef = useRef(false);\n const [selection, setSelection] = useState<Selection>(BLURRED);\n\n const inputRef = useRef<HTMLInputElement | null>(null);\n const beforeRef = useRef<EditBefore | null>(null);\n /** The value change the Field is told about next was an edit (design 7: `takeEdit`). */\n const editRef = useRef(false);\n const webOTPAbort = useRef<AbortController | null>(null);\n /** A completion happened: no WebOTP request starts any more, and a late code is ignored. */\n const webOTPDone = useRef(false);\n /** `submitOnComplete` was held back by an open composition. */\n const submitAfterComposition = useRef(false);\n\n const { pending, run } = useActionTrigger();\n\n // The latest committed props and `run`, for the stable callbacks below. Named `*Ref` so that\n // React Compiler's lint treats `.current` as a ref read, not a missing memo dependency.\n // `run` is a new function on every render: reading it through the ref keeps every handler,\n // the context and the Input's listeners and WebOTP request stable across renders.\n const propsRef = useLatest({\n charset,\n length,\n disabled,\n readOnly,\n controlled,\n onValueChange,\n completeAction,\n submitOnComplete,\n run,\n notifyField: fieldContext?.notifyChange,\n });\n\n // --- Writes ---------------------------------------------------------------------------------\n\n /** Writes the value (urgent) and reports it to `onValueChange` (design 3.2). */\n const commitValue = useCallback(\n (next: string, reason: ChangeReason, event: Event | undefined, edit: boolean) => {\n const props = propsRef.current;\n editRef.current = edit;\n // Controlled: the value is the parent's until it renders it (it may not adopt the edit).\n if (!props.controlled) {\n valueRef.current = next;\n setInternalValue(next);\n }\n callUrgentHandler(\n props.onValueChange,\n next,\n createActionDetails(event, { reason }) as OTPFieldValueChangeDetails,\n );\n },\n [propsRef],\n );\n\n /** Submits the Input's form, after the Field re-validated the text just written (design 3.1). */\n const submit = useCallback(() => {\n const form = inputRef.current?.form;\n if (form == null) return;\n // The Field validated the browser's pre-filter text in its `input` listener; the commit\n // that re-validates the written text comes after this event. Refresh it now.\n propsRef.current.notifyField?.(true);\n form.requestSubmit();\n }, [propsRef]);\n\n /** The code is complete after an edit or a WebOTP fill (design 3.1). */\n const complete = useCallback(\n (code: string, reason: ChangeReason, event: Event | undefined, composing = false) => {\n const {\n disabled: off,\n readOnly: ro,\n completeAction: action,\n submitOnComplete: submitting,\n run: runAction,\n } = propsRef.current;\n if (off || ro) return;\n // A code completed by hand makes a waiting WebOTP request moot (design 8), and no new one\n // starts after it.\n webOTPDone.current = true;\n webOTPAbort.current?.abort();\n webOTPAbort.current = null;\n if (action !== undefined) {\n const details = createActionDetails(event, { reason }) as OTPFieldCompleteDetails;\n runAction(() => {\n for (const type of COMPLETE_TYPES) addTransitionType(type);\n return action(code, details);\n });\n }\n if (!submitting) return;\n // The IME has not committed the text yet: submit at `compositionend` (design 4.1).\n if (composing) submitAfterComposition.current = true;\n else submit();\n },\n [propsRef, submit],\n );\n\n const readSelection = useCallback(() => {\n const input = inputRef.current;\n const next: Selection =\n input === null || !isFocused(input)\n ? BLURRED\n : { focused: true, start: input.selectionStart, end: input.selectionEnd };\n setSelection((prev) =>\n prev.focused === next.focused && prev.start === next.start && prev.end === next.end\n ? prev\n : next,\n );\n }, []);\n\n // --- Editing (design 4.1) -------------------------------------------------------------------\n\n const beforeInput = useCallback((event: InputEvent) => {\n const input = event.target as HTMLInputElement;\n beforeRef.current = {\n text: input.value,\n selectionStart: input.selectionStart ?? input.value.length,\n selectionEnd: input.selectionEnd ?? input.value.length,\n };\n }, []);\n\n const change = useCallback(\n (event: ChangeEvent<HTMLInputElement>) => {\n const input = event.currentTarget;\n const native = event.nativeEvent as Partial<InputEvent> & Event;\n const before = beforeRef.current;\n beforeRef.current = null;\n const { charset: cs, length: len, disabled: off, readOnly: ro } = propsRef.current;\n if (off || ro) return;\n\n if (composingRef.current || native.isComposing === true) {\n // Derive always, write when allowed: the composition keeps its text in the input.\n const next = normalizeCode(input.value, cs, len);\n setComposingText(input.value);\n readSelection();\n if (next === valueRef.current) return;\n commitValue(next, \"activate\", native, true);\n if (next.length === len) complete(next, \"activate\", native, true);\n return;\n }\n\n const result = applyEdit({\n before,\n dom: input.value,\n caret: input.selectionStart,\n inputType: native.inputType ?? \"\",\n charset: cs,\n length: len,\n });\n setComposingText(null);\n if (result.selection !== null) {\n writeInput(input, result.value, result.selection[0], result.selection[1]);\n } else {\n writeInput(input, result.value, result.caret);\n }\n readSelection();\n const changed = result.value !== valueRef.current;\n if (changed) commitValue(result.value, \"activate\", native, true);\n if (result.value.length === len && (changed || result.kept > 0)) {\n complete(result.value, \"activate\", native);\n }\n },\n [commitValue, complete, propsRef, readSelection],\n );\n\n const compositionStart = useCallback(() => {\n composingRef.current = true;\n }, []);\n\n const compositionEnd = useCallback(\n (event: CompositionEvent) => {\n composingRef.current = false;\n const input = inputRef.current;\n setComposingText(null);\n if (input === null) return;\n const { charset: cs, length: len } = propsRef.current;\n const dom = input.value;\n const next = normalizeCode(dom, cs, len);\n const caret = Math.min(\n filterChars(dom.slice(0, input.selectionStart ?? dom.length), cs).length,\n next.length,\n );\n writeInput(input, next, caret);\n readSelection();\n const held = submitAfterComposition.current;\n submitAfterComposition.current = false;\n if (next !== valueRef.current) {\n commitValue(next, \"activate\", event, true);\n if (next.length === len) complete(next, \"activate\", event);\n return;\n }\n // The code was completed while composing: the submission held back then goes now.\n if (held && next.length === len && propsRef.current.submitOnComplete) submit();\n },\n [commitValue, complete, propsRef, readSelection, submit],\n );\n\n const blurred = useCallback(() => {\n setSelection(BLURRED);\n }, []);\n\n // --- The pointer (design 4.3) ---------------------------------------------------------------\n\n const slots = useRef(new Map<HTMLElement, number>());\n const registerSlot = useCallback((index: number, element: HTMLElement) => {\n slots.current.set(element, index);\n return () => {\n if (slots.current.get(element) === index) slots.current.delete(element);\n };\n }, []);\n\n const click = useCallback((event: MouseEvent) => {\n const input = inputRef.current;\n // A double click selects; a drag made a range: the browser's selection stands.\n if (input === null || event.detail > 1 || input.selectionStart !== input.selectionEnd) return;\n let target: number | null = null;\n let distance = Infinity;\n for (const [element, index] of slots.current) {\n const box = element.getBoundingClientRect();\n if (box.width === 0 && box.height === 0) continue;\n const dx = Math.max(box.left - event.clientX, 0, event.clientX - box.right);\n const dy = Math.max(box.top - event.clientY, 0, event.clientY - box.bottom);\n const d = dx * dx + dy * dy;\n if (d < distance) {\n distance = d;\n target = index;\n }\n }\n if (target === null) return;\n const caret = Math.min(target, input.value.length);\n if (input.selectionStart !== caret || input.selectionEnd !== caret) {\n input.setSelectionRange(caret, caret);\n }\n }, []);\n\n // --- Residual paths: hydration, reset, WebOTP (design 7, 8, 9) -----------------------------\n\n const inputMounted = useCallback(() => {\n const input = inputRef.current;\n if (input === null) return;\n const { charset: cs, length: len } = propsRef.current;\n const dom = input.value;\n const next = normalizeCode(dom, cs, len);\n if (dom !== next) input.value = next;\n // No completion for adopted text (design 9): a restored code is not a fresh one.\n if (next !== valueRef.current) commitValue(next, \"platform\", undefined, false);\n }, [commitValue, propsRef]);\n\n const resetTarget = normalizeCode(useResetTarget(valueProp, defaultValue) ?? \"\", charset, length);\n const [resync, setResync] = useState(0);\n const inputText = composingText ?? value;\n const inputTextRef = useLatest(inputText);\n useLayoutEffect(() => {\n // A controlled parent that did not adopt the reset gets the DOM put back.\n const input = inputRef.current;\n if (resync > 0 && input !== null && input.value !== inputTextRef.current) {\n input.value = inputTextRef.current;\n }\n }, [resync, inputTextRef]);\n useFormReset(\n inputRef as RefObject<FormAssociatedElement | null>,\n (event) => {\n composingRef.current = false;\n setComposingText(null);\n // The target is computed, not read from the input: after a click on a reset button this\n // microtask runs before the browser's reset algorithm (there is a microtask checkpoint\n // after each `reset` listener), so the input may still hold the old text here.\n if (resetTarget !== valueRef.current) commitValue(resetTarget, \"platform\", event, false);\n setResync((n) => n + 1);\n },\n () => {\n // react-dom rewrites a controlled input's `defaultValue` after every update; the reset\n // target is written right before the browser's reset algorithm reads it.\n const input = inputRef.current;\n if (input !== null && input.defaultValue !== resetTarget) input.defaultValue = resetTarget;\n },\n );\n\n const webOTPCode = useCallback(\n (code: string) => {\n const { charset: cs, length: len, disabled: off, readOnly: ro } = propsRef.current;\n if (off || ro || webOTPDone.current) return;\n const next = filterChars(code, cs);\n if (next.length !== len) return;\n // Written now, so that `submitOnComplete` posts it before React renders.\n const input = inputRef.current;\n if (input !== null) input.value = next;\n if (next !== valueRef.current) commitValue(next, \"platform\", undefined, true);\n complete(next, \"platform\", undefined);\n },\n [commitValue, complete, propsRef],\n );\n\n // A write-back or a WebOTP fill changed the value with no event the Field heard after it.\n useFieldValueChange(value, () => {\n const edit = editRef.current;\n editRef.current = false;\n return edit;\n });\n\n // --- Development warnings -------------------------------------------------------------------\n\n const slotWarned = useRef(false);\n useLayoutEffect(() => {\n if (!isDev || slotWarned.current) return;\n const problem = slotIndexProblem(slots.current, length);\n if (problem === null) return;\n slotWarned.current = true;\n console.error(problem);\n });\n\n // --- Render ---------------------------------------------------------------------------------\n\n const state = useMemo<OTPFieldState>(\n () => ({\n value,\n length,\n complete: value.length === length,\n pending,\n disabled,\n readOnly,\n invalid: fieldInvalid === true,\n }),\n [value, length, pending, disabled, readOnly, fieldInvalid],\n );\n\n const [activeFrom, activeTo] = activeRange(\n selection.focused,\n selection.start,\n selection.end,\n length,\n );\n\n const context = useMemo<OTPFieldContextValue>(\n () => ({\n state,\n charset,\n name,\n form,\n required,\n fieldInvalid,\n inputText,\n active: [activeFrom, activeTo],\n experimentalWebOTP,\n inputRef,\n change,\n beforeInput,\n compositionStart,\n compositionEnd,\n selectionChanged: readSelection,\n blurred,\n click,\n inputMounted,\n webOTPCode,\n registerSlot,\n webOTPAbort,\n webOTPDone,\n }),\n [\n state,\n charset,\n name,\n form,\n required,\n fieldInvalid,\n inputText,\n activeFrom,\n activeTo,\n experimentalWebOTP,\n change,\n beforeInput,\n compositionStart,\n compositionEnd,\n readSelection,\n blurred,\n click,\n inputMounted,\n webOTPCode,\n registerSlot,\n ],\n );\n\n const libraryProps: Partial<ComponentPropsWithRef<\"div\">> & DataAttributes = {\n ...(fieldContext !== null ? { \"data-invalid\": dataAttr(fieldInvalid === true) } : {}),\n \"data-complete\": dataAttr(state.complete),\n \"data-pending\": dataAttr(pending),\n \"data-disabled\": dataAttr(disabled),\n };\n\n const element = useRender<ComponentPropsWithRef<\"div\">, OTPFieldState>({\n defaultElement: \"div\",\n render,\n state,\n className,\n // `position: relative` is the Input's containing block (design 6), unless the consumer's\n // style places the Root itself.\n style: (s) => ({ position: \"relative\", ...resolveStateValue(style, s) }),\n children,\n props: { ...rest,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