import { ReactNode, useEffect, useRef } from "react"; import { cn } from "@/lib/utils"; import { Position } from "./useSuggestions"; interface SuggestionsPopupProps { position: Position; suggestions: T[]; selectedIndex: number; onItemSelect: (item: T) => void; renderItem: (item: T, isSelected: boolean) => ReactNode; getItemKey: (item: T, index: number) => string; } /** * Shared popup component for displaying suggestion items. * Provides consistent styling and behavior across different suggestion types. * * Features: * - Automatically scrolls selected item into view * - Handles keyboard navigation highlighting * - Prevents text selection during mouse interaction * - Consistent styling with max height constraints */ export function SuggestionsPopup({ position, suggestions, selectedIndex, onItemSelect, renderItem, getItemKey, }: SuggestionsPopupProps) { const containerRef = useRef(null); const selectedItemRef = useRef(null); // Scroll selected item into view when selection changes useEffect(() => { if (selectedItemRef.current && containerRef.current) { selectedItemRef.current.scrollIntoView({ block: "nearest", behavior: "smooth", }); } }, [selectedIndex]); return (
{suggestions.map((item, i) => (
onItemSelect(item)} className={cn( "rounded p-1 px-2 w-full text-sm cursor-pointer transition-colors select-none", "hover:bg-accent hover:text-accent-foreground", i === selectedIndex ? "bg-accent text-accent-foreground" : "", )} > {renderItem(item, i === selectedIndex)}
))}
); }