import { h } from 'preact'; import { useEffect, useRef, useState } from 'preact/hooks'; import styles from './NameEntryModal.module.css'; import newGameStyles from '../NewGame.module.css'; interface NameEntryModalProps { open: boolean; title: string; placeholder: string; inputId: string; initialValue?: string; enterKeyHint?: 'next' | 'done'; onClose: () => void; onConfirm: (name: string) => string | null; } export function NameEntryModal({ open, title, placeholder, inputId, initialValue = '', enterKeyHint = 'done', onClose, onConfirm, }: NameEntryModalProps) { const [name, setName] = useState(initialValue); const [error, setError] = useState(null); const inputRef = useRef(null); useEffect(() => { if (!open) return; setName(initialValue); setError(null); window.setTimeout(() => inputRef.current?.focus(), 0); }, [open, initialValue]); if (!open) return null; return (
e.stopPropagation()}>

{title}

{ setName((e.target as HTMLInputElement).value); if (error) setError(null); }} onKeyDown={(e) => { if (e.key !== 'Enter') return; e.preventDefault(); const validationError = onConfirm(name); if (validationError) setError(validationError); }} /> {error &&

{error}

}
); }