@sereno-ds/ui 0.40.1 → 0.41.1

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.
@@ -0,0 +1,23 @@
1
+ import * as React from 'react';
2
+
3
+ type Size = 'sm' | 'md' | 'lg';
4
+ /** The vertical padding of an option row: finger-sized on touch, by field size on a mouse. */
5
+ declare function menuRowPadY(coarse: boolean, size: Size): number;
6
+ /**
7
+ * The look of the floating list shared by `Select` and `Autocomplete`: the surface (border, fill,
8
+ * shadow, radius, type) and the scrolling `<ul>` inside it. One place, so the two cannot drift apart.
9
+ */
10
+ declare function menuSurfaceStyle(size: Size): React.CSSProperties;
11
+ declare const menuListStyle: React.CSSProperties;
12
+ interface MenuOptionProps extends Omit<React.LiHTMLAttributes<HTMLLIElement>, 'children'> {
13
+ label: string;
14
+ selected: boolean;
15
+ active: boolean;
16
+ disabled?: boolean;
17
+ coarse: boolean;
18
+ padY: number;
19
+ }
20
+ /** One row of the list: the label, a check on the selected one, the hover/active fill. */
21
+ declare function MenuOption({ label, selected, active, disabled, coarse, padY, style, ...rest }: MenuOptionProps): React.JSX.Element;
22
+
23
+ export { MenuOption, type MenuOptionProps, menuListStyle, menuRowPadY, menuSurfaceStyle };
@@ -0,0 +1,61 @@
1
+ import { jsx, jsxs } from "react/jsx-runtime";
2
+ import { Check } from "lucide-react";
3
+ import { sx } from "./style.js";
4
+ const OPTION_PAD_Y = { sm: 6, md: 8, lg: 10 };
5
+ function menuRowPadY(coarse, size) {
6
+ return coarse ? 12 : OPTION_PAD_Y[size];
7
+ }
8
+ function menuSurfaceStyle(size) {
9
+ return {
10
+ minWidth: 160,
11
+ background: "var(--bg-surface)",
12
+ border: "var(--border-width-hairline) solid var(--border-default)",
13
+ borderRadius: "var(--radius-md)",
14
+ boxShadow: "var(--shadow-lg)",
15
+ fontFamily: "var(--font-body)",
16
+ fontSize: size === "sm" ? "var(--text-sm)" : "var(--text-base)"
17
+ };
18
+ }
19
+ const menuListStyle = {
20
+ overflowY: "auto",
21
+ margin: 0,
22
+ padding: "var(--space-1)",
23
+ listStyle: "none"
24
+ };
25
+ function MenuOption({ label, selected, active, disabled, coarse, padY, style, ...rest }) {
26
+ return /* @__PURE__ */ jsxs(
27
+ "li",
28
+ {
29
+ role: "option",
30
+ "aria-selected": selected,
31
+ "aria-disabled": disabled || void 0,
32
+ ...rest,
33
+ style: sx({
34
+ display: "flex",
35
+ alignItems: "center",
36
+ justifyContent: "space-between",
37
+ gap: "var(--space-2)",
38
+ minHeight: coarse ? 44 : void 0,
39
+ padding: `${padY}px var(--space-3)`,
40
+ borderRadius: "var(--radius-sm)",
41
+ cursor: disabled ? "not-allowed" : "pointer",
42
+ color: disabled ? "var(--text-disabled)" : "var(--text-primary)",
43
+ background: active && !disabled ? "var(--bg-subtle)" : "transparent",
44
+ fontWeight: selected ? "var(--weight-semibold)" : "var(--weight-regular)",
45
+ fontSize: coarse ? "var(--text-base)" : void 0,
46
+ ...style
47
+ }),
48
+ children: [
49
+ /* @__PURE__ */ jsx("span", { style: sx({ overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }), children: label }),
50
+ selected && /* @__PURE__ */ jsx("span", { style: sx({ display: "flex", flex: "0 0 auto", color: "var(--text-brand)" }), children: /* @__PURE__ */ jsx(Check, { size: coarse ? 18 : 16, strokeWidth: 2 }) })
51
+ ]
52
+ }
53
+ );
54
+ }
55
+ export {
56
+ MenuOption,
57
+ menuListStyle,
58
+ menuRowPadY,
59
+ menuSurfaceStyle
60
+ };
61
+ //# sourceMappingURL=MenuOption.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/_internal/MenuOption.tsx"],"sourcesContent":["import * as React from 'react';\nimport { Check } from 'lucide-react';\nimport { sx } from './style';\n\ntype Size = 'sm' | 'md' | 'lg';\n\nconst OPTION_PAD_Y = { sm: 6, md: 8, lg: 10 } as const;\n\n/** The vertical padding of an option row: finger-sized on touch, by field size on a mouse. */\nexport function menuRowPadY(coarse: boolean, size: Size): number {\n return coarse ? 12 : OPTION_PAD_Y[size];\n}\n\n/**\n * The look of the floating list shared by `Select` and `Autocomplete`: the surface (border, fill,\n * shadow, radius, type) and the scrolling `<ul>` inside it. One place, so the two cannot drift apart.\n */\nexport function menuSurfaceStyle(size: Size): React.CSSProperties {\n return {\n minWidth: 160,\n background: 'var(--bg-surface)',\n border: 'var(--border-width-hairline) solid var(--border-default)',\n borderRadius: 'var(--radius-md)',\n boxShadow: 'var(--shadow-lg)',\n fontFamily: 'var(--font-body)',\n fontSize: size === 'sm' ? 'var(--text-sm)' : 'var(--text-base)',\n };\n}\n\nexport const menuListStyle: React.CSSProperties = {\n overflowY: 'auto',\n margin: 0,\n padding: 'var(--space-1)',\n listStyle: 'none',\n};\n\nexport interface MenuOptionProps extends Omit<React.LiHTMLAttributes<HTMLLIElement>, 'children'> {\n label: string;\n selected: boolean;\n active: boolean;\n disabled?: boolean;\n coarse: boolean;\n padY: number;\n}\n\n/** One row of the list: the label, a check on the selected one, the hover/active fill. */\nexport function MenuOption({ label, selected, active, disabled, coarse, padY, style, ...rest }: MenuOptionProps) {\n return (\n <li\n role=\"option\"\n aria-selected={selected}\n aria-disabled={disabled || undefined}\n {...rest}\n style={sx({\n display: 'flex',\n alignItems: 'center',\n justifyContent: 'space-between',\n gap: 'var(--space-2)',\n minHeight: coarse ? 44 : undefined,\n padding: `${padY}px var(--space-3)`,\n borderRadius: 'var(--radius-sm)',\n cursor: disabled ? 'not-allowed' : 'pointer',\n color: disabled ? 'var(--text-disabled)' : 'var(--text-primary)',\n background: active && !disabled ? 'var(--bg-subtle)' : 'transparent',\n fontWeight: selected ? 'var(--weight-semibold)' : 'var(--weight-regular)',\n fontSize: coarse ? 'var(--text-base)' : undefined,\n ...style,\n })}\n >\n <span style={sx({ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' })}>{label}</span>\n {selected && (\n <span style={sx({ display: 'flex', flex: '0 0 auto', color: 'var(--text-brand)' })}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,GAAI,iBAAM;AAAA,QAC/F,YACC,oBAAC,UAAK,OAAO,GAAG,EAAE,SAAS,QAAQ,MAAM,YAAY,OAAO,oBAAoB,CAAC,GAC/E,8BAAC,SAAM,MAAM,SAAS,KAAK,IAAI,aAAa,GAAG,GACjD;AAAA;AAAA;AAAA,EAEJ;AAEJ;","names":[]}
@@ -0,0 +1,4 @@
1
+ /** True on touch / pen devices, where rows get finger-sized (and `Select` opens a sheet). SSR-safe. */
2
+ declare function useCoarsePointer(): boolean;
3
+
4
+ export { useCoarsePointer };
@@ -0,0 +1,16 @@
1
+ import * as React from "react";
2
+ function useCoarsePointer() {
3
+ return React.useSyncExternalStore(
4
+ (onChange) => {
5
+ const mq = window.matchMedia("(pointer: coarse)");
6
+ mq.addEventListener("change", onChange);
7
+ return () => mq.removeEventListener("change", onChange);
8
+ },
9
+ () => window.matchMedia("(pointer: coarse)").matches,
10
+ () => false
11
+ );
12
+ }
13
+ export {
14
+ useCoarsePointer
15
+ };
16
+ //# sourceMappingURL=useCoarsePointer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/_internal/useCoarsePointer.ts"],"sourcesContent":["import * as React from 'react';\n\n/** True on touch / pen devices, where rows get finger-sized (and `Select` opens a sheet). SSR-safe. */\nexport function useCoarsePointer(): boolean {\n return React.useSyncExternalStore(\n (onChange) => {\n const mq = window.matchMedia('(pointer: coarse)');\n mq.addEventListener('change', onChange);\n return () => mq.removeEventListener('change', onChange);\n },\n () => window.matchMedia('(pointer: coarse)').matches,\n () => false,\n );\n}\n"],"mappings":"AAAA,YAAY,WAAW;AAGhB,SAAS,mBAA4B;AAC1C,SAAO,MAAM;AAAA,IACX,CAAC,aAAa;AACZ,YAAM,KAAK,OAAO,WAAW,mBAAmB;AAChD,SAAG,iBAAiB,UAAU,QAAQ;AACtC,aAAO,MAAM,GAAG,oBAAoB,UAAU,QAAQ;AAAA,IACxD;AAAA,IACA,MAAM,OAAO,WAAW,mBAAmB,EAAE;AAAA,IAC7C,MAAM;AAAA,EACR;AACF;","names":[]}
@@ -0,0 +1,9 @@
1
+ import * as React from 'react';
2
+
3
+ /**
4
+ * The newest value of something that changes every render (a callback prop, usually), readable from
5
+ * an effect or an event listener without that effect having to depend on it and re-run.
6
+ */
7
+ declare function useLatest<T>(value: T): React.RefObject<T>;
8
+
9
+ export { useLatest };
@@ -0,0 +1,13 @@
1
+ import * as React from "react";
2
+ import { useIsoLayoutEffect } from "./useIsoLayoutEffect.js";
3
+ function useLatest(value) {
4
+ const ref = React.useRef(value);
5
+ useIsoLayoutEffect(() => {
6
+ ref.current = value;
7
+ });
8
+ return ref;
9
+ }
10
+ export {
11
+ useLatest
12
+ };
13
+ //# sourceMappingURL=useLatest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/_internal/useLatest.ts"],"sourcesContent":["import * as React from 'react';\nimport { useIsoLayoutEffect } from './useIsoLayoutEffect';\n\n/**\n * The newest value of something that changes every render (a callback prop, usually), readable from\n * an effect or an event listener without that effect having to depend on it and re-run.\n */\nexport function useLatest<T>(value: T): React.RefObject<T> {\n const ref = React.useRef(value);\n useIsoLayoutEffect(() => {\n ref.current = value;\n });\n return ref;\n}\n"],"mappings":"AAAA,YAAY,WAAW;AACvB,SAAS,0BAA0B;AAM5B,SAAS,UAAa,OAA8B;AACzD,QAAM,MAAM,MAAM,OAAO,KAAK;AAC9B,qBAAmB,MAAM;AACvB,QAAI,UAAU;AAAA,EAChB,CAAC;AACD,SAAO;AACT;","names":[]}
@@ -0,0 +1,43 @@
1
+ import * as React from 'react';
2
+
3
+ type MenuPlace = {
4
+ strategy: 'inline';
5
+ above: boolean;
6
+ maxH: number;
7
+ } | {
8
+ strategy: 'fixed';
9
+ above: boolean;
10
+ maxH: number;
11
+ top: number;
12
+ left: number;
13
+ width: number;
14
+ };
15
+ /**
16
+ * Where a dropdown list sits under its field, for `Select` and `Autocomplete`. Two strategies, picked
17
+ * once per open:
18
+ * - 'inline' - the common case. A plain `position: absolute` child of the field wrapper. The browser
19
+ * glues it to the field through *any* scroll (page or an inner overflow container) for free - zero
20
+ * JS, zero jitter, never closes on scroll. Only re-flips the side if room runs out.
21
+ * - 'fixed' - used when the field is inside a `position: fixed` container (a Dialog), where an inline
22
+ * child would be clipped by the dialog's overflow. Portalled to <body>, `position: fixed` at the
23
+ * anchor's viewport rect. The dialog itself doesn't scroll, so it stays glued; scrolling the dialog
24
+ * *body* detaches it, and `onDismiss` closes it then.
25
+ *
26
+ * `place` is null until the layout effect has measured, one render after `open`, so the list is only
27
+ * rendered once it has somewhere to be. It is never reset on close, so a second open has it at once.
28
+ * `itemCount` feeds the height estimate used to decide whether to flip above the field.
29
+ */
30
+ declare function useMenuPlacement({ open, disabled, anchorRef, panelRef, itemCount, onDismiss, }: {
31
+ open: boolean;
32
+ /** No dropdown to place (the touch sheet of `Select` needs no anchoring). */
33
+ disabled?: boolean;
34
+ /** The element the list hangs from: the field's trigger or box. */
35
+ anchorRef: React.RefObject<HTMLElement | null>;
36
+ /** The list panel, so scrolling *inside* it is not read as the page moving away. */
37
+ panelRef: React.RefObject<HTMLElement | null>;
38
+ itemCount: number;
39
+ /** The anchor moved away (fixed strategy): close the list. */
40
+ onDismiss: () => void;
41
+ }): MenuPlace | null;
42
+
43
+ export { type MenuPlace, useMenuPlacement };
@@ -0,0 +1,80 @@
1
+ import * as React from "react";
2
+ import { useIsoLayoutEffect } from "./useIsoLayoutEffect.js";
3
+ import { useLatest } from "./useLatest.js";
4
+ function useMenuPlacement({
5
+ open,
6
+ disabled = false,
7
+ anchorRef,
8
+ panelRef,
9
+ itemCount,
10
+ onDismiss
11
+ }) {
12
+ const [place, setPlace] = React.useState(null);
13
+ const dismissRef = useLatest(onDismiss);
14
+ useIsoLayoutEffect(() => {
15
+ if (!open || disabled) return;
16
+ const t = anchorRef.current;
17
+ if (!t) return;
18
+ const GAP = 6;
19
+ const vh = window.innerHeight || document.documentElement.clientHeight || 900;
20
+ const estH = Math.min(288, itemCount * 36 + 8);
21
+ const evalRoom = () => {
22
+ const r = t.getBoundingClientRect();
23
+ const roomBelow = vh - r.bottom - 8;
24
+ const roomAbove = r.top - 8;
25
+ const above = roomBelow < Math.min(estH, 200) && roomAbove > roomBelow;
26
+ const maxH = Math.max(120, Math.min(288, Math.round((above ? roomAbove : roomBelow) - GAP)));
27
+ return { r, above, maxH };
28
+ };
29
+ let fixed = false;
30
+ for (let el = t.parentElement; el && el !== document.body; el = el.parentElement) {
31
+ if (getComputedStyle(el).position === "fixed") {
32
+ fixed = true;
33
+ break;
34
+ }
35
+ }
36
+ if (fixed) {
37
+ const { r, above, maxH } = evalRoom();
38
+ setPlace({ strategy: "fixed", above, maxH, left: r.left, width: r.width, top: above ? r.top - GAP : r.bottom + GAP });
39
+ const anchorTop = r.top;
40
+ const onScroll2 = (e) => {
41
+ if (e.target instanceof Node && panelRef.current?.contains(e.target)) return;
42
+ if (Math.abs(t.getBoundingClientRect().top - anchorTop) > 1) dismissRef.current();
43
+ };
44
+ const anchorWidth = r.width;
45
+ const onResize = () => {
46
+ const now = t.getBoundingClientRect();
47
+ if (Math.abs(now.top - anchorTop) > 1 || Math.abs(now.width - anchorWidth) > 1) dismissRef.current();
48
+ };
49
+ window.addEventListener("scroll", onScroll2, true);
50
+ window.addEventListener("resize", onResize);
51
+ return () => {
52
+ window.removeEventListener("scroll", onScroll2, true);
53
+ window.removeEventListener("resize", onResize);
54
+ };
55
+ }
56
+ const applyFull = () => {
57
+ const { above, maxH } = evalRoom();
58
+ setPlace(
59
+ (prev) => prev?.strategy === "inline" && prev.above === above && prev.maxH === maxH ? prev : { strategy: "inline", above, maxH }
60
+ );
61
+ };
62
+ applyFull();
63
+ const onScroll = (e) => {
64
+ if (e.target instanceof Node && panelRef.current?.contains(e.target)) return;
65
+ const { above } = evalRoom();
66
+ setPlace((prev) => prev?.strategy === "inline" && prev.above !== above ? { ...prev, above } : prev);
67
+ };
68
+ window.addEventListener("scroll", onScroll, true);
69
+ window.addEventListener("resize", applyFull);
70
+ return () => {
71
+ window.removeEventListener("scroll", onScroll, true);
72
+ window.removeEventListener("resize", applyFull);
73
+ };
74
+ }, [open, disabled, itemCount, anchorRef, panelRef, dismissRef]);
75
+ return place;
76
+ }
77
+ export {
78
+ useMenuPlacement
79
+ };
80
+ //# sourceMappingURL=useMenuPlacement.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/_internal/useMenuPlacement.ts"],"sourcesContent":["import * as React from 'react';\nimport { useIsoLayoutEffect } from './useIsoLayoutEffect';\nimport { useLatest } from './useLatest';\n\nexport type MenuPlace =\n | { strategy: 'inline'; above: boolean; maxH: number }\n | { strategy: 'fixed'; above: boolean; maxH: number; top: number; left: number; width: number };\n\n/**\n * Where a dropdown list sits under its field, for `Select` and `Autocomplete`. Two strategies, picked\n * once per open:\n * - 'inline' - the common case. A plain `position: absolute` child of the field wrapper. The browser\n * glues it to the field through *any* scroll (page or an inner overflow container) for free - zero\n * JS, zero jitter, never closes on scroll. Only re-flips the side if room runs out.\n * - 'fixed' - used when the field is inside a `position: fixed` container (a Dialog), where an inline\n * child would be clipped by the dialog's overflow. Portalled to <body>, `position: fixed` at the\n * anchor's viewport rect. The dialog itself doesn't scroll, so it stays glued; scrolling the dialog\n * *body* detaches it, and `onDismiss` closes it then.\n *\n * `place` is null until the layout effect has measured, one render after `open`, so the list is only\n * rendered once it has somewhere to be. It is never reset on close, so a second open has it at once.\n * `itemCount` feeds the height estimate used to decide whether to flip above the field.\n */\nexport function useMenuPlacement({\n open,\n disabled = false,\n anchorRef,\n panelRef,\n itemCount,\n onDismiss,\n}: {\n open: boolean;\n /** No dropdown to place (the touch sheet of `Select` needs no anchoring). */\n disabled?: boolean;\n /** The element the list hangs from: the field's trigger or box. */\n anchorRef: React.RefObject<HTMLElement | null>;\n /** The list panel, so scrolling *inside* it is not read as the page moving away. */\n panelRef: React.RefObject<HTMLElement | null>;\n itemCount: number;\n /** The anchor moved away (fixed strategy): close the list. */\n onDismiss: () => void;\n}): MenuPlace | null {\n const [place, setPlace] = React.useState<MenuPlace | null>(null);\n\n // Read through a ref: a new `onDismiss` every render must not re-run the measuring effect below,\n // which would set `place` again and loop.\n const dismissRef = useLatest(onDismiss);\n\n useIsoLayoutEffect(() => {\n if (!open || disabled) return;\n const t = anchorRef.current;\n if (!t) return;\n\n const GAP = 6;\n // Non-zero fallback: if the viewport height is unreadable, assume room below\n // rather than flipping the menu up on a bad reading.\n const vh = window.innerHeight || document.documentElement.clientHeight || 900;\n const estH = Math.min(288, itemCount * 36 + 8); // rough list height, no DOM\n const evalRoom = () => {\n const r = t.getBoundingClientRect();\n const roomBelow = vh - r.bottom - 8;\n const roomAbove = r.top - 8;\n const above = roomBelow < Math.min(estH, 200) && roomAbove > roomBelow;\n const maxH = Math.max(120, Math.min(288, Math.round((above ? roomAbove : roomBelow) - GAP)));\n return { r, above, maxH };\n };\n\n let fixed = false;\n for (let el: HTMLElement | null = t.parentElement; el && el !== document.body; el = el.parentElement) {\n if (getComputedStyle(el).position === 'fixed') {\n fixed = true;\n break;\n }\n }\n\n if (fixed) {\n const { r, above, maxH } = evalRoom();\n setPlace({ strategy: 'fixed', above, maxH, left: r.left, width: r.width, top: above ? r.top - GAP : r.bottom + GAP });\n const anchorTop = r.top;\n const onScroll = (e: Event) => {\n if (e.target instanceof Node && panelRef.current?.contains(e.target)) return;\n if (Math.abs(t.getBoundingClientRect().top - anchorTop) > 1) dismissRef.current();\n };\n // A resize only detaches the list if the anchor moved or changed width: the on-screen keyboard of a\n // phone resizes the window while the field stays where it is, and must not close the list under the user.\n const anchorWidth = r.width;\n const onResize = () => {\n const now = t.getBoundingClientRect();\n if (Math.abs(now.top - anchorTop) > 1 || Math.abs(now.width - anchorWidth) > 1) dismissRef.current();\n };\n window.addEventListener('scroll', onScroll, true);\n window.addEventListener('resize', onResize);\n return () => {\n window.removeEventListener('scroll', onScroll, true);\n window.removeEventListener('resize', onResize);\n };\n }\n\n // Inline: the menu is a plain absolute child, glued for free. Never reposition\n // or close on scroll - only re-flip the side (rare) and re-cap height on\n // resize.\n const applyFull = () => {\n const { above, maxH } = evalRoom();\n setPlace((prev) =>\n prev?.strategy === 'inline' && prev.above === above && prev.maxH === maxH ? prev : { strategy: 'inline', above, maxH },\n );\n };\n applyFull();\n const onScroll = (e: Event) => {\n if (e.target instanceof Node && panelRef.current?.contains(e.target)) return;\n const { above } = evalRoom();\n setPlace((prev) => (prev?.strategy === 'inline' && prev.above !== above ? { ...prev, above } : prev));\n };\n window.addEventListener('scroll', onScroll, true);\n window.addEventListener('resize', applyFull);\n return () => {\n window.removeEventListener('scroll', onScroll, true);\n window.removeEventListener('resize', applyFull);\n };\n }, [open, disabled, itemCount, anchorRef, panelRef, dismissRef]);\n\n return place;\n}\n"],"mappings":"AAAA,YAAY,WAAW;AACvB,SAAS,0BAA0B;AACnC,SAAS,iBAAiB;AAqBnB,SAAS,iBAAiB;AAAA,EAC/B;AAAA,EACA,WAAW;AAAA,EACX;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAWqB;AACnB,QAAM,CAAC,OAAO,QAAQ,IAAI,MAAM,SAA2B,IAAI;AAI/D,QAAM,aAAa,UAAU,SAAS;AAEtC,qBAAmB,MAAM;AACvB,QAAI,CAAC,QAAQ,SAAU;AACvB,UAAM,IAAI,UAAU;AACpB,QAAI,CAAC,EAAG;AAER,UAAM,MAAM;AAGZ,UAAM,KAAK,OAAO,eAAe,SAAS,gBAAgB,gBAAgB;AAC1E,UAAM,OAAO,KAAK,IAAI,KAAK,YAAY,KAAK,CAAC;AAC7C,UAAM,WAAW,MAAM;AACrB,YAAM,IAAI,EAAE,sBAAsB;AAClC,YAAM,YAAY,KAAK,EAAE,SAAS;AAClC,YAAM,YAAY,EAAE,MAAM;AAC1B,YAAM,QAAQ,YAAY,KAAK,IAAI,MAAM,GAAG,KAAK,YAAY;AAC7D,YAAM,OAAO,KAAK,IAAI,KAAK,KAAK,IAAI,KAAK,KAAK,OAAO,QAAQ,YAAY,aAAa,GAAG,CAAC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AAAA,MACvH;AAAA,IACF;AACA,cAAU;AACV,UAAM,WAAW,CAAC,MAAa;AAC7B,UAAI,EAAE,kBAAkB,QAAQ,SAAS,SAAS,SAAS,EAAE,MAAM,EAAG;AACtE,YAAM,EAAE,MAAM,IAAI,SAAS;AAC3B,eAAS,CAAC,SAAU,MAAM,aAAa,YAAY,KAAK,UAAU,QAAQ,EAAE,GAAG,MAAM,MAAM,IAAI,IAAK;AAAA,IACtG;AACA,WAAO,iBAAiB,UAAU,UAAU,IAAI;AAChD,WAAO,iBAAiB,UAAU,SAAS;AAC3C,WAAO,MAAM;AACX,aAAO,oBAAoB,UAAU,UAAU,IAAI;AACnD,aAAO,oBAAoB,UAAU,SAAS;AAAA,IAChD;AAAA,EACF,GAAG,CAAC,MAAM,UAAU,WAAW,WAAW,UAAU,UAAU,CAAC;AAE/D,SAAO;AACT;","names":["onScroll"]}
@@ -0,0 +1,27 @@
1
+ /** The minimal scroll: nothing moves if the option is already visible. Also scrolls the ancestors, if need be. */
2
+ declare function revealNearest(el: HTMLElement): void;
3
+ /**
4
+ * The same minimal scroll, but only inside the option's own list: the page and a Dialog behind it
5
+ * never move. For a list that opens under a text box that has the keyboard open.
6
+ */
7
+ declare function revealInList(el: HTMLElement): void;
8
+ /**
9
+ * Keep the active option of an open list in view: once the list exists, and again whenever the active
10
+ * option itself changes (the keyboard, the pointer). On the desktop the list is only rendered after
11
+ * `place` is known (one render after the open), so on the first open there is nothing to scroll in the
12
+ * commit where `open` turns true; `place` is in the deps so this runs again the moment the list is
13
+ * there (SS-568). It is a layout effect so the list is already on the option when it is first painted,
14
+ * not at the top for a frame.
15
+ *
16
+ * It reveals once per option (`revealed`): a list the app pages from the server grows while it is open
17
+ * (`options.length`, and so `place` in the portalled case, change with every page), and a user who
18
+ * scrolled down to ask for the next page must not be pulled back to the active option. Closing resets it.
19
+ *
20
+ * `activeId` is the DOM id of the active option, or null when none is. `shouldReveal` can decline a
21
+ * reveal (the option is then counted as revealed, so it is not asked again): for an option that
22
+ * becomes active by itself, not by the user, that should not take the list away from where the
23
+ * user has put it.
24
+ */
25
+ declare function useRevealActive(open: boolean, activeId: string | null, place: unknown, reveal?: (el: HTMLElement) => void, shouldReveal?: (el: HTMLElement) => boolean): void;
26
+
27
+ export { revealInList, revealNearest, useRevealActive };
@@ -0,0 +1,36 @@
1
+ import * as React from "react";
2
+ import { useIsoLayoutEffect } from "./useIsoLayoutEffect.js";
3
+ import { useLatest } from "./useLatest.js";
4
+ function revealNearest(el) {
5
+ el.scrollIntoView({ block: "nearest" });
6
+ }
7
+ function revealInList(el) {
8
+ const list = el.parentElement;
9
+ if (!list) return;
10
+ const top = el.offsetTop;
11
+ const bottom = top + el.offsetHeight;
12
+ if (top < list.scrollTop) list.scrollTop = top;
13
+ else if (bottom > list.scrollTop + list.clientHeight) list.scrollTop = bottom - list.clientHeight;
14
+ }
15
+ function useRevealActive(open, activeId, place, reveal = revealNearest, shouldReveal) {
16
+ const revealed = React.useRef(null);
17
+ const shouldRevealRef = useLatest(shouldReveal);
18
+ useIsoLayoutEffect(() => {
19
+ if (!open) {
20
+ revealed.current = null;
21
+ return;
22
+ }
23
+ if (activeId === null || activeId === revealed.current) return;
24
+ const el = document.getElementById(activeId);
25
+ if (!el) return;
26
+ revealed.current = activeId;
27
+ if (shouldRevealRef.current && !shouldRevealRef.current(el)) return;
28
+ reveal(el);
29
+ }, [open, activeId, place, reveal, shouldRevealRef]);
30
+ }
31
+ export {
32
+ revealInList,
33
+ revealNearest,
34
+ useRevealActive
35
+ };
36
+ //# sourceMappingURL=useRevealActive.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/_internal/useRevealActive.ts"],"sourcesContent":["import * as React from 'react';\nimport { useIsoLayoutEffect } from './useIsoLayoutEffect';\nimport { useLatest } from './useLatest';\n\n/** The minimal scroll: nothing moves if the option is already visible. Also scrolls the ancestors, if need be. */\nexport function revealNearest(el: HTMLElement): void {\n el.scrollIntoView({ block: 'nearest' });\n}\n\n/**\n * The same minimal scroll, but only inside the option's own list: the page and a Dialog behind it\n * never move. For a list that opens under a text box that has the keyboard open.\n */\nexport function revealInList(el: HTMLElement): void {\n const list = el.parentElement;\n if (!list) return;\n const top = el.offsetTop;\n const bottom = top + el.offsetHeight;\n if (top < list.scrollTop) list.scrollTop = top;\n else if (bottom > list.scrollTop + list.clientHeight) list.scrollTop = bottom - list.clientHeight;\n}\n\n/**\n * Keep the active option of an open list in view: once the list exists, and again whenever the active\n * option itself changes (the keyboard, the pointer). On the desktop the list is only rendered after\n * `place` is known (one render after the open), so on the first open there is nothing to scroll in the\n * commit where `open` turns true; `place` is in the deps so this runs again the moment the list is\n * there (SS-568). It is a layout effect so the list is already on the option when it is first painted,\n * not at the top for a frame.\n *\n * It reveals once per option (`revealed`): a list the app pages from the server grows while it is open\n * (`options.length`, and so `place` in the portalled case, change with every page), and a user who\n * scrolled down to ask for the next page must not be pulled back to the active option. Closing resets it.\n *\n * `activeId` is the DOM id of the active option, or null when none is. `shouldReveal` can decline a\n * reveal (the option is then counted as revealed, so it is not asked again): for an option that\n * becomes active by itself, not by the user, that should not take the list away from where the\n * user has put it.\n */\nexport function useRevealActive(\n open: boolean,\n activeId: string | null,\n place: unknown,\n reveal: (el: HTMLElement) => void = revealNearest,\n shouldReveal?: (el: HTMLElement) => boolean,\n): void {\n const revealed = React.useRef<string | null>(null);\n const shouldRevealRef = useLatest(shouldReveal);\n useIsoLayoutEffect(() => {\n if (!open) {\n revealed.current = null;\n return;\n }\n if (activeId === null || activeId === revealed.current) return;\n const el = document.getElementById(activeId);\n if (!el) return; // the list is not rendered yet; `place` arriving runs this again\n revealed.current = activeId;\n if (shouldRevealRef.current && !shouldRevealRef.current(el)) return;\n reveal(el);\n }, [open, activeId, place, reveal, shouldRevealRef]);\n}\n"],"mappings":"AAAA,YAAY,WAAW;AACvB,SAAS,0BAA0B;AACnC,SAAS,iBAAiB;AAGnB,SAAS,cAAc,IAAuB;AACnD,KAAG,eAAe,EAAE,OAAO,UAAU,CAAC;AACxC;AAMO,SAAS,aAAa,IAAuB;AAClD,QAAM,OAAO,GAAG;AAChB,MAAI,CAAC,KAAM;AACX,QAAM,MAAM,GAAG;AACf,QAAM,SAAS,MAAM,GAAG;AACxB,MAAI,MAAM,KAAK,UAAW,MAAK,YAAY;AAAA,WAClC,SAAS,KAAK,YAAY,KAAK,aAAc,MAAK,YAAY,SAAS,KAAK;AACvF;AAmBO,SAAS,gBACd,MACA,UACA,OACA,SAAoC,eACpC,cACM;AACN,QAAM,WAAW,MAAM,OAAsB,IAAI;AACjD,QAAM,kBAAkB,UAAU,YAAY;AAC9C,qBAAmB,MAAM;AACvB,QAAI,CAAC,MAAM;AACT,eAAS,UAAU;AACnB;AAAA,IACF;AACA,QAAI,aAAa,QAAQ,aAAa,SAAS,QAAS;AACxD,UAAM,KAAK,SAAS,eAAe,QAAQ;AAC3C,QAAI,CAAC,GAAI;AACT,aAAS,UAAU;AACnB,QAAI,gBAAgB,WAAW,CAAC,gBAAgB,QAAQ,EAAE,EAAG;AAC7D,WAAO,EAAE;AAAA,EACX,GAAG,CAAC,MAAM,UAAU,OAAO,QAAQ,eAAe,CAAC;AACrD;","names":[]}
@@ -0,0 +1,111 @@
1
+ import * as React from 'react';
2
+
3
+ interface AutocompleteOption {
4
+ value: string;
5
+ label: string;
6
+ disabled?: boolean;
7
+ }
8
+ /** Every string the component says. Defaults come from `locale`; pass `labels` to change some. */
9
+ interface AutocompleteLabels {
10
+ /** Placeholder of the text box while nothing is chosen. */
11
+ placeholder: string;
12
+ /** aria-label of the clear (×) button. */
13
+ clear: string;
14
+ /** aria-label of the chevron button while the list is closed. */
15
+ open: string;
16
+ /** aria-label of the chevron button while the list is open. */
17
+ close: string;
18
+ /** What a screen reader hears while `loading` (the list itself shows only a spinner). */
19
+ loading: string;
20
+ /** The row shown when no option is left to show. */
21
+ empty: string;
22
+ /** What a screen reader hears when the list changes size. */
23
+ results: (count: number) => string;
24
+ }
25
+ /**
26
+ * Single choice from a list you search by typing. Like `Select`, but the box is a text field: type
27
+ * to narrow the list, pick one row, and the value is that row. For a list too long to scroll
28
+ * (past ~12 options), or one the server searches and pages (a thousand clients, fifty at a time).
29
+ *
30
+ * The value is the chosen option itself, `{ value, label }`, and it carries its own label. The
31
+ * box shows `value.label` whether or not that option is in `options` right now, which is what
32
+ * keeps the label when the page it came from is no longer loaded. `options` is only what the list
33
+ * shows. `onValueChange` gives the option (or `null` when cleared), not a string.
34
+ *
35
+ * Two kinds of search, one prop apart:
36
+ * - the default filters `options` itself, as you type (case and accents ignored);
37
+ * - `filter={false}` leaves the list alone and tells you what was typed (`onSearch`, debounced),
38
+ * for a list the server already filtered.
39
+ *
40
+ * It is a text box, not free text: what is typed never becomes the value. Leaving the box puts the
41
+ * chosen label back.
42
+ */
43
+ interface AutocompleteProps {
44
+ label?: string;
45
+ hint?: string;
46
+ /** Error message; also turns the control red. */
47
+ error?: string;
48
+ required?: boolean;
49
+ /** What the list shows now. Appending rows while it is open is fine: the list keeps its scroll. */
50
+ options?: AutocompleteOption[];
51
+ /** Controlled value: the chosen option, or `null` for none. */
52
+ value?: AutocompleteOption | null;
53
+ /** Uncontrolled initial value. */
54
+ defaultValue?: AutocompleteOption | null;
55
+ /** The chosen option, or `null` when the value is cleared. */
56
+ onValueChange?: (option: AutocompleteOption | null) => void;
57
+ /**
58
+ * How typing narrows `options`. `true` (default): keep the rows whose label contains the text,
59
+ * ignoring case and accents. `false`: show `options` as they are (the server did the filtering).
60
+ * A function decides per row.
61
+ */
62
+ filter?: boolean | ((option: AutocompleteOption, query: string) => boolean);
63
+ /**
64
+ * The text to search for changed: fired as the user types (after `debounce`) and with `''` once
65
+ * the box goes back to showing the chosen label (pick, clear, leave, Esc). Never with the label
66
+ * itself, so it is safe to run the query here. Use it with `filter={false}`.
67
+ */
68
+ onSearch?: (query: string) => void;
69
+ /** Debounce for `onSearch` while typing, in ms. */
70
+ debounce?: number;
71
+ /** The list is waiting for rows: a spinner under them (or alone), the list marked busy, and a screen reader told. */
72
+ loading?: boolean;
73
+ /**
74
+ * The list was scrolled to within about five rows of its end. Fires once per list size, never
75
+ * while `loading`. Pass it only while there is a next page, so a list that is complete asks for nothing.
76
+ */
77
+ onReachEnd?: () => void;
78
+ /** The list opened or closed. For a list that loads when it opens. */
79
+ onOpenChange?: (open: boolean) => void;
80
+ /** A × button, and an emptied box clears the value. */
81
+ clearable?: boolean;
82
+ /** Shown in muted text while nothing is chosen. Defaults to the `locale` text. */
83
+ placeholder?: string;
84
+ size?: 'sm' | 'md' | 'lg';
85
+ disabled?: boolean;
86
+ /** Form field name - mirrored to a hidden input so the chosen value can be submitted. */
87
+ name?: string;
88
+ id?: string;
89
+ containerStyle?: React.CSSProperties;
90
+ /**
91
+ * Accessible name for the text box. Only needed when there's no visible `label`. Takes
92
+ * precedence over `label` if both are set - don't pass both.
93
+ */
94
+ 'aria-label'?: string;
95
+ /** Reserve the hint/error row's height even with neither set - stops the field from growing
96
+ * the moment a validation message appears. */
97
+ preserveHelperSpace?: boolean;
98
+ /** Language of the built-in texts. */
99
+ locale?: 'pt-BR' | 'en';
100
+ /** Override any built-in text. */
101
+ labels?: Partial<AutocompleteLabels>;
102
+ }
103
+ /**
104
+ * `ref` reaches the hidden `<input type="hidden">` that mirrors the chosen value (rendered only
105
+ * when `name` is set - pass it, as `register()` does), the same as `Select`. A plain
106
+ * `{...register(name)}` spread still won't validate on change: wire it through `Controller`, or
107
+ * call the registered `onChange` yourself from `onValueChange`.
108
+ */
109
+ declare const Autocomplete: React.ForwardRefExoticComponent<AutocompleteProps & React.RefAttributes<HTMLInputElement>>;
110
+
111
+ export { Autocomplete, type AutocompleteLabels, type AutocompleteOption, type AutocompleteProps };