@cloudwick/astral-ui-cli 2.0.1 → 2.2.0
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/README.md +17 -5
- package/dist/index.js +1235 -139
- package/dist/index.js.map +1 -1
- package/dist/templates/constants/index.ts +1 -0
- package/dist/templates/constants/keyboardKey.ts +1 -0
- package/dist/templates/css/fonts.min.css +1 -1
- package/dist/templates/fonts/NotoSans-Bold.woff2 +0 -0
- package/dist/templates/fonts/NotoSans-BoldItalic.woff2 +0 -0
- package/dist/templates/fonts/NotoSans-Italic.woff2 +0 -0
- package/dist/templates/fonts/NotoSans-Medium.woff2 +0 -0
- package/dist/templates/fonts/NotoSans-MediumItalic.woff2 +0 -0
- package/dist/templates/fonts/NotoSans-Regular.woff2 +0 -0
- package/dist/templates/fonts/NotoSans-SemiBold.woff2 +0 -0
- package/dist/templates/fonts/NotoSans-SemiBoldItalic.woff2 +0 -0
- package/dist/templates/hooks/index.ts +20 -1
- package/dist/templates/hooks/useAsyncOptions.ts +346 -0
- package/dist/templates/hooks/useAsyncOptionsReducer.ts +182 -0
- package/dist/templates/hooks/useAvailableProjection.ts +77 -0
- package/dist/templates/hooks/useDebouncedCallback.ts +42 -0
- package/dist/templates/hooks/useFilterQuery.ts +145 -0
- package/dist/templates/hooks/useIgnoredPropWarning.ts +17 -0
- package/dist/templates/hooks/useInfiniteScroll.ts +63 -0
- package/dist/templates/hooks/useIsTruncated.ts +63 -0
- package/dist/templates/hooks/useListboxKeyboardNav.ts +120 -0
- package/dist/templates/hooks/useOnValueChange.ts +17 -0
- package/dist/templates/hooks/usePrefetchBaseline.ts +38 -0
- package/dist/templates/schema/components-config.schema.json +3 -0
- package/dist/templates/tailwind/tailwind.config.js +8 -8
- package/dist/templates/utils/devWarn.ts +9 -0
- package/dist/templates/utils/elementUtils.ts +26 -0
- package/dist/templates/utils/index.ts +7 -1
- package/dist/templates/utils/matchesQuery.ts +23 -0
- package/dist/templates/utils/mergeUnique.ts +24 -0
- package/dist/templates/utils/resolveLoadErrors.ts +40 -0
- package/dist/templates/utils/resolveReloadControls.ts +31 -0
- package/package.json +2 -2
- package/dist/templates/fonts/Inter_18pt-Bold.ttf +0 -0
- package/dist/templates/fonts/Inter_18pt-BoldItalic.ttf +0 -0
- package/dist/templates/fonts/Inter_18pt-Italic.ttf +0 -0
- package/dist/templates/fonts/Inter_18pt-Medium.ttf +0 -0
- package/dist/templates/fonts/Inter_18pt-MediumItalic.ttf +0 -0
- package/dist/templates/fonts/Inter_18pt-Regular.ttf +0 -0
- package/dist/templates/fonts/Inter_18pt-SemiBold.ttf +0 -0
- package/dist/templates/fonts/Inter_18pt-SemiBoldItalic.ttf +0 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns a debounced wrapper around `callback` plus a `cancel` handle. The latest `callback`
|
|
5
|
+
* is always invoked (read through a ref, so passing a fresh inline function each render is safe),
|
|
6
|
+
* and the pending timer is cleared on unmount. A `delay` of `0` (or less) invokes synchronously,
|
|
7
|
+
* which is handy for an "instant" mode without special-casing at the call site.
|
|
8
|
+
*/
|
|
9
|
+
export function useDebouncedCallback<Params extends unknown[]>(
|
|
10
|
+
callback: ( ...args: Params ) => void,
|
|
11
|
+
delay: number
|
|
12
|
+
): { call: ( ...args: Params ) => void; cancel: () => void } {
|
|
13
|
+
const callbackRef = useRef( callback );
|
|
14
|
+
callbackRef.current = callback;
|
|
15
|
+
const timerRef = useRef<ReturnType<typeof setTimeout> | null>( null );
|
|
16
|
+
|
|
17
|
+
const cancel = useCallback(() => {
|
|
18
|
+
if ( timerRef.current ) {
|
|
19
|
+
clearTimeout( timerRef.current );
|
|
20
|
+
timerRef.current = null;
|
|
21
|
+
}
|
|
22
|
+
}, []);
|
|
23
|
+
|
|
24
|
+
const call = useCallback(
|
|
25
|
+
( ...args: Params ) => {
|
|
26
|
+
cancel();
|
|
27
|
+
if ( delay <= 0 ) {
|
|
28
|
+
callbackRef.current( ...args );
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
timerRef.current = setTimeout(() => {
|
|
32
|
+
timerRef.current = null;
|
|
33
|
+
callbackRef.current( ...args );
|
|
34
|
+
}, delay );
|
|
35
|
+
},
|
|
36
|
+
[ delay, cancel ]
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
useEffect(() => cancel, [cancel]);
|
|
40
|
+
|
|
41
|
+
return { call, cancel };
|
|
42
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useCallback,
|
|
3
|
+
useEffect,
|
|
4
|
+
useRef,
|
|
5
|
+
useState,
|
|
6
|
+
type ChangeEvent,
|
|
7
|
+
type FocusEvent,
|
|
8
|
+
type KeyboardEvent
|
|
9
|
+
} from "react";
|
|
10
|
+
|
|
11
|
+
import { keyboardKeys } from "@constants";
|
|
12
|
+
import { useDebouncedCallback } from "./useDebouncedCallback";
|
|
13
|
+
|
|
14
|
+
interface IUseFilterQueryArgs {
|
|
15
|
+
/** Seeds/re-syncs the search box on change; not fully controlled — pair with onFilterChange. */
|
|
16
|
+
filterValue?: string;
|
|
17
|
+
/** Server-side filter handler; its presence is the client/server mode switch. */
|
|
18
|
+
onFilterChange?: ( query: string ) => void;
|
|
19
|
+
/** Debounce delay in ms for `onFilterChange`. */
|
|
20
|
+
filterDebounceMs: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface IUseFilterQueryResult {
|
|
24
|
+
/** Current search text. */
|
|
25
|
+
filterQuery: string;
|
|
26
|
+
/** Change handler for the search input. */
|
|
27
|
+
handleFilterInput: ( e: ChangeEvent<HTMLInputElement> ) => void;
|
|
28
|
+
/** The same path as `handleFilterInput` for surfaces that report their search as a plain string. */
|
|
29
|
+
setFilterQuery: ( query: string ) => void;
|
|
30
|
+
/** Marks Backspace/Delete held so debounce waits for release (see handleFilterKeyUp). */
|
|
31
|
+
handleFilterKeyDown: ( e: KeyboardEvent<HTMLInputElement> ) => void;
|
|
32
|
+
/** On Backspace/Delete release, starts the deferred debounce. */
|
|
33
|
+
handleFilterKeyUp: ( e: KeyboardEvent<HTMLInputElement> ) => void;
|
|
34
|
+
/** On blur during a Backspace/Delete hold, release and start the deferred debounce. */
|
|
35
|
+
handleFilterBlur: ( e: FocusEvent<HTMLInputElement> ) => void;
|
|
36
|
+
/** Clear the query, drop any pending debounce, and notify a server-side consumer. */
|
|
37
|
+
resetFilter: () => void;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const isDeleteKey = ( key: string ): boolean =>
|
|
41
|
+
key === keyboardKeys.backspace || key === keyboardKeys.delete;
|
|
42
|
+
|
|
43
|
+
/** Owns a search query and the debounced bridge to a server-side `onFilterChange`. */
|
|
44
|
+
export function useFilterQuery({
|
|
45
|
+
filterValue,
|
|
46
|
+
onFilterChange,
|
|
47
|
+
filterDebounceMs
|
|
48
|
+
}: IUseFilterQueryArgs ): IUseFilterQueryResult {
|
|
49
|
+
const [ filterQuery, setQueryState ] = useState( filterValue ?? "" );
|
|
50
|
+
|
|
51
|
+
// Refs keep resetFilter stable for outside-click deps (avoid re-subscribing each render).
|
|
52
|
+
const filterQueryRef = useRef( filterQuery );
|
|
53
|
+
filterQueryRef.current = filterQuery;
|
|
54
|
+
const onFilterChangeRef = useRef( onFilterChange );
|
|
55
|
+
onFilterChangeRef.current = onFilterChange;
|
|
56
|
+
|
|
57
|
+
// Defer debounce while Backspace/Delete is held (OS key-repeat gap exceeds filterDebounceMs).
|
|
58
|
+
const isDeleteKeyHeldRef = useRef( false );
|
|
59
|
+
// True only when an onChange was suppressed during the hold (skips no-op empty Backspace).
|
|
60
|
+
const deferredDuringDeleteRef = useRef( false );
|
|
61
|
+
|
|
62
|
+
// filterDebounceMs=0 fires synchronously; pending timer is cancelled on unmount.
|
|
63
|
+
const { call: debouncedFilterChange, cancel: cancelFilterChange } = useDebouncedCallback(
|
|
64
|
+
( query: string ) => onFilterChangeRef.current?.( query ),
|
|
65
|
+
filterDebounceMs
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
const releaseDeleteKeyAndFlush = useCallback(() => {
|
|
69
|
+
if ( !isDeleteKeyHeldRef.current ) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
isDeleteKeyHeldRef.current = false;
|
|
73
|
+
if ( !deferredDuringDeleteRef.current ) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
deferredDuringDeleteRef.current = false;
|
|
77
|
+
debouncedFilterChange( filterQueryRef.current );
|
|
78
|
+
}, [debouncedFilterChange]);
|
|
79
|
+
|
|
80
|
+
// Sync filterValue from parent; cancel pending debounce so stale queries don't fire.
|
|
81
|
+
useEffect(() => {
|
|
82
|
+
if ( filterValue !== undefined ) {
|
|
83
|
+
isDeleteKeyHeldRef.current = false;
|
|
84
|
+
deferredDuringDeleteRef.current = false;
|
|
85
|
+
cancelFilterChange();
|
|
86
|
+
setQueryState( filterValue );
|
|
87
|
+
}
|
|
88
|
+
}, [ filterValue, cancelFilterChange ]);
|
|
89
|
+
|
|
90
|
+
const handleFilterKeyDown = useCallback(( e: KeyboardEvent<HTMLInputElement> ) => {
|
|
91
|
+
if ( isDeleteKey( e.key )) {
|
|
92
|
+
isDeleteKeyHeldRef.current = true;
|
|
93
|
+
}
|
|
94
|
+
}, []);
|
|
95
|
+
|
|
96
|
+
const handleFilterKeyUp = useCallback(( e: KeyboardEvent<HTMLInputElement> ) => {
|
|
97
|
+
if ( isDeleteKey( e.key )) {
|
|
98
|
+
releaseDeleteKeyAndFlush();
|
|
99
|
+
}
|
|
100
|
+
}, [releaseDeleteKeyAndFlush]);
|
|
101
|
+
|
|
102
|
+
const handleFilterBlur = useCallback(() => {
|
|
103
|
+
releaseDeleteKeyAndFlush();
|
|
104
|
+
}, [releaseDeleteKeyAndFlush]);
|
|
105
|
+
|
|
106
|
+
const setFilterQuery = useCallback(
|
|
107
|
+
( query: string ) => {
|
|
108
|
+
setQueryState( query );
|
|
109
|
+
if ( isDeleteKeyHeldRef.current ) {
|
|
110
|
+
// Wait for keyup/blur to schedule debounce with the final query.
|
|
111
|
+
cancelFilterChange();
|
|
112
|
+
deferredDuringDeleteRef.current = true;
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
debouncedFilterChange( query );
|
|
116
|
+
},
|
|
117
|
+
[ debouncedFilterChange, cancelFilterChange ]
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
const handleFilterInput = useCallback(
|
|
121
|
+
( e: ChangeEvent<HTMLInputElement> ) => setFilterQuery( e.target.value ),
|
|
122
|
+
[setFilterQuery]
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
const resetFilter = useCallback(() => {
|
|
126
|
+
isDeleteKeyHeldRef.current = false;
|
|
127
|
+
deferredDuringDeleteRef.current = false;
|
|
128
|
+
cancelFilterChange();
|
|
129
|
+
setQueryState( "" );
|
|
130
|
+
// Notify immediately on clear so a pending keystroke can't overtake the reset.
|
|
131
|
+
if ( filterQueryRef.current !== "" ) {
|
|
132
|
+
onFilterChangeRef.current?.( "" );
|
|
133
|
+
}
|
|
134
|
+
}, [cancelFilterChange]);
|
|
135
|
+
|
|
136
|
+
return {
|
|
137
|
+
filterQuery,
|
|
138
|
+
handleFilterInput,
|
|
139
|
+
setFilterQuery,
|
|
140
|
+
handleFilterKeyDown,
|
|
141
|
+
handleFilterKeyUp,
|
|
142
|
+
handleFilterBlur,
|
|
143
|
+
resetFilter
|
|
144
|
+
};
|
|
145
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useEffect, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
import { devWarn } from "@utils";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Warns once per instance while `active`. Raised from an effect, not the render body, so StrictMode's
|
|
7
|
+
* double render doesn't print it twice.
|
|
8
|
+
*/
|
|
9
|
+
export function useIgnoredPropWarning( active: boolean, message: string ): void {
|
|
10
|
+
const warnedRef = useRef( false );
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
if ( active && !warnedRef.current ) {
|
|
13
|
+
warnedRef.current = true;
|
|
14
|
+
devWarn( message );
|
|
15
|
+
}
|
|
16
|
+
}, [ active, message ]);
|
|
17
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from "react";
|
|
2
|
+
|
|
3
|
+
/** Callback ref for the sentinel element. Assign it with `ref={...}`; it takes no `.current`. */
|
|
4
|
+
export type TSentinelRef<T extends HTMLElement = HTMLDivElement> = ( node: T | null ) => void;
|
|
5
|
+
|
|
6
|
+
interface IUseInfiniteScrollArgs {
|
|
7
|
+
/** Whether more items can be loaded. The observer is inert when false. */
|
|
8
|
+
hasMore?: boolean;
|
|
9
|
+
/** In-flight load flag; while true an intersection will not re-trigger a load. */
|
|
10
|
+
isLoading?: boolean;
|
|
11
|
+
/** Called when the sentinel scrolls into view and no load is in flight. */
|
|
12
|
+
onLoadMore?: () => void;
|
|
13
|
+
/** Extra gate (e.g. an open dropdown). The observer is inert when false. @default true */
|
|
14
|
+
enabled?: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Attaches an IntersectionObserver to a sentinel element and fires `onLoadMore` when it
|
|
19
|
+
* scrolls into view. Returns the callback ref to put on the sentinel element.
|
|
20
|
+
*
|
|
21
|
+
* A **callback ref backed by state**, not a `RefObject`, because the sentinel's identity has to be
|
|
22
|
+
* one of the effect's dependencies. The surfaces here unmount the rows — and the sentinel with them —
|
|
23
|
+
* while a search fetch replaces the list, and none of the other dependencies change across that round
|
|
24
|
+
* trip: the observer would stay bound to the detached node and paging would be silently dead for
|
|
25
|
+
* every search result set. A callback ref re-runs the effect on each detach/attach, so the observer
|
|
26
|
+
* always watches the node that is actually in the document.
|
|
27
|
+
*
|
|
28
|
+
* `onLoadMore` is read through a ref so passing a fresh inline callback each render doesn't tear
|
|
29
|
+
* the observer down and recreate it. `isLoading`, by contrast, IS a dependency: no observer exists
|
|
30
|
+
* while a load is in flight, and finishing a load recreates the observer. That re-arm matters —
|
|
31
|
+
* IntersectionObserver only fires on a boundary *crossing*, so a sentinel that stayed in view after
|
|
32
|
+
* a page loaded (e.g. the new page didn't fill the container) would never re-trigger otherwise. A
|
|
33
|
+
* freshly-observed sentinel fires an initial callback with its current intersection, so the next
|
|
34
|
+
* page loads immediately instead of stalling until the user scrolls.
|
|
35
|
+
*/
|
|
36
|
+
export function useInfiniteScroll<T extends HTMLElement = HTMLDivElement>({
|
|
37
|
+
hasMore = false,
|
|
38
|
+
isLoading = false,
|
|
39
|
+
onLoadMore,
|
|
40
|
+
enabled = true
|
|
41
|
+
}: IUseInfiniteScrollArgs ): TSentinelRef<T> {
|
|
42
|
+
// `useState`'s setter is permanently stable, so React never re-invokes the ref for a re-render —
|
|
43
|
+
// only for a genuine detach (null) or attach (node).
|
|
44
|
+
const [ sentinel, setSentinel ] = useState<T | null>( null );
|
|
45
|
+
|
|
46
|
+
const onLoadMoreRef = useRef( onLoadMore );
|
|
47
|
+
onLoadMoreRef.current = onLoadMore;
|
|
48
|
+
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if ( !sentinel || !hasMore || !enabled || isLoading ) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
const observer = new IntersectionObserver(([entry]) => {
|
|
54
|
+
if ( entry.isIntersecting ) {
|
|
55
|
+
onLoadMoreRef.current?.();
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
observer.observe( sentinel );
|
|
59
|
+
return () => observer.disconnect();
|
|
60
|
+
}, [ sentinel, hasMore, enabled, isLoading ]);
|
|
61
|
+
|
|
62
|
+
return setSentinel;
|
|
63
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Detects whether a truncated (`overflow:hidden` + `white-space:nowrap`) element is actually
|
|
5
|
+
* clipping its content, by comparing `scrollWidth` to `clientWidth`. Re-measures on resize.
|
|
6
|
+
*
|
|
7
|
+
* Two things guard against a flicker feedback loop when the caller uses `isTruncated` to expand
|
|
8
|
+
* the element on hover/focus (nowrap -> normal, single line -> wrapped, often via
|
|
9
|
+
* `position:absolute`):
|
|
10
|
+
*
|
|
11
|
+
* 1. `measure()` bails while the element is expanded. Once expanded, the element is laid out to
|
|
12
|
+
* fit its container (`scrollWidth === clientWidth`), so a measurement then would wrongly read
|
|
13
|
+
* "not truncated", strip the expand styles, collapse the element, and re-trigger - forever.
|
|
14
|
+
* We only trust a reading taken in the base (non-expanded) layout, detected via
|
|
15
|
+
* `position !== "absolute"`.
|
|
16
|
+
* 2. Only a change in the measured *width* matters. Expanding the element also changes its
|
|
17
|
+
* *height* (and, through `self-stretch`, its wrapper's height), which would otherwise fire the
|
|
18
|
+
* ResizeObserver mid-hover; tracking the last width lets us ignore those height-only fires.
|
|
19
|
+
*
|
|
20
|
+
* `containerRef` (optional) attaches to an ancestor to observe instead of the element itself
|
|
21
|
+
* (e.g. the flex wrapper around a label); falls back to the measured element when unattached.
|
|
22
|
+
*/
|
|
23
|
+
export function useIsTruncated<T extends HTMLElement, C extends HTMLElement = T>(): {
|
|
24
|
+
ref: React.RefObject<T>;
|
|
25
|
+
containerRef: React.RefObject<C>;
|
|
26
|
+
isTruncated: boolean;
|
|
27
|
+
} {
|
|
28
|
+
const ref = useRef<T>( null );
|
|
29
|
+
const containerRef = useRef<C>( null );
|
|
30
|
+
const [ isTruncated, setIsTruncated ] = useState( false );
|
|
31
|
+
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
const node = ref.current;
|
|
34
|
+
const observedNode = containerRef.current ?? node;
|
|
35
|
+
|
|
36
|
+
if ( !node || !observedNode ) {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let lastWidth = -1;
|
|
41
|
+
|
|
42
|
+
const measure = () => {
|
|
43
|
+
// Ignore the element while it is expanded and any resize that did not change its width -
|
|
44
|
+
// both would otherwise feed a hover flicker loop (see the doc comment above).
|
|
45
|
+
if ( window.getComputedStyle( node ).position === "absolute" ) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const width = node.clientWidth;
|
|
49
|
+
if ( width === lastWidth ) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
lastWidth = width;
|
|
53
|
+
setIsTruncated( node.scrollWidth > width );
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
measure();
|
|
57
|
+
const observer = new ResizeObserver( measure );
|
|
58
|
+
observer.observe( observedNode );
|
|
59
|
+
return () => observer.disconnect();
|
|
60
|
+
}, []);
|
|
61
|
+
|
|
62
|
+
return { ref, containerRef, isTruncated };
|
|
63
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { useEffect, useState, type RefObject } from "react";
|
|
2
|
+
import { useVirtualizer } from "@tanstack/react-virtual";
|
|
3
|
+
|
|
4
|
+
interface IUseListboxKeyboardNavArgs {
|
|
5
|
+
/** Number of navigable rows. */
|
|
6
|
+
itemCount: number;
|
|
7
|
+
/** Whether the row at `index` is disabled (skipped by arrow navigation). */
|
|
8
|
+
isItemDisabled: ( index: number ) => boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Stable string describing the current row SET. The active row resets to the first enabled row
|
|
11
|
+
* whenever this changes (e.g. after a search/filter) - not on every render, which would fight
|
|
12
|
+
* the user's arrow-key position. Callers that must survive a pure reorder (same set, new order)
|
|
13
|
+
* should build this from the values sorted, so a reorder does not change the key.
|
|
14
|
+
*/
|
|
15
|
+
resetKey: string;
|
|
16
|
+
/** Scroll container the virtualizer measures and (when windowed) scrolls the active row into. */
|
|
17
|
+
scrollElementRef: RefObject<HTMLDivElement | null>;
|
|
18
|
+
/**
|
|
19
|
+
* Element the active row is looked up inside (querySelector) for the non-windowed scrollIntoView.
|
|
20
|
+
* @default scrollElementRef
|
|
21
|
+
*/
|
|
22
|
+
queryElementRef?: RefObject<HTMLDivElement | null>;
|
|
23
|
+
/** Maps a row index to its DOM id, used to locate the active row for scrollIntoView. */
|
|
24
|
+
optionId: ( index: number ) => string;
|
|
25
|
+
/** Estimated row height (px) used before real rows are measured; measureElement corrects it. */
|
|
26
|
+
estimateSize: number;
|
|
27
|
+
/** Vertical gap between rows (px). @default 0 */
|
|
28
|
+
gap?: number;
|
|
29
|
+
/** Virtualizer overscan (rows rendered beyond the viewport). @default 12 */
|
|
30
|
+
overscan?: number;
|
|
31
|
+
/** Above this many rows the list windows (virtualizes) itself. @default 100 */
|
|
32
|
+
virtualizeThreshold?: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The shared keyboard-navigation + optional-windowing engine for a single-tab-stop listbox driven
|
|
37
|
+
* by `aria-activedescendant`: arrow/Home/End walk an "active" row (skipping disabled rows, with
|
|
38
|
+
* wrap-around), the active row is kept scrolled into view, and the list virtualizes itself past
|
|
39
|
+
* `virtualizeThreshold`. Consumed by TransferListPanel and MultiSelectTransferDropdown, which keep
|
|
40
|
+
* only their own key-handler switch and row renderer.
|
|
41
|
+
*/
|
|
42
|
+
export function useListboxKeyboardNav({
|
|
43
|
+
itemCount,
|
|
44
|
+
isItemDisabled,
|
|
45
|
+
resetKey,
|
|
46
|
+
scrollElementRef,
|
|
47
|
+
queryElementRef,
|
|
48
|
+
optionId,
|
|
49
|
+
estimateSize,
|
|
50
|
+
gap,
|
|
51
|
+
overscan = 12,
|
|
52
|
+
virtualizeThreshold = 100
|
|
53
|
+
}: IUseListboxKeyboardNavArgs ) {
|
|
54
|
+
// Windowing only kicks in past the threshold. The virtualizer is always instantiated (hooks
|
|
55
|
+
// can't be conditional) but does no work while count is 0.
|
|
56
|
+
const shouldVirtualize = itemCount > virtualizeThreshold;
|
|
57
|
+
const virtualizer = useVirtualizer({
|
|
58
|
+
count: shouldVirtualize ? itemCount : 0,
|
|
59
|
+
getScrollElement: () => scrollElementRef.current,
|
|
60
|
+
estimateSize: () => estimateSize,
|
|
61
|
+
gap,
|
|
62
|
+
// Measure real row heights in the browser; skipped in jsdom where layout is unavailable.
|
|
63
|
+
measureElement:
|
|
64
|
+
typeof window !== "undefined" && typeof window.ResizeObserver !== "undefined"
|
|
65
|
+
? ( element ) => element?.getBoundingClientRect().height
|
|
66
|
+
: undefined,
|
|
67
|
+
overscan
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const firstEnabledIndex = ( from: number, dir: 1 | -1 ): number => {
|
|
71
|
+
for ( let step = 0, i = from; step < itemCount; step++, i = ( i + dir + itemCount ) % itemCount ) {
|
|
72
|
+
if ( !isItemDisabled( i )) {
|
|
73
|
+
return i;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return -1;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const [ activeIndex, setActiveIndex ] = useState( 0 );
|
|
80
|
+
|
|
81
|
+
// Reset the active row to the first enabled one whenever the visible SET changes - keyed on
|
|
82
|
+
// resetKey so a pure reorder (same set) or an unrelated re-render does not yank it to the top.
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
setActiveIndex( firstEnabledIndex( 0, 1 ));
|
|
85
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
86
|
+
}, [resetKey]);
|
|
87
|
+
|
|
88
|
+
// Keep the active row in view as the arrow keys walk it. When windowed the row may not be
|
|
89
|
+
// mounted, so drive the scroll through the virtualizer (which also renders it).
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
if ( activeIndex < 0 ) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if ( shouldVirtualize ) {
|
|
95
|
+
virtualizer.scrollToIndex( activeIndex, { align: "auto" });
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const queryEl = ( queryElementRef ?? scrollElementRef ).current;
|
|
99
|
+
const activeEl = queryEl?.querySelector<HTMLElement>( `#${CSS.escape( optionId( activeIndex ))}` );
|
|
100
|
+
// jsdom (test env) doesn't implement scrollIntoView.
|
|
101
|
+
if ( typeof activeEl?.scrollIntoView === "function" ) {
|
|
102
|
+
activeEl.scrollIntoView({ block: "nearest" });
|
|
103
|
+
}
|
|
104
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
105
|
+
}, [activeIndex]);
|
|
106
|
+
|
|
107
|
+
const moveActive = ( dir: 1 | -1 ) => {
|
|
108
|
+
if ( itemCount === 0 ) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
setActiveIndex(( prev ) => {
|
|
112
|
+
// Guard a -1 (nothing active) start so the first arrow press still lands on a real row.
|
|
113
|
+
const start = prev < 0 ? 0 : prev;
|
|
114
|
+
const next = firstEnabledIndex(( start + dir + itemCount ) % itemCount, dir );
|
|
115
|
+
return next === -1 ? prev : next;
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
return { activeIndex, setActiveIndex, moveActive, firstEnabledIndex, virtualizer, shouldVirtualize };
|
|
120
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useEffect, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Runs `onChange` when `value` CHANGES, never on mount. The ref comparison is the whole point: a
|
|
5
|
+
* plain effect on `[value]` also fires on paint, and the callers here route the change into a reset
|
|
6
|
+
* path that would clobber consumer-seeded state if it ran before the user had touched anything.
|
|
7
|
+
*/
|
|
8
|
+
export function useOnValueChange<TValue>( value: TValue, onChange: () => void ): void {
|
|
9
|
+
const previousRef = useRef( value );
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
if ( previousRef.current === value ) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
previousRef.current = value;
|
|
15
|
+
onChange();
|
|
16
|
+
}, [ value, onChange ]);
|
|
17
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
|
|
3
|
+
import type { TPrefetch } from "./useAsyncOptions";
|
|
4
|
+
|
|
5
|
+
interface IUsePrefetchBaselineArgs {
|
|
6
|
+
/** Which trigger arms the baseline fetch. A surface with no open/close narrows the union. */
|
|
7
|
+
prefetch: TPrefetch;
|
|
8
|
+
/** Whether the surface is currently open. Omit on a surface that is always visible. */
|
|
9
|
+
isOpen?: boolean;
|
|
10
|
+
/** Identity of the option set — a change re-arms an already-armed prefetch. */
|
|
11
|
+
optionsKey?: string | number;
|
|
12
|
+
/** `useAsyncOptions().ensureDefaults`. Idempotent and latch-guarded. */
|
|
13
|
+
ensureDefaults: () => void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Translates a `prefetch` trigger into the one effect that calls `ensureDefaults`. Shared so the
|
|
18
|
+
* three option surfaces cannot answer "when is the baseline fetched" differently.
|
|
19
|
+
*
|
|
20
|
+
* `armed` — not the raw flags — is the dependency, which is what keeps `"mount"` from re-firing when
|
|
21
|
+
* the surface opens (it is already true) while still letting `"open"` fire on the false→true edge.
|
|
22
|
+
* `optionsKey` is a dependency (and only a dependency) so a dependent surface whose parent changes
|
|
23
|
+
* while armed repopulates immediately.
|
|
24
|
+
*/
|
|
25
|
+
export function usePrefetchBaseline({
|
|
26
|
+
prefetch,
|
|
27
|
+
isOpen = false,
|
|
28
|
+
optionsKey,
|
|
29
|
+
ensureDefaults
|
|
30
|
+
}: IUsePrefetchBaselineArgs ): void {
|
|
31
|
+
const armed = prefetch === "mount" || ( prefetch === "open" && isOpen );
|
|
32
|
+
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
if ( armed ) {
|
|
35
|
+
ensureDefaults();
|
|
36
|
+
}
|
|
37
|
+
}, [ armed, optionsKey, ensureDefaults ]);
|
|
38
|
+
}
|
|
@@ -132,14 +132,14 @@ export default {
|
|
|
132
132
|
}
|
|
133
133
|
},
|
|
134
134
|
fontFamily: {
|
|
135
|
-
"regular": [ "
|
|
136
|
-
"italic": [ "
|
|
137
|
-
"medium": [ "
|
|
138
|
-
"medium-italic": [ "
|
|
139
|
-
"semibold": [ "
|
|
140
|
-
"semibold-italic": [ "
|
|
141
|
-
"bold": [ "
|
|
142
|
-
"bold-italic": [ "
|
|
135
|
+
"regular": [ "noto-sans-regular", "sans-serif" ],
|
|
136
|
+
"italic": [ "noto-sans-italic", "sans-serif" ],
|
|
137
|
+
"medium": [ "noto-sans-medium", "sans-serif" ],
|
|
138
|
+
"medium-italic": [ "noto-sans-medium-italic", "sans-serif" ],
|
|
139
|
+
"semibold": [ "noto-sans-semibold", "sans-serif" ],
|
|
140
|
+
"semibold-italic": [ "noto-sans-semibold-italic", "sans-serif" ],
|
|
141
|
+
"bold": [ "noto-sans-bold", "sans-serif" ],
|
|
142
|
+
"bold-italic": [ "noto-sans-bold-italic", "sans-serif" ]
|
|
143
143
|
},
|
|
144
144
|
fontSize: {
|
|
145
145
|
xxs: [ "0.625rem", { lineHeight: "100%" }], // font-size: 10px
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dev-only warning shim. The `typeof process` guard keeps unbundled ESM consumers safe — the Rollup
|
|
3
|
+
* build has no `@rollup/plugin-replace`, so `process.env.NODE_ENV` is not substituted at build time.
|
|
4
|
+
*/
|
|
5
|
+
export function devWarn( message: string ): void {
|
|
6
|
+
if ( typeof process !== "undefined" && process.env.NODE_ENV !== "production" ) {
|
|
7
|
+
console.warn( message );
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { semanticInteractiveElements } from "@constants";
|
|
2
|
+
import React from "react";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Determines whether a trigger node is already a natively-interactive,
|
|
6
|
+
* keyboard-focusable element (e.g. `<button>`, `<a>`, `<input>`).
|
|
7
|
+
*
|
|
8
|
+
* Only intrinsic HTML elements from the {@link semanticInteractiveElements}
|
|
9
|
+
* allowlist are treated as semantic. Custom React components are intentionally
|
|
10
|
+
* excluded: their rendered output cannot be introspected at runtime, so a
|
|
11
|
+
* component that renders a non-interactive element (such as an icon or `<span>`)
|
|
12
|
+
* would otherwise be stripped of its supplied interactivity/focus affordances
|
|
13
|
+
* and become keyboard-inaccessible. Excluded triggers fall back to being wrapped
|
|
14
|
+
* with the appropriate role/tabindex, which keeps them keyboard accessible.
|
|
15
|
+
*
|
|
16
|
+
* If you need a custom component to be treated as the interactive trigger, have
|
|
17
|
+
* it render one of the allowlisted native elements as its root and forward props
|
|
18
|
+
* and ref to it.
|
|
19
|
+
*/
|
|
20
|
+
export const isSemanticTrigger = ( ctaContent: React.ReactNode ): boolean => {
|
|
21
|
+
return (
|
|
22
|
+
React.isValidElement( ctaContent ) &&
|
|
23
|
+
typeof ctaContent.type === "string" &&
|
|
24
|
+
semanticInteractiveElements.includes( ctaContent.type )
|
|
25
|
+
);
|
|
26
|
+
};
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
export * from "./numberUtils";
|
|
2
2
|
export * from "./stringUtils";
|
|
3
3
|
export * from "./findParentAttribute";
|
|
4
|
-
export
|
|
4
|
+
export * from "./elementUtils";
|
|
5
|
+
export * from "./debounce";
|
|
6
|
+
export * from "./matchesQuery";
|
|
7
|
+
export * from "./mergeUnique";
|
|
8
|
+
export * from "./devWarn";
|
|
9
|
+
export * from "./resolveLoadErrors";
|
|
10
|
+
export * from "./resolveReloadControls";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Case-insensitive substring match used for the client-side search in TransferList,
|
|
5
|
+
* MultiSelectTransfer and Select. Match precedence: an explicit `searchValue` first (the way to
|
|
6
|
+
* make an item with a ReactNode `label` searchable by its visible text), then a string `label`,
|
|
7
|
+
* then the stringified `value`. An empty query matches everything. Kept internal (not re-exported
|
|
8
|
+
* from the package index).
|
|
9
|
+
*/
|
|
10
|
+
export function matchesQuery(
|
|
11
|
+
item: { label: ReactNode; value: unknown; searchValue?: string },
|
|
12
|
+
query: string
|
|
13
|
+
): boolean {
|
|
14
|
+
if ( !query ) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
const needle = query.toLowerCase();
|
|
18
|
+
if ( typeof item.searchValue === "string" && item.searchValue.length > 0 ) {
|
|
19
|
+
return item.searchValue.toLowerCase().includes( needle );
|
|
20
|
+
}
|
|
21
|
+
const labelMatch = typeof item.label === "string" && item.label.toLowerCase().includes( needle );
|
|
22
|
+
return labelMatch || String( item.value ).toLowerCase().includes( needle );
|
|
23
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Concatenates `sources` in order, keeping the first occurrence of each key and dropping anything in
|
|
3
|
+
* `excluded`. Iterating the sources directly rather than spreading them into one array matters here:
|
|
4
|
+
* the callers re-run this on every landed page, over the whole pool.
|
|
5
|
+
*/
|
|
6
|
+
export function mergeUnique<TItem, TKey>(
|
|
7
|
+
sources: readonly Iterable<TItem>[],
|
|
8
|
+
getKey: ( item: TItem ) => TKey,
|
|
9
|
+
excluded?: ReadonlySet<TKey>
|
|
10
|
+
): TItem[] {
|
|
11
|
+
const seen = new Set<TKey>();
|
|
12
|
+
const merged: TItem[] = [];
|
|
13
|
+
for ( const source of sources ) {
|
|
14
|
+
for ( const item of source ) {
|
|
15
|
+
const key = getKey( item );
|
|
16
|
+
if ( seen.has( key ) || excluded?.has( key ) === true ) {
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
seen.add( key );
|
|
20
|
+
merged.push( item );
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return merged;
|
|
24
|
+
}
|