@brightlocal/ui-components 2.9.2 → 2.11.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 +3 -1
- package/component-meta.json +41 -9
- package/dist/badge.cjs +1 -1
- package/dist/badge.d.ts.map +1 -1
- package/dist/badge.js +42 -25
- package/dist/blocks/sidebar.cjs +1 -1
- package/dist/blocks/sidebar.d.ts +1 -3
- package/dist/blocks/sidebar.d.ts.map +1 -1
- package/dist/blocks/sidebar.js +46 -49
- package/dist/browser-entry.d.ts +17 -0
- package/dist/browser-entry.d.ts.map +1 -0
- package/dist/button.cjs +1 -1
- package/dist/button.js +25 -25
- package/dist/card.cjs +1 -1
- package/dist/card.d.ts +10 -1
- package/dist/card.d.ts.map +1 -1
- package/dist/card.js +85 -82
- package/dist/carousel.cjs +1 -1
- package/dist/carousel.d.ts +13 -1
- package/dist/carousel.d.ts.map +1 -1
- package/dist/carousel.js +196 -150
- package/dist/combobox.cjs +3 -3
- package/dist/combobox.d.ts +3 -3
- package/dist/combobox.d.ts.map +1 -1
- package/dist/combobox.js +480 -440
- package/dist/command.cjs +1 -1
- package/dist/command.d.ts +101 -1
- package/dist/command.d.ts.map +1 -1
- package/dist/command.js +383 -119
- package/dist/hooks/use-breakpoint.cjs +1 -1
- package/dist/hooks/use-breakpoint.d.ts.map +1 -1
- package/dist/hooks/use-breakpoint.js +10 -10
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +402 -391
- package/dist/input-chip.cjs +1 -1
- package/dist/input-chip.d.ts +1 -4
- package/dist/input-chip.d.ts.map +1 -1
- package/dist/input-chip.js +163 -170
- package/dist/map.cjs +1 -0
- package/dist/map.d.ts +192 -0
- package/dist/map.d.ts.map +1 -0
- package/dist/map.js +250 -0
- package/dist/sonner.cjs +1 -1
- package/dist/sonner.d.ts.map +1 -1
- package/dist/sonner.js +32 -30
- package/dist/tabs.cjs +1 -1
- package/dist/tabs.d.ts +4 -0
- package/dist/tabs.d.ts.map +1 -1
- package/dist/tabs.js +101 -52
- package/package.json +4 -1
- package/src/blocks/sidebar.tsx +21 -26
- package/src/browser-entry.ts +89 -0
- package/src/browser-tokens.css +1 -0
- package/src/components/badge.tsx +22 -5
- package/src/components/button.tsx +13 -13
- package/src/components/card.tsx +35 -19
- package/src/components/carousel.tsx +101 -9
- package/src/components/combobox.tsx +106 -28
- package/src/components/command.tsx +637 -16
- package/src/components/input-chip.tsx +102 -65
- package/src/components/map.tsx +574 -0
- package/src/components/sonner.tsx +9 -3
- package/src/components/tabs.tsx +118 -22
- package/src/hooks/use-breakpoint.ts +2 -3
- package/src/index.ts +1 -0
|
@@ -2,10 +2,41 @@
|
|
|
2
2
|
|
|
3
3
|
import * as React from "react";
|
|
4
4
|
import { Command as CommandPrimitive } from "cmdk";
|
|
5
|
+
import { defaultFilter } from "cmdk";
|
|
6
|
+
import { useVirtualizer } from "@tanstack/react-virtual";
|
|
5
7
|
import { Check, Search, X } from "@brightlocal/icons";
|
|
6
8
|
|
|
7
9
|
import { Button } from "@brightlocal/ui-components/button";
|
|
8
10
|
import { cn } from "@brightlocal/ui-components/lib/utils";
|
|
11
|
+
import { useDevWarning } from "@brightlocal/ui-components/lib/use-dev-warning";
|
|
12
|
+
|
|
13
|
+
/* -------------------------------------------------------------------------------------------------
|
|
14
|
+
* Command Context — shared state between CommandInput and CommandVirtualList
|
|
15
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
16
|
+
|
|
17
|
+
interface CommandContextValue {
|
|
18
|
+
searchValue: string;
|
|
19
|
+
setSearchValue: (value: string) => void;
|
|
20
|
+
activeDescendantId: string | undefined;
|
|
21
|
+
setActiveDescendantId: React.Dispatch<
|
|
22
|
+
React.SetStateAction<string | undefined>
|
|
23
|
+
>;
|
|
24
|
+
listboxId: string | undefined;
|
|
25
|
+
setListboxId: React.Dispatch<React.SetStateAction<string | undefined>>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const CommandContext = React.createContext<CommandContextValue>({
|
|
29
|
+
searchValue: "",
|
|
30
|
+
setSearchValue: () => {},
|
|
31
|
+
activeDescendantId: undefined,
|
|
32
|
+
setActiveDescendantId: () => {},
|
|
33
|
+
listboxId: undefined,
|
|
34
|
+
setListboxId: () => {},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
/* -------------------------------------------------------------------------------------------------
|
|
38
|
+
* Command
|
|
39
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
9
40
|
|
|
10
41
|
/**
|
|
11
42
|
* Props for the Command component
|
|
@@ -34,22 +65,49 @@ export interface CommandProps extends React.ComponentProps<
|
|
|
34
65
|
const Command = React.forwardRef<
|
|
35
66
|
React.ComponentRef<typeof CommandPrimitive>,
|
|
36
67
|
CommandProps
|
|
37
|
-
>(({ className, dataHook, trackingEl, trackingLabel, ...props }, ref) =>
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
68
|
+
>(({ className, dataHook, trackingEl, trackingLabel, ...props }, ref) => {
|
|
69
|
+
const [searchValue, setSearchValue] = React.useState("");
|
|
70
|
+
const [activeDescendantId, setActiveDescendantId] = React.useState<
|
|
71
|
+
string | undefined
|
|
72
|
+
>(undefined);
|
|
73
|
+
const [listboxId, setListboxId] = React.useState<string | undefined>(
|
|
74
|
+
undefined
|
|
75
|
+
);
|
|
76
|
+
const contextValue = React.useMemo(
|
|
77
|
+
() => ({
|
|
78
|
+
searchValue,
|
|
79
|
+
setSearchValue,
|
|
80
|
+
activeDescendantId,
|
|
81
|
+
setActiveDescendantId,
|
|
82
|
+
listboxId,
|
|
83
|
+
setListboxId,
|
|
84
|
+
}),
|
|
85
|
+
[searchValue, activeDescendantId, listboxId]
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
return (
|
|
89
|
+
<CommandContext.Provider value={contextValue}>
|
|
90
|
+
<CommandPrimitive
|
|
91
|
+
ref={ref}
|
|
92
|
+
className={cn(
|
|
93
|
+
"bg-popover text-popover-foreground flex w-full flex-col overflow-hidden rounded-lg border shadow-md",
|
|
94
|
+
className
|
|
95
|
+
)}
|
|
96
|
+
data-hook={dataHook}
|
|
97
|
+
data-slot="command"
|
|
98
|
+
data-tracking-el={trackingEl}
|
|
99
|
+
data-tracking-label={trackingLabel}
|
|
100
|
+
{...props}
|
|
101
|
+
/>
|
|
102
|
+
</CommandContext.Provider>
|
|
103
|
+
);
|
|
104
|
+
});
|
|
51
105
|
Command.displayName = "Command";
|
|
52
106
|
|
|
107
|
+
/* -------------------------------------------------------------------------------------------------
|
|
108
|
+
* Command Input
|
|
109
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
110
|
+
|
|
53
111
|
/**
|
|
54
112
|
* Props for the CommandInput component
|
|
55
113
|
*/
|
|
@@ -72,6 +130,13 @@ export interface CommandInputProps extends React.ComponentProps<
|
|
|
72
130
|
* Callback when clear button is clicked
|
|
73
131
|
*/
|
|
74
132
|
onClear?: () => void;
|
|
133
|
+
/**
|
|
134
|
+
* Debounce delay in milliseconds for the `onValueChange` callback.
|
|
135
|
+
* The input display updates immediately; only the callback is debounced.
|
|
136
|
+
* Useful for async filtering to avoid firing on every keystroke.
|
|
137
|
+
* @example 300
|
|
138
|
+
*/
|
|
139
|
+
debounceMs?: number;
|
|
75
140
|
}
|
|
76
141
|
|
|
77
142
|
/**
|
|
@@ -91,16 +156,52 @@ const CommandInput = React.forwardRef<
|
|
|
91
156
|
value: controlledValue,
|
|
92
157
|
onValueChange,
|
|
93
158
|
onClear,
|
|
159
|
+
debounceMs,
|
|
94
160
|
...props
|
|
95
161
|
},
|
|
96
162
|
ref
|
|
97
163
|
) => {
|
|
98
164
|
const inputRef = React.useRef<HTMLInputElement>(null);
|
|
99
165
|
const [internalValue, setInternalValue] = React.useState("");
|
|
166
|
+
const timerRef = React.useRef<ReturnType<typeof setTimeout> | undefined>(
|
|
167
|
+
undefined
|
|
168
|
+
);
|
|
169
|
+
const { setSearchValue, activeDescendantId, listboxId } =
|
|
170
|
+
React.useContext(CommandContext);
|
|
100
171
|
|
|
101
|
-
// Merge refs
|
|
102
172
|
React.useImperativeHandle(ref, () => inputRef.current!, []);
|
|
103
173
|
|
|
174
|
+
useDevWarning(
|
|
175
|
+
() => !!(debounceMs && debounceMs > 0 && controlledValue !== undefined),
|
|
176
|
+
"debounceMs is set on <CommandInput> together with a controlled `value` prop.\n" +
|
|
177
|
+
"The debounced onValueChange callback cannot update the controlled value " +
|
|
178
|
+
"synchronously, so the input will appear to lag behind typing.\n" +
|
|
179
|
+
"Remove `value` to let the input manage its own display, or remove " +
|
|
180
|
+
"debounceMs if you need a controlled input.",
|
|
181
|
+
[debounceMs, controlledValue]
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
React.useEffect(() => {
|
|
185
|
+
return () => {
|
|
186
|
+
if (timerRef.current) clearTimeout(timerRef.current);
|
|
187
|
+
};
|
|
188
|
+
}, []);
|
|
189
|
+
|
|
190
|
+
// cmdk sets aria-controls/aria-activedescendant internally after
|
|
191
|
+
// spreading props, so we override via DOM to point at our virtual listbox.
|
|
192
|
+
React.useEffect(() => {
|
|
193
|
+
const el = inputRef.current;
|
|
194
|
+
if (!el) return;
|
|
195
|
+
if (listboxId) {
|
|
196
|
+
el.setAttribute("aria-controls", listboxId);
|
|
197
|
+
}
|
|
198
|
+
if (activeDescendantId) {
|
|
199
|
+
el.setAttribute("aria-activedescendant", activeDescendantId);
|
|
200
|
+
} else {
|
|
201
|
+
el.removeAttribute("aria-activedescendant");
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
|
|
104
205
|
const isControlled = controlledValue !== undefined;
|
|
105
206
|
const value = isControlled ? controlledValue : internalValue;
|
|
106
207
|
const showClearButton = String(value).length > 0;
|
|
@@ -109,13 +210,24 @@ const CommandInput = React.forwardRef<
|
|
|
109
210
|
if (!isControlled) {
|
|
110
211
|
setInternalValue(newValue);
|
|
111
212
|
}
|
|
112
|
-
|
|
213
|
+
setSearchValue(newValue);
|
|
214
|
+
|
|
215
|
+
if (debounceMs && debounceMs > 0) {
|
|
216
|
+
clearTimeout(timerRef.current);
|
|
217
|
+
timerRef.current = setTimeout(() => {
|
|
218
|
+
onValueChange?.(newValue);
|
|
219
|
+
}, debounceMs);
|
|
220
|
+
} else {
|
|
221
|
+
onValueChange?.(newValue);
|
|
222
|
+
}
|
|
113
223
|
};
|
|
114
224
|
|
|
115
225
|
const handleClear = () => {
|
|
116
226
|
if (!isControlled) {
|
|
117
227
|
setInternalValue("");
|
|
118
228
|
}
|
|
229
|
+
setSearchValue("");
|
|
230
|
+
clearTimeout(timerRef.current);
|
|
119
231
|
onValueChange?.("");
|
|
120
232
|
onClear?.();
|
|
121
233
|
inputRef.current?.focus();
|
|
@@ -158,6 +270,10 @@ const CommandInput = React.forwardRef<
|
|
|
158
270
|
);
|
|
159
271
|
CommandInput.displayName = "CommandInput";
|
|
160
272
|
|
|
273
|
+
/* -------------------------------------------------------------------------------------------------
|
|
274
|
+
* Command List
|
|
275
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
276
|
+
|
|
161
277
|
/**
|
|
162
278
|
* Props for the CommandList component
|
|
163
279
|
*/
|
|
@@ -190,6 +306,10 @@ const CommandList = React.forwardRef<
|
|
|
190
306
|
));
|
|
191
307
|
CommandList.displayName = "CommandList";
|
|
192
308
|
|
|
309
|
+
/* -------------------------------------------------------------------------------------------------
|
|
310
|
+
* Command Empty
|
|
311
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
312
|
+
|
|
193
313
|
/**
|
|
194
314
|
* Props for the CommandEmpty component
|
|
195
315
|
*/
|
|
@@ -235,6 +355,10 @@ const CommandEmpty = React.forwardRef<
|
|
|
235
355
|
});
|
|
236
356
|
CommandEmpty.displayName = "CommandEmpty";
|
|
237
357
|
|
|
358
|
+
/* -------------------------------------------------------------------------------------------------
|
|
359
|
+
* Command Group
|
|
360
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
361
|
+
|
|
238
362
|
/**
|
|
239
363
|
* Props for the CommandGroup component
|
|
240
364
|
*/
|
|
@@ -267,6 +391,10 @@ const CommandGroup = React.forwardRef<
|
|
|
267
391
|
));
|
|
268
392
|
CommandGroup.displayName = "CommandGroup";
|
|
269
393
|
|
|
394
|
+
/* -------------------------------------------------------------------------------------------------
|
|
395
|
+
* Command Separator
|
|
396
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
397
|
+
|
|
270
398
|
/**
|
|
271
399
|
* Props for the CommandSeparator component
|
|
272
400
|
*/
|
|
@@ -296,6 +424,10 @@ const CommandSeparator = React.forwardRef<
|
|
|
296
424
|
));
|
|
297
425
|
CommandSeparator.displayName = "CommandSeparator";
|
|
298
426
|
|
|
427
|
+
/* -------------------------------------------------------------------------------------------------
|
|
428
|
+
* Command Item
|
|
429
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
430
|
+
|
|
299
431
|
/**
|
|
300
432
|
* Props for the CommandItem component
|
|
301
433
|
*/
|
|
@@ -359,6 +491,10 @@ const CommandItem = React.forwardRef<
|
|
|
359
491
|
);
|
|
360
492
|
CommandItem.displayName = "CommandItem";
|
|
361
493
|
|
|
494
|
+
/* -------------------------------------------------------------------------------------------------
|
|
495
|
+
* Command Shortcut
|
|
496
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
497
|
+
|
|
362
498
|
/**
|
|
363
499
|
* Props for the CommandShortcut component
|
|
364
500
|
*/
|
|
@@ -398,6 +534,489 @@ const CommandShortcut = React.forwardRef<HTMLSpanElement, CommandShortcutProps>(
|
|
|
398
534
|
);
|
|
399
535
|
CommandShortcut.displayName = "CommandShortcut";
|
|
400
536
|
|
|
537
|
+
/* -------------------------------------------------------------------------------------------------
|
|
538
|
+
* Command Virtual List Context
|
|
539
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
540
|
+
|
|
541
|
+
interface CommandVirtualListContextValue {
|
|
542
|
+
activeValue: string | null;
|
|
543
|
+
onItemPointerMove: (itemValue: string) => void;
|
|
544
|
+
idPrefix: string;
|
|
545
|
+
filteredCount: number;
|
|
546
|
+
getItemPosition: (itemValue: string) => number;
|
|
547
|
+
onSelectCallbacks: React.RefObject<Map<string, (() => void) | undefined>>;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
const CommandVirtualListContext =
|
|
551
|
+
React.createContext<CommandVirtualListContextValue | null>(null);
|
|
552
|
+
|
|
553
|
+
/* -------------------------------------------------------------------------------------------------
|
|
554
|
+
* Command Virtual List
|
|
555
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Props for the CommandVirtualList component
|
|
559
|
+
*/
|
|
560
|
+
export interface CommandVirtualListProps<T> {
|
|
561
|
+
/**
|
|
562
|
+
* Array of items to render. Memoize to avoid re-registration on every render.
|
|
563
|
+
*/
|
|
564
|
+
items: T[];
|
|
565
|
+
/**
|
|
566
|
+
* Extract a unique string value from each item.
|
|
567
|
+
*/
|
|
568
|
+
getItemValue: (item: T) => string;
|
|
569
|
+
/**
|
|
570
|
+
* Extract a display label for filtering. Defaults to `getItemValue`.
|
|
571
|
+
*/
|
|
572
|
+
getItemLabel?: (item: T) => string;
|
|
573
|
+
/**
|
|
574
|
+
* Extra searchable keywords per item.
|
|
575
|
+
*/
|
|
576
|
+
getItemKeywords?: (item: T) => string[];
|
|
577
|
+
/**
|
|
578
|
+
* Estimated item height in px for the virtualiser.
|
|
579
|
+
* @default 44
|
|
580
|
+
*/
|
|
581
|
+
estimateSize?: number;
|
|
582
|
+
/**
|
|
583
|
+
* Number of items to render outside the visible viewport.
|
|
584
|
+
* @default 5
|
|
585
|
+
*/
|
|
586
|
+
overscan?: number;
|
|
587
|
+
/**
|
|
588
|
+
* Render function called for each visible item.
|
|
589
|
+
*/
|
|
590
|
+
children: (item: T, index: number, searchValue: string) => React.ReactNode;
|
|
591
|
+
/**
|
|
592
|
+
* Content rendered when the filtered list is empty.
|
|
593
|
+
*/
|
|
594
|
+
renderEmpty?: React.ReactNode;
|
|
595
|
+
/**
|
|
596
|
+
* Accessible label for the listbox.
|
|
597
|
+
* Required for a11y — describes the list's purpose to screen readers.
|
|
598
|
+
*/
|
|
599
|
+
"aria-label": string;
|
|
600
|
+
/**
|
|
601
|
+
* Additional CSS classes for the scroll container.
|
|
602
|
+
*/
|
|
603
|
+
className?: string;
|
|
604
|
+
/**
|
|
605
|
+
* Data hook for automated testing.
|
|
606
|
+
*/
|
|
607
|
+
dataHook?: string;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* Virtualised list for Command that renders only visible items.
|
|
612
|
+
* Use with `shouldFilter={false}` on the parent Command component.
|
|
613
|
+
*
|
|
614
|
+
* @example
|
|
615
|
+
* ```tsx
|
|
616
|
+
* <Command dataHook="cmd" shouldFilter={false}>
|
|
617
|
+
* <CommandInput placeholder="Search..." />
|
|
618
|
+
* <CommandVirtualList
|
|
619
|
+
* items={items}
|
|
620
|
+
* getItemValue={(item) => item.value}
|
|
621
|
+
* getItemLabel={(item) => item.label}
|
|
622
|
+
* dataHook="virtual-list"
|
|
623
|
+
* >
|
|
624
|
+
* {(item) => (
|
|
625
|
+
* <CommandVirtualItem value={item.value}>{item.label}</CommandVirtualItem>
|
|
626
|
+
* )}
|
|
627
|
+
* </CommandVirtualList>
|
|
628
|
+
* </Command>
|
|
629
|
+
* ```
|
|
630
|
+
*/
|
|
631
|
+
function CommandVirtualListInner<T>(
|
|
632
|
+
{
|
|
633
|
+
items,
|
|
634
|
+
getItemValue,
|
|
635
|
+
getItemLabel: getItemLabelProp,
|
|
636
|
+
getItemKeywords,
|
|
637
|
+
estimateSize = 44,
|
|
638
|
+
overscan = 5,
|
|
639
|
+
children,
|
|
640
|
+
renderEmpty,
|
|
641
|
+
"aria-label": ariaLabel,
|
|
642
|
+
className,
|
|
643
|
+
dataHook,
|
|
644
|
+
}: CommandVirtualListProps<T>,
|
|
645
|
+
ref: React.ForwardedRef<HTMLDivElement>
|
|
646
|
+
): React.ReactElement | null {
|
|
647
|
+
const parentRef = React.useRef<HTMLDivElement>(null);
|
|
648
|
+
const { searchValue, setActiveDescendantId, setListboxId } =
|
|
649
|
+
React.useContext(CommandContext);
|
|
650
|
+
const [activeIndex, setActiveIndex] = React.useState(0);
|
|
651
|
+
const idPrefix = React.useId();
|
|
652
|
+
const listboxId = React.useId();
|
|
653
|
+
const onSelectCallbacksRef = React.useRef<
|
|
654
|
+
Map<string, (() => void) | undefined>
|
|
655
|
+
>(new Map());
|
|
656
|
+
|
|
657
|
+
React.useImperativeHandle(ref, () => parentRef.current!, []);
|
|
658
|
+
|
|
659
|
+
const getItemLabel = React.useMemo(
|
|
660
|
+
() => getItemLabelProp ?? getItemValue,
|
|
661
|
+
[getItemLabelProp, getItemValue]
|
|
662
|
+
);
|
|
663
|
+
|
|
664
|
+
const filteredItems = React.useMemo(() => {
|
|
665
|
+
if (!searchValue.trim()) return items;
|
|
666
|
+
|
|
667
|
+
const scored: { item: T; score: number }[] = [];
|
|
668
|
+
for (const item of items) {
|
|
669
|
+
let score = defaultFilter(getItemLabel(item), searchValue);
|
|
670
|
+
|
|
671
|
+
if (getItemKeywords) {
|
|
672
|
+
for (const kw of getItemKeywords(item)) {
|
|
673
|
+
score = Math.max(score, defaultFilter(kw, searchValue));
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
if (score > 0) scored.push({ item, score });
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
scored.sort((a, b) => b.score - a.score);
|
|
681
|
+
return scored.map((s) => s.item);
|
|
682
|
+
}, [items, searchValue, getItemLabel, getItemKeywords]);
|
|
683
|
+
|
|
684
|
+
// eslint-disable-next-line react-hooks/incompatible-library -- compiler already skips this component; virtualizer is not passed to memoized consumers
|
|
685
|
+
const virtualizer = useVirtualizer({
|
|
686
|
+
count: filteredItems.length,
|
|
687
|
+
getScrollElement: () => parentRef.current,
|
|
688
|
+
estimateSize: () => estimateSize,
|
|
689
|
+
overscan,
|
|
690
|
+
});
|
|
691
|
+
|
|
692
|
+
const activeValue =
|
|
693
|
+
activeIndex >= 0 && activeIndex < filteredItems.length
|
|
694
|
+
? getItemValue(filteredItems[activeIndex]!)
|
|
695
|
+
: null;
|
|
696
|
+
|
|
697
|
+
React.useEffect(() => {
|
|
698
|
+
setActiveIndex(0);
|
|
699
|
+
}, [searchValue, filteredItems.length]);
|
|
700
|
+
|
|
701
|
+
const scrollOnNextIndexChange = React.useRef(false);
|
|
702
|
+
|
|
703
|
+
React.useEffect(() => {
|
|
704
|
+
if (
|
|
705
|
+
scrollOnNextIndexChange.current &&
|
|
706
|
+
activeIndex >= 0 &&
|
|
707
|
+
activeIndex < filteredItems.length
|
|
708
|
+
) {
|
|
709
|
+
virtualizer.scrollToIndex(activeIndex, { align: "auto" });
|
|
710
|
+
}
|
|
711
|
+
scrollOnNextIndexChange.current = false;
|
|
712
|
+
}, [activeIndex, filteredItems.length, virtualizer]);
|
|
713
|
+
|
|
714
|
+
React.useEffect(() => {
|
|
715
|
+
const el = parentRef.current?.closest(
|
|
716
|
+
'[data-slot="command"]'
|
|
717
|
+
) as HTMLElement | null;
|
|
718
|
+
if (!el) return;
|
|
719
|
+
|
|
720
|
+
const handleKeyDown = (e: KeyboardEvent) => {
|
|
721
|
+
switch (e.key) {
|
|
722
|
+
case "ArrowDown":
|
|
723
|
+
e.preventDefault();
|
|
724
|
+
e.stopPropagation();
|
|
725
|
+
scrollOnNextIndexChange.current = true;
|
|
726
|
+
setActiveIndex((prev) =>
|
|
727
|
+
prev < filteredItems.length - 1 ? prev + 1 : prev
|
|
728
|
+
);
|
|
729
|
+
break;
|
|
730
|
+
case "ArrowUp":
|
|
731
|
+
e.preventDefault();
|
|
732
|
+
e.stopPropagation();
|
|
733
|
+
scrollOnNextIndexChange.current = true;
|
|
734
|
+
setActiveIndex((prev) => (prev > 0 ? prev - 1 : prev));
|
|
735
|
+
break;
|
|
736
|
+
case "Enter": {
|
|
737
|
+
e.preventDefault();
|
|
738
|
+
e.stopPropagation();
|
|
739
|
+
if (activeValue != null) {
|
|
740
|
+
const callback = onSelectCallbacksRef.current.get(activeValue);
|
|
741
|
+
callback?.();
|
|
742
|
+
}
|
|
743
|
+
break;
|
|
744
|
+
}
|
|
745
|
+
case "Home":
|
|
746
|
+
e.preventDefault();
|
|
747
|
+
e.stopPropagation();
|
|
748
|
+
scrollOnNextIndexChange.current = true;
|
|
749
|
+
setActiveIndex(0);
|
|
750
|
+
break;
|
|
751
|
+
case "End":
|
|
752
|
+
e.preventDefault();
|
|
753
|
+
e.stopPropagation();
|
|
754
|
+
scrollOnNextIndexChange.current = true;
|
|
755
|
+
setActiveIndex(filteredItems.length - 1);
|
|
756
|
+
break;
|
|
757
|
+
}
|
|
758
|
+
};
|
|
759
|
+
|
|
760
|
+
el.addEventListener("keydown", handleKeyDown, { capture: true });
|
|
761
|
+
return () =>
|
|
762
|
+
el.removeEventListener("keydown", handleKeyDown, { capture: true });
|
|
763
|
+
}, [filteredItems, activeValue]);
|
|
764
|
+
|
|
765
|
+
const itemIndexMap = React.useMemo(() => {
|
|
766
|
+
const map = new Map<string, number>();
|
|
767
|
+
filteredItems.forEach((item, i) => {
|
|
768
|
+
map.set(getItemValue(item), i);
|
|
769
|
+
});
|
|
770
|
+
return map;
|
|
771
|
+
}, [filteredItems, getItemValue]);
|
|
772
|
+
|
|
773
|
+
const onItemPointerMove = React.useCallback(
|
|
774
|
+
(itemValue: string) => {
|
|
775
|
+
const index = itemIndexMap.get(itemValue);
|
|
776
|
+
if (index !== undefined) setActiveIndex(index);
|
|
777
|
+
},
|
|
778
|
+
[itemIndexMap]
|
|
779
|
+
);
|
|
780
|
+
|
|
781
|
+
const getItemPosition = React.useCallback(
|
|
782
|
+
(itemValue: string) => (itemIndexMap.get(itemValue) ?? 0) + 1,
|
|
783
|
+
[itemIndexMap]
|
|
784
|
+
);
|
|
785
|
+
|
|
786
|
+
const filteredCount = filteredItems.length;
|
|
787
|
+
|
|
788
|
+
const contextValue = React.useMemo<CommandVirtualListContextValue>(
|
|
789
|
+
() => ({
|
|
790
|
+
activeValue,
|
|
791
|
+
onItemPointerMove,
|
|
792
|
+
idPrefix,
|
|
793
|
+
filteredCount,
|
|
794
|
+
getItemPosition,
|
|
795
|
+
onSelectCallbacks: onSelectCallbacksRef,
|
|
796
|
+
}),
|
|
797
|
+
[activeValue, onItemPointerMove, idPrefix, filteredCount, getItemPosition]
|
|
798
|
+
);
|
|
799
|
+
|
|
800
|
+
// Push active descendant ID into CommandContext for CommandInput's aria-activedescendant
|
|
801
|
+
const activeItemId =
|
|
802
|
+
activeValue != null ? `${idPrefix}item-${activeValue}` : undefined;
|
|
803
|
+
|
|
804
|
+
React.useEffect(() => {
|
|
805
|
+
setActiveDescendantId(activeItemId);
|
|
806
|
+
return () => setActiveDescendantId(undefined);
|
|
807
|
+
}, [activeItemId, setActiveDescendantId]);
|
|
808
|
+
|
|
809
|
+
// Push listbox ID into CommandContext for CommandInput's aria-controls
|
|
810
|
+
React.useEffect(() => {
|
|
811
|
+
setListboxId(listboxId);
|
|
812
|
+
return () => setListboxId(undefined);
|
|
813
|
+
}, [listboxId, setListboxId]);
|
|
814
|
+
|
|
815
|
+
const scrollContainerClass = cn(
|
|
816
|
+
"scrollbar max-h-[300px] min-h-0 flex-1 scroll-py-1 overflow-x-hidden overflow-y-auto py-1",
|
|
817
|
+
className
|
|
818
|
+
);
|
|
819
|
+
|
|
820
|
+
if (filteredItems.length === 0) {
|
|
821
|
+
return (
|
|
822
|
+
<div
|
|
823
|
+
ref={parentRef}
|
|
824
|
+
aria-label={ariaLabel}
|
|
825
|
+
className={scrollContainerClass}
|
|
826
|
+
data-hook={dataHook}
|
|
827
|
+
data-slot="command-virtual-list"
|
|
828
|
+
id={listboxId}
|
|
829
|
+
role="listbox"
|
|
830
|
+
tabIndex={-1}
|
|
831
|
+
>
|
|
832
|
+
{renderEmpty}
|
|
833
|
+
</div>
|
|
834
|
+
);
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
return (
|
|
838
|
+
<CommandVirtualListContext.Provider value={contextValue}>
|
|
839
|
+
<div
|
|
840
|
+
ref={parentRef}
|
|
841
|
+
aria-label={ariaLabel}
|
|
842
|
+
className={scrollContainerClass}
|
|
843
|
+
data-hook={dataHook}
|
|
844
|
+
data-slot="command-virtual-list"
|
|
845
|
+
id={listboxId}
|
|
846
|
+
role="listbox"
|
|
847
|
+
tabIndex={-1}
|
|
848
|
+
>
|
|
849
|
+
<div
|
|
850
|
+
className="relative w-full"
|
|
851
|
+
style={{ height: `${virtualizer.getTotalSize()}px` }}
|
|
852
|
+
>
|
|
853
|
+
{virtualizer.getVirtualItems().map((virtualItem) => (
|
|
854
|
+
<div
|
|
855
|
+
key={virtualItem.key}
|
|
856
|
+
ref={virtualizer.measureElement}
|
|
857
|
+
className="absolute top-0 left-0 w-full px-2"
|
|
858
|
+
data-index={virtualItem.index}
|
|
859
|
+
style={{
|
|
860
|
+
transform: `translateY(${virtualItem.start}px)`,
|
|
861
|
+
}}
|
|
862
|
+
>
|
|
863
|
+
{children(
|
|
864
|
+
filteredItems[virtualItem.index]!,
|
|
865
|
+
virtualItem.index,
|
|
866
|
+
searchValue
|
|
867
|
+
)}
|
|
868
|
+
</div>
|
|
869
|
+
))}
|
|
870
|
+
</div>
|
|
871
|
+
</div>
|
|
872
|
+
</CommandVirtualListContext.Provider>
|
|
873
|
+
);
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
const CommandVirtualList = React.forwardRef(CommandVirtualListInner) as <T>(
|
|
877
|
+
props: CommandVirtualListProps<T> & { ref?: React.Ref<HTMLDivElement> }
|
|
878
|
+
) => React.ReactElement | null;
|
|
879
|
+
|
|
880
|
+
/* -------------------------------------------------------------------------------------------------
|
|
881
|
+
* Command Virtual Item
|
|
882
|
+
* -----------------------------------------------------------------------------------------------*/
|
|
883
|
+
|
|
884
|
+
/**
|
|
885
|
+
* Props for the CommandVirtualItem component
|
|
886
|
+
*/
|
|
887
|
+
export interface CommandVirtualItemProps extends React.ComponentPropsWithoutRef<"div"> {
|
|
888
|
+
/**
|
|
889
|
+
* Unique value identifying this item.
|
|
890
|
+
*/
|
|
891
|
+
value: string;
|
|
892
|
+
/**
|
|
893
|
+
* Data hook for automated testing.
|
|
894
|
+
*/
|
|
895
|
+
dataHook?: string;
|
|
896
|
+
/**
|
|
897
|
+
* Tracking element identifier for analytics.
|
|
898
|
+
*/
|
|
899
|
+
trackingEl?: string;
|
|
900
|
+
/**
|
|
901
|
+
* Tracking label for analytics context.
|
|
902
|
+
*/
|
|
903
|
+
trackingLabel?: string;
|
|
904
|
+
/**
|
|
905
|
+
* Whether this item is selected (shows checkmark).
|
|
906
|
+
*/
|
|
907
|
+
selected?: boolean;
|
|
908
|
+
/**
|
|
909
|
+
* Whether this item is disabled.
|
|
910
|
+
*/
|
|
911
|
+
disabled?: boolean;
|
|
912
|
+
/**
|
|
913
|
+
* Callback when the item is activated (click or Enter).
|
|
914
|
+
*/
|
|
915
|
+
onSelect?: () => void;
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
/**
|
|
919
|
+
* Item within a CommandVirtualList. Renders as a plain div with proper
|
|
920
|
+
* ARIA attributes, bypassing cmdk item registration for performance.
|
|
921
|
+
*/
|
|
922
|
+
const CommandVirtualItem = React.forwardRef<
|
|
923
|
+
HTMLDivElement,
|
|
924
|
+
CommandVirtualItemProps
|
|
925
|
+
>(
|
|
926
|
+
(
|
|
927
|
+
{
|
|
928
|
+
value,
|
|
929
|
+
dataHook,
|
|
930
|
+
trackingEl,
|
|
931
|
+
trackingLabel,
|
|
932
|
+
selected,
|
|
933
|
+
disabled,
|
|
934
|
+
onSelect,
|
|
935
|
+
children,
|
|
936
|
+
className,
|
|
937
|
+
onClick: consumerOnClick,
|
|
938
|
+
onKeyDown: consumerOnKeyDown,
|
|
939
|
+
onPointerMove: consumerOnPointerMove,
|
|
940
|
+
...props
|
|
941
|
+
},
|
|
942
|
+
ref
|
|
943
|
+
) => {
|
|
944
|
+
const virtualCtx = React.useContext(CommandVirtualListContext);
|
|
945
|
+
const isHighlighted = virtualCtx?.activeValue === value;
|
|
946
|
+
|
|
947
|
+
// Register onSelect callback so the virtual list's Enter handler
|
|
948
|
+
// can invoke it via state instead of DOM querying.
|
|
949
|
+
const onSelectRef = React.useRef(onSelect);
|
|
950
|
+
React.useEffect(() => {
|
|
951
|
+
onSelectRef.current = onSelect;
|
|
952
|
+
});
|
|
953
|
+
|
|
954
|
+
React.useEffect(() => {
|
|
955
|
+
if (!virtualCtx) return;
|
|
956
|
+
virtualCtx.onSelectCallbacks.current.set(value, () =>
|
|
957
|
+
onSelectRef.current?.()
|
|
958
|
+
);
|
|
959
|
+
return () => {
|
|
960
|
+
virtualCtx.onSelectCallbacks.current.delete(value);
|
|
961
|
+
};
|
|
962
|
+
}, [value, virtualCtx]);
|
|
963
|
+
|
|
964
|
+
return (
|
|
965
|
+
<div
|
|
966
|
+
ref={ref}
|
|
967
|
+
{...props}
|
|
968
|
+
aria-disabled={disabled || undefined}
|
|
969
|
+
aria-posinset={virtualCtx?.getItemPosition(value)}
|
|
970
|
+
aria-selected={selected || undefined}
|
|
971
|
+
aria-setsize={virtualCtx?.filteredCount}
|
|
972
|
+
className={cn(
|
|
973
|
+
"data-[highlighted=true]:bg-accent data-[highlighted=true]:text-accent-foreground relative flex cursor-default items-center gap-2 rounded-sm px-2 py-3 text-sm outline-hidden select-none",
|
|
974
|
+
"[&_svg]:pointer-events-none [&_svg]:shrink-0",
|
|
975
|
+
disabled && "pointer-events-none opacity-50",
|
|
976
|
+
className
|
|
977
|
+
)}
|
|
978
|
+
data-highlighted={isHighlighted || undefined}
|
|
979
|
+
data-hook={dataHook}
|
|
980
|
+
data-selected-item={selected || undefined}
|
|
981
|
+
data-slot="command-virtual-item"
|
|
982
|
+
data-tracking-el={trackingEl}
|
|
983
|
+
data-tracking-label={trackingLabel}
|
|
984
|
+
id={virtualCtx ? `${virtualCtx.idPrefix}item-${value}` : undefined}
|
|
985
|
+
role="option"
|
|
986
|
+
tabIndex={-1}
|
|
987
|
+
onClick={
|
|
988
|
+
disabled
|
|
989
|
+
? undefined
|
|
990
|
+
: (e) => {
|
|
991
|
+
onSelect?.();
|
|
992
|
+
consumerOnClick?.(e);
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
onKeyDown={
|
|
996
|
+
disabled
|
|
997
|
+
? undefined
|
|
998
|
+
: (e) => {
|
|
999
|
+
if (e.key === "Enter" || e.key === " ") onSelect?.();
|
|
1000
|
+
consumerOnKeyDown?.(e);
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
onPointerMove={
|
|
1004
|
+
disabled
|
|
1005
|
+
? undefined
|
|
1006
|
+
: (e) => {
|
|
1007
|
+
virtualCtx?.onItemPointerMove(value);
|
|
1008
|
+
consumerOnPointerMove?.(e);
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
>
|
|
1012
|
+
{children}
|
|
1013
|
+
{selected && <Check className="text-foreground ml-auto" />}
|
|
1014
|
+
</div>
|
|
1015
|
+
);
|
|
1016
|
+
}
|
|
1017
|
+
);
|
|
1018
|
+
CommandVirtualItem.displayName = "CommandVirtualItem";
|
|
1019
|
+
|
|
401
1020
|
export {
|
|
402
1021
|
Command,
|
|
403
1022
|
CommandEmpty,
|
|
@@ -407,4 +1026,6 @@ export {
|
|
|
407
1026
|
CommandList,
|
|
408
1027
|
CommandSeparator,
|
|
409
1028
|
CommandShortcut,
|
|
1029
|
+
CommandVirtualItem,
|
|
1030
|
+
CommandVirtualList,
|
|
410
1031
|
};
|