import { useEffect, useId, useRef, useState } from 'react'; import type { KeyboardEvent } from 'react'; import { Input } from '@/components/ui/input'; import { Textarea } from '@/components/ui/textarea'; import { Popover, PopoverAnchor, PopoverContent } from '@/components/ui/popover'; import { defaultPlaceholders, type PlaceholderConfig } from './sections/placeholders-section'; import { findPlaceholderMention, matchingPlaceholders, insertPlaceholderMention, type PlaceholderMention } from './placeholder-mentions'; interface PlaceholderInputProps { id: string; field: string; value: string; onValueChange: (value: string) => void; onSelection?: (start: number, end: number) => void; placeholders?: PlaceholderConfig[]; multiline?: boolean; rows?: number; placeholder?: string; maxLength?: number; className?: string; } /** Native text editing with a non-modal, keyboard-accessible @ suggestion list. */ export function PlaceholderInput({ id, field, value, onValueChange, onSelection, placeholders = defaultPlaceholders, multiline, rows, ...props }: PlaceholderInputProps) { const input = useRef(null); const menu = useRef(null); const composing = useRef(false); const keyboardNavigation = useRef(false); const [mention, setMention] = useState(null); const [active, setActive] = useState(0); const listId = useId(); const options = mention ? matchingPlaceholders(placeholders, mention.query) : []; const open = mention !== null; useEffect(() => { setActive(0); }, [mention?.query]); useEffect(() => { if (keyboardNavigation.current) { menu.current?.querySelector('[aria-selected="true"]')?.scrollIntoView({ block: 'nearest' }); keyboardNavigation.current = false; } }, [active]); const inspect = (element: HTMLInputElement | HTMLTextAreaElement) => { const start = element.selectionStart ?? element.value.length; const end = element.selectionEnd ?? start; onSelection?.(start, end); if (!composing.current) setMention(findPlaceholderMention(element.value, start, end)); }; const insert = (tag: string) => { if (!mention) return; const result = insertPlaceholderMention(value, mention, tag, props.maxLength); if (!result) return; onValueChange(result.text); setMention(null); requestAnimationFrame(() => { input.current?.focus(); input.current?.setSelectionRange(result.caret, result.caret); onSelection?.(result.caret, result.caret); }); }; const onKeyDown = (event: KeyboardEvent) => { if (!open || event.nativeEvent.isComposing || composing.current) return; if (event.key === 'Escape') { event.preventDefault(); event.stopPropagation(); setMention(null); } else if ((event.key === 'ArrowDown' || event.key === 'ArrowUp') && options.length) { event.preventDefault(); keyboardNavigation.current = true; setActive(previous => (previous + (event.key === 'ArrowDown' ? 1 : -1) + options.length) % options.length); } else if (event.key === 'Enter' && options[active]) { event.preventDefault(); insert(options[active].tag); } else if (event.key === 'Tab') setMention(null); }; const shared = { ...props, id, value, 'data-email-content-field': field, role: 'combobox', 'aria-autocomplete': 'list' as const, 'aria-haspopup': 'listbox' as const, 'aria-expanded': open, 'aria-controls': open ? listId : undefined, 'aria-activedescendant': open && options[active] ? `${listId}-${active}` : undefined, onChange: (event: React.ChangeEvent) => { onValueChange(event.target.value); inspect(event.currentTarget); }, onFocus: (event: React.FocusEvent) => inspect(event.currentTarget), onSelect: (event: React.SyntheticEvent) => inspect(event.currentTarget), onBlur: (event: React.FocusEvent) => onSelection?.(event.currentTarget.selectionStart ?? 0, event.currentTarget.selectionEnd ?? 0), onCompositionStart: () => { composing.current = true; setMention(null); }, onCompositionEnd: (event: React.CompositionEvent) => { composing.current = false; inspect(event.currentTarget); }, onKeyDown, }; return { if (!next) setMention(null); }}>
{multiline ?