/
TabPanel.js
87 lines (83 loc) · 2.67 KB
/
TabPanel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import React, { useContext } from 'react'
import A11yReusable from './A11yReusable'
import TabButton from './TabButton'
import AlertDialogTab from '../components/AlertDialogTab'
import DialogTab from '../components/DialogTab'
import MenuTab from '../components/MenuTab'
import TabContext from '../context/TabContext'
const TabPanel = () => {
const {
modalIsOpen,
alertIsOpen,
menuIsOpen
} = useContext(TabContext)
// controls arrow left and right
const moveFocusUp = () => {
const tabItems = document.querySelector('[role="tablist"]').childNodes
const activeItem = document.activeElement
for (let i = 0; i < tabItems.length; i++) {
if (activeItem === tabItems[i] && activeItem !== tabItems[0]) {
tabItems[i - 1].focus()
}
}
}
const moveFocusDown = () => {
const tabItems = document.querySelector('[role="tablist"]').childNodes
const activeItem = document.activeElement
for (let i = 0; i < tabItems.length; i++) {
const listLength = tabItems.length
if (
activeItem === tabItems[i] &&
activeItem !== tabItems[listLength - 1]
) {
tabItems[i + 1].focus()
}
}
}
const handleKeyDown = (e) => {
if (e.key === 'ArrowLeft') {
moveFocusUp()
}
if (e.key === 'ArrowRight') {
moveFocusDown()
}
}
const handleOnKeyDown = (e) => {
const tabPanels = document.querySelector('#tab-panels')
if(e.key === 'ArrowDown') {
tabPanels.focus()
}
}
return (
<div>
<A11yReusable ariaRole='tablist' onKeyDown={handleKeyDown}>
<TabButton
id='show-modal'
ariaControls='dialog-modal'
onKeyDown={handleOnKeyDown}
>
Render the dialog modal app
<span className="arrow right">▶</span>
<span className="arrow down">▼</span>
</TabButton>
<TabButton id='show-alert' ariaControls='alert-modal' onKeyDown={handleOnKeyDown}>
<span className="arrow left">◀</span>
Render the alert modal
<span className="arrow right">▶</span>
<span className="arrow down">▼</span>
</TabButton>
<TabButton id='show-menu' ariaControls='menu-tab' onKeyDown={handleOnKeyDown}>
<span className="arrow left">◀</span>
Render the menu
<span className="arrow down">▼</span>
</TabButton>
</A11yReusable>
<A11yReusable id="tab-panels" ariaRole="region" ariaLabel="App Components" tabIndex={0}>
{modalIsOpen && <DialogTab />}
{alertIsOpen && <AlertDialogTab />}
{menuIsOpen && <MenuTab />}
</A11yReusable>
</div>
)
}
export default TabPanel