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
import { FocusEvent, useRef } from "react";
import { Checkbox, useCheckboxState } from "ariakit/checkbox";
import { FocusTrap } from "ariakit/focus-trap";
import "./style.css";
export default function Example() {
const focusTrapped = useCheckboxState({ defaultValue: true });
const firstRef = useRef<HTMLInputElement>(null);
const lastRef = useRef<HTMLButtonElement>(null);
const onTrapFocus = (event: FocusEvent) => {
if (event.relatedTarget === firstRef.current) {
lastRef.current?.focus();
} else {
firstRef.current?.focus();
}
};
return (
<>
{focusTrapped.value && <FocusTrap onFocus={onTrapFocus} />}
<div className="wrapper">
<label className="label">
<Checkbox state={focusTrapped} ref={firstRef} className="checkbox" />
Trap focus
</label>
<button className="button" ref={lastRef}>
Button
</button>
</div>
{focusTrapped.value && <FocusTrap onFocus={onTrapFocus} />}
</>
);
}