1- import React , { useState , useEffect } from "react"
1+ import React , { useEffect } from "react"
22import { Switch , Route } from "react-router-dom"
33import { ContextUtil } from "undernet"
44
@@ -14,8 +14,10 @@ import "./styles.scss"
1414import { FOCUSABLE_TABINDEX , UNFOCUSABLE_TABINDEX } from "./constants"
1515
1616export default function Main ( ) {
17- const [ headerTabIndex , setHeaderTabIndex ] = useState ( UNFOCUSABLE_TABINDEX )
18- const [ mainTabIndex , setMainTabIndex ] = useState ( UNFOCUSABLE_TABINDEX )
17+ const Attributes = {
18+ TABINDEX : "tabindex" ,
19+ }
20+
1921 const headerRef = React . createRef ( )
2022 const mainRef = React . createRef ( )
2123 const componentUnmountFunction = ( ) => {
@@ -27,41 +29,31 @@ export default function Main() {
2729 return componentUnmountFunction
2830 } , [ ] )
2931
30- useEffect ( ( ) => {
31- if ( headerTabIndex === FOCUSABLE_TABINDEX ) {
32- headerRef . current . focus ( )
33- } else if ( mainTabIndex === FOCUSABLE_TABINDEX ) {
34- mainRef . current . focus ( )
35- }
36- } , [ headerTabIndex , mainTabIndex ] )
37-
38- const handleHeaderFocusClick = event => {
39- event . preventDefault ( )
40- setHeaderTabIndex ( FOCUSABLE_TABINDEX )
32+ const handleRefocusClick = ref => {
33+ ref . current . setAttribute ( Attributes . TABINDEX , FOCUSABLE_TABINDEX )
34+ ref . current . focus ( )
4135 }
4236
43- const handleMainFocusClick = event => {
44- event . preventDefault ( )
45- setMainTabIndex ( FOCUSABLE_TABINDEX )
46- }
37+ const getHeaderTabIndex = ( ) => headerRef . current . getAttribute ( Attributes . TABINDEX )
38+ const getMainTabIndex = ( ) => mainRef . current . getAttribute ( Attributes . TABINDEX )
4739
4840 const handleHeaderBlur = ( ) => {
49- if ( headerTabIndex === UNFOCUSABLE_TABINDEX ) return
50- setHeaderTabIndex ( UNFOCUSABLE_TABINDEX )
41+ if ( getHeaderTabIndex ( ) === UNFOCUSABLE_TABINDEX ) return
42+ headerRef . current . removeAttribute ( Attributes . TABINDEX )
5143 }
5244
5345 const handleMainBlur = ( ) => {
54- if ( mainTabIndex === UNFOCUSABLE_TABINDEX ) return
55- setMainTabIndex ( UNFOCUSABLE_TABINDEX )
46+ if ( getMainTabIndex ( ) === UNFOCUSABLE_TABINDEX ) return
47+ mainRef . current . removeAttribute ( Attributes . TABINDEX )
5648 }
5749
5850 return (
5951 < React . Fragment >
60- < header tabIndex = { headerTabIndex } ref = { headerRef } onBlur = { handleHeaderBlur } role = "banner" >
61- < GlobalNav handleMainFocusClick = { handleMainFocusClick } />
52+ < header ref = { headerRef } onBlur = { handleHeaderBlur } role = "banner" >
53+ < GlobalNav handleRefocusClick = { handleRefocusClick } mainRef = { mainRef } />
6254 </ header >
6355
64- < main tabIndex = { mainTabIndex } ref = { mainRef } onBlur = { handleMainBlur } role = "main" >
56+ < main ref = { mainRef } onBlur = { handleMainBlur } role = "main" >
6557 < Switch >
6658 < Route exact path = { rootPath } component = { Home } />
6759 < Route path = { docsPath } component = { Docs } />
@@ -70,7 +62,7 @@ export default function Main() {
7062 </ main >
7163
7264 < footer >
73- < Footer handleHeaderFocusClick = { handleHeaderFocusClick } />
65+ < Footer handleRefocusClick = { handleRefocusClick } headerRef = { headerRef } />
7466 </ footer >
7567 </ React . Fragment >
7668 )
0 commit comments