import { useEffect, useMemo, useRef, useState } from "react"; import { useBackdropClose } from "./backdrop"; /** * Asking for one name. * * A real dialog rather than `window.prompt`, for the same reasons the link * dialog is one: the browser's box cannot be styled, cannot be reached by the * tests, and on a phone it arrives as a system alert with the app greyed out * behind it. * * Deliberately small. Naming a chapter is one field and two buttons, and * anything more would be a form. * * The one exception is the colour, offered when `colors` is given: it belongs to * the same act. Making a chapter and then having to right-click it to say which * one it is meant two steps for one decision. */ export function NameDialog({ title, value = "", label = "Name", confirmLabel = "Save", colors, color: initialColor = null, onSave, onClose, dark, }) { const [name, setName] = useState(value); const [color, setColor] = useState(initialColor); const backdrop = useBackdropClose(onClose); const inputRef = useRef(null); const T = useMemo( () => ({ bg: dark ? "#161b22" : "#fff", border: dark ? "#30363d" : "#d0d7de", text: dark ? "#e6edf3" : "#1f2328", muted: dark ? "#8b949e" : "#656d76", accent: dark ? "#388bfd" : "#0969da", field: dark ? "#0d1117" : "#f6f8fa", }), [dark] ); useEffect(() => { inputRef.current?.focus(); inputRef.current?.select(); }, []); const ready = !!name.trim(); const save = () => { if (!ready) return; onSave(name.trim(), color); onClose(); }; return (