@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.
Files changed (50) hide show
  1. package/components/Combobox/Combobox.d.ts +16 -11
  2. package/components/Combobox/Combobox.js +42 -196
  3. package/components/Combobox/types.d.ts +3 -7
  4. package/components/MultiCombobox/MultiCombobox.d.ts +16 -11
  5. package/components/MultiCombobox/MultiCombobox.js +42 -154
  6. package/components/MultiCombobox/types.d.ts +4 -7
  7. package/components/MultiSelect/MultiSelect.d.ts +16 -11
  8. package/components/MultiSelect/MultiSelect.js +70 -207
  9. package/components/MultiSelect/types.d.ts +5 -8
  10. package/components/SearchSelect/SearchSelect.d.ts +34 -0
  11. package/components/SearchSelect/SearchSelect.js +462 -0
  12. package/components/SearchSelect/SearchSelectContext.d.ts +7 -0
  13. package/components/SearchSelect/SearchSelectContext.js +21 -0
  14. package/components/SearchSelect/index.d.ts +2 -0
  15. package/components/SearchSelect/index.js +1 -0
  16. package/components/SearchSelect/types.d.ts +153 -0
  17. package/components/SearchSelect/types.js +5 -0
  18. package/components/Select/Select.d.ts +16 -11
  19. package/components/Select/Select.js +78 -237
  20. package/components/Select/types.d.ts +5 -8
  21. package/components/index.d.ts +2 -0
  22. package/components/index.js +1 -0
  23. package/package.json +5 -1
  24. package/styles/all.css +1 -1
  25. package/styles/all.expanded.css +180 -0
  26. package/styles/all.expanded.unlayered.css +180 -0
  27. package/styles/all.unlayered.css +1 -1
  28. package/styles/components/_bundle.scss +8 -4
  29. package/styles/components/search-select.scss +244 -0
  30. package/tui-manifest.json +74 -2
  31. package/utils/listbox/blur.d.ts +14 -0
  32. package/utils/listbox/blur.js +51 -0
  33. package/utils/listbox/filter-active-index.d.ts +16 -0
  34. package/utils/listbox/filter-active-index.js +72 -0
  35. package/utils/listbox/floating.d.ts +7 -0
  36. package/utils/listbox/floating.js +34 -0
  37. package/utils/listbox/group.d.ts +26 -0
  38. package/utils/listbox/group.js +40 -0
  39. package/utils/listbox/ids.d.ts +9 -0
  40. package/utils/listbox/ids.js +31 -0
  41. package/utils/listbox/index.d.ts +9 -0
  42. package/utils/listbox/index.js +11 -0
  43. package/utils/listbox/navigation.d.ts +27 -0
  44. package/utils/listbox/navigation.js +53 -0
  45. package/utils/listbox/registry.d.ts +61 -0
  46. package/utils/listbox/registry.js +121 -0
  47. package/utils/listbox/scroll.d.ts +7 -0
  48. package/utils/listbox/scroll.js +15 -0
  49. package/utils/listbox/trigger.d.ts +16 -0
  50. package/utils/listbox/trigger.js +35 -0
@@ -1,11 +1,10 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
- import React, { useCallback, useEffect, useId, useLayoutEffect, useMemo, useRef, useState, } from 'react';
3
- import { isDev } from '../../utils/is-dev.js';
4
- import { useFloating, offset, flip, shift, size as sizeMiddleware, autoUpdate, FloatingPortal, useDismiss, useInteractions, useListNavigation, useRole, } from '@floating-ui/react';
2
+ import React, { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, } from 'react';
3
+ import { FloatingPortal } from '@floating-ui/react';
5
4
  import { cx } from '../../utils/cx.js';
6
5
  import { toKey } from '../../utils/value-key.js';
7
6
  import { getPortalRootFor } from '../../utils/portal.js';
8
- import { hashForId } from '../../utils/hash.js';
7
+ import { useOptionRegistry, useOptionRegistration, useListboxIds, useListboxFloating, useListboxNavigation, useListboxBlurGuard, useActiveOptionScroll, optionIdForValue, createListboxGroupComponents, } from '../../utils/listbox/index.js';
9
8
  import { composeEventHandlers } from '../../utils/compose-events.js';
10
9
  import { Icon } from '../Icon/index.js';
11
10
  import { Chip } from '../Chip/index.js';
@@ -50,16 +49,10 @@ function MultiComboboxRoot({ value: controlledValue, defaultValue, onValueChange
50
49
  }
51
50
  }, [query, onQueryChange]);
52
51
  const maxReached = max !== undefined && value.length >= max;
53
- // Option registration
54
- const optionsRef = useRef(new Map());
55
- const [registryVersion, setRegistryVersion] = useState(0);
56
- // Track open state via ref so unregisterOption can check synchronously
57
- const openRef = useRef(false);
58
- openRef.current = open;
52
+ // Shared option registry (listbox engine)
53
+ const { registerOption, unregisterOption, orderedOptions } = useOptionRegistry({ open });
59
54
  // IDs
60
- const baseId = useId();
61
- const inputId = inputIdProp ?? `${baseId}-input`;
62
- const listboxId = `${baseId}-listbox`;
55
+ const { controlId: inputId, listboxId } = useListboxIds(inputIdProp, 'input');
63
56
  // Refs
64
57
  const inputRef = useRef(null);
65
58
  const listRef = useRef([]);
@@ -129,25 +122,6 @@ function MultiComboboxRoot({ value: controlledValue, defaultValue, onValueChange
129
122
  setInputValue('');
130
123
  setSearching(false);
131
124
  }, [commitValue, setInputValue]);
132
- // Get ordered options
133
- const getOrderedOptions = useCallback(() => {
134
- const options = Array.from(optionsRef.current.values());
135
- return options.sort((a, b) => {
136
- if (!a.ref.current || !b.ref.current)
137
- return 0;
138
- const position = a.ref.current.compareDocumentPosition(b.ref.current);
139
- if (position & Node.DOCUMENT_POSITION_FOLLOWING)
140
- return -1;
141
- if (position & Node.DOCUMENT_POSITION_PRECEDING)
142
- return 1;
143
- return 0;
144
- });
145
- }, []);
146
- // Memoized ordered options
147
- const orderedOptions = useMemo(() => {
148
- return getOrderedOptions();
149
- // eslint-disable-next-line react-hooks/exhaustive-deps
150
- }, [registryVersion]);
151
125
  // Compute disabled indices for keyboard navigation.
152
126
  // At max, unselected options are treated as disabled (not navigable-to-select).
153
127
  const disabledIndices = useMemo(() => orderedOptions.reduce((acc, opt, i) => {
@@ -159,69 +133,22 @@ function MultiComboboxRoot({ value: controlledValue, defaultValue, onValueChange
159
133
  // eslint-disable-next-line react-hooks/exhaustive-deps
160
134
  [orderedOptions, maxReached, value]);
161
135
  // Floating UI setup
162
- const { refs, floatingStyles, context } = useFloating({
163
- placement: 'bottom-start',
136
+ const { refs, floatingStyles, context } = useListboxFloating({
164
137
  open,
165
138
  onOpenChange: setOpen,
166
- middleware: [
167
- offset(4),
168
- flip({ padding: 8 }),
169
- shift({ padding: 8 }),
170
- sizeMiddleware({
171
- apply({ availableHeight, rects, elements }) {
172
- Object.assign(elements.floating.style, {
173
- maxHeight: `${Math.min(availableHeight - 16, 300)}px`,
174
- minWidth: `${rects.reference.width}px`,
175
- });
176
- },
177
- padding: 8,
178
- }),
179
- ],
180
- whileElementsMounted: autoUpdate,
181
139
  });
182
- // Floating UI interactions
183
- const dismiss = useDismiss(context);
184
- const role = useRole(context, { role: 'listbox' });
185
- const listNavigation = useListNavigation(context, {
140
+ // Shared interaction stack (listbox engine) — no click/typeahead: the input
141
+ // opens itself and typing IS the search.
142
+ const { getReferenceProps, getFloatingProps, getItemProps } = useListboxNavigation(context, {
186
143
  listRef,
187
144
  activeIndex: activeIndex >= 0 ? activeIndex : null,
188
145
  onNavigate: (index) => setActiveIndex(index ?? -1),
189
- loop: true,
190
- virtual: true,
191
146
  disabledIndices,
147
+ withClick: false,
192
148
  });
193
- const { getReferenceProps, getFloatingProps, getItemProps } = useInteractions([
194
- dismiss,
195
- role,
196
- listNavigation,
197
- ]);
198
- // Flag: first registerOption call after open clears stale entries.
199
- const prevOpenForFlush = useRef(open);
200
- const needsFlushRef = useRef(false);
201
- if (open && !prevOpenForFlush.current)
202
- needsFlushRef.current = true;
203
- prevOpenForFlush.current = open;
204
- // Register option
205
- const registerOption = useCallback((option) => {
206
- if (needsFlushRef.current) {
207
- optionsRef.current.clear();
208
- needsFlushRef.current = false;
209
- }
210
- optionsRef.current.set(toKey(option.value), option);
211
- setRegistryVersion((v) => v + 1);
212
- }, []);
213
- const unregisterOption = useCallback((optionValue) => {
214
- // When the dropdown closes, options unmount and call unregister in cleanup.
215
- // Skip the delete to preserve the registry — chip labels and filtering
216
- // depend on it while closed. Options re-register on next open (same keys).
217
- if (!openRef.current)
218
- return;
219
- optionsRef.current.delete(toKey(optionValue));
220
- setRegistryVersion((v) => v + 1);
221
- }, []);
222
149
  // Active option ID for aria-activedescendant (hash-based for stability)
223
150
  const activeOptionId = activeIndex >= 0 && orderedOptions[activeIndex]
224
- ? `${listboxId}-opt-${hashForId(toKey(orderedOptions[activeIndex].value))}`
151
+ ? optionIdForValue(listboxId, orderedOptions[activeIndex].value)
225
152
  : undefined;
226
153
  // Update activeIndex when options change (filtering)
227
154
  // Keep highlighted if still present, else first enabled, else -1
@@ -263,11 +190,7 @@ function MultiComboboxRoot({ value: controlledValue, defaultValue, onValueChange
263
190
  }
264
191
  }, [open]);
265
192
  // Scroll active option into view
266
- useEffect(() => {
267
- if (open && activeIndex >= 0 && listRef.current[activeIndex]) {
268
- listRef.current[activeIndex]?.scrollIntoView({ block: 'nearest' });
269
- }
270
- }, [open, activeIndex]);
193
+ useActiveOptionScroll(open, activeIndex, listRef);
271
194
  // Trim mode: keep the typing area in view by scrolling the token row to its
272
195
  // end whenever the selection changes; chips clip off the inline-start.
273
196
  useLayoutEffect(() => {
@@ -344,27 +267,17 @@ function MultiComboboxRoot({ value: controlledValue, defaultValue, onValueChange
344
267
  }
345
268
  }, [disabled, open, setOpen, activeIndex, orderedOptions, toggleOption, removeValue, inputValue, maxReached]);
346
269
  // Handle input blur with blur guard
347
- const handleInputBlur = useCallback((e) => {
348
- if (!open)
349
- return;
350
- const relatedTarget = e.relatedTarget;
351
- if (relatedTarget === null) {
352
- setSearching(false);
353
- setOpen(false);
354
- return;
355
- }
356
- const floating = refs.floating.current;
357
- if (floating && relatedTarget && floating.contains(relatedTarget)) {
358
- return;
359
- }
360
- // Focus moving to a chip's remove button inside our own wrapper — stay open
361
- const reference = refs.reference.current;
362
- if (reference && relatedTarget && reference.contains?.(relatedTarget)) {
363
- return;
364
- }
365
- setSearching(false);
366
- setOpen(false);
367
- }, [open, setOpen, refs.floating, refs.reference]);
270
+ // Shared three-branch focus-loss guard (listbox engine). The reference here
271
+ // is the token-field wrapper — focus moving to a chip's remove button inside
272
+ // it must NOT close the listbox; searching mode ends on every close path.
273
+ const endSearchingOnClose = useCallback(() => setSearching(false), []);
274
+ const handleInputBlur = useListboxBlurGuard({
275
+ open,
276
+ setOpen,
277
+ floatingRef: refs.floating,
278
+ extraContainerRef: refs.reference,
279
+ onBeforeClose: endSearchingOnClose,
280
+ });
368
281
  // Handle clear-all button
369
282
  const handleClear = useCallback((e) => {
370
283
  e.preventDefault();
@@ -510,19 +423,17 @@ function MultiComboboxOptionComponent({ value: optionValue, disabled = false, te
510
423
  const { isSelected, maxReached, toggleOption, listboxId, registerOption, unregisterOption, getItemProps, } = useMultiComboboxContext();
511
424
  const { listRef, activeIndex, orderedOptions } = useMultiComboboxContentContext();
512
425
  const ref = useRef(null);
513
- const textValue = explicitTextValue ?? (typeof children === 'string' ? children : '');
514
- const warnedTextValueRef = useRef(false);
515
- useEffect(() => {
516
- if (isDev() && !textValue && !warnedTextValueRef.current) {
517
- warnedTextValueRef.current = true;
518
- console.warn(`MultiCombobox.Option with value="${optionValue}" has no textValue. Provide textValue prop when children is not a string.`);
519
- }
520
- }, [textValue, optionValue]);
521
- // Register option
522
- useLayoutEffect(() => {
523
- registerOption({ value: optionValue, ref, disabled, textValue });
524
- return () => unregisterOption(optionValue);
525
- }, [optionValue, disabled, textValue, registerOption, unregisterOption]);
426
+ // Shared registration: textValue derivation, dev warn, layout-effect register
427
+ const textValue = useOptionRegistration({
428
+ value: optionValue,
429
+ disabled,
430
+ textValue: explicitTextValue,
431
+ children,
432
+ componentName: 'MultiCombobox.Option',
433
+ elementRef: ref,
434
+ registerOption,
435
+ unregisterOption,
436
+ });
526
437
  const index = orderedOptions.findIndex((opt) => toKey(opt.value) === toKey(optionValue));
527
438
  useEffect(() => {
528
439
  if (index >= 0) {
@@ -542,7 +453,7 @@ function MultiComboboxOptionComponent({ value: optionValue, disabled = false, te
542
453
  const handlePointerDown = useCallback((e) => {
543
454
  e.preventDefault();
544
455
  }, []);
545
- const optionId = `${listboxId}-opt-${hashForId(toKey(optionValue))}`;
456
+ const optionId = optionIdForValue(listboxId, optionValue);
546
457
  const itemProps = getItemProps({
547
458
  onClick: handleClick,
548
459
  });
@@ -552,35 +463,12 @@ MultiComboboxOptionComponent.displayName = 'MultiCombobox.Option';
552
463
  // =============================================================================
553
464
  // MultiCombobox.Group
554
465
  // =============================================================================
555
- function MultiComboboxGroupComponent({ className, children }) {
556
- const groupId = useId();
557
- const groupRef = useRef(null);
558
- const [hasLabel, setHasLabel] = useState(false);
559
- useLayoutEffect(() => {
560
- if (groupRef.current) {
561
- const labelEl = groupRef.current.querySelector(`#${CSS.escape(`${groupId}-label`)}`);
562
- setHasLabel(!!labelEl);
563
- }
564
- }, [groupId, children]);
565
- return (_jsx("div", { ref: groupRef, role: "group", "aria-labelledby": hasLabel ? `${groupId}-label` : undefined, className: cx('tui-multicombobox__group', className), children: _jsx(MultiComboboxGroupContext.Provider, { value: { groupId }, children: children }) }));
566
- }
567
- MultiComboboxGroupComponent.displayName = 'MultiCombobox.Group';
568
- const MultiComboboxGroupContext = React.createContext(null);
569
- // =============================================================================
570
- // MultiCombobox.Label
571
- // =============================================================================
572
- function MultiComboboxLabelComponent({ className, children }) {
573
- const groupContext = React.useContext(MultiComboboxGroupContext);
574
- const warnedRef = useRef(false);
575
- useEffect(() => {
576
- if (isDev() && !groupContext && !warnedRef.current) {
577
- warnedRef.current = true;
578
- console.warn('MultiCombobox.Label should be used inside MultiCombobox.Group for accessibility.');
579
- }
580
- }, [groupContext]);
581
- return (_jsx("div", { id: groupContext ? `${groupContext.groupId}-label` : undefined, className: cx('tui-multicombobox__label', className), children: children }));
582
- }
583
- MultiComboboxLabelComponent.displayName = 'MultiCombobox.Label';
466
+ // Shared Group/Label pair from the listbox engine.
467
+ const { Group: MultiComboboxGroupComponent, Label: MultiComboboxLabelComponent } = createListboxGroupComponents({
468
+ componentName: 'MultiCombobox',
469
+ groupClass: 'tui-multicombobox__group',
470
+ labelClass: 'tui-multicombobox__label',
471
+ });
584
472
  export const MultiCombobox = MultiComboboxRoot;
585
473
  MultiCombobox.Content = MultiComboboxContentComponent;
586
474
  MultiCombobox.Option = MultiComboboxOptionComponent;
@@ -185,14 +185,11 @@ export type MultiComboboxLabelProps = {
185
185
  children: React.ReactNode;
186
186
  };
187
187
  /**
188
- * Registration record for an option.
188
+ * Registration record for an option — now defined by the shared listbox
189
+ * engine; re-exported here for back-compat.
189
190
  */
190
- export type RegisteredOption = {
191
- value: OptionValue;
192
- ref: RefObject<HTMLElement | null>;
193
- disabled: boolean;
194
- textValue: string;
195
- };
191
+ import type { RegisteredOption } from '../../utils/listbox';
192
+ export type { RegisteredOption };
196
193
  /**
197
194
  * Stable context: config, IDs, refs, and stable callbacks.
198
195
  */
@@ -1,4 +1,4 @@
1
- import { type MultiSelectProps, type MultiSelectTriggerProps, type MultiSelectContentProps, type MultiSelectOptionProps, type MultiSelectGroupProps, type MultiSelectLabelProps } from './types';
1
+ import { type MultiSelectProps, type MultiSelectTriggerProps, type MultiSelectContentProps, type MultiSelectOptionProps } from './types';
2
2
  declare function MultiSelectRoot({ id: triggerIdProp, value: controlledValue, defaultValue, onValueChange, open: controlledOpen, defaultOpen, onOpenChange, disabled, placeholder, size, display, maxChips, max, onMaxReached, labels: labelsProp, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, 'aria-describedby': ariaDescribedBy, children, }: MultiSelectProps): import("react/jsx-runtime").JSX.Element;
3
3
  declare namespace MultiSelectRoot {
4
4
  var displayName: string;
@@ -15,14 +15,13 @@ declare function MultiSelectOptionComponent({ value: optionValue, disabled, text
15
15
  declare namespace MultiSelectOptionComponent {
16
16
  var displayName: string;
17
17
  }
18
- declare function MultiSelectGroupComponent({ className, children }: MultiSelectGroupProps): import("react/jsx-runtime").JSX.Element;
19
- declare namespace MultiSelectGroupComponent {
20
- var displayName: string;
21
- }
22
- declare function MultiSelectLabelComponent({ className, children }: MultiSelectLabelProps): import("react/jsx-runtime").JSX.Element;
23
- declare namespace MultiSelectLabelComponent {
24
- var displayName: string;
25
- }
18
+ declare const MultiSelectGroupComponent: {
19
+ ({ className, children }: import("..").SearchSelectGroupProps): import("react/jsx-runtime").JSX.Element;
20
+ displayName: string;
21
+ }, MultiSelectLabelComponent: {
22
+ ({ className, children }: import("..").SearchSelectLabelProps): import("react/jsx-runtime").JSX.Element;
23
+ displayName: string;
24
+ };
26
25
  type MultiSelectCompound = typeof MultiSelectRoot & {
27
26
  Trigger: typeof MultiSelectTriggerComponent;
28
27
  Content: typeof MultiSelectContentComponent;
@@ -34,6 +33,12 @@ export declare const MultiSelect: MultiSelectCompound;
34
33
  export declare const MultiSelectTrigger: typeof MultiSelectTriggerComponent;
35
34
  export declare const MultiSelectContent: typeof MultiSelectContentComponent;
36
35
  export declare const MultiSelectOption: typeof MultiSelectOptionComponent;
37
- export declare const MultiSelectGroup: typeof MultiSelectGroupComponent;
38
- export declare const MultiSelectLabel: typeof MultiSelectLabelComponent;
36
+ export declare const MultiSelectGroup: {
37
+ ({ className, children }: import("..").SearchSelectGroupProps): import("react/jsx-runtime").JSX.Element;
38
+ displayName: string;
39
+ };
40
+ export declare const MultiSelectLabel: {
41
+ ({ className, children }: import("..").SearchSelectLabelProps): import("react/jsx-runtime").JSX.Element;
42
+ displayName: string;
43
+ };
39
44
  export { useMultiSelectContext as useMultiSelect } from './MultiSelectContext';