-
Notifications
You must be signed in to change notification settings - Fork 7
/
component-sandbox.js
118 lines (109 loc) · 4.64 KB
/
component-sandbox.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import React, {useRef, useState, useEffect} from "react"
import HeaderPortal from "components/header-portal"
import "/component-sandbox.scss"
import ReorderableList from "/exercise5-advanced-scripting-ARIA/reorderable-list"
const DemoSandbox = () => {
let [currentCSSClass, setCurrentCSSClass] = useState(null)
let [dialogActive, setDialogActive] = useState(false)
const [ariaHiddenActive, setAriaHiddenActive] = useState(false)
const cssTargetBtnRef = useRef(null)
const modalLaunchBtnRef = useRef(null)
const dialogHeadingRef = useRef(null)
const confirmDialogRef = useRef(null)
const handleChange = (event) => {
setCurrentCSSClass(event.target.id)
if (event.target.id === 'ariaHidden') {
setAriaHiddenActive(true)
} else {
setAriaHiddenActive(false)
}
}
const launchModal = () => {
setDialogActive(true)
}
const hideModal = () => {
setDialogActive(false)
}
const handleKey = (event) => {
if (event.key === 'Escape') {
setDialogActive(false)
}
}
useEffect(()=> {
if (dialogActive) {
// Note: inert requires a polyfill to work in non-Chrome browsers
document.getElementById('app-root').setAttribute('inert', 'inert')
dialogHeadingRef.current.focus()
} else {
document.getElementById('app-root').removeAttribute('inert')
modalLaunchBtnRef.current.focus()
}
}, [dialogActive])
return (
<>
<div className="layout component-sandbox">
<h1>Component Sandbox</h1>
<section aria-labelledby="header1">
<h2 className="h3-style" id="header1">CSS Visibility Techniques</h2>
<button aria-hidden={ariaHiddenActive ? true : null} className={currentCSSClass} id="css-target-button" ref={cssTargetBtnRef}>
Reserve
</button>
<fieldset>
<legend>Button class controls</legend>
<label>
Visually-hidden
<input onChange={handleChange} type="radio" name="radios" id="visuallyHidden" />
</label>
<label>
Opacity
<input onChange={handleChange} type="radio" name="radios" id="opacityNone" />
</label>
<label>
Display
<input onChange={handleChange} type="radio" name="radios" id="displayNone" />
</label>
<label>
Visibility
<input onChange={handleChange} type="radio" name="radios" id="visibilityHidden" />
</label>
<label>
aria-hidden
<input onChange={handleChange} type="radio" name="radios" id="ariaHidden" />
</label>
<label>
None
<input onChange={handleChange} type="radio" name="radios" id="none" />
</label>
</fieldset>
</section>
<section aria-labelledby="header2">
<h2 className="h3-style" id="header2">Modal Dialog</h2>
<button onClick={launchModal} ref={modalLaunchBtnRef}>Launch modal</button>
</section>
<section aria-labelledby="header3">
<h2 className="h3-style" id="header3">Sortable List</h2>
<ReorderableList />
</section>
</div>
<HeaderPortal>
<dialog
aria-labelledby="dialogHeading"
aria-modal={dialogActive ? 'true' : 'false'}
open={dialogActive ? 'open' : null}
onKeyUp={handleKey}
ref={confirmDialogRef}
>
<h1 id="dialogHeading" ref={dialogHeadingRef} tabIndex="-1">Confirm selection</h1>
<p>You have selected these dates:</p>
<ul>
<li>2022-04-17</li>
<li>2022-04-18</li>
</ul>
<button onClick={hideModal}>Cool</button>
</dialog>
<div className={`backdrop ${dialogActive ? 'active' : null}`}></div>
</HeaderPortal>
</>
)
}
export default DemoSandbox