@dxos/react-ui-searchlist 0.8.3 → 0.8.4-main.1068cf700f

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 (81) hide show
  1. package/dist/lib/browser/index.mjs +741 -265
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node-esm/index.mjs +741 -265
  5. package/dist/lib/node-esm/index.mjs.map +4 -4
  6. package/dist/lib/node-esm/meta.json +1 -1
  7. package/dist/types/src/components/Combobox/Combobox.d.ts +84 -0
  8. package/dist/types/src/components/Combobox/Combobox.d.ts.map +1 -0
  9. package/dist/types/src/components/Combobox/Combobox.stories.d.ts +21 -0
  10. package/dist/types/src/components/Combobox/Combobox.stories.d.ts.map +1 -0
  11. package/dist/types/src/components/Combobox/index.d.ts +2 -0
  12. package/dist/types/src/components/Combobox/index.d.ts.map +1 -0
  13. package/dist/types/src/components/Listbox/Listbox.d.ts +31 -0
  14. package/dist/types/src/components/Listbox/Listbox.d.ts.map +1 -0
  15. package/dist/types/src/components/Listbox/Listbox.stories.d.ts +21 -0
  16. package/dist/types/src/components/Listbox/Listbox.stories.d.ts.map +1 -0
  17. package/dist/types/src/components/Listbox/index.d.ts +2 -0
  18. package/dist/types/src/components/Listbox/index.d.ts.map +1 -0
  19. package/dist/types/src/components/SearchList/SearchList.d.ts +88 -0
  20. package/dist/types/src/components/SearchList/SearchList.d.ts.map +1 -0
  21. package/dist/types/src/components/SearchList/SearchList.stories.d.ts +28 -0
  22. package/dist/types/src/components/SearchList/SearchList.stories.d.ts.map +1 -0
  23. package/dist/types/src/components/SearchList/context.d.ts +33 -0
  24. package/dist/types/src/components/SearchList/context.d.ts.map +1 -0
  25. package/dist/types/src/components/SearchList/hooks/index.d.ts +5 -0
  26. package/dist/types/src/components/SearchList/hooks/index.d.ts.map +1 -0
  27. package/dist/types/src/components/SearchList/hooks/useGlobalFilter.d.ts +34 -0
  28. package/dist/types/src/components/SearchList/hooks/useGlobalFilter.d.ts.map +1 -0
  29. package/dist/types/src/components/SearchList/hooks/useSearchListInput.d.ts +12 -0
  30. package/dist/types/src/components/SearchList/hooks/useSearchListInput.d.ts.map +1 -0
  31. package/dist/types/src/components/SearchList/hooks/useSearchListItem.d.ts +10 -0
  32. package/dist/types/src/components/SearchList/hooks/useSearchListItem.d.ts.map +1 -0
  33. package/dist/types/src/components/SearchList/hooks/useSearchListResults.d.ts +36 -0
  34. package/dist/types/src/components/SearchList/hooks/useSearchListResults.d.ts.map +1 -0
  35. package/dist/types/src/components/SearchList/index.d.ts +3 -0
  36. package/dist/types/src/components/SearchList/index.d.ts.map +1 -0
  37. package/dist/types/src/components/index.d.ts +2 -0
  38. package/dist/types/src/components/index.d.ts.map +1 -1
  39. package/dist/types/src/index.d.ts +1 -2
  40. package/dist/types/src/index.d.ts.map +1 -1
  41. package/dist/types/src/translations.d.ts +7 -6
  42. package/dist/types/src/translations.d.ts.map +1 -1
  43. package/dist/types/tsconfig.tsbuildinfo +1 -1
  44. package/package.json +24 -20
  45. package/src/components/Combobox/Combobox.stories.tsx +62 -0
  46. package/src/components/Combobox/Combobox.tsx +348 -0
  47. package/src/components/Combobox/index.ts +5 -0
  48. package/src/components/Listbox/Listbox.stories.tsx +53 -0
  49. package/src/components/Listbox/Listbox.tsx +214 -0
  50. package/src/components/Listbox/index.ts +5 -0
  51. package/src/components/SearchList/SearchList.stories.tsx +532 -0
  52. package/src/components/SearchList/SearchList.tsx +560 -0
  53. package/src/components/SearchList/context.ts +43 -0
  54. package/src/components/SearchList/hooks/index.ts +8 -0
  55. package/src/components/SearchList/hooks/useGlobalFilter.tsx +61 -0
  56. package/src/components/SearchList/hooks/useSearchListInput.ts +14 -0
  57. package/src/components/SearchList/hooks/useSearchListItem.ts +14 -0
  58. package/src/components/SearchList/hooks/useSearchListResults.ts +104 -0
  59. package/src/components/SearchList/index.ts +6 -0
  60. package/src/components/index.ts +2 -0
  61. package/src/index.ts +1 -2
  62. package/src/translations.ts +8 -4
  63. package/src/types/command-score.d.ts +16 -0
  64. package/dist/lib/node/index.cjs +0 -324
  65. package/dist/lib/node/index.cjs.map +0 -7
  66. package/dist/lib/node/meta.json +0 -1
  67. package/dist/types/src/components/SearchList.d.ts +0 -44
  68. package/dist/types/src/components/SearchList.d.ts.map +0 -1
  69. package/dist/types/src/components/SearchList.stories.d.ts +0 -15
  70. package/dist/types/src/components/SearchList.stories.d.ts.map +0 -1
  71. package/dist/types/src/composites/PopoverCombobox.d.ts +0 -32
  72. package/dist/types/src/composites/PopoverCombobox.d.ts.map +0 -1
  73. package/dist/types/src/composites/PopoverCombobox.stories.d.ts +0 -28
  74. package/dist/types/src/composites/PopoverCombobox.stories.d.ts.map +0 -1
  75. package/dist/types/src/composites/index.d.ts +0 -2
  76. package/dist/types/src/composites/index.d.ts.map +0 -1
  77. package/src/components/SearchList.stories.tsx +0 -47
  78. package/src/components/SearchList.tsx +0 -250
  79. package/src/composites/PopoverCombobox.stories.tsx +0 -44
  80. package/src/composites/PopoverCombobox.tsx +0 -186
  81. package/src/composites/index.ts +0 -5
@@ -0,0 +1,560 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { useControllableState } from '@radix-ui/react-use-controllable-state';
6
+ import React, {
7
+ type ChangeEvent,
8
+ type ComponentPropsWithRef,
9
+ type KeyboardEvent,
10
+ type PropsWithChildren,
11
+ type ReactNode,
12
+ forwardRef,
13
+ useCallback,
14
+ useEffect,
15
+ useMemo,
16
+ useRef,
17
+ useState,
18
+ } from 'react';
19
+
20
+ import {
21
+ type Density,
22
+ type Elevation,
23
+ Icon,
24
+ type ThemedClassName,
25
+ useDensityContext,
26
+ useElevationContext,
27
+ useThemeContext,
28
+ useTranslation,
29
+ } from '@dxos/react-ui';
30
+ import { descriptionText, mx } from '@dxos/ui-theme';
31
+
32
+ import { translationKey } from '../../translations';
33
+
34
+ import {
35
+ SearchListInputContextProvider,
36
+ SearchListItemContextProvider,
37
+ useSearchListInputContext,
38
+ useSearchListItemContext,
39
+ } from './context';
40
+
41
+ //
42
+ // Internal types
43
+ //
44
+
45
+ type ItemData = {
46
+ element: HTMLElement;
47
+ disabled?: boolean;
48
+ onSelect?: () => void;
49
+ };
50
+
51
+ //
52
+ // Root
53
+ //
54
+
55
+ type SearchListRootProps = PropsWithChildren<{
56
+ /** Controlled query value. */
57
+ value?: string;
58
+
59
+ /** Default query value for uncontrolled mode. */
60
+ defaultValue?: string;
61
+
62
+ /** Debounce delay in milliseconds. */
63
+ debounceMs?: number;
64
+
65
+ /** Callback when search query changes (debounced). */
66
+ onSearch?: (query: string) => void;
67
+ }>;
68
+
69
+ const SearchListRoot = ({
70
+ children,
71
+ value: valueProp,
72
+ defaultValue = '',
73
+ debounceMs = 200,
74
+ onSearch,
75
+ }: SearchListRootProps) => {
76
+ const [query = '', setQuery] = useControllableState({
77
+ prop: valueProp,
78
+ defaultProp: defaultValue,
79
+ onChange: undefined,
80
+ });
81
+
82
+ const [selectedValue, setSelectedValue] = useState<string | undefined>(undefined);
83
+
84
+ // Track registered items: value -> { element, onSelect, disabled }.
85
+ const itemsRef = useRef<Map<string, ItemData>>(new Map());
86
+
87
+ const debounceRef = useRef<ReturnType<typeof setTimeout> | null>(null);
88
+
89
+ const handleQueryChange = useCallback(
90
+ (newQuery: string) => {
91
+ setQuery(newQuery);
92
+ // Don't update selectedValue here - let the effect handle it when items actually change.
93
+ // This prevents unnecessary re-renders of items when query changes.
94
+
95
+ // Debounce onSearch callback.
96
+ if (debounceRef.current) {
97
+ clearTimeout(debounceRef.current);
98
+ }
99
+ debounceRef.current = setTimeout(() => {
100
+ onSearch?.(newQuery);
101
+ }, debounceMs);
102
+ },
103
+ [setQuery, onSearch, debounceMs],
104
+ );
105
+
106
+ // Track when items change to trigger first-item selection.
107
+ const [itemVersion, setItemVersion] = useState(0);
108
+
109
+ // Cleanup debounce on unmount.
110
+ useEffect(() => {
111
+ return () => {
112
+ if (debounceRef.current) {
113
+ clearTimeout(debounceRef.current);
114
+ }
115
+ };
116
+ }, []);
117
+
118
+ // Auto-select first non-disabled item when items change and no valid selection exists.
119
+ useEffect(() => {
120
+ // Check if current selection is still valid (exists and not disabled).
121
+ const currentItem = selectedValue !== undefined ? itemsRef.current.get(selectedValue) : undefined;
122
+ const isSelectionValid = currentItem !== undefined && !currentItem.disabled;
123
+ if (!isSelectionValid && itemsRef.current.size > 0) {
124
+ // Get first non-disabled item in DOM order.
125
+ const entries = Array.from(itemsRef.current.entries()).filter(([, data]) => !data.disabled);
126
+ if (entries.length > 0) {
127
+ entries.sort(([, a], [, b]) => {
128
+ const position = a.element.compareDocumentPosition(b.element);
129
+ if (position & Node.DOCUMENT_POSITION_FOLLOWING) {
130
+ return -1;
131
+ }
132
+ if (position & Node.DOCUMENT_POSITION_PRECEDING) {
133
+ return 1;
134
+ }
135
+ return 0;
136
+ });
137
+ const firstValue = entries[0]?.[0];
138
+ if (firstValue !== undefined && firstValue !== selectedValue) {
139
+ setSelectedValue(firstValue);
140
+ }
141
+ } else if (selectedValue !== undefined) {
142
+ // No valid items available, clear selection
143
+ setSelectedValue(undefined);
144
+ }
145
+ }
146
+ }, [itemVersion, selectedValue]);
147
+
148
+ const registerItem = useCallback(
149
+ (value: string, element: HTMLElement | null, onSelect: (() => void) | undefined, disabled?: boolean) => {
150
+ if (element) {
151
+ itemsRef.current.set(value, { element, onSelect, disabled });
152
+ setItemVersion((v) => v + 1);
153
+ }
154
+ },
155
+ [],
156
+ );
157
+
158
+ const unregisterItem = useCallback((value: string) => {
159
+ itemsRef.current.delete(value);
160
+ setItemVersion((v) => v + 1);
161
+ }, []);
162
+
163
+ // Get item values in DOM order by sorting registered elements (excludes disabled items).
164
+ const getItemValues = useCallback(() => {
165
+ return Array.from(itemsRef.current.entries())
166
+ .filter(([, data]) => !data.disabled)
167
+ .sort(([, a], [, b]) => {
168
+ // Sort by DOM position using compareDocumentPosition.
169
+ const position = a.element.compareDocumentPosition(b.element);
170
+ return position & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : position & Node.DOCUMENT_POSITION_PRECEDING ? 1 : 0;
171
+ })
172
+ .map(([value]) => value);
173
+ }, []);
174
+
175
+ const triggerSelect = useCallback(() => {
176
+ if (selectedValue !== undefined) {
177
+ const item = itemsRef.current.get(selectedValue);
178
+ item?.onSelect?.();
179
+ }
180
+ }, [selectedValue]);
181
+
182
+ // Item context; stable, doesn't change when query changes.
183
+ const itemContextValue = useMemo(
184
+ () => ({
185
+ selectedValue,
186
+ onSelectedValueChange: setSelectedValue,
187
+ registerItem,
188
+ unregisterItem,
189
+ }),
190
+ [selectedValue, registerItem, unregisterItem],
191
+ );
192
+
193
+ const inputContextValue = useMemo(
194
+ () => ({
195
+ query,
196
+ onQueryChange: handleQueryChange,
197
+ selectedValue,
198
+ onSelectedValueChange: setSelectedValue,
199
+ getItemValues,
200
+ triggerSelect,
201
+ }),
202
+ [query, handleQueryChange, selectedValue, getItemValues, triggerSelect],
203
+ );
204
+
205
+ // NOTE: Separate contexts for items and input to avoid unnecessary re-renders of items when query changes.
206
+ return (
207
+ <SearchListInputContextProvider
208
+ query={inputContextValue.query}
209
+ onQueryChange={inputContextValue.onQueryChange}
210
+ selectedValue={inputContextValue.selectedValue}
211
+ onSelectedValueChange={inputContextValue.onSelectedValueChange}
212
+ getItemValues={inputContextValue.getItemValues}
213
+ triggerSelect={inputContextValue.triggerSelect}
214
+ >
215
+ <SearchListItemContextProvider
216
+ selectedValue={itemContextValue.selectedValue}
217
+ onSelectedValueChange={itemContextValue.onSelectedValueChange}
218
+ registerItem={itemContextValue.registerItem}
219
+ unregisterItem={itemContextValue.unregisterItem}
220
+ >
221
+ {children}
222
+ </SearchListItemContextProvider>
223
+ </SearchListInputContextProvider>
224
+ );
225
+ };
226
+
227
+ SearchListRoot.displayName = 'SearchList.Root';
228
+
229
+ //
230
+ // Content
231
+ //
232
+
233
+ type SearchListContentProps = ThemedClassName<PropsWithChildren<{}>>;
234
+
235
+ const SearchListContent = forwardRef<HTMLDivElement, SearchListContentProps>(
236
+ ({ classNames, children }, forwardedRef) => {
237
+ return (
238
+ <div
239
+ role='none'
240
+ // TODO(burdon): Remove p-1 hack.
241
+ className={mx('flex flex-col gap-2 bs-full is-full min-bs-0 overflow-hidden p-1', classNames)}
242
+ ref={forwardedRef}
243
+ >
244
+ {children}
245
+ </div>
246
+ );
247
+ },
248
+ );
249
+
250
+ SearchListContent.displayName = 'SearchList.Content';
251
+
252
+ //
253
+ // Viewport
254
+ //
255
+
256
+ type SearchListViewportProps = ThemedClassName<PropsWithChildren>;
257
+
258
+ /**
259
+ * Scrollable viewport wrapper for Content.
260
+ * Only Content wrapped in Viewport will be scrollable.
261
+ */
262
+ // TODO(burdon): Reconcile with Mosaic.Viewport (factor out common core?).
263
+ const SearchListViewport = forwardRef<HTMLDivElement, SearchListViewportProps>(
264
+ ({ classNames, children }, forwardedRef) => {
265
+ return (
266
+ <div role='listbox' className={mx('bs-full is-full min-bs-0 overflow-y-auto', classNames)} ref={forwardedRef}>
267
+ {children}
268
+ </div>
269
+ );
270
+ },
271
+ );
272
+
273
+ SearchListViewport.displayName = 'SearchList.Viewport';
274
+
275
+ //
276
+ // Input
277
+ //
278
+
279
+ type InputVariant = 'default' | 'subdued';
280
+
281
+ type SearchListInputProps = ThemedClassName<
282
+ Omit<ComponentPropsWithRef<'input'>, 'value'> & {
283
+ density?: Density;
284
+ elevation?: Elevation;
285
+ variant?: InputVariant;
286
+ }
287
+ >;
288
+
289
+ const SearchListInput = forwardRef<HTMLInputElement, SearchListInputProps>(
290
+ (
291
+ { classNames, density: propsDensity, elevation: propsElevation, variant, placeholder, onChange, ...props },
292
+ forwardedRef,
293
+ ) => {
294
+ const { query, onQueryChange, selectedValue, onSelectedValueChange, getItemValues, triggerSelect } =
295
+ useSearchListInputContext('SearchList.Input');
296
+ const { t } = useTranslation(translationKey);
297
+ const { hasIosKeyboard, tx } = useThemeContext();
298
+ const density = useDensityContext(propsDensity);
299
+ const elevation = useElevationContext(propsElevation);
300
+ const defaultPlaceholder = t('search.placeholder');
301
+
302
+ const handleChange = useCallback(
303
+ (event: ChangeEvent<HTMLInputElement>) => {
304
+ onQueryChange(event.target.value);
305
+ onChange?.(event);
306
+ },
307
+ [onQueryChange, onChange],
308
+ );
309
+
310
+ const handleKeyDown = useCallback(
311
+ (event: KeyboardEvent<HTMLInputElement>) => {
312
+ const values = getItemValues();
313
+ if (values.length === 0) {
314
+ if (event.key === 'Escape') {
315
+ onQueryChange('');
316
+ }
317
+ return;
318
+ }
319
+
320
+ const currentIndex = selectedValue !== undefined ? values.indexOf(selectedValue) : -1;
321
+
322
+ switch (event.key) {
323
+ case 'ArrowDown': {
324
+ event.preventDefault();
325
+ const nextIndex = currentIndex === -1 ? 0 : Math.min(currentIndex + 1, values.length - 1);
326
+ const nextValue = values[nextIndex];
327
+ if (nextValue !== undefined) {
328
+ onSelectedValueChange(nextValue);
329
+ }
330
+ break;
331
+ }
332
+ case 'ArrowUp': {
333
+ event.preventDefault();
334
+ const prevIndex = currentIndex === -1 ? values.length - 1 : Math.max(currentIndex - 1, 0);
335
+ const prevValue = values[prevIndex];
336
+ if (prevValue !== undefined) {
337
+ onSelectedValueChange(prevValue);
338
+ }
339
+ break;
340
+ }
341
+ case 'Enter': {
342
+ if (selectedValue !== undefined) {
343
+ event.preventDefault();
344
+ triggerSelect();
345
+ }
346
+ break;
347
+ }
348
+ case 'Home': {
349
+ event.preventDefault();
350
+ const firstValue = values[0];
351
+ if (firstValue !== undefined) {
352
+ onSelectedValueChange(firstValue);
353
+ }
354
+ break;
355
+ }
356
+ case 'End': {
357
+ event.preventDefault();
358
+ const lastValue = values[values.length - 1];
359
+ if (lastValue !== undefined) {
360
+ onSelectedValueChange(lastValue);
361
+ }
362
+ break;
363
+ }
364
+ case 'Escape': {
365
+ event.preventDefault();
366
+ if (selectedValue !== undefined) {
367
+ onSelectedValueChange(undefined);
368
+ } else {
369
+ onQueryChange('');
370
+ }
371
+ break;
372
+ }
373
+ }
374
+ },
375
+ [selectedValue, onSelectedValueChange, getItemValues, triggerSelect, onQueryChange],
376
+ );
377
+
378
+ return (
379
+ <input
380
+ {...props}
381
+ {...(props.autoFocus && !hasIosKeyboard && { autoFocus: true })}
382
+ type='text'
383
+ placeholder={placeholder ?? defaultPlaceholder}
384
+ className={tx(
385
+ 'input.input',
386
+ {
387
+ variant,
388
+ disabled: props.disabled,
389
+ density,
390
+ elevation,
391
+ },
392
+ classNames,
393
+ )}
394
+ value={query}
395
+ onChange={handleChange}
396
+ onKeyDown={handleKeyDown}
397
+ ref={forwardedRef}
398
+ />
399
+ );
400
+ },
401
+ );
402
+
403
+ SearchListInput.displayName = 'SearchList.Input';
404
+
405
+ //
406
+ // Item
407
+ //
408
+
409
+ type SearchListItemProps = ThemedClassName<{
410
+ /** Unique identifier for the item. */
411
+ value: string;
412
+ /** Display label for the item. */
413
+ label: string;
414
+ /** Icon to display (string identifier for Icon component). */
415
+ icon?: string;
416
+ /** Whether to show a check icon. */
417
+ checked?: boolean;
418
+ /** Suffix text to display after the label. */
419
+ suffix?: string;
420
+ /** Callback when item is selected. */
421
+ onSelect?: () => void;
422
+ /** Whether the item is disabled. */
423
+ disabled?: boolean;
424
+ }>;
425
+
426
+ const SearchListItem = forwardRef<HTMLDivElement, SearchListItemProps>(
427
+ ({ classNames, value, label, icon, checked, suffix, onSelect, disabled }, forwardedRef) => {
428
+ const { selectedValue, registerItem, unregisterItem } = useSearchListItemContext('SearchList.Item');
429
+ const internalRef = useRef<HTMLDivElement>(null);
430
+
431
+ const isSelected = selectedValue === value && !disabled;
432
+
433
+ // Register this item.
434
+ useEffect(() => {
435
+ const element = internalRef.current;
436
+ if (element) {
437
+ registerItem(value, element, onSelect, disabled);
438
+ }
439
+ return () => unregisterItem(value);
440
+ }, [value, onSelect, disabled, registerItem, unregisterItem]);
441
+
442
+ // Scroll into view when selected.
443
+ useEffect(() => {
444
+ if (isSelected && internalRef.current) {
445
+ internalRef.current.scrollIntoView({ block: 'nearest', behavior: 'smooth' });
446
+ }
447
+ }, [isSelected]);
448
+
449
+ const handleClick = useCallback(() => {
450
+ if (!disabled) {
451
+ onSelect?.();
452
+ }
453
+ }, [onSelect, disabled]);
454
+
455
+ return (
456
+ <div
457
+ ref={(node) => {
458
+ internalRef.current = node;
459
+ if (typeof forwardedRef === 'function') {
460
+ forwardedRef(node);
461
+ } else if (forwardedRef) {
462
+ forwardedRef.current = node;
463
+ }
464
+ }}
465
+ role='option'
466
+ aria-selected={isSelected}
467
+ aria-disabled={disabled}
468
+ data-selected={isSelected}
469
+ data-disabled={disabled}
470
+ data-value={value}
471
+ tabIndex={-1}
472
+ className={mx(
473
+ 'flex gap-2 items-center',
474
+ 'plb-1 pli-2 rounded-sm select-none cursor-pointer data-[selected=true]:bg-hoverOverlay hover:bg-hoverOverlay',
475
+ disabled && 'opacity-50 cursor-not-allowed hover:bg-transparent data-[selected=true]:bg-transparent',
476
+ classNames,
477
+ )}
478
+ onClick={handleClick}
479
+ >
480
+ {icon && <Icon icon={icon} size={5} />}
481
+ <span className='is-0 grow truncate'>{label}</span>
482
+ {suffix && <span className={mx('shrink-0', descriptionText)}>{suffix}</span>}
483
+ {checked && <Icon icon='ph--check--regular' size={5} />}
484
+ </div>
485
+ );
486
+ },
487
+ );
488
+
489
+ SearchListItem.displayName = 'SearchList.Item';
490
+
491
+ //
492
+ // Empty
493
+ //
494
+
495
+ type SearchListEmptyProps = ThemedClassName<PropsWithChildren<{}>>;
496
+
497
+ const SearchListEmpty = ({ classNames, children }: SearchListEmptyProps) => {
498
+ return (
499
+ <div role='status' className={mx('flex flex-col is-full pli-2 plb-1', classNames)}>
500
+ {children}
501
+ </div>
502
+ );
503
+ };
504
+
505
+ SearchListEmpty.displayName = 'SearchList.Empty';
506
+
507
+ //
508
+ // Group
509
+ //
510
+
511
+ type SearchListGroupProps = ThemedClassName<
512
+ PropsWithChildren<{
513
+ /** Heading for the group. */
514
+ heading?: ReactNode;
515
+ }>
516
+ >;
517
+
518
+ /**
519
+ * Groups related search items with an optional heading.
520
+ */
521
+ const SearchListGroup = forwardRef<HTMLDivElement, SearchListGroupProps>(
522
+ ({ classNames, heading, children }, forwardedRef) => {
523
+ return (
524
+ <div ref={forwardedRef} role='group' className={mx('flex flex-col', classNames)}>
525
+ {heading && (
526
+ <div role='presentation' className='pli-2 plb-1 text-xs font-medium text-description'>
527
+ {heading}
528
+ </div>
529
+ )}
530
+ {children}
531
+ </div>
532
+ );
533
+ },
534
+ );
535
+
536
+ SearchListGroup.displayName = 'SearchList.Group';
537
+
538
+ //
539
+ // SearchList
540
+ //
541
+
542
+ export const SearchList = {
543
+ Root: SearchListRoot,
544
+ Content: SearchListContent,
545
+ Viewport: SearchListViewport,
546
+ Input: SearchListInput,
547
+ Item: SearchListItem,
548
+ Empty: SearchListEmpty,
549
+ Group: SearchListGroup,
550
+ };
551
+
552
+ export type {
553
+ SearchListRootProps,
554
+ SearchListContentProps,
555
+ SearchListViewportProps,
556
+ SearchListInputProps,
557
+ SearchListItemProps,
558
+ SearchListEmptyProps,
559
+ SearchListGroupProps,
560
+ };
@@ -0,0 +1,43 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { createContext } from '@radix-ui/react-context';
6
+
7
+ /** Context for items - stable, doesn't change when query changes */
8
+ export type SearchListItemContextValue = {
9
+ /** Currently selected item value for keyboard navigation. */
10
+ selectedValue: string | undefined;
11
+ /** Update the selected value. */
12
+ onSelectedValueChange: (value: string | undefined) => void;
13
+ /** Register an item for keyboard navigation. */
14
+ registerItem: (
15
+ value: string,
16
+ element: HTMLElement | null,
17
+ onSelect: (() => void) | undefined,
18
+ disabled?: boolean,
19
+ ) => void;
20
+ /** Unregister an item. */
21
+ unregisterItem: (value: string) => void;
22
+ };
23
+
24
+ /** Context for input - can change frequently with query */
25
+ export type SearchListInputContextValue = {
26
+ /** Current search query. */
27
+ query: string;
28
+ /** Update the query value. */
29
+ onQueryChange: (query: string) => void;
30
+ /** Currently selected item value for keyboard navigation. */
31
+ selectedValue: string | undefined;
32
+ /** Update the selected value. */
33
+ onSelectedValueChange: (value: string | undefined) => void;
34
+ /** Get ordered list of registered item values (excludes disabled items). */
35
+ getItemValues: () => string[];
36
+ /** Trigger selection of the currently highlighted item. */
37
+ triggerSelect: () => void;
38
+ };
39
+
40
+ export const [SearchListItemContextProvider, useSearchListItemContext] =
41
+ createContext<SearchListItemContextValue>('SearchListItem');
42
+ export const [SearchListInputContextProvider, useSearchListInputContext] =
43
+ createContext<SearchListInputContextValue>('SearchListInput');
@@ -0,0 +1,8 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ export * from './useGlobalFilter';
6
+ export * from './useSearchListInput';
7
+ export * from './useSearchListItem';
8
+ export * from './useSearchListResults';
@@ -0,0 +1,61 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import React, { type PropsWithChildren, createContext, useContext, useMemo } from 'react';
6
+
7
+ /**
8
+ * Type for a filter function that filters an array of objects.
9
+ */
10
+ export type FilterFunction<T = any> = (objects: T[]) => T[];
11
+
12
+ type GlobalFilterContextType = {
13
+ /** The current filter function. */
14
+ filter?: FilterFunction;
15
+ };
16
+
17
+ const GlobalFilterContext = createContext<GlobalFilterContextType>({});
18
+
19
+ export type GlobalFilterProviderProps = PropsWithChildren<{
20
+ /** The filter function to apply globally. */
21
+ filter?: FilterFunction;
22
+ }>;
23
+
24
+ /**
25
+ * Provider that makes a filter function available globally.
26
+ * Used by plugin-search to provide its regex-based filtering.
27
+ */
28
+ export const GlobalFilterProvider = ({ children, filter }: GlobalFilterProviderProps) => {
29
+ const value = useMemo(() => ({ filter }), [filter]);
30
+ return <GlobalFilterContext.Provider value={value}>{children}</GlobalFilterContext.Provider>;
31
+ };
32
+
33
+ /**
34
+ * Hook to access the global filter context.
35
+ * Returns the filter function if one is provided.
36
+ */
37
+ export const useGlobalFilter = () => {
38
+ return useContext(GlobalFilterContext);
39
+ };
40
+
41
+ /**
42
+ * Hook that applies the global filter to an array of objects.
43
+ * If no filter is set, returns the original objects unchanged.
44
+ *
45
+ * @example
46
+ * const objects = useQuery(db, Filter.everything());
47
+ * const filteredObjects = useGlobalFilteredObjects(objects);
48
+ */
49
+ export const useGlobalFilteredObjects = <T extends Record<string, any>>(objects?: T[]): T[] => {
50
+ const { filter } = useGlobalFilter();
51
+
52
+ return useMemo(() => {
53
+ if (!objects) {
54
+ return [];
55
+ }
56
+ if (!filter) {
57
+ return objects;
58
+ }
59
+ return filter(objects);
60
+ }, [objects, filter]);
61
+ };
@@ -0,0 +1,14 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { useSearchListInputContext } from '../context';
6
+
7
+ /**
8
+ * Hook to access the search context for custom input implementations.
9
+ */
10
+ export const useSearchListInput = () => {
11
+ const { query, onQueryChange, selectedValue, onSelectedValueChange, getItemValues, triggerSelect } =
12
+ useSearchListInputContext('useSearchListInput');
13
+ return { query, onQueryChange, selectedValue, onSelectedValueChange, getItemValues, triggerSelect };
14
+ };
@@ -0,0 +1,14 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { useSearchListItemContext } from '../context';
6
+
7
+ /**
8
+ * Hook to access selection state for custom item renderers.
9
+ * Returns the current selected value and registration functions.
10
+ */
11
+ export const useSearchListItem = () => {
12
+ const { selectedValue, registerItem, unregisterItem } = useSearchListItemContext('useSearchListItem');
13
+ return { selectedValue, registerItem, unregisterItem };
14
+ };