@foundrykit/components 1.0.22 → 1.1.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.
@@ -2,7 +2,7 @@
2
2
 
3
3
  import * as React from 'react';
4
4
  import { Slot } from '@radix-ui/react-slot';
5
- import { cva, VariantProps } from 'class-variance-authority';
5
+ import { cva, type VariantProps } from 'class-variance-authority';
6
6
  import { Search, X, ChevronDown } from 'lucide-react';
7
7
 
8
8
  import { cn } from '@foundrykit/utils';
@@ -15,7 +15,6 @@ import { Input } from '@foundrykit/primitives';
15
15
  import { Button } from '@foundrykit/primitives';
16
16
  import { Separator } from '@foundrykit/primitives';
17
17
  import { Badge } from '@foundrykit/primitives';
18
- import { Card, CardContent } from '@foundrykit/primitives';
19
18
  import { ScrollArea } from '@foundrykit/primitives';
20
19
 
21
20
  // Re-export types from hooks package
@@ -71,7 +70,8 @@ export interface AdvancedSearchProviderProps {
71
70
  */
72
71
  styles?: {
73
72
  /** CSS properties for the results dropdown */
74
- results: React.CSSProperties;
73
+ results?: React.CSSProperties;
74
+ width?: string | number;
75
75
  };
76
76
  }
77
77
 
@@ -110,65 +110,81 @@ export function AdvancedSearchProvider({
110
110
  results: {
111
111
  maxHeight: '24rem',
112
112
  },
113
+ width: '100%',
113
114
  },
114
115
  }: AdvancedSearchProviderProps) {
116
+ const inputId = React.useId();
117
+ const resultsId = React.useId();
115
118
  const [query, setQuery] = React.useState('');
116
119
  const [isOpen, setIsOpen] = React.useState(false);
117
120
  const [results, setResults] = React.useState<SearchResult[]>(initialResults);
118
121
  const [isLoading, setIsLoading] = React.useState(false);
122
+ const [activeIndex, setActiveIndex] = React.useState(-1);
123
+ const timeoutRef = React.useRef<ReturnType<typeof setTimeout> | null>(null);
124
+ const requestIdRef = React.useRef(0);
125
+
126
+ React.useEffect(() => {
127
+ return () => {
128
+ if (timeoutRef.current) {
129
+ clearTimeout(timeoutRef.current);
130
+ }
131
+ };
132
+ }, []);
119
133
 
120
- const debouncedSearch = React.useCallback(
134
+ const handleSearch = React.useCallback(
121
135
  (searchQuery: string) => {
122
- const timeoutId = setTimeout(async () => {
123
- if (searchQuery.length >= minQueryLength && onSearch) {
124
- setIsLoading(true);
125
- try {
126
- const searchResults = await onSearch(searchQuery);
136
+ if (timeoutRef.current) {
137
+ clearTimeout(timeoutRef.current);
138
+ }
139
+
140
+ setActiveIndex(-1);
141
+
142
+ const trimmedQuery = searchQuery.trim();
143
+ const isEmptyQuery = trimmedQuery.length === 0;
144
+ const isTooShort = trimmedQuery.length < minQueryLength;
145
+
146
+ if (isEmptyQuery || isTooShort || !onSearch) {
147
+ requestIdRef.current += 1;
148
+ setIsLoading(false);
149
+ setResults(
150
+ emptyStateBehavior === 'show-all-results' &&
151
+ (isEmptyQuery || isTooShort)
152
+ ? initialResults
153
+ : []
154
+ );
155
+ return;
156
+ }
157
+
158
+ const requestId = requestIdRef.current + 1;
159
+ requestIdRef.current = requestId;
160
+
161
+ timeoutRef.current = setTimeout(async () => {
162
+ setIsLoading(true);
163
+ try {
164
+ const searchResults = await onSearch(trimmedQuery);
165
+ if (requestIdRef.current === requestId) {
127
166
  setResults(searchResults);
128
- } catch (error) {
167
+ }
168
+ } catch (error) {
169
+ if (requestIdRef.current === requestId) {
129
170
  console.error('Search error:', error);
130
171
  setResults([]);
131
- } finally {
132
- setIsLoading(false);
133
172
  }
134
- } else {
135
- // Handle different empty state behaviors
136
- if (
137
- emptyStateBehavior === 'show-all-results' &&
138
- searchQuery.length === 0
139
- ) {
140
- // Show initial results when query is empty
141
- setResults(initialResults);
142
- } else if (
143
- emptyStateBehavior === 'show-all-results' &&
144
- searchQuery.length > 0 &&
145
- searchQuery.length < minQueryLength
146
- ) {
147
- // Show initial results when query is too short
148
- setResults(initialResults);
149
- } else {
150
- // Default behavior: show empty results
151
- setResults([]);
173
+ } finally {
174
+ if (requestIdRef.current === requestId) {
175
+ setIsLoading(false);
152
176
  }
153
177
  }
154
178
  }, debounceMs);
155
-
156
- return () => clearTimeout(timeoutId);
157
- },
158
- [onSearch, debounceMs, minQueryLength, emptyStateBehavior, initialResults]
159
- );
160
-
161
- const handleSearch = React.useCallback(
162
- (searchQuery: string) => {
163
- debouncedSearch(searchQuery);
164
179
  },
165
- [debouncedSearch]
180
+ [debounceMs, emptyStateBehavior, initialResults, minQueryLength, onSearch]
166
181
  );
167
182
 
168
183
  const handleResultSelect = React.useCallback(
169
184
  (result: SearchResult) => {
170
185
  onResultSelect?.(result);
171
186
  setQuery(result.title);
187
+ setActiveIndex(-1);
172
188
  setIsOpen(false);
173
189
  },
174
190
  [onResultSelect]
@@ -180,6 +196,10 @@ export function AdvancedSearchProvider({
180
196
  setQuery,
181
197
  isOpen,
182
198
  setIsOpen,
199
+ inputId,
200
+ resultsId,
201
+ activeIndex,
202
+ setActiveIndex,
183
203
  results,
184
204
  setResults,
185
205
  isLoading,
@@ -192,6 +212,9 @@ export function AdvancedSearchProvider({
192
212
  [
193
213
  query,
194
214
  isOpen,
215
+ inputId,
216
+ resultsId,
217
+ activeIndex,
195
218
  results,
196
219
  isLoading,
197
220
  handleSearch,
@@ -206,7 +229,8 @@ export function AdvancedSearchProvider({
206
229
  <div
207
230
  style={
208
231
  {
209
- '--results-max-height': styles.results.maxHeight,
232
+ '--results-max-height': styles.results?.maxHeight ?? '24rem',
233
+ width: styles.width,
210
234
  } as React.CSSProperties
211
235
  }
212
236
  >
@@ -313,7 +337,14 @@ export const SearchInput = React.forwardRef<HTMLInputElement, SearchInputProps>(
313
337
  query,
314
338
  setQuery,
315
339
  onSearch,
340
+ onResultSelect,
341
+ results,
342
+ isOpen,
316
343
  setIsOpen,
344
+ inputId,
345
+ resultsId,
346
+ activeIndex,
347
+ setActiveIndex,
317
348
  emptyStateBehavior,
318
349
  minQueryLength,
319
350
  } = useAdvancedSearch();
@@ -351,12 +382,65 @@ export const SearchInput = React.forwardRef<HTMLInputElement, SearchInputProps>(
351
382
 
352
383
  const handleClear = () => {
353
384
  setQuery('');
385
+ setActiveIndex(-1);
354
386
  setTimeout(() => {
355
387
  onSearch('');
356
388
  }, 0);
357
389
  onClear?.();
358
390
  };
359
391
 
392
+ const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
393
+ props.onKeyDown?.(e);
394
+ if (e.defaultPrevented) return;
395
+
396
+ if (e.key === 'Escape') {
397
+ setIsOpen(false);
398
+ setActiveIndex(-1);
399
+ return;
400
+ }
401
+
402
+ if (e.key === 'ArrowDown') {
403
+ e.preventDefault();
404
+ setIsOpen(true);
405
+ setActiveIndex(
406
+ results.length ? (activeIndex + 1) % results.length : -1
407
+ );
408
+ return;
409
+ }
410
+
411
+ if (e.key === 'ArrowUp') {
412
+ e.preventDefault();
413
+ setIsOpen(true);
414
+ setActiveIndex(
415
+ results.length
416
+ ? activeIndex <= 0
417
+ ? results.length - 1
418
+ : activeIndex - 1
419
+ : -1
420
+ );
421
+ return;
422
+ }
423
+
424
+ if (e.key === 'Home') {
425
+ if (!results.length) return;
426
+ e.preventDefault();
427
+ setActiveIndex(0);
428
+ return;
429
+ }
430
+
431
+ if (e.key === 'End') {
432
+ if (!results.length) return;
433
+ e.preventDefault();
434
+ setActiveIndex(results.length - 1);
435
+ return;
436
+ }
437
+
438
+ if (e.key === 'Enter' && activeIndex >= 0 && results[activeIndex]) {
439
+ e.preventDefault();
440
+ onResultSelect(results[activeIndex]);
441
+ }
442
+ };
443
+
360
444
  return (
361
445
  <Comp
362
446
  className={cn(
@@ -376,24 +460,37 @@ export const SearchInput = React.forwardRef<HTMLInputElement, SearchInputProps>(
376
460
  )}
377
461
  <Input
378
462
  ref={ref}
379
- type='text'
463
+ {...props}
464
+ id={inputId}
465
+ type='search'
466
+ role='combobox'
380
467
  aria-label='Search'
468
+ aria-autocomplete='list'
469
+ aria-expanded={isOpen}
470
+ aria-controls={resultsId}
471
+ aria-activedescendant={
472
+ activeIndex >= 0 && results[activeIndex]
473
+ ? `${resultsId}-option-${results[activeIndex].id}`
474
+ : undefined
475
+ }
381
476
  placeholder={placeholder}
382
477
  value={query}
383
478
  onChange={handleChange}
384
479
  onFocus={handleFocus}
480
+ onKeyDown={handleKeyDown}
385
481
  className={cn(
386
482
  'border-0 bg-transparent focus-visible:ring-0 focus-visible:ring-offset-0',
387
483
  showSearchIcon && 'pl-10',
388
484
  showClearButton && query && 'pr-10'
389
485
  )}
390
486
  size={undefined}
391
- {...props}
392
487
  />
393
488
  {showClearButton && query && (
394
489
  <Button
490
+ type='button'
395
491
  variant='ghost'
396
492
  size='sm'
493
+ aria-label='Clear search'
397
494
  className='absolute right-1 h-6 w-6 p-0'
398
495
  onClick={handleClear}
399
496
  >
@@ -433,16 +530,24 @@ export interface SearchTriggerProps
433
530
  export const SearchTrigger = React.forwardRef<
434
531
  HTMLButtonElement,
435
532
  SearchTriggerProps
436
- >(({ className, asChild = false, children, ...props }, ref) => {
437
- const { isOpen, setIsOpen } = useAdvancedSearch();
533
+ >(({ className, asChild = false, children, onClick, ...props }, ref) => {
534
+ const { isOpen, setIsOpen, resultsId } = useAdvancedSearch();
438
535
  const Comp = asChild ? Slot : Button;
439
536
 
440
537
  return (
441
538
  <Comp
442
539
  ref={ref}
443
- className={cn(className)}
444
- onClick={() => setIsOpen(!isOpen)}
445
540
  {...props}
541
+ type='button'
542
+ aria-expanded={isOpen}
543
+ aria-controls={resultsId}
544
+ className={cn(className)}
545
+ onClick={event => {
546
+ onClick?.(event);
547
+ if (!event.defaultPrevented) {
548
+ setIsOpen(!isOpen);
549
+ }
550
+ }}
446
551
  >
447
552
  {children}
448
553
  </Comp>
@@ -465,6 +570,10 @@ export interface SearchResultsProps
465
570
  emptyStateMessage?: string;
466
571
  /** Whether to show loading state while searching */
467
572
  showLoadingState?: boolean;
573
+ /** Whether loading replaces results or keeps previous results visible */
574
+ loadingBehavior?: 'replace-results' | 'preserve-results';
575
+ /** Custom loading state rendered instead of loadingMessage */
576
+ loadingState?: React.ReactNode;
468
577
  /** Custom message to show while loading */
469
578
  loadingMessage?: string;
470
579
  }
@@ -496,6 +605,8 @@ export const SearchResults = React.forwardRef<
496
605
  showEmptyState = true,
497
606
  emptyStateMessage = 'No results found',
498
607
  showLoadingState = true,
608
+ loadingBehavior = 'replace-results',
609
+ loadingState,
499
610
  loadingMessage = 'Searching...',
500
611
  ...props
501
612
  },
@@ -506,34 +617,45 @@ export const SearchResults = React.forwardRef<
506
617
  isLoading,
507
618
  query,
508
619
  onResultSelect,
620
+ resultsId,
621
+ activeIndex,
509
622
  emptyStateBehavior,
510
623
  minQueryLength,
511
624
  } = useAdvancedSearch();
512
625
 
513
- if (isLoading && showLoadingState) {
626
+ const shouldReplaceResultsWithLoading =
627
+ loadingBehavior === 'replace-results' || results.length === 0;
628
+
629
+ if (isLoading && showLoadingState && shouldReplaceResultsWithLoading) {
514
630
  return (
515
631
  <div
516
632
  ref={ref}
633
+ id={resultsId}
517
634
  className={cn(
518
635
  searchResultsVariants({ size }),
519
636
  'text-muted-foreground p-4 text-center',
520
637
  className
521
638
  )}
639
+ role='status'
640
+ aria-live='polite'
522
641
  {...props}
523
642
  >
524
- {loadingMessage}
643
+ {loadingState ?? loadingMessage}
525
644
  </div>
526
645
  );
527
646
  }
528
647
 
529
648
  // Handle different empty state behaviors
530
- if (!query || results.length === 0) {
649
+ const shouldShowEmptyState =
650
+ results.length === 0 ||
651
+ (!query && emptyStateBehavior !== 'show-all-results');
652
+
653
+ if (shouldShowEmptyState) {
531
654
  if (!showEmptyState) return null;
532
655
 
533
- // For hide-until-min-chars behavior, don't show anything if query is too short
656
+ // For hide-until-min-chars behavior, keep the dropdown hidden until the threshold is met.
534
657
  if (
535
658
  emptyStateBehavior === 'hide-until-min-chars' &&
536
- query.length > 0 &&
537
659
  query.length < minQueryLength
538
660
  ) {
539
661
  return null;
@@ -542,11 +664,14 @@ export const SearchResults = React.forwardRef<
542
664
  return (
543
665
  <div
544
666
  ref={ref}
667
+ id={resultsId}
545
668
  className={cn(
546
669
  searchResultsVariants({ size }),
547
670
  'text-muted-foreground p-4 text-center',
548
671
  className
549
672
  )}
673
+ role='status'
674
+ aria-live='polite'
550
675
  {...props}
551
676
  >
552
677
  {emptyStateMessage}
@@ -566,12 +691,15 @@ export const SearchResults = React.forwardRef<
566
691
  return (
567
692
  <div
568
693
  ref={ref}
694
+ id={resultsId}
569
695
  className={cn(
570
696
  searchResultsVariants({ size }),
571
697
  'max-h-[var(--results-max-height)]',
572
698
  className
573
699
  )}
700
+ role='listbox'
574
701
  aria-label='Search results'
702
+ aria-busy={isLoading || undefined}
575
703
  {...props}
576
704
  >
577
705
  <ScrollArea className='h-fit max-h-[var(--results-max-height)] *:max-h-[var(--results-max-height)]'>
@@ -584,13 +712,21 @@ export const SearchResults = React.forwardRef<
584
712
  {section.title}
585
713
  </h3>
586
714
  <div className='space-y-1'>
587
- {section.results.map((result: SearchResult) => (
588
- <SearchResultItem
589
- key={result.id}
590
- result={result}
591
- onClick={() => onResultSelect(result)}
592
- />
593
- ))}
715
+ {section.results.map((result: SearchResult) => {
716
+ const resultIndex = results.findIndex(
717
+ item => item.id === result.id
718
+ );
719
+
720
+ return (
721
+ <SearchResultItem
722
+ key={result.id}
723
+ id={`${resultsId}-option-${result.id}`}
724
+ result={result}
725
+ isActive={resultIndex === activeIndex}
726
+ onClick={() => onResultSelect(result)}
727
+ />
728
+ );
729
+ })}
594
730
  {section.showMore && section.onShowMore && (
595
731
  <Button
596
732
  variant='ghost'
@@ -619,16 +755,34 @@ SearchResults.displayName = 'SearchResults';
619
755
  * Internal component used to render individual search results.
620
756
  */
621
757
  interface SearchResultItemProps {
758
+ /** Stable id used by aria-activedescendant */
759
+ id: string;
622
760
  /** The search result data to display */
623
761
  result: SearchResult;
762
+ /** Whether this item is currently highlighted by keyboard navigation */
763
+ isActive: boolean;
624
764
  /** Callback function called when the result is clicked */
625
765
  onClick: () => void;
626
766
  }
627
767
 
628
- const SearchResultItem = ({ result, onClick }: SearchResultItemProps) => {
768
+ const SearchResultItem = ({
769
+ id,
770
+ result,
771
+ isActive,
772
+ onClick,
773
+ }: SearchResultItemProps) => {
774
+ const itemClasses = cn(
775
+ 'hover:bg-accent hover:text-accent-foreground focus-visible:border-ring focus-visible:ring-ring/50 w-full rounded-md text-left transition-colors outline-none focus-visible:ring-[3px]',
776
+ isActive && 'bg-accent text-accent-foreground'
777
+ );
778
+
629
779
  if (result.type === 'category') {
630
780
  return (
631
- <div className='text-muted-foreground p-2 text-sm font-medium'>
781
+ <div
782
+ id={id}
783
+ role='presentation'
784
+ className='text-muted-foreground p-2 text-sm font-medium'
785
+ >
632
786
  {result.title}
633
787
  </div>
634
788
  );
@@ -636,52 +790,76 @@ const SearchResultItem = ({ result, onClick }: SearchResultItemProps) => {
636
790
 
637
791
  if (result.type === 'suggestion') {
638
792
  return (
639
- <Button
640
- variant='ghost'
641
- size='sm'
642
- className='h-auto w-full justify-start p-2'
793
+ <button
794
+ id={id}
795
+ type='button'
796
+ role='option'
797
+ aria-selected={isActive}
798
+ className={cn(itemClasses, 'p-2 text-sm')}
643
799
  onClick={onClick}
644
800
  >
645
- <span className='text-sm'>{result.title}</span>
646
- </Button>
801
+ {result.title}
802
+ </button>
803
+ );
804
+ }
805
+
806
+ const content = (
807
+ <div className='flex gap-3 p-3'>
808
+ {result.image && (
809
+ <div className='flex-shrink-0'>
810
+ <img
811
+ src={result.image}
812
+ alt={result.title}
813
+ className='h-12 w-12 rounded-md object-cover'
814
+ loading='lazy'
815
+ />
816
+ </div>
817
+ )}
818
+ <div className='min-w-0 flex-1'>
819
+ <div className='flex items-start justify-between gap-2'>
820
+ <h4 className='truncate text-sm font-semibold'>{result.title}</h4>
821
+ {result.badge && (
822
+ <Badge variant='secondary' className='text-xs'>
823
+ {result.badge}
824
+ </Badge>
825
+ )}
826
+ </div>
827
+ {result.description && (
828
+ <p className='text-muted-foreground mt-1 line-clamp-2 text-xs'>
829
+ {result.description}
830
+ </p>
831
+ )}
832
+ </div>
833
+ </div>
834
+ );
835
+
836
+ if (result.href) {
837
+ return (
838
+ <a
839
+ id={id}
840
+ role='option'
841
+ aria-selected={isActive}
842
+ href={result.href}
843
+ className={cn(itemClasses, 'block')}
844
+ onClick={onClick}
845
+ >
846
+ {content}
847
+ </a>
647
848
  );
648
849
  }
649
850
 
650
851
  // Complex result with image, description, etc.
651
852
  return (
652
- <Card
653
- className='hover:bg-muted/50 w-full cursor-pointer transition-colors'
853
+ <button
854
+ id={id}
855
+ type='button'
856
+ role='option'
857
+ aria-selected={isActive}
858
+ className={itemClasses}
654
859
  onClick={onClick}
655
860
  >
656
- <CardContent className='p-3'>
657
- <div className='flex gap-3'>
658
- {result.image && (
659
- <div className='flex-shrink-0'>
660
- <img
661
- src={result.image}
662
- alt={result.title}
663
- className='h-12 w-12 rounded-md object-cover'
664
- />
665
- </div>
666
- )}
667
- <div className='min-w-0 flex-1'>
668
- <div className='flex items-start justify-between gap-2'>
669
- <h4 className='truncate text-sm font-semibold'>{result.title}</h4>
670
- {result.badge && (
671
- <Badge variant='secondary' className='text-xs'>
672
- {result.badge}
673
- </Badge>
674
- )}
675
- </div>
676
- {result.description && (
677
- <p className='text-muted-foreground mt-1 line-clamp-2 text-xs'>
678
- {result.description}
679
- </p>
680
- )}
681
- </div>
682
- </div>
683
- </CardContent>
684
- </Card>
861
+ {content}
862
+ </button>
685
863
  );
686
864
  };
687
865
 
@@ -712,30 +890,98 @@ export interface AdvancedSearchBarProps
712
890
  export const AdvancedSearchBar = React.forwardRef<
713
891
  HTMLDivElement,
714
892
  AdvancedSearchBarProps
715
- >(({ className, children, open, ...props }, ref) => {
716
- const { isOpen } = useAdvancedSearch();
893
+ >(({ className, children, open, onOpenChange, ...props }, ref) => {
894
+ const { isOpen, setIsOpen, setActiveIndex } = useAdvancedSearch();
895
+ const rootRef = React.useRef<HTMLDivElement | null>(null);
896
+ const previousOpenRef = React.useRef(isOpen);
717
897
 
718
898
  const isControlled = open !== undefined;
719
899
  const isOpenState = isControlled ? open : isOpen;
720
900
 
721
- // Find Input and Results components in children
722
- const childrenArray = React.Children.toArray(children);
723
- const inputChild = childrenArray.find(
724
- child => React.isValidElement(child) && child.type === SearchInput
901
+ React.useEffect(() => {
902
+ if (previousOpenRef.current !== isOpen) {
903
+ previousOpenRef.current = isOpen;
904
+ onOpenChange?.(isOpen);
905
+ }
906
+ }, [isOpen, onOpenChange]);
907
+
908
+ React.useEffect(() => {
909
+ if (!isOpenState) return;
910
+
911
+ const handlePointerDown = (event: PointerEvent) => {
912
+ if (!rootRef.current?.contains(event.target as Node)) {
913
+ setActiveIndex(-1);
914
+ setIsOpen(false);
915
+ onOpenChange?.(false);
916
+ }
917
+ };
918
+
919
+ const handleKeyDown = (event: KeyboardEvent) => {
920
+ if (event.key !== 'Escape') return;
921
+ setActiveIndex(-1);
922
+ setIsOpen(false);
923
+ onOpenChange?.(false);
924
+ const focusTarget = rootRef.current?.querySelector<HTMLElement>(
925
+ 'input, button, [href], [tabindex]:not([tabindex="-1"])'
926
+ );
927
+ focusTarget?.focus();
928
+ };
929
+
930
+ document.addEventListener('pointerdown', handlePointerDown);
931
+ document.addEventListener('keydown', handleKeyDown);
932
+
933
+ return () => {
934
+ document.removeEventListener('pointerdown', handlePointerDown);
935
+ document.removeEventListener('keydown', handleKeyDown);
936
+ };
937
+ }, [isOpenState, onOpenChange, setActiveIndex, setIsOpen]);
938
+
939
+ const setRefs = React.useCallback(
940
+ (node: HTMLDivElement | null) => {
941
+ rootRef.current = node;
942
+ if (typeof ref === 'function') {
943
+ ref(node);
944
+ } else if (ref) {
945
+ ref.current = node;
946
+ }
947
+ },
948
+ [ref]
725
949
  );
726
- const resultsChild = childrenArray.find(
727
- child => React.isValidElement(child) && child.type === SearchResults
950
+
951
+ const isSearchResultsChild = (child: React.ReactNode) => {
952
+ if (!React.isValidElement(child)) return false;
953
+ const childType = child.type as { displayName?: string };
954
+
955
+ return (
956
+ child.type === SearchResults ||
957
+ childType.displayName === SearchResults.displayName
958
+ );
959
+ };
960
+
961
+ // Results are rendered in the positioned panel; every other authored child is a trigger/input.
962
+ const childrenArray = React.Children.toArray(children);
963
+ const resultsChildren = childrenArray.filter(isSearchResultsChild);
964
+ const triggerChildren = childrenArray.filter(
965
+ child => !isSearchResultsChild(child)
728
966
  );
729
967
 
730
- // If no Input found, create a default one
731
- const triggerElement = inputChild || <SearchInput />;
968
+ const triggerElement = triggerChildren.length ? (
969
+ triggerChildren
970
+ ) : (
971
+ <SearchInput />
972
+ );
973
+ const resultsElement = resultsChildren.length ? (
974
+ resultsChildren
975
+ ) : (
976
+ <SearchResults />
977
+ );
732
978
 
733
979
  return (
734
- <div ref={ref} className={cn('relative', className)} {...props}>
980
+ <div ref={setRefs} className={cn('relative w-full', className)} {...props}>
735
981
  <div className='w-full'>{triggerElement}</div>
736
982
  {isOpenState && (
737
- <div className='absolute left-0 top-full z-50 mt-1 w-full rounded-md border border-gray-200 bg-white shadow-lg'>
738
- {resultsChild || <SearchResults />}
983
+ <div className='bg-popover text-popover-foreground border-border absolute left-0 top-full z-50 mt-1 max-h-[min(var(--results-max-height),calc(100vh-8rem))] w-full overflow-hidden rounded-md border shadow-lg'>
984
+ {resultsElement}
739
985
  </div>
740
986
  )}
741
987
  </div>
@@ -801,7 +1047,7 @@ export const SearchSection = React.forwardRef<
801
1047
  </div>
802
1048
  );
803
1049
  });
804
- SearchSection.displayName = 'SearchSection ';
1050
+ SearchSection.displayName = 'SearchSection';
805
1051
 
806
1052
  /**
807
1053
  * Props for the SearchItem component.
@@ -839,13 +1085,23 @@ export const SearchItem = React.forwardRef<HTMLDivElement, SearchItemProps>(
839
1085
  onClick?.();
840
1086
  };
841
1087
 
1088
+ const handleKeyDown = (event: React.KeyboardEvent<HTMLElement>) => {
1089
+ if (event.key === 'Enter' || event.key === ' ') {
1090
+ event.preventDefault();
1091
+ handleClick();
1092
+ }
1093
+ };
1094
+
842
1095
  // If children are provided, render them directly
843
1096
  if (children) {
844
1097
  return (
845
1098
  <div
846
1099
  ref={ref}
847
- className={cn('cursor-pointer', className)}
1100
+ role='button'
1101
+ tabIndex={0}
1102
+ className={cn('cursor-pointer outline-none', className)}
848
1103
  onClick={handleClick}
1104
+ onKeyDown={handleKeyDown}
849
1105
  {...props}
850
1106
  >
851
1107
  {children}
@@ -860,6 +1116,7 @@ export const SearchItem = React.forwardRef<HTMLDivElement, SearchItemProps>(
860
1116
  return (
861
1117
  <div
862
1118
  ref={ref}
1119
+ role='presentation'
863
1120
  className={cn(
864
1121
  'text-muted-foreground p-2 text-sm font-medium',
865
1122
  className
@@ -888,46 +1145,44 @@ export const SearchItem = React.forwardRef<HTMLDivElement, SearchItemProps>(
888
1145
 
889
1146
  // Complex result with image, description, etc.
890
1147
  return (
891
- <Card
892
- ref={ref as unknown as React.RefObject<HTMLDivElement>}
1148
+ <button
1149
+ ref={ref as unknown as React.RefObject<HTMLButtonElement>}
1150
+ type='button'
893
1151
  className={cn(
894
- 'hover:bg-muted/50 w-full cursor-pointer transition-colors',
1152
+ 'hover:bg-accent hover:text-accent-foreground focus-visible:border-ring focus-visible:ring-ring/50 w-full cursor-pointer rounded-md text-left outline-none transition-colors focus-visible:ring-[3px]',
895
1153
  className
896
1154
  )}
897
1155
  onClick={handleClick}
898
- {...(props as React.HTMLAttributes<HTMLDivElement>)}
1156
+ {...(props as React.ButtonHTMLAttributes<HTMLButtonElement>)}
899
1157
  >
900
- <CardContent className='p-3'>
901
- <div className='flex gap-3'>
902
- {result.image && (
903
- <div className='flex-shrink-0'>
904
- <img
905
- src={result.image}
906
- alt={result.title}
907
- className='h-12 w-12 rounded-md object-cover'
908
- />
909
- </div>
910
- )}
911
- <div className='min-w-0 flex-1'>
912
- <div className='flex items-start justify-between gap-2'>
913
- <h4 className='truncate text-sm font-semibold'>
914
- {result.title}
915
- </h4>
916
- {result.badge && (
917
- <Badge variant='secondary' className='text-xs'>
918
- {result.badge}
919
- </Badge>
920
- )}
921
- </div>
922
- {result.description && (
923
- <p className='text-muted-foreground mt-1 line-clamp-2 text-xs'>
924
- {result.description}
925
- </p>
1158
+ <div className='flex gap-3 p-3'>
1159
+ {result.image && (
1160
+ <div className='flex-shrink-0'>
1161
+ <img
1162
+ src={result.image}
1163
+ alt={result.title}
1164
+ className='h-12 w-12 rounded-md object-cover'
1165
+ loading='lazy'
1166
+ />
1167
+ </div>
1168
+ )}
1169
+ <div className='min-w-0 flex-1'>
1170
+ <div className='flex items-start justify-between gap-2'>
1171
+ <h4 className='truncate text-sm font-semibold'>{result.title}</h4>
1172
+ {result.badge && (
1173
+ <Badge variant='secondary' className='text-xs'>
1174
+ {result.badge}
1175
+ </Badge>
926
1176
  )}
927
1177
  </div>
1178
+ {result.description && (
1179
+ <p className='text-muted-foreground mt-1 line-clamp-2 text-xs'>
1180
+ {result.description}
1181
+ </p>
1182
+ )}
928
1183
  </div>
929
- </CardContent>
930
- </Card>
1184
+ </div>
1185
+ </button>
931
1186
  );
932
1187
  }
933
1188
  );
@@ -974,6 +1229,9 @@ export { AdvancedSearchProvider as Provider };
974
1229
  * Wrapper component for documentation and preview purposes.
975
1230
  * Provides a complete search implementation with mock data for testing and demos.
976
1231
  *
1232
+ * @deprecated Demo-only helper. Prefer composing AdvancedSearchProvider,
1233
+ * AdvancedSearchBar, SearchInput, and SearchResults in application code.
1234
+ *
977
1235
  * @param props - Configuration props for the search wrapper
978
1236
  * @returns Complete search component with mock functionality
979
1237
  *