forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
198 lines (175 loc) · 5.25 KB
/
Copy pathindex.tsx
File metadata and controls
198 lines (175 loc) · 5.25 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// This implements a Button that triggers a panel. The panel will have a small
// arrow pointing towards the button, which is implemented in ArrowPanel.
//
// Here is a simple example:
//
// import { ButtonWithPanel } from // 'firefox-profiler/components/ButtonWithPanel';
// ...
// <ButtonWithPanel
// className="MyButtonWithPanel"
// buttonClassName="MyPanelButton"
// panelClassName="MyPanel"
// label="Click me!"
// panelContent={<>
// <p>We explain lots of useful things here.</p>
// <p>If you want to know more <a href='/'>click here</a>.</p>
// </>}
// />
//
// This registers the events on the window object (mouse and keyboard events).
import * as React from 'react';
import classNames from 'classnames';
import { ArrowPanel } from './ArrowPanel';
import './ButtonWithPanel.css';
type Props = {
readonly className?: string;
readonly label: string;
readonly panelContent: React.ReactNode;
readonly panelClassName?: string;
// Setting this prop to true opens the panel.
readonly open?: boolean;
// The class name of the button input element.
readonly buttonClassName?: string;
readonly onPanelOpen?: () => unknown;
readonly onPanelClose?: () => unknown;
readonly title?: string;
};
type State = {
readonly open: boolean;
};
export class ButtonWithPanel extends React.PureComponent<Props, State> {
_panel: ArrowPanel | null = null;
_buttonRef = React.createRef<HTMLButtonElement>();
_wrapperRef = React.createRef<HTMLElement>();
constructor(props: Props) {
super(props);
this.state = { open: !!props.open };
}
override componentDidMount() {
// the panel can be closed by clicking anywhere on the window
window.addEventListener('click', this._onWindowClick);
// the panel can be closed by pressing the Esc key
window.addEventListener('keydown', this._onKeyDown);
if (this.state.open) {
this.openPanel();
}
}
override componentDidUpdate(prevProps: Props) {
// Open the panel when the open prop becomes true.
if (!prevProps.open && this.props.open) {
this.setState({ open: true });
this.openPanel();
}
}
override componentWillUnmount() {
window.removeEventListener('keydown', this._onKeyDown);
window.removeEventListener('click', this._onWindowClick);
}
_onPanelOpen = () => {
this.setState({ open: true });
if (this.props.onPanelOpen) {
this.props.onPanelOpen();
}
};
_onPanelClose = () => {
// Let's focus the delete button after dismissing the dialog, but _only_ if
// the focus was part of the dialog before.
// Note this branch isn't tested because jsdom doesn't support the
// :focus-within selector.
/* istanbul ignore if */
if (this.isFocusWithin()) {
this.focus();
}
if (this.props.onPanelClose) {
this.props.onPanelClose();
}
};
_takePanelRef = (panel: ArrowPanel | null) => {
this._panel = panel;
};
openPanel() {
if (this._panel) {
this._panel.open();
}
}
closePanel() {
if (this._panel && this.state.open) {
// Close immediately so the button doesn't stay active while the panel animates out.
this.setState({ open: false });
this._panel.close();
}
}
_onWindowClick = () => {
this.closePanel();
};
_onButtonClick = () => {
if (!this.state.open) {
// We use a timeout so that we let the event bubble up to the handlers bound
// on `window`, closing all other panels, before opening this one.
setTimeout(() => this.openPanel());
}
};
_onKeyDown = (e: KeyboardEvent) => {
if (e.key === 'Escape') {
this.closePanel();
}
};
// This function isn't called in tests because isFocusWithin never returns
// true in jsdom. So let's ignore ir from the code coverage report.
/* istanbul ignore next */
focus() {
if (this._buttonRef.current) {
this._buttonRef.current.focus();
}
}
isFocusWithin(): boolean {
try {
if (this._wrapperRef.current) {
return this._wrapperRef.current.matches(':focus-within');
}
} catch {
// This browser doesn't support :focus-within (especially JSDOM), let's
// degrade gracefully.
}
return false;
}
override render() {
const {
className,
label,
panelContent,
panelClassName,
buttonClassName,
title,
} = this.props;
const { open } = this.state;
return (
<div
className={classNames('buttonWithPanel', className, { open })}
ref={this._wrapperRef as React.RefObject<HTMLDivElement>}
>
<button
type="button"
className={classNames('buttonWithPanelButton', buttonClassName)}
aria-expanded={open}
title={open ? undefined : title}
onClick={this._onButtonClick}
ref={this._buttonRef}
>
{label}
</button>
<ArrowPanel
className={panelClassName}
onOpen={this._onPanelOpen}
onClose={this._onPanelClose}
ref={this._takePanelRef}
>
{panelContent}
</ArrowPanel>
</div>
);
}
}