File tree Expand file tree Collapse file tree 3 files changed +27
-3
lines changed Expand file tree Collapse file tree 3 files changed +27
-3
lines changed Original file line number Diff line number Diff line change 1
- import { AnimatePresence , motion } from "framer-motion"
1
+ import { motion } from "framer-motion"
2
2
import { Box , type BoxProps , Flex , Text } from "@chakra-ui/react"
3
3
import * as NavigationMenu from "@radix-ui/react-navigation-menu"
4
4
5
5
import { Button } from "@/components/Buttons"
6
6
7
- import { SECTION_LABELS } from "@/lib/constants"
7
+ import { MAIN_NAV_ID , SECTION_LABELS } from "@/lib/constants"
8
8
9
9
import type { NavSections } from "../types"
10
10
@@ -35,7 +35,7 @@ const Menu = ({ sections, ...props }: NavMenuProps) => {
35
35
onValueChange = { handleSectionChange }
36
36
>
37
37
< NavigationMenu . List asChild >
38
- < Flex as = "ul" listStyleType = "none" >
38
+ < Flex id = { MAIN_NAV_ID } as = "ul" listStyleType = "none" >
39
39
{ SECTION_LABELS . map ( ( sectionKey ) => {
40
40
const { label, items } = sections [ sectionKey ]
41
41
const isActive = activeSection === sectionKey
Original file line number Diff line number Diff line change 1
1
import { useState } from "react"
2
2
import type { MotionProps } from "framer-motion"
3
+ import { useEventListener } from "@chakra-ui/react"
4
+
5
+ import { MAIN_NAV_ID , SECTION_LABELS } from "@/lib/constants"
3
6
4
7
import type { NavSectionKey , NavSections } from "../types"
5
8
@@ -11,6 +14,25 @@ export const useNavMenu = (sections: NavSections) => {
11
14
const menuColors = useNavMenuColors ( )
12
15
const [ activeSection , setActiveSection ] = useState < NavSectionKey | null > ( null )
13
16
17
+ // Focus corresponding nav section when number keys pressed
18
+ useEventListener ( "keydown" , ( event ) => {
19
+ if ( ! document || ! event . key . match ( / [ 1 - 9 ] / ) ) return
20
+ if ( event . target instanceof HTMLInputElement ) return
21
+ if ( event . target instanceof HTMLTextAreaElement ) return
22
+ if ( event . target instanceof HTMLSelectElement ) return
23
+
24
+ const sectionIdx = parseInt ( event . key ) - 1
25
+ if ( sectionIdx >= SECTION_LABELS . length ) return
26
+
27
+ const button = document . querySelector (
28
+ `#${ MAIN_NAV_ID } li:nth-of-type(${ sectionIdx + 1 } ) button`
29
+ )
30
+ if ( ! button ) return
31
+
32
+ event . preventDefault ( )
33
+ ; ( button as HTMLButtonElement ) . focus ( )
34
+ } )
35
+
14
36
const getEnglishSectionName = (
15
37
activeSection : string
16
38
) : NavSectionKey | null => {
Original file line number Diff line number Diff line change @@ -85,3 +85,5 @@ export const SECTION_LABELS: NavSectionKey[] = [
85
85
"participate" ,
86
86
"research" ,
87
87
]
88
+
89
+ export const MAIN_NAV_ID = "main-navigation"
You can’t perform that action at this time.
0 commit comments