|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { type ComponentPropsWithoutRef, forwardRef, type KeyboardEvent } from 'react' |
| 4 | +import { cn } from '@sim/emcn' |
| 5 | +import { Command } from 'cmdk' |
| 6 | +import { Search } from 'lucide-react' |
| 7 | + |
| 8 | +type CommandInputProps = ComponentPropsWithoutRef<typeof Command.Input> |
| 9 | +type CommandListProps = ComponentPropsWithoutRef<typeof Command.List> |
| 10 | + |
| 11 | +interface CommandSearchProps extends Omit<CommandInputProps, 'className'> { |
| 12 | + surface: 'canvas' | 'palette' |
| 13 | + cycleResultsOnTab?: boolean |
| 14 | +} |
| 15 | + |
| 16 | +interface CommandFadedListProps extends CommandListProps { |
| 17 | + fade: 'canvas' | 'palette' |
| 18 | +} |
| 19 | + |
| 20 | +const SEARCH_SURFACE_CLASSNAME = { |
| 21 | + canvas: |
| 22 | + 'bg-[linear-gradient(to_bottom,var(--surface-2)_0%,color-mix(in_srgb,var(--surface-2)_88%,transparent)_68%,transparent_100%)]', |
| 23 | + palette: |
| 24 | + 'bg-[linear-gradient(to_bottom,var(--surface-2)_0%,color-mix(in_srgb,var(--surface-2)_88%,transparent)_68%,transparent_100%)]', |
| 25 | +} as const |
| 26 | + |
| 27 | +const LIST_FADE_CLASSNAME = { |
| 28 | + canvas: |
| 29 | + '[-webkit-mask-image:linear-gradient(to_bottom,transparent_0%,transparent_8%,black_18%,black_94%,transparent_100%)] [mask-image:linear-gradient(to_bottom,transparent_0%,transparent_8%,black_18%,black_94%,transparent_100%)]', |
| 30 | + palette: |
| 31 | + '[&::-webkit-scrollbar-track]:mt-12 [&::-webkit-scrollbar-track]:mb-1.5 [-webkit-mask-composite:source-over] [-webkit-mask-image:linear-gradient(to_bottom,transparent_0%,transparent_8%,black_18%,black_94%,transparent_100%),linear-gradient(black,black)] [-webkit-mask-position:left_top,right_top] [-webkit-mask-repeat:no-repeat,no-repeat] [-webkit-mask-size:calc(100%_-_8px)_100%,8px_100%] [mask-composite:add] [mask-image:linear-gradient(to_bottom,transparent_0%,transparent_8%,black_18%,black_94%,transparent_100%),linear-gradient(black,black)] [mask-position:left_top,right_top] [mask-repeat:no-repeat,no-repeat] [mask-size:calc(100%_-_8px)_100%,8px_100%]', |
| 32 | +} as const |
| 33 | + |
| 34 | +/** Borderless search field layered over a fading command-result list. */ |
| 35 | +export const CommandSearch = forwardRef<HTMLInputElement, CommandSearchProps>( |
| 36 | + function CommandSearch({ surface, cycleResultsOnTab = false, onKeyDown, ...props }, ref) { |
| 37 | + const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => { |
| 38 | + onKeyDown?.(event) |
| 39 | + if (!cycleResultsOnTab || event.defaultPrevented || event.key !== 'Tab') return |
| 40 | + |
| 41 | + event.preventDefault() |
| 42 | + event.currentTarget.dispatchEvent( |
| 43 | + new window.KeyboardEvent('keydown', { |
| 44 | + key: event.shiftKey ? 'ArrowUp' : 'ArrowDown', |
| 45 | + bubbles: true, |
| 46 | + cancelable: true, |
| 47 | + }) |
| 48 | + ) |
| 49 | + } |
| 50 | + |
| 51 | + return ( |
| 52 | + <div |
| 53 | + className={cn( |
| 54 | + 'nodrag nopan absolute inset-x-[3px] top-[3px] z-20 flex h-12 cursor-text items-center gap-2 rounded-t-[13px] px-2.5 pb-2', |
| 55 | + SEARCH_SURFACE_CLASSNAME[surface] |
| 56 | + )} |
| 57 | + > |
| 58 | + <Search className='size-[14px] flex-shrink-0 text-[var(--text-muted)]' /> |
| 59 | + <Command.Input |
| 60 | + ref={ref} |
| 61 | + className='h-8 min-w-0 flex-1 cursor-text bg-transparent text-[var(--text-body)] text-sm outline-none placeholder:text-[var(--text-muted)] focus:outline-none' |
| 62 | + onKeyDown={handleKeyDown} |
| 63 | + {...props} |
| 64 | + /> |
| 65 | + </div> |
| 66 | + ) |
| 67 | + } |
| 68 | +) |
| 69 | + |
| 70 | +CommandSearch.displayName = 'CommandSearch' |
| 71 | + |
| 72 | +/** Scrollable command list with soft edge fades tuned for each command surface. */ |
| 73 | +export const CommandFadedList = forwardRef<HTMLDivElement, CommandFadedListProps>( |
| 74 | + function CommandFadedList({ className, fade, ...props }, ref) { |
| 75 | + return ( |
| 76 | + <Command.List |
| 77 | + ref={ref} |
| 78 | + className={cn( |
| 79 | + 'overflow-y-auto overflow-x-hidden px-1.5 pt-12 pb-1.5 [&_[cmdk-group-items]]:flex [&_[cmdk-group-items]]:flex-col', |
| 80 | + LIST_FADE_CLASSNAME[fade], |
| 81 | + className |
| 82 | + )} |
| 83 | + {...props} |
| 84 | + /> |
| 85 | + ) |
| 86 | + } |
| 87 | +) |
| 88 | + |
| 89 | +CommandFadedList.displayName = 'CommandFadedList' |
0 commit comments