-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathdemo-defaults.js
77 lines (66 loc) · 1.77 KB
/
demo-defaults.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
const React = require('react');
const { createRoot } = require('react-dom/client');
const { FocusTrap } = require('../../dist/focus-trap-react');
const container = document.getElementById('demo-defaults');
class DemoDefaults extends React.Component {
constructor(props) {
super(props);
this.state = {
activeTrap: false,
};
this.mountTrap = this.mountTrap.bind(this);
this.unmountTrap = this.unmountTrap.bind(this);
this.containerEl = null;
}
mountTrap() {
this.setState({ activeTrap: true });
}
unmountTrap() {
this.setState({ activeTrap: false });
}
// purposely using a ref here to test new React 19 "ref as a prop" behavior
// with fallback to React 18 deprecated behavior
handleCallbackRef(el) {
this.containerEl = el;
this.containerEl?.classList.add('is-active');
}
render() {
const trap = this.state.activeTrap && (
<FocusTrap
focusTrapOptions={{
onDeactivate: this.unmountTrap,
}}
>
<div
className="trap is-active"
ref={(el) => this.handleCallbackRef(el)}
>
<p>
Here is a focus trap <a href="#">with</a> <a href="#">some</a>{' '}
<a href="#">focusable</a> parts.
</p>
<p>
<button
onClick={this.unmountTrap}
aria-describedby="defaults-heading"
>
deactivate trap
</button>
</p>
</div>
</FocusTrap>
);
return (
<div>
<p>
<button onClick={this.mountTrap} aria-describedby="defaults-heading">
activate trap
</button>
</p>
{trap}
</div>
);
}
}
const root = createRoot(container);
root.render(<DemoDefaults />);