"use client"; import { useCallback, useState, type KeyboardEvent } from "react"; interface AddressBarProps { route: string; onNavigate: (route: string) => void; } export function AddressBar({ route, onNavigate }: AddressBarProps) { const [value, setValue] = useState(route); const [focused, setFocused] = useState(false); const commit = useCallback(() => { let normalized = value.trim(); if (!normalized.startsWith("/")) normalized = "/" + normalized; onNavigate(normalized); setValue(normalized); }, [value, onNavigate]); const handleKeyDown = useCallback( (e: KeyboardEvent) => { if (e.key === "Enter") { commit(); (e.target as HTMLInputElement).blur(); } }, [commit], ); const displayValue = focused ? value : route; return (
setValue(e.target.value)} onFocus={() => { setFocused(true); setValue(route); }} onBlur={() => { setFocused(false); commit(); }} onKeyDown={handleKeyDown} spellCheck={false} className="flex-1 bg-transparent text-xs font-mono text-foreground outline-none placeholder:text-muted-foreground" placeholder="/" />
); }