@foundrykit/components 1.0.8 → 1.0.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.
@@ -0,0 +1,658 @@
1
+ 'use client';
2
+
3
+ import * as React from 'react';
4
+ import { Slot } from '@radix-ui/react-slot';
5
+ import { cva, VariantProps } from 'class-variance-authority';
6
+ import { Search, X, ChevronDown } from 'lucide-react';
7
+
8
+ import { cn } from '@foundrykit/utils';
9
+ import { useAdvancedSearch, SearchContext } from '@foundrykit/hooks';
10
+ import { Input } from '@foundrykit/primitives';
11
+ import { Button } from '@foundrykit/primitives';
12
+ import { Separator } from '@foundrykit/primitives';
13
+ import { Badge } from '@foundrykit/primitives';
14
+ import { Card, CardContent } from '@foundrykit/primitives';
15
+ import {
16
+ Popover,
17
+ PopoverContent,
18
+ PopoverTrigger,
19
+ } from '@foundrykit/primitives';
20
+
21
+ // Re-export types from hooks package
22
+ import type {
23
+ SearchResult,
24
+ SearchSection as SearchSectionType,
25
+ } from '@foundrykit/types';
26
+
27
+ // Provider Component
28
+ export interface AdvancedSearchProviderProps {
29
+ children: React.ReactNode;
30
+ onSearch?: (query: string) => Promise<SearchResult[]> | SearchResult[];
31
+ onResultSelect?: (result: SearchResult) => void;
32
+ debounceMs?: number;
33
+ minQueryLength?: number;
34
+ }
35
+
36
+ export function AdvancedSearchProvider({
37
+ children,
38
+ onSearch,
39
+ onResultSelect,
40
+ debounceMs = 300,
41
+ minQueryLength = 1,
42
+ }: AdvancedSearchProviderProps) {
43
+ const [query, setQuery] = React.useState('');
44
+ const [isOpen, setIsOpen] = React.useState(false);
45
+ const [results, setResults] = React.useState<SearchResult[]>([]);
46
+ const [isLoading, setIsLoading] = React.useState(false);
47
+
48
+ const debouncedSearch = React.useCallback(
49
+ (searchQuery: string) => {
50
+ const timeoutId = setTimeout(async () => {
51
+ if (searchQuery.length >= minQueryLength && onSearch) {
52
+ setIsLoading(true);
53
+ try {
54
+ const searchResults = await onSearch(searchQuery);
55
+ setResults(searchResults);
56
+ } catch (error) {
57
+ console.error('Search error:', error);
58
+ setResults([]);
59
+ } finally {
60
+ setIsLoading(false);
61
+ }
62
+ } else {
63
+ setResults([]);
64
+ }
65
+ }, debounceMs);
66
+
67
+ return () => clearTimeout(timeoutId);
68
+ },
69
+ [onSearch, debounceMs, minQueryLength]
70
+ );
71
+
72
+ const handleSearch = React.useCallback(
73
+ (searchQuery: string) => {
74
+ setQuery(searchQuery);
75
+ debouncedSearch(searchQuery);
76
+ },
77
+ [debouncedSearch]
78
+ );
79
+
80
+ const handleResultSelect = React.useCallback(
81
+ (result: SearchResult) => {
82
+ onResultSelect?.(result);
83
+ setIsOpen(false);
84
+ },
85
+ [onResultSelect]
86
+ );
87
+
88
+ const value = React.useMemo(
89
+ () => ({
90
+ query,
91
+ setQuery,
92
+ isOpen,
93
+ setIsOpen,
94
+ results,
95
+ setResults,
96
+ isLoading,
97
+ setIsLoading,
98
+ onSearch: handleSearch,
99
+ onResultSelect: handleResultSelect,
100
+ }),
101
+ [query, isOpen, results, isLoading, handleSearch, handleResultSelect]
102
+ );
103
+
104
+ return (
105
+ <SearchContext.Provider value={value}>{children}</SearchContext.Provider>
106
+ );
107
+ }
108
+
109
+ // Search Input Wrapper Variants
110
+ const searchInputWrapperVariants = cva('relative flex items-center w-full', {
111
+ variants: {
112
+ size: {
113
+ sm: 'h-8',
114
+ md: 'h-10',
115
+ lg: 'h-12',
116
+ xl: 'h-14',
117
+ },
118
+ variant: {
119
+ default: 'border border-input bg-background',
120
+ ghost: 'bg-transparent',
121
+ filled: 'bg-muted',
122
+ },
123
+ },
124
+ defaultVariants: {
125
+ size: 'md',
126
+ variant: 'default',
127
+ },
128
+ });
129
+
130
+ // Search Results Variants
131
+ const searchResultsVariants = cva('w-full', {
132
+ variants: {
133
+ size: {
134
+ sm: 'max-w-sm',
135
+ md: 'max-w-md',
136
+ lg: 'max-w-lg',
137
+ xl: 'max-w-xl',
138
+ '2xl': 'max-w-2xl',
139
+ full: 'max-w-full',
140
+ },
141
+ },
142
+ defaultVariants: {
143
+ size: 'lg',
144
+ },
145
+ });
146
+
147
+ // Search Input Component
148
+ export interface SearchInputProps
149
+ extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'size'>,
150
+ VariantProps<typeof searchInputWrapperVariants> {
151
+ placeholder?: string;
152
+ showSearchIcon?: boolean;
153
+ showClearButton?: boolean;
154
+ onClear?: () => void;
155
+ asChild?: boolean;
156
+ }
157
+
158
+ export const SearchInput = React.forwardRef<HTMLInputElement, SearchInputProps>(
159
+ (
160
+ {
161
+ className,
162
+ size,
163
+ variant,
164
+ placeholder = 'Search...',
165
+ showSearchIcon = true,
166
+ showClearButton = true,
167
+ onClear,
168
+ asChild = false,
169
+ ...props
170
+ },
171
+ ref
172
+ ) => {
173
+ const { query, setQuery, onSearch } = useAdvancedSearch();
174
+ const Comp = asChild ? Slot : 'div';
175
+
176
+ const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
177
+ const value = e.target.value;
178
+ setQuery(value);
179
+ onSearch(value);
180
+ props.onChange?.(e);
181
+ };
182
+
183
+ const handleClear = () => {
184
+ setQuery('');
185
+ onSearch('');
186
+ onClear?.();
187
+ };
188
+
189
+ return (
190
+ <Comp
191
+ className={cn(
192
+ searchInputWrapperVariants({ size, variant }),
193
+ 'rounded-md',
194
+ className
195
+ )}
196
+ >
197
+ {showSearchIcon && (
198
+ <Search className='text-muted-foreground absolute left-3 h-4 w-4' />
199
+ )}
200
+ <Input
201
+ ref={ref}
202
+ type='search'
203
+ placeholder={placeholder}
204
+ value={query}
205
+ onChange={handleChange}
206
+ className={cn(
207
+ 'border-0 bg-transparent focus-visible:ring-0 focus-visible:ring-offset-0',
208
+ showSearchIcon && 'pl-10',
209
+ showClearButton && query && 'pr-10'
210
+ )}
211
+ size={undefined}
212
+ {...props}
213
+ />
214
+ {showClearButton && query && (
215
+ <Button
216
+ variant='ghost'
217
+ size='sm'
218
+ className='absolute right-1 h-6 w-6 p-0'
219
+ onClick={handleClear}
220
+ >
221
+ <X className='h-3 w-3' />
222
+ </Button>
223
+ )}
224
+ </Comp>
225
+ );
226
+ }
227
+ );
228
+ SearchInput.displayName = 'SearchInput';
229
+
230
+ // Search Trigger Component
231
+ export interface SearchTriggerProps
232
+ extends React.ButtonHTMLAttributes<HTMLButtonElement> {
233
+ asChild?: boolean;
234
+ }
235
+
236
+ export const SearchTrigger = React.forwardRef<
237
+ HTMLButtonElement,
238
+ SearchTriggerProps
239
+ >(({ className, asChild = false, children, ...props }, ref) => {
240
+ const { isOpen, setIsOpen } = useAdvancedSearch();
241
+ const Comp = asChild ? Slot : Button;
242
+
243
+ return (
244
+ <Comp
245
+ ref={ref}
246
+ className={cn(className)}
247
+ onClick={() => setIsOpen(!isOpen)}
248
+ {...props}
249
+ >
250
+ {children}
251
+ </Comp>
252
+ );
253
+ });
254
+ SearchTrigger.displayName = 'SearchTrigger';
255
+
256
+ // Search Results Component
257
+ export interface SearchResultsProps
258
+ extends React.HTMLAttributes<HTMLDivElement>,
259
+ VariantProps<typeof searchResultsVariants> {
260
+ sections?: SearchSectionType[];
261
+ showEmptyState?: boolean;
262
+ emptyStateMessage?: string;
263
+ showLoadingState?: boolean;
264
+ loadingMessage?: string;
265
+ }
266
+
267
+ export const SearchResults = React.forwardRef<
268
+ HTMLDivElement,
269
+ SearchResultsProps
270
+ >(
271
+ (
272
+ {
273
+ className,
274
+ size,
275
+ sections,
276
+ showEmptyState = true,
277
+ emptyStateMessage = 'No results found',
278
+ showLoadingState = true,
279
+ loadingMessage = 'Searching...',
280
+ ...props
281
+ },
282
+ ref
283
+ ) => {
284
+ const { results, isLoading, query, onResultSelect } = useAdvancedSearch();
285
+
286
+ if (isLoading && showLoadingState) {
287
+ return (
288
+ <div
289
+ ref={ref}
290
+ className={cn(
291
+ searchResultsVariants({ size }),
292
+ 'text-muted-foreground p-4 text-center',
293
+ className
294
+ )}
295
+ {...props}
296
+ >
297
+ {loadingMessage}
298
+ </div>
299
+ );
300
+ }
301
+
302
+ if (!query || results.length === 0) {
303
+ if (!showEmptyState) return null;
304
+ return (
305
+ <div
306
+ ref={ref}
307
+ className={cn(
308
+ searchResultsVariants({ size }),
309
+ 'text-muted-foreground p-4 text-center',
310
+ className
311
+ )}
312
+ {...props}
313
+ >
314
+ {emptyStateMessage}
315
+ </div>
316
+ );
317
+ }
318
+
319
+ // Use sections if provided, otherwise use default results
320
+ const displaySections = sections || [
321
+ {
322
+ id: 'results',
323
+ title: 'Results',
324
+ results: results,
325
+ },
326
+ ];
327
+
328
+ return (
329
+ <div
330
+ ref={ref}
331
+ className={cn(
332
+ searchResultsVariants({ size }),
333
+ 'max-h-96 overflow-hidden',
334
+ className
335
+ )}
336
+ {...props}
337
+ >
338
+ <div className='h-full overflow-auto'>
339
+ <div className='space-y-4 p-4'>
340
+ {displaySections.map((section, index) => (
341
+ <div key={section.id}>
342
+ {index > 0 && <Separator className='my-4' />}
343
+ <div className='space-y-2'>
344
+ <h3 className='text-muted-foreground text-sm font-semibold uppercase tracking-wide'>
345
+ {section.title}
346
+ </h3>
347
+ <div className='space-y-1'>
348
+ {section.results.map((result: SearchResult) => (
349
+ <SearchResultItem
350
+ key={result.id}
351
+ result={result}
352
+ onClick={() => onResultSelect(result)}
353
+ />
354
+ ))}
355
+ {section.showMore && section.onShowMore && (
356
+ <Button
357
+ variant='ghost'
358
+ size='sm'
359
+ className='text-muted-foreground w-full justify-start'
360
+ onClick={section.onShowMore}
361
+ >
362
+ Show more
363
+ <ChevronDown className='ml-2 h-3 w-3' />
364
+ </Button>
365
+ )}
366
+ </div>
367
+ </div>
368
+ </div>
369
+ ))}
370
+ </div>
371
+ </div>
372
+ </div>
373
+ );
374
+ }
375
+ );
376
+ SearchResults.displayName = 'SearchResults';
377
+
378
+ // Search Result Item Component
379
+ interface SearchResultItemProps {
380
+ result: SearchResult;
381
+ onClick: () => void;
382
+ }
383
+
384
+ const SearchResultItem = ({ result, onClick }: SearchResultItemProps) => {
385
+ if (result.type === 'category') {
386
+ return (
387
+ <div className='text-muted-foreground p-2 text-sm font-medium'>
388
+ {result.title}
389
+ </div>
390
+ );
391
+ }
392
+
393
+ if (result.type === 'suggestion') {
394
+ return (
395
+ <Button
396
+ variant='ghost'
397
+ size='sm'
398
+ className='h-auto w-full justify-start p-2'
399
+ onClick={onClick}
400
+ >
401
+ <span className='text-sm'>{result.title}</span>
402
+ </Button>
403
+ );
404
+ }
405
+
406
+ // Complex result with image, description, etc.
407
+ return (
408
+ <Card
409
+ className='hover:bg-muted/50 cursor-pointer transition-colors'
410
+ onClick={onClick}
411
+ >
412
+ <CardContent className='p-3'>
413
+ <div className='flex gap-3'>
414
+ {result.image && (
415
+ <div className='flex-shrink-0'>
416
+ <img
417
+ src={result.image}
418
+ alt={result.title}
419
+ className='h-12 w-12 rounded-md object-cover'
420
+ />
421
+ </div>
422
+ )}
423
+ <div className='min-w-0 flex-1'>
424
+ <div className='flex items-start justify-between gap-2'>
425
+ <h4 className='truncate text-sm font-semibold'>{result.title}</h4>
426
+ {result.badge && (
427
+ <Badge variant='secondary' className='text-xs'>
428
+ {result.badge}
429
+ </Badge>
430
+ )}
431
+ </div>
432
+ {result.description && (
433
+ <p className='text-muted-foreground mt-1 line-clamp-2 text-xs'>
434
+ {result.description}
435
+ </p>
436
+ )}
437
+ </div>
438
+ </div>
439
+ </CardContent>
440
+ </Card>
441
+ );
442
+ };
443
+
444
+ // Main Advanced Search Bar Component (Compound Pattern)
445
+ export interface AdvancedSearchBarProps
446
+ extends React.HTMLAttributes<HTMLDivElement> {
447
+ open?: boolean;
448
+ onOpenChange?: (open: boolean) => void;
449
+ }
450
+
451
+ export const AdvancedSearchBar = React.forwardRef<
452
+ HTMLDivElement,
453
+ AdvancedSearchBarProps
454
+ >(({ className, children, open, onOpenChange, ...props }, ref) => {
455
+ const { isOpen, setIsOpen } = useAdvancedSearch();
456
+
457
+ const handleOpenChange = (newOpen: boolean) => {
458
+ if (open !== undefined) {
459
+ onOpenChange?.(newOpen);
460
+ } else {
461
+ setIsOpen(newOpen);
462
+ }
463
+ };
464
+
465
+ const isControlled = open !== undefined;
466
+ const isOpenState = isControlled ? open : isOpen;
467
+
468
+ // Find Input and Results components in children
469
+ const childrenArray = React.Children.toArray(children);
470
+ const inputChild = childrenArray.find(
471
+ child => React.isValidElement(child) && child.type === SearchInput
472
+ );
473
+ const resultsChild = childrenArray.find(
474
+ child => React.isValidElement(child) && child.type === SearchResults
475
+ );
476
+
477
+ // If no Input found, create a default one
478
+ const triggerElement = inputChild || <SearchInput />;
479
+
480
+ return (
481
+ <div ref={ref} className={cn('relative', className)} {...props}>
482
+ <Popover open={isOpenState} onOpenChange={handleOpenChange}>
483
+ <PopoverTrigger asChild>{triggerElement}</PopoverTrigger>
484
+ <PopoverContent className='p-0' align='start'>
485
+ {resultsChild || <SearchResults />}
486
+ </PopoverContent>
487
+ </Popover>
488
+ </div>
489
+ );
490
+ });
491
+ AdvancedSearchBar.displayName = 'AdvancedSearchBar';
492
+
493
+ // Compound Pattern Components
494
+
495
+ // Search Section Component (for compound pattern)
496
+ export interface SearchSectionProps
497
+ extends React.HTMLAttributes<HTMLDivElement> {
498
+ title?: string;
499
+ maxResults?: number;
500
+ showMore?: boolean;
501
+ onShowMore?: () => void;
502
+ }
503
+
504
+ export const SearchSection = React.forwardRef<
505
+ HTMLDivElement,
506
+ SearchSectionProps
507
+ >(({ className, title, showMore, onShowMore, children, ...props }, ref) => {
508
+ return (
509
+ <div ref={ref} className={cn('space-y-2', className)} {...props}>
510
+ {title && (
511
+ <h3 className='text-muted-foreground text-sm font-semibold uppercase tracking-wide'>
512
+ {title}
513
+ </h3>
514
+ )}
515
+ <div className='space-y-1'>
516
+ {children}
517
+ {showMore && onShowMore && (
518
+ <Button
519
+ variant='ghost'
520
+ size='sm'
521
+ className='text-muted-foreground w-full justify-start'
522
+ onClick={onShowMore}
523
+ >
524
+ Show more
525
+ <ChevronDown className='ml-2 h-3 w-3' />
526
+ </Button>
527
+ )}
528
+ </div>
529
+ </div>
530
+ );
531
+ });
532
+ SearchSection.displayName = 'SearchSection ';
533
+
534
+ // Search Item Component (for compound pattern)
535
+ export interface SearchItemProps extends React.HTMLAttributes<HTMLDivElement> {
536
+ result?: SearchResult;
537
+ onClick?: () => void;
538
+ }
539
+
540
+ export const SearchItem = React.forwardRef<HTMLDivElement, SearchItemProps>(
541
+ ({ className, result, onClick, children, ...props }, ref) => {
542
+ const { onResultSelect } = useAdvancedSearch();
543
+
544
+ const handleClick = () => {
545
+ if (result) {
546
+ onResultSelect(result);
547
+ }
548
+ onClick?.();
549
+ };
550
+
551
+ // If children are provided, render them directly
552
+ if (children) {
553
+ return (
554
+ <div
555
+ ref={ref}
556
+ className={cn('cursor-pointer', className)}
557
+ onClick={handleClick}
558
+ {...props}
559
+ >
560
+ {children}
561
+ </div>
562
+ );
563
+ }
564
+
565
+ // Otherwise render based on result type
566
+ if (!result) return null;
567
+
568
+ if (result.type === 'category') {
569
+ return (
570
+ <div
571
+ ref={ref}
572
+ className={cn(
573
+ 'text-muted-foreground p-2 text-sm font-medium',
574
+ className
575
+ )}
576
+ {...props}
577
+ >
578
+ {result.title}
579
+ </div>
580
+ );
581
+ }
582
+
583
+ if (result.type === 'suggestion') {
584
+ return (
585
+ <Button
586
+ ref={ref as unknown as React.RefObject<HTMLButtonElement>}
587
+ variant='ghost'
588
+ size='sm'
589
+ className={cn('h-auto w-full justify-start p-2', className)}
590
+ onClick={handleClick}
591
+ {...(props as React.ButtonHTMLAttributes<HTMLButtonElement>)}
592
+ >
593
+ <span className='text-sm'>{result.title}</span>
594
+ </Button>
595
+ );
596
+ }
597
+
598
+ // Complex result with image, description, etc.
599
+ return (
600
+ <Card
601
+ ref={ref as unknown as React.RefObject<HTMLDivElement>}
602
+ className={cn(
603
+ 'hover:bg-muted/50 cursor-pointer transition-colors',
604
+ className
605
+ )}
606
+ onClick={handleClick}
607
+ {...(props as React.HTMLAttributes<HTMLDivElement>)}
608
+ >
609
+ <CardContent className='p-3'>
610
+ <div className='flex gap-3'>
611
+ {result.image && (
612
+ <div className='flex-shrink-0'>
613
+ <img
614
+ src={result.image}
615
+ alt={result.title}
616
+ className='h-12 w-12 rounded-md object-cover'
617
+ />
618
+ </div>
619
+ )}
620
+ <div className='min-w-0 flex-1'>
621
+ <div className='flex items-start justify-between gap-2'>
622
+ <h4 className='truncate text-sm font-semibold'>
623
+ {result.title}
624
+ </h4>
625
+ {result.badge && (
626
+ <Badge variant='secondary' className='text-xs'>
627
+ {result.badge}
628
+ </Badge>
629
+ )}
630
+ </div>
631
+ {result.description && (
632
+ <p className='text-muted-foreground mt-1 line-clamp-2 text-xs'>
633
+ {result.description}
634
+ </p>
635
+ )}
636
+ </div>
637
+ </div>
638
+ </CardContent>
639
+ </Card>
640
+ );
641
+ }
642
+ );
643
+ SearchItem.displayName = 'SearchItem';
644
+
645
+ // Export all components with compound pattern
646
+ export {
647
+ AdvancedSearchBar as Root,
648
+ SearchInput as Input,
649
+ SearchTrigger as Trigger,
650
+ SearchResults as Results,
651
+ SearchSection as Section,
652
+ SearchItem as Item,
653
+ searchInputWrapperVariants,
654
+ searchResultsVariants,
655
+ };
656
+
657
+ // Export Provider separately to avoid conflicts
658
+ export { AdvancedSearchProvider as Provider };
@@ -0,0 +1,29 @@
1
+ import type { ComponentConfig } from '@foundrykit/types';
2
+
3
+ export const sidebarConfig: ComponentConfig = {
4
+ name: 'Sidebar',
5
+ description: 'Sidebar component for navigation',
6
+ category: 'components',
7
+ tags: ['navigation', 'sidebar'],
8
+ preview: {
9
+ type: 'static',
10
+ html: '<div class="flex h-12 w-full items-center justify-center rounded-md bg-gray-200"><div class="h-8 w-20 rounded bg-gray-400 flex items-center justify-center text-xs text-white">Sidebar</div></div>',
11
+ },
12
+ variants: [],
13
+ files: ['components/sidebar/index.tsx'],
14
+ dependencies: [
15
+ 'utils',
16
+ 'useIsMobile',
17
+ 'tooltip',
18
+ 'sheet',
19
+ 'skeleton',
20
+ 'button',
21
+ 'input',
22
+ 'separator',
23
+ ],
24
+ packageDependencies: [
25
+ '@radix-ui/react-slot',
26
+ 'class-variance-authority',
27
+ 'lucide-react',
28
+ ],
29
+ };