diff --git a/src/app/components/Header.tsx b/src/app/components/Header.tsx index 53e35cca..b64a3670 100644 --- a/src/app/components/Header.tsx +++ b/src/app/components/Header.tsx @@ -66,6 +66,7 @@ function GeneratorTitle({ title, gen }: GeneratorTitleProps) { const [active, setActive] = useFocus() const [search, setSearch] = useState('') const inputRef = useRef(null) + const resultsRef = useRef(null) const icon = Object.keys(Icons).includes(gen.id) ? gen.id as keyof typeof Icons : undefined @@ -92,14 +93,41 @@ function GeneratorTitle({ title, gen }: GeneratorTitleProps) { }) }, [setActive, inputRef]) + const handleKeyDown = useCallback((e: KeyboardEvent) => { + if (e.key == 'Enter') { + if (document.activeElement == inputRef.current) { + const firstResult = resultsRef.current?.firstElementChild + if (firstResult instanceof HTMLElement) { + firstResult.click() + } + } + } else if (e.key == 'ArrowDown') { + const nextElement = document.activeElement == inputRef.current + ? resultsRef.current?.firstElementChild + : document.activeElement?.nextElementSibling + if (nextElement instanceof HTMLElement) { + nextElement.focus() + } + e.preventDefault() + } else if (e.key == 'ArrowUp') { + const prevElement = document.activeElement?.previousElementSibling + if (prevElement instanceof HTMLElement) { + prevElement.focus() + } + e.preventDefault() + } else if (e.key == 'Escape') { + setActive(false) + } + }, [setActive, inputRef]) + return

{title} {icon && Icons[icon]}

-
- setSearch((e.target as HTMLInputElement).value)} onClick={e => e.stopPropagation()} /> - {active &&
+
+ setSearch((e.target as HTMLInputElement).value)} onClick={(e) => e.stopPropagation()} /> + {active &&
{generators.length === 0 && {locale('generators.no_results')}} {generators.map(g => setActive(false)}> diff --git a/src/styles/global.css b/src/styles/global.css index 5eb80ac7..7408a2f0 100644 --- a/src/styles/global.css +++ b/src/styles/global.css @@ -234,6 +234,10 @@ nav li .btn svg { background-color: var(--background-1); } +.gen-results > a { + outline-offset: -2px; +} + .gen-results > a svg { width: 16px; height: 16px; @@ -243,10 +247,12 @@ nav li .btn svg { transition: margin 0.2s; } +.gen-results > a:focus-visible, .gen-results > a:hover { background-color: var(--background-3); } +.gen-results > a:focus-visible svg, .gen-results > a:hover svg { margin-left: 14px; margin-right: 0px;