@tangible/ui 0.2.8 → 0.2.10
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/components/Combobox/Combobox.d.ts +16 -11
- package/components/Combobox/Combobox.js +42 -196
- package/components/Combobox/types.d.ts +3 -7
- package/components/MultiCombobox/MultiCombobox.d.ts +16 -11
- package/components/MultiCombobox/MultiCombobox.js +42 -154
- package/components/MultiCombobox/types.d.ts +4 -7
- package/components/MultiSelect/MultiSelect.d.ts +16 -11
- package/components/MultiSelect/MultiSelect.js +70 -207
- package/components/MultiSelect/types.d.ts +5 -8
- package/components/SearchSelect/SearchSelect.d.ts +34 -0
- package/components/SearchSelect/SearchSelect.js +462 -0
- package/components/SearchSelect/SearchSelectContext.d.ts +7 -0
- package/components/SearchSelect/SearchSelectContext.js +21 -0
- package/components/SearchSelect/index.d.ts +2 -0
- package/components/SearchSelect/index.js +1 -0
- package/components/SearchSelect/types.d.ts +153 -0
- package/components/SearchSelect/types.js +5 -0
- package/components/Select/Select.d.ts +16 -11
- package/components/Select/Select.js +78 -237
- package/components/Select/types.d.ts +5 -8
- package/components/index.d.ts +2 -0
- package/components/index.js +1 -0
- package/package.json +5 -1
- package/styles/all.css +1 -1
- package/styles/all.expanded.css +180 -0
- package/styles/all.expanded.unlayered.css +180 -0
- package/styles/all.unlayered.css +1 -1
- package/styles/components/_bundle.scss +8 -4
- package/styles/components/search-select.scss +244 -0
- package/tui-manifest.json +74 -2
- package/utils/listbox/blur.d.ts +14 -0
- package/utils/listbox/blur.js +51 -0
- package/utils/listbox/filter-active-index.d.ts +16 -0
- package/utils/listbox/filter-active-index.js +72 -0
- package/utils/listbox/floating.d.ts +7 -0
- package/utils/listbox/floating.js +34 -0
- package/utils/listbox/group.d.ts +26 -0
- package/utils/listbox/group.js +40 -0
- package/utils/listbox/ids.d.ts +9 -0
- package/utils/listbox/ids.js +31 -0
- package/utils/listbox/index.d.ts +9 -0
- package/utils/listbox/index.js +11 -0
- package/utils/listbox/navigation.d.ts +27 -0
- package/utils/listbox/navigation.js +53 -0
- package/utils/listbox/registry.d.ts +61 -0
- package/utils/listbox/registry.js +121 -0
- package/utils/listbox/scroll.d.ts +7 -0
- package/utils/listbox/scroll.js +15 -0
- package/utils/listbox/trigger.d.ts +16 -0
- package/utils/listbox/trigger.js +35 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useId } from 'react';
|
|
2
|
+
import { hashForId } from '../hash.js';
|
|
3
|
+
import { toKey } from '../value-key.js';
|
|
4
|
+
// =============================================================================
|
|
5
|
+
// Listbox ID plumbing
|
|
6
|
+
// =============================================================================
|
|
7
|
+
//
|
|
8
|
+
// Two option-ID schemes exist deliberately — do not unify:
|
|
9
|
+
// - INDEX-based (Select/MultiSelect): `${listboxId}-option-${index}`. Values
|
|
10
|
+
// may contain characters that are invalid in HTML IDs, so the index is the
|
|
11
|
+
// safe key. Static lists — indices are stable.
|
|
12
|
+
// - HASH-based (Combobox/MultiCombobox): `${listboxId}-opt-${hash}`. Filtered
|
|
13
|
+
// lists shift indices as the user types, so IDs key off the value instead
|
|
14
|
+
// (FNV-1a hash keeps them ID-safe).
|
|
15
|
+
//
|
|
16
|
+
// =============================================================================
|
|
17
|
+
export function useListboxIds(controlIdProp, controlSuffix) {
|
|
18
|
+
const baseId = useId();
|
|
19
|
+
return {
|
|
20
|
+
controlId: controlIdProp ?? `${baseId}-${controlSuffix}`,
|
|
21
|
+
listboxId: `${baseId}-listbox`,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/** Index-based option ID (Select/MultiSelect). Undefined when unregistered. */
|
|
25
|
+
export function optionIdForIndex(listboxId, index) {
|
|
26
|
+
return index >= 0 ? `${listboxId}-option-${index}` : undefined;
|
|
27
|
+
}
|
|
28
|
+
/** Hash-based option ID (Combobox/MultiCombobox) — stable across filtering. */
|
|
29
|
+
export function optionIdForValue(listboxId, value) {
|
|
30
|
+
return `${listboxId}-opt-${hashForId(toKey(value))}`;
|
|
31
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { useOptionRegistry, useOptionRegistration, deriveOptionText, type RegisteredOption, type OptionRegistry, } from './registry';
|
|
2
|
+
export { useListboxIds, optionIdForIndex, optionIdForValue } from './ids';
|
|
3
|
+
export { useListboxFloating } from './floating';
|
|
4
|
+
export { useActiveOptionScroll } from './scroll';
|
|
5
|
+
export { createListboxGroupComponents, type ListboxGroupProps, type ListboxLabelProps, } from './group';
|
|
6
|
+
export { useListboxNavigation } from './navigation';
|
|
7
|
+
export { useFilterStableActiveIndex } from './filter-active-index';
|
|
8
|
+
export { useListboxBlurGuard } from './blur';
|
|
9
|
+
export { useTriggerPointerDownFix, useFocusTriggerOnOpen } from './trigger';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Shared listbox engine — internal to TUI, not exported from the package.
|
|
2
|
+
// Consumers: Select, MultiSelect, Combobox, MultiCombobox (and SearchSelect).
|
|
3
|
+
export { useOptionRegistry, useOptionRegistration, deriveOptionText, } from './registry.js';
|
|
4
|
+
export { useListboxIds, optionIdForIndex, optionIdForValue } from './ids.js';
|
|
5
|
+
export { useListboxFloating } from './floating.js';
|
|
6
|
+
export { useActiveOptionScroll } from './scroll.js';
|
|
7
|
+
export { createListboxGroupComponents, } from './group.js';
|
|
8
|
+
export { useListboxNavigation } from './navigation.js';
|
|
9
|
+
export { useFilterStableActiveIndex } from './filter-active-index.js';
|
|
10
|
+
export { useListboxBlurGuard } from './blur.js';
|
|
11
|
+
export { useTriggerPointerDownFix, useFocusTriggerOnOpen } from './trigger.js';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type MutableRefObject } from 'react';
|
|
2
|
+
import { type FloatingContext } from '@floating-ui/react';
|
|
3
|
+
export declare function useListboxNavigation(context: FloatingContext, options: {
|
|
4
|
+
listRef: MutableRefObject<(HTMLElement | null)[]>;
|
|
5
|
+
/** Floating UI model: null = no active option (callers adapt sentinels). */
|
|
6
|
+
activeIndex: number | null;
|
|
7
|
+
onNavigate: (index: number | null) => void;
|
|
8
|
+
disabledIndices: number[];
|
|
9
|
+
/** Open on trigger click (Select/MultiSelect). Inputs open themselves. */
|
|
10
|
+
withClick: boolean;
|
|
11
|
+
/** Only spread when defined — Floating UI's default ('auto') differs from false. */
|
|
12
|
+
focusItemOnOpen?: boolean;
|
|
13
|
+
/** Select only: seed navigation from the selected option. */
|
|
14
|
+
selectedIndex?: number;
|
|
15
|
+
/** Enables typeahead (Select/MultiSelect). onMatch keeps caller guards local. */
|
|
16
|
+
typeahead?: {
|
|
17
|
+
listContentRef: MutableRefObject<string[]>;
|
|
18
|
+
onMatch: (index: number) => void;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Custom useDismiss outsidePress predicate. Needed when the component
|
|
22
|
+
* toggles open from its own click handler on an element that is NOT the
|
|
23
|
+
* domReference (SearchSelect's trigger): without excluding it, dismiss
|
|
24
|
+
* closes on pointerdown and the click handler re-opens — a flicker loop.
|
|
25
|
+
*/
|
|
26
|
+
dismissOutsidePress?: (event: MouseEvent) => boolean;
|
|
27
|
+
}): import("@floating-ui/react").UseInteractionsReturn;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { useRef } from 'react';
|
|
2
|
+
import { useClick, useDismiss, useInteractions, useListNavigation, useRole, useTypeahead, } from '@floating-ui/react';
|
|
3
|
+
// =============================================================================
|
|
4
|
+
// Listbox navigation composition
|
|
5
|
+
// =============================================================================
|
|
6
|
+
//
|
|
7
|
+
// The shared useInteractions stack. Composition ORDER is load-bearing — it
|
|
8
|
+
// decides which composed handler's preventDefault wins (see the Floating UI
|
|
9
|
+
// composed-handlers gotcha in CLAUDE.md):
|
|
10
|
+
//
|
|
11
|
+
// [click, dismiss, role, listNavigation, typeahead]
|
|
12
|
+
//
|
|
13
|
+
// Trigger-style listboxes (Select/MultiSelect) enable click + typeahead;
|
|
14
|
+
// input-style ones (Combobox/MultiCombobox) disable both via Floating UI's
|
|
15
|
+
// `enabled` option — a disabled hook contributes no props, so the effective
|
|
16
|
+
// stack matches their historic [dismiss, role, listNavigation].
|
|
17
|
+
//
|
|
18
|
+
// The activeIndex MODEL stays with the caller: Select/MultiSelect use
|
|
19
|
+
// `number | null`, Combobox/MultiCombobox use a `-1` sentinel and adapt at
|
|
20
|
+
// the call site (`activeIndex >= 0 ? activeIndex : null`). Deliberately not
|
|
21
|
+
// unified — the sentinel leaks into their content contexts and option
|
|
22
|
+
// highlighting.
|
|
23
|
+
//
|
|
24
|
+
// =============================================================================
|
|
25
|
+
export function useListboxNavigation(context, options) {
|
|
26
|
+
const { listRef, activeIndex, onNavigate, disabledIndices, withClick, focusItemOnOpen, selectedIndex, typeahead: typeaheadOptions, dismissOutsidePress, } = options;
|
|
27
|
+
// Use 'click' (not 'mousedown') so the trigger has focus when the dropdown
|
|
28
|
+
// opens — keyboard navigation works immediately.
|
|
29
|
+
const click = useClick(context, { enabled: withClick });
|
|
30
|
+
const dismiss = useDismiss(context, dismissOutsidePress ? { outsidePress: dismissOutsidePress } : undefined);
|
|
31
|
+
const role = useRole(context, { role: 'listbox' });
|
|
32
|
+
const listNavigation = useListNavigation(context, {
|
|
33
|
+
listRef,
|
|
34
|
+
activeIndex,
|
|
35
|
+
onNavigate,
|
|
36
|
+
loop: true,
|
|
37
|
+
virtual: true,
|
|
38
|
+
...(focusItemOnOpen !== undefined ? { focusItemOnOpen } : {}),
|
|
39
|
+
...(selectedIndex !== undefined ? { selectedIndex } : {}),
|
|
40
|
+
disabledIndices,
|
|
41
|
+
});
|
|
42
|
+
// Hooks must be called unconditionally; a disabled typeahead gets an empty
|
|
43
|
+
// list and a no-op match.
|
|
44
|
+
const emptyContentRef = useRef([]);
|
|
45
|
+
const typeahead = useTypeahead(context, {
|
|
46
|
+
enabled: typeaheadOptions !== undefined,
|
|
47
|
+
listRef: typeaheadOptions?.listContentRef ?? emptyContentRef,
|
|
48
|
+
activeIndex,
|
|
49
|
+
onMatch: typeaheadOptions?.onMatch ?? (() => { }),
|
|
50
|
+
resetMs: 500,
|
|
51
|
+
});
|
|
52
|
+
return useInteractions([click, dismiss, role, listNavigation, typeahead]);
|
|
53
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { type MutableRefObject, type ReactNode, type RefObject } from 'react';
|
|
2
|
+
import { type OptionValue } from '../value-key';
|
|
3
|
+
export type RegisteredOption = {
|
|
4
|
+
value: OptionValue;
|
|
5
|
+
ref: RefObject<HTMLElement | null>;
|
|
6
|
+
disabled: boolean;
|
|
7
|
+
textValue: string;
|
|
8
|
+
};
|
|
9
|
+
export type OptionRegistry = {
|
|
10
|
+
/** Raw registry map — keyed by `toKey(value)`. */
|
|
11
|
+
optionsRef: MutableRefObject<Map<string, RegisteredOption>>;
|
|
12
|
+
/** Bumped on every register/unregister; dependency for derived memos. */
|
|
13
|
+
registryVersion: number;
|
|
14
|
+
registerOption: (option: RegisteredOption) => void;
|
|
15
|
+
unregisterOption: (optionValue: OptionValue) => void;
|
|
16
|
+
/** Options in DOM order. Identity changes only with registryVersion. */
|
|
17
|
+
orderedOptions: RegisteredOption[];
|
|
18
|
+
/** Render-fresh mirror of orderedOptions for stable callbacks. */
|
|
19
|
+
orderedOptionsRef: MutableRefObject<RegisteredOption[]>;
|
|
20
|
+
/** O(1) key → index lookup into orderedOptions. */
|
|
21
|
+
optionIndexMap: Map<string, number>;
|
|
22
|
+
};
|
|
23
|
+
export declare function useOptionRegistry(options: {
|
|
24
|
+
open: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* When provided, receives the ordered textValues synchronously inside the
|
|
27
|
+
* orderedOptions memo (during render — safe for refs). This is the
|
|
28
|
+
* typeahead source for Select/MultiSelect.
|
|
29
|
+
*/
|
|
30
|
+
listContentRef?: MutableRefObject<string[]>;
|
|
31
|
+
}): OptionRegistry;
|
|
32
|
+
/**
|
|
33
|
+
* The single derivation rule for an option's text: explicit textValue wins,
|
|
34
|
+
* else string children, else ''. Exported so filtering logic (which needs the
|
|
35
|
+
* text BEFORE the registration hook runs) can't drift from the hook's own
|
|
36
|
+
* derivation.
|
|
37
|
+
*/
|
|
38
|
+
export declare function deriveOptionText(children: ReactNode, explicitTextValue: string | undefined): string;
|
|
39
|
+
/**
|
|
40
|
+
* The Option-component half: derives textValue from children when not
|
|
41
|
+
* explicit, warns once in dev when neither yields text, and registers/
|
|
42
|
+
* unregisters via layout effect. Returns the resolved textValue.
|
|
43
|
+
*/
|
|
44
|
+
export declare function useOptionRegistration(options: {
|
|
45
|
+
value: OptionValue;
|
|
46
|
+
disabled: boolean;
|
|
47
|
+
textValue: string | undefined;
|
|
48
|
+
children: ReactNode;
|
|
49
|
+
/** For the dev warning, e.g. 'Select.Option'. */
|
|
50
|
+
componentName: string;
|
|
51
|
+
elementRef: RefObject<HTMLElement | null>;
|
|
52
|
+
registerOption: (option: RegisteredOption) => void;
|
|
53
|
+
unregisterOption: (optionValue: OptionValue) => void;
|
|
54
|
+
/**
|
|
55
|
+
* When false the option does not register (and unregisters if it was) —
|
|
56
|
+
* used by built-in filtering (SearchSelect), where a non-matching option
|
|
57
|
+
* renders null and must leave the registry so navigation skips it.
|
|
58
|
+
* @default true
|
|
59
|
+
*/
|
|
60
|
+
enabled?: boolean;
|
|
61
|
+
}): string;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, } from 'react';
|
|
2
|
+
import { isDev } from '../is-dev.js';
|
|
3
|
+
import { toKey } from '../value-key.js';
|
|
4
|
+
export function useOptionRegistry(options) {
|
|
5
|
+
const { open, listContentRef } = options;
|
|
6
|
+
const optionsRef = useRef(new Map());
|
|
7
|
+
const [registryVersion, setRegistryVersion] = useState(0);
|
|
8
|
+
// Track open state via ref so unregisterOption can check synchronously
|
|
9
|
+
const openRef = useRef(false);
|
|
10
|
+
openRef.current = open;
|
|
11
|
+
// Flag: when the dropdown opens, the first registerOption call clears the
|
|
12
|
+
// registry before adding. This ensures stale entries from options that
|
|
13
|
+
// changed while closed don't persist — and avoids a useLayoutEffect ordering
|
|
14
|
+
// issue where a parent clear would run after children register.
|
|
15
|
+
const prevOpenForFlush = useRef(open);
|
|
16
|
+
const needsFlushRef = useRef(false);
|
|
17
|
+
if (open && !prevOpenForFlush.current)
|
|
18
|
+
needsFlushRef.current = true;
|
|
19
|
+
prevOpenForFlush.current = open;
|
|
20
|
+
// Register option (stable callback - no value/display deps)
|
|
21
|
+
const registerOption = useCallback((option) => {
|
|
22
|
+
if (needsFlushRef.current) {
|
|
23
|
+
optionsRef.current.clear();
|
|
24
|
+
needsFlushRef.current = false;
|
|
25
|
+
}
|
|
26
|
+
optionsRef.current.set(toKey(option.value), option);
|
|
27
|
+
setRegistryVersion((v) => v + 1);
|
|
28
|
+
}, []);
|
|
29
|
+
const unregisterOption = useCallback((optionValue) => {
|
|
30
|
+
// When the dropdown closes, options unmount and call unregister in cleanup.
|
|
31
|
+
// Skip the delete to preserve the registry — display text, chip labels and
|
|
32
|
+
// filtering depend on it while closed. Options re-register on next open
|
|
33
|
+
// (same keys).
|
|
34
|
+
if (!openRef.current)
|
|
35
|
+
return;
|
|
36
|
+
optionsRef.current.delete(toKey(optionValue));
|
|
37
|
+
setRegistryVersion((v) => v + 1);
|
|
38
|
+
}, []);
|
|
39
|
+
// Get ordered options (DOM order)
|
|
40
|
+
const getOrderedOptions = useCallback(() => {
|
|
41
|
+
const opts = Array.from(optionsRef.current.values());
|
|
42
|
+
return opts.sort((a, b) => {
|
|
43
|
+
if (!a.ref.current || !b.ref.current)
|
|
44
|
+
return 0;
|
|
45
|
+
const position = a.ref.current.compareDocumentPosition(b.ref.current);
|
|
46
|
+
if (position & Node.DOCUMENT_POSITION_FOLLOWING)
|
|
47
|
+
return -1;
|
|
48
|
+
if (position & Node.DOCUMENT_POSITION_PRECEDING)
|
|
49
|
+
return 1;
|
|
50
|
+
return 0;
|
|
51
|
+
});
|
|
52
|
+
}, []);
|
|
53
|
+
// Memoized ordered options - recompute when registry changes.
|
|
54
|
+
// Also update listContentRef synchronously for typeahead.
|
|
55
|
+
const orderedOptions = useMemo(() => {
|
|
56
|
+
const opts = getOrderedOptions();
|
|
57
|
+
if (listContentRef) {
|
|
58
|
+
// Update typeahead ref synchronously (during render is safe for refs)
|
|
59
|
+
listContentRef.current = opts.map((opt) => opt.textValue);
|
|
60
|
+
}
|
|
61
|
+
return opts;
|
|
62
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
63
|
+
}, [registryVersion]);
|
|
64
|
+
// O(1) index lookup map — rebuilt when orderedOptions changes
|
|
65
|
+
const optionIndexMap = useMemo(() => {
|
|
66
|
+
const map = new Map();
|
|
67
|
+
orderedOptions.forEach((opt, i) => map.set(toKey(opt.value), i));
|
|
68
|
+
return map;
|
|
69
|
+
}, [orderedOptions]);
|
|
70
|
+
// Ref for stable callbacks — avoids recreating them on registry changes
|
|
71
|
+
const orderedOptionsRef = useRef(orderedOptions);
|
|
72
|
+
orderedOptionsRef.current = orderedOptions;
|
|
73
|
+
return {
|
|
74
|
+
optionsRef,
|
|
75
|
+
registryVersion,
|
|
76
|
+
registerOption,
|
|
77
|
+
unregisterOption,
|
|
78
|
+
orderedOptions,
|
|
79
|
+
orderedOptionsRef,
|
|
80
|
+
optionIndexMap,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
// =============================================================================
|
|
84
|
+
// Option-side registration
|
|
85
|
+
// =============================================================================
|
|
86
|
+
/**
|
|
87
|
+
* The single derivation rule for an option's text: explicit textValue wins,
|
|
88
|
+
* else string children, else ''. Exported so filtering logic (which needs the
|
|
89
|
+
* text BEFORE the registration hook runs) can't drift from the hook's own
|
|
90
|
+
* derivation.
|
|
91
|
+
*/
|
|
92
|
+
export function deriveOptionText(children, explicitTextValue) {
|
|
93
|
+
return explicitTextValue ?? (typeof children === 'string' ? children : '');
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* The Option-component half: derives textValue from children when not
|
|
97
|
+
* explicit, warns once in dev when neither yields text, and registers/
|
|
98
|
+
* unregisters via layout effect. Returns the resolved textValue.
|
|
99
|
+
*/
|
|
100
|
+
export function useOptionRegistration(options) {
|
|
101
|
+
const { value, disabled, textValue: explicitTextValue, children, componentName, elementRef, registerOption, unregisterOption, enabled = true, } = options;
|
|
102
|
+
// Derive textValue from children if not explicitly provided
|
|
103
|
+
const textValue = deriveOptionText(children, explicitTextValue);
|
|
104
|
+
// Warn in dev if textValue couldn't be derived (once per mount)
|
|
105
|
+
const warnedTextValueRef = useRef(false);
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
if (isDev() && !textValue && !warnedTextValueRef.current) {
|
|
108
|
+
warnedTextValueRef.current = true;
|
|
109
|
+
console.warn(`${componentName} with value="${value}" has no textValue. Provide textValue prop when children is not a string.`);
|
|
110
|
+
}
|
|
111
|
+
}, [textValue, value, componentName]);
|
|
112
|
+
// Register option
|
|
113
|
+
useLayoutEffect(() => {
|
|
114
|
+
if (!enabled)
|
|
115
|
+
return;
|
|
116
|
+
registerOption({ value, ref: elementRef, disabled, textValue });
|
|
117
|
+
return () => unregisterOption(value);
|
|
118
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
119
|
+
}, [value, disabled, textValue, registerOption, unregisterOption, enabled]);
|
|
120
|
+
return textValue;
|
|
121
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type MutableRefObject } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Scroll the active option into view while the listbox is open. Accepts both
|
|
4
|
+
* activeIndex models: `number | null` (Select/MultiSelect) and `-1` sentinel
|
|
5
|
+
* (Combobox/MultiCombobox) — null and negative both mean "no active option".
|
|
6
|
+
*/
|
|
7
|
+
export declare function useActiveOptionScroll(open: boolean, activeIndex: number | null, listRef: MutableRefObject<(HTMLElement | null)[]>): void;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Scroll the active option into view while the listbox is open. Accepts both
|
|
4
|
+
* activeIndex models: `number | null` (Select/MultiSelect) and `-1` sentinel
|
|
5
|
+
* (Combobox/MultiCombobox) — null and negative both mean "no active option".
|
|
6
|
+
*/
|
|
7
|
+
export function useActiveOptionScroll(open, activeIndex, listRef) {
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
if (open && activeIndex !== null && activeIndex >= 0 && listRef.current[activeIndex]) {
|
|
10
|
+
listRef.current[activeIndex]?.scrollIntoView({ block: 'nearest' });
|
|
11
|
+
}
|
|
12
|
+
// listRef is a ref — stable identity, reading .current in an effect is fine
|
|
13
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
14
|
+
}, [open, activeIndex]);
|
|
15
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safari doesn't focus buttons on mousedown — focus jumps to `<body>`, firing
|
|
3
|
+
* a blur that the blur guard reads as focus-loss and closes the menu, so a
|
|
4
|
+
* click on the open trigger closes-then-reopens and never visibly toggles.
|
|
5
|
+
* Focus the trigger ourselves and cancel the browser's focus juggling.
|
|
6
|
+
* Mouse/pen only: cancelling pointerdown on touch swallows the compatibility
|
|
7
|
+
* click.
|
|
8
|
+
*/
|
|
9
|
+
export declare function useTriggerPointerDownFix(): (e: React.PointerEvent) => void;
|
|
10
|
+
/**
|
|
11
|
+
* Ensure the trigger has focus when the dropdown opens (Safari doesn't focus
|
|
12
|
+
* buttons on click) — keyboard navigation must work immediately.
|
|
13
|
+
*/
|
|
14
|
+
export declare function useFocusTriggerOnOpen(open: boolean, referenceRef: {
|
|
15
|
+
current: unknown;
|
|
16
|
+
}): void;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { useCallback, useEffect } from 'react';
|
|
2
|
+
// =============================================================================
|
|
3
|
+
// Trigger-button fixes (Select/MultiSelect)
|
|
4
|
+
// =============================================================================
|
|
5
|
+
/**
|
|
6
|
+
* Safari doesn't focus buttons on mousedown — focus jumps to `<body>`, firing
|
|
7
|
+
* a blur that the blur guard reads as focus-loss and closes the menu, so a
|
|
8
|
+
* click on the open trigger closes-then-reopens and never visibly toggles.
|
|
9
|
+
* Focus the trigger ourselves and cancel the browser's focus juggling.
|
|
10
|
+
* Mouse/pen only: cancelling pointerdown on touch swallows the compatibility
|
|
11
|
+
* click.
|
|
12
|
+
*/
|
|
13
|
+
export function useTriggerPointerDownFix() {
|
|
14
|
+
return useCallback((e) => {
|
|
15
|
+
if (e.pointerType !== 'mouse' && e.pointerType !== 'pen')
|
|
16
|
+
return;
|
|
17
|
+
if (e.button !== 0)
|
|
18
|
+
return;
|
|
19
|
+
e.preventDefault();
|
|
20
|
+
e.currentTarget.focus();
|
|
21
|
+
}, []);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Ensure the trigger has focus when the dropdown opens (Safari doesn't focus
|
|
25
|
+
* buttons on click) — keyboard navigation must work immediately.
|
|
26
|
+
*/
|
|
27
|
+
export function useFocusTriggerOnOpen(open, referenceRef) {
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (open && referenceRef.current) {
|
|
30
|
+
referenceRef.current.focus();
|
|
31
|
+
}
|
|
32
|
+
// referenceRef is a stable ref
|
|
33
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
34
|
+
}, [open]);
|
|
35
|
+
}
|