|
| 1 | +import { ArrowDown, ArrowUp, X } from "@phosphor-icons/react"; |
| 2 | +import { IconButton } from "@radix-ui/themes"; |
| 3 | +import { |
| 4 | + forwardRef, |
| 5 | + type KeyboardEvent as ReactKeyboardEvent, |
| 6 | + useCallback, |
| 7 | + useImperativeHandle, |
| 8 | + useLayoutEffect, |
| 9 | + useRef, |
| 10 | +} from "react"; |
| 11 | + |
| 12 | +export interface ConversationSearchBarHandle { |
| 13 | + focusAndSelect: () => void; |
| 14 | +} |
| 15 | + |
| 16 | +interface ConversationSearchBarProps { |
| 17 | + query: string; |
| 18 | + currentMatch: number; |
| 19 | + totalMatches: number; |
| 20 | + onQueryChange: (query: string) => void; |
| 21 | + onNext: () => void; |
| 22 | + onPrev: () => void; |
| 23 | + onClose: () => void; |
| 24 | +} |
| 25 | + |
| 26 | +export const ConversationSearchBar = forwardRef< |
| 27 | + ConversationSearchBarHandle, |
| 28 | + ConversationSearchBarProps |
| 29 | +>(function ConversationSearchBar( |
| 30 | + { query, currentMatch, totalMatches, onQueryChange, onNext, onPrev, onClose }, |
| 31 | + ref, |
| 32 | +) { |
| 33 | + const inputRef = useRef<HTMLInputElement>(null); |
| 34 | + |
| 35 | + useLayoutEffect(() => { |
| 36 | + inputRef.current?.focus(); |
| 37 | + }, []); |
| 38 | + |
| 39 | + useImperativeHandle( |
| 40 | + ref, |
| 41 | + () => ({ |
| 42 | + focusAndSelect: () => { |
| 43 | + const input = inputRef.current; |
| 44 | + if (!input) return; |
| 45 | + input.focus(); |
| 46 | + input.select(); |
| 47 | + }, |
| 48 | + }), |
| 49 | + [], |
| 50 | + ); |
| 51 | + |
| 52 | + const handleKeyDown = useCallback( |
| 53 | + (e: ReactKeyboardEvent<HTMLInputElement>) => { |
| 54 | + if (e.key === "Escape") { |
| 55 | + e.preventDefault(); |
| 56 | + onClose(); |
| 57 | + } else if (e.key === "Enter" || e.key === "ArrowDown") { |
| 58 | + e.preventDefault(); |
| 59 | + if (e.shiftKey) { |
| 60 | + onPrev(); |
| 61 | + } else { |
| 62 | + onNext(); |
| 63 | + } |
| 64 | + } else if (e.key === "ArrowUp") { |
| 65 | + e.preventDefault(); |
| 66 | + onPrev(); |
| 67 | + } |
| 68 | + }, |
| 69 | + [onClose, onNext, onPrev], |
| 70 | + ); |
| 71 | + |
| 72 | + return ( |
| 73 | + <div |
| 74 | + data-overlay |
| 75 | + className="absolute top-2 right-6 z-30 flex items-center gap-1 rounded-lg border border-(--gray-6) bg-(--color-background) px-2 py-1 shadow-md" |
| 76 | + > |
| 77 | + <input |
| 78 | + ref={inputRef} |
| 79 | + type="text" |
| 80 | + value={query} |
| 81 | + onChange={(e) => onQueryChange(e.target.value)} |
| 82 | + onKeyDown={handleKeyDown} |
| 83 | + placeholder="Find in conversation..." |
| 84 | + className="w-48 border-none bg-transparent text-(--gray-12) text-[13px] outline-none placeholder:text-(--gray-9)" |
| 85 | + /> |
| 86 | + {query && ( |
| 87 | + <span className="shrink-0 text-(--gray-10) text-[12px]"> |
| 88 | + {totalMatches > 0 |
| 89 | + ? `${currentMatch + 1} of ${totalMatches}` |
| 90 | + : "No results"} |
| 91 | + </span> |
| 92 | + )} |
| 93 | + <IconButton |
| 94 | + size="1" |
| 95 | + variant="ghost" |
| 96 | + color="gray" |
| 97 | + onClick={onPrev} |
| 98 | + disabled={totalMatches === 0} |
| 99 | + aria-label="Previous match" |
| 100 | + > |
| 101 | + <ArrowUp size={14} /> |
| 102 | + </IconButton> |
| 103 | + <IconButton |
| 104 | + size="1" |
| 105 | + variant="ghost" |
| 106 | + color="gray" |
| 107 | + onClick={onNext} |
| 108 | + disabled={totalMatches === 0} |
| 109 | + aria-label="Next match" |
| 110 | + > |
| 111 | + <ArrowDown size={14} /> |
| 112 | + </IconButton> |
| 113 | + <IconButton |
| 114 | + size="1" |
| 115 | + variant="ghost" |
| 116 | + color="gray" |
| 117 | + onClick={onClose} |
| 118 | + aria-label="Close search" |
| 119 | + > |
| 120 | + <X size={14} /> |
| 121 | + </IconButton> |
| 122 | + </div> |
| 123 | + ); |
| 124 | +}); |
0 commit comments