Skip to content

Commit d91635d

Browse files
feat: adds keyboard navigation
1 parent d23df5b commit d91635d

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/components/Header.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type FlavorName, flavors } from '@catppuccin/palette';
2-
import { useLocation } from '@solidjs/router';
3-
import { Show } from 'solid-js';
2+
import { useLocation, useNavigate } from '@solidjs/router';
3+
import { Show, createEffect, onCleanup } from 'solid-js';
44
import { useI18nContext } from '~/i18n/i18n-solid';
55
import type { Locales } from '~/i18n/i18n-types';
66
import { locales } from '~/i18n/i18n-util';
@@ -12,6 +12,7 @@ import { IconsEnum } from './Icon/types/IconsEnum';
1212

1313
export default function Header() {
1414
const { locale, setLocale } = useI18nContext();
15+
const navigate = useNavigate();
1516
const location = useLocation();
1617

1718
const switchLocale = async (newLocale: Locales) => {
@@ -29,6 +30,18 @@ export default function Header() {
2930
}`;
3031
};
3132

33+
createEffect(() => {
34+
function handleKeyDown(e: KeyboardEvent) {
35+
if (e.key === 'Backspace') navigate('/');
36+
}
37+
38+
addEventListener('keydown', handleKeyDown);
39+
40+
onCleanup(() => {
41+
removeEventListener('keydown', handleKeyDown);
42+
});
43+
});
44+
3245
return (
3346
<header class="bg-mantle flex justify-end px-4 py-2">
3447
<Show when={location.pathname !== '/'}>

src/routes/(home).tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Show, createEffect, createSignal } from 'solid-js';
1+
import { useNavigate } from '@solidjs/router';
2+
import { Show, createEffect, createSignal, onCleanup } from 'solid-js';
23
import Typewriter from '~/components/Typewriter/Typewriter';
34
import { useI18nContext } from '~/i18n/i18n-solid';
45
import type { Locales } from '~/i18n/i18n-types';
@@ -9,12 +10,35 @@ export default function Home() {
910
const [renderSub, setRenderSub] = createSignal(false);
1011
const [renderList, setRenderList] = createSignal(false);
1112
const isVisited = useIsVisited();
13+
const navigate = useNavigate();
1214

1315
createEffect<Locales>((prevLocale) => {
1416
if (prevLocale !== locale()) setRenderSub(false);
1517
return locale();
1618
});
1719

20+
createEffect(() => {
21+
function handleKeyDown(e: KeyboardEvent) {
22+
switch (e.key) {
23+
case '1':
24+
navigate('/whoami');
25+
break;
26+
case '2':
27+
navigate('/contacts');
28+
break;
29+
case '3':
30+
navigate('/thingsivedone');
31+
break;
32+
}
33+
}
34+
35+
addEventListener('keydown', handleKeyDown);
36+
37+
onCleanup(() => {
38+
removeEventListener('keydown', handleKeyDown);
39+
});
40+
});
41+
1842
return (
1943
<div class="text-left mx-auto grid grid-rows-home-grid">
2044
{/* Presentation */}

0 commit comments

Comments
 (0)