@khanacademy/wonder-blocks-clickable 4.2.7 → 4.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/package.json +3 -3
- package/src/components/__tests__/clickable-behavior.test.tsx +0 -1438
- package/src/components/__tests__/clickable-behavior.typestest.tsx +0 -20
- package/src/components/__tests__/clickable.test.tsx +0 -661
- package/src/components/__tests__/clickable.typestest.tsx +0 -64
- package/src/components/clickable-behavior.ts +0 -659
- package/src/components/clickable.tsx +0 -429
- package/src/index.ts +0 -14
- package/src/util/__tests__/get-clickable-behavior.test.tsx +0 -104
- package/src/util/__tests__/is-client-side-url.js.test.ts +0 -49
- package/src/util/get-clickable-behavior.ts +0 -46
- package/src/util/is-client-side-url.ts +0 -15
- package/tsconfig-build.json +0 -12
- package/tsconfig-build.tsbuildinfo +0 -1
|
@@ -1,659 +0,0 @@
|
|
|
1
|
-
import * as React from "react";
|
|
2
|
-
|
|
3
|
-
// NOTE: Potentially add to this as more cases come up.
|
|
4
|
-
export type ClickableRole =
|
|
5
|
-
| "button"
|
|
6
|
-
| "checkbox"
|
|
7
|
-
| "link"
|
|
8
|
-
| "listbox"
|
|
9
|
-
| "menu"
|
|
10
|
-
| "menuitem"
|
|
11
|
-
| "menuitemcheckbox"
|
|
12
|
-
| "option"
|
|
13
|
-
| "radio"
|
|
14
|
-
| "switch"
|
|
15
|
-
| "tab";
|
|
16
|
-
|
|
17
|
-
const getAppropriateTriggersForRole = (role?: ClickableRole | null) => {
|
|
18
|
-
switch (role) {
|
|
19
|
-
// Triggers on ENTER, but not SPACE
|
|
20
|
-
case "link":
|
|
21
|
-
return {
|
|
22
|
-
triggerOnEnter: true,
|
|
23
|
-
triggerOnSpace: false,
|
|
24
|
-
};
|
|
25
|
-
// Triggers on SPACE, but not ENTER
|
|
26
|
-
case "checkbox":
|
|
27
|
-
case "radio":
|
|
28
|
-
case "listbox":
|
|
29
|
-
return {
|
|
30
|
-
triggerOnEnter: false,
|
|
31
|
-
triggerOnSpace: true,
|
|
32
|
-
};
|
|
33
|
-
// Triggers on both ENTER and SPACE
|
|
34
|
-
case "button":
|
|
35
|
-
case "menuitem":
|
|
36
|
-
case "menu":
|
|
37
|
-
case "option":
|
|
38
|
-
default:
|
|
39
|
-
return {
|
|
40
|
-
triggerOnEnter: true,
|
|
41
|
-
triggerOnSpace: true,
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
type CommonProps = Readonly<{
|
|
47
|
-
/**
|
|
48
|
-
* A function that returns the a React `Element`.
|
|
49
|
-
*
|
|
50
|
-
* The React `Element` returned should take in this component's state
|
|
51
|
-
* (`{hovered, focused, pressed}`) as props.
|
|
52
|
-
*/
|
|
53
|
-
children: (
|
|
54
|
-
state: ClickableState,
|
|
55
|
-
childrenProps: ChildrenProps,
|
|
56
|
-
) => React.ReactNode;
|
|
57
|
-
/**
|
|
58
|
-
* Whether the component is disabled.
|
|
59
|
-
*
|
|
60
|
-
* If the component is disabled, this component will return handlers
|
|
61
|
-
* that do nothing.
|
|
62
|
-
*/
|
|
63
|
-
disabled: boolean;
|
|
64
|
-
/**
|
|
65
|
-
* A URL.
|
|
66
|
-
*
|
|
67
|
-
* If specified, clicking on the component will navigate to the location
|
|
68
|
-
* provided.
|
|
69
|
-
* For keyboard navigation, the default is that both an enter and space
|
|
70
|
-
* press would also navigate to this location. See the triggerOnEnter and
|
|
71
|
-
* triggerOnSpace props for more details.
|
|
72
|
-
*/
|
|
73
|
-
href?: string;
|
|
74
|
-
/**
|
|
75
|
-
* This should only be used by button.js.
|
|
76
|
-
*/
|
|
77
|
-
type?: "submit";
|
|
78
|
-
/**
|
|
79
|
-
* Specifies the type of relationship between the current document and the
|
|
80
|
-
* linked document. Should only be used when `href` is specified. This
|
|
81
|
-
* defaults to "noopener noreferrer" when `target="_blank"`, but can be
|
|
82
|
-
* overridden by setting this prop to something else.
|
|
83
|
-
*/
|
|
84
|
-
rel?: string;
|
|
85
|
-
skipClientNav?: boolean;
|
|
86
|
-
/**
|
|
87
|
-
* Used to indicate the tab order of an element.
|
|
88
|
-
* Use 0 to make an element focusable, and use -1 to make an
|
|
89
|
-
* element non-focusable via keyboard navigation.
|
|
90
|
-
*/
|
|
91
|
-
tabIndex?: number;
|
|
92
|
-
/**
|
|
93
|
-
* A function to be executed `onclick`.
|
|
94
|
-
*/
|
|
95
|
-
onClick?: (e: React.SyntheticEvent) => unknown;
|
|
96
|
-
/**
|
|
97
|
-
* Run async code in the background while client-side navigating. If the
|
|
98
|
-
* browser does a full page load navigation, the callback promise must be
|
|
99
|
-
* settled before the navigation will occur. Errors are ignored so that
|
|
100
|
-
* navigation is guaranteed to succeed.
|
|
101
|
-
*/
|
|
102
|
-
safeWithNav?: () => Promise<unknown>;
|
|
103
|
-
/**
|
|
104
|
-
* Passed in by withRouter HOC.
|
|
105
|
-
* @ignore
|
|
106
|
-
*/
|
|
107
|
-
history?: any;
|
|
108
|
-
/**
|
|
109
|
-
* A role that encapsulates how the clickable component should behave, which
|
|
110
|
-
* affects which keyboard actions trigger the component. For example, a
|
|
111
|
-
* component with role="button" should be able to be clicked with both the
|
|
112
|
-
* enter and space keys.
|
|
113
|
-
*/
|
|
114
|
-
role?: ClickableRole;
|
|
115
|
-
/**
|
|
116
|
-
* Respond to raw "onfocus" event.
|
|
117
|
-
*/
|
|
118
|
-
onFocus?: (e: React.FocusEvent) => unknown;
|
|
119
|
-
/**
|
|
120
|
-
* Respond to raw "keydown" event.
|
|
121
|
-
*/
|
|
122
|
-
onKeyDown?: (e: React.KeyboardEvent) => unknown;
|
|
123
|
-
/**
|
|
124
|
-
* Respond to raw "keyup" event.
|
|
125
|
-
*/
|
|
126
|
-
onKeyUp?: (e: React.KeyboardEvent) => unknown;
|
|
127
|
-
/**
|
|
128
|
-
* Respond to a raw "mousedown" event.
|
|
129
|
-
*/
|
|
130
|
-
onMouseDown?: (e: React.MouseEvent) => unknown;
|
|
131
|
-
/**
|
|
132
|
-
* Respond to a raw "mouseup" event.
|
|
133
|
-
*/
|
|
134
|
-
onMouseUp?: (e: React.MouseEvent) => unknown;
|
|
135
|
-
}>;
|
|
136
|
-
|
|
137
|
-
type Props =
|
|
138
|
-
| (CommonProps &
|
|
139
|
-
Readonly<{
|
|
140
|
-
/**
|
|
141
|
-
* A target destination window for a link to open in. Should only be used
|
|
142
|
-
* when `href` is specified.
|
|
143
|
-
*/
|
|
144
|
-
// TODO(WB-1262): only allow this prop when `href` is also set.
|
|
145
|
-
target?: "_blank";
|
|
146
|
-
|
|
147
|
-
beforeNav?: never; // disallow beforeNav when target="_blank"
|
|
148
|
-
}>)
|
|
149
|
-
| (CommonProps &
|
|
150
|
-
Readonly<{
|
|
151
|
-
/**
|
|
152
|
-
* Run async code before navigating to the URL passed to `href`. If the
|
|
153
|
-
* promise returned rejects then navigation will not occur.
|
|
154
|
-
*
|
|
155
|
-
* If both safeWithNav and beforeNav are provided, beforeNav will be run
|
|
156
|
-
* first and safeWithNav will only be run if beforeNav does not reject.
|
|
157
|
-
*
|
|
158
|
-
* WARNING: Using this with `target="_blank"` will trigger built-in popup
|
|
159
|
-
* blockers in Firefox and Safari. This is because we do navigation
|
|
160
|
-
* programmatically and `beforeNav` causes a delay which means that the
|
|
161
|
-
* browser can't make a directly link between a user action and the
|
|
162
|
-
* navigation.
|
|
163
|
-
*/
|
|
164
|
-
beforeNav?: () => Promise<unknown>;
|
|
165
|
-
|
|
166
|
-
target?: never; // disallow target="_blank" when beforeNav is set
|
|
167
|
-
}>);
|
|
168
|
-
|
|
169
|
-
export type ClickableState = Readonly<{
|
|
170
|
-
/**
|
|
171
|
-
* Whether the component is hovered.
|
|
172
|
-
*
|
|
173
|
-
* See component documentation for more details.
|
|
174
|
-
*/
|
|
175
|
-
hovered: boolean;
|
|
176
|
-
/**
|
|
177
|
-
* Whether the component is hovered.
|
|
178
|
-
*
|
|
179
|
-
* See component documentation for more details.
|
|
180
|
-
*/
|
|
181
|
-
focused: boolean;
|
|
182
|
-
/**
|
|
183
|
-
* Whether the component is hovered.
|
|
184
|
-
*
|
|
185
|
-
* See component documentation for more details.
|
|
186
|
-
*/
|
|
187
|
-
pressed: boolean;
|
|
188
|
-
/**
|
|
189
|
-
* When we're waiting for beforeNav or safeWithNav to complete an async
|
|
190
|
-
* action, this will be true.
|
|
191
|
-
*
|
|
192
|
-
* NOTE: We only wait for safeWithNav to complete when doing a full page
|
|
193
|
-
* load navigation.
|
|
194
|
-
*/
|
|
195
|
-
waiting: boolean;
|
|
196
|
-
}>;
|
|
197
|
-
|
|
198
|
-
type DefaultProps = Readonly<{
|
|
199
|
-
disabled: Props["disabled"];
|
|
200
|
-
}>;
|
|
201
|
-
|
|
202
|
-
export type ChildrenProps = Readonly<{
|
|
203
|
-
onClick: (e: React.SyntheticEvent) => unknown;
|
|
204
|
-
onMouseEnter: (e: React.MouseEvent) => unknown;
|
|
205
|
-
onMouseLeave: () => unknown;
|
|
206
|
-
onMouseDown: (e: React.MouseEvent) => unknown;
|
|
207
|
-
onMouseUp: (e: React.MouseEvent) => unknown;
|
|
208
|
-
onTouchStart: () => unknown;
|
|
209
|
-
onTouchEnd: () => unknown;
|
|
210
|
-
onTouchCancel: () => unknown;
|
|
211
|
-
onKeyDown: (e: React.KeyboardEvent) => unknown;
|
|
212
|
-
onKeyUp: (e: React.KeyboardEvent) => unknown;
|
|
213
|
-
onFocus: (e: React.FocusEvent) => unknown;
|
|
214
|
-
onBlur: (e: React.FocusEvent) => unknown;
|
|
215
|
-
tabIndex?: number;
|
|
216
|
-
rel?: string;
|
|
217
|
-
}>;
|
|
218
|
-
|
|
219
|
-
const disabledHandlers = {
|
|
220
|
-
onClick: () => void 0,
|
|
221
|
-
onMouseEnter: () => void 0,
|
|
222
|
-
onMouseLeave: () => void 0,
|
|
223
|
-
onMouseDown: () => void 0,
|
|
224
|
-
onMouseUp: () => void 0,
|
|
225
|
-
onTouchStart: () => void 0,
|
|
226
|
-
onTouchEnd: () => void 0,
|
|
227
|
-
onTouchCancel: () => void 0,
|
|
228
|
-
onKeyDown: () => void 0,
|
|
229
|
-
onKeyUp: () => void 0,
|
|
230
|
-
} as const;
|
|
231
|
-
|
|
232
|
-
const keyCodes = {
|
|
233
|
-
enter: 13,
|
|
234
|
-
space: 32,
|
|
235
|
-
} as const;
|
|
236
|
-
|
|
237
|
-
const startState: ClickableState = {
|
|
238
|
-
hovered: false,
|
|
239
|
-
focused: false,
|
|
240
|
-
pressed: false,
|
|
241
|
-
waiting: false,
|
|
242
|
-
};
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Add hover, focus, and active status updates to a clickable component.
|
|
246
|
-
*
|
|
247
|
-
* Via mouse:
|
|
248
|
-
*
|
|
249
|
-
* 1. Hover over button -> hover state
|
|
250
|
-
* 2. Mouse down -> active state
|
|
251
|
-
* 3. Mouse up -> default state
|
|
252
|
-
* 4. Press tab -> focus state
|
|
253
|
-
*
|
|
254
|
-
* Via touch:
|
|
255
|
-
*
|
|
256
|
-
* 1. Touch down -> press state
|
|
257
|
-
* 2. Touch up -> default state
|
|
258
|
-
*
|
|
259
|
-
* Via keyboard:
|
|
260
|
-
*
|
|
261
|
-
* 1. Tab to focus -> focus state
|
|
262
|
-
* 2. Keydown (spacebar/enter) -> active state
|
|
263
|
-
* 3. Keyup (spacebar/enter) -> focus state
|
|
264
|
-
*
|
|
265
|
-
* Warning: The event handlers returned (onClick, onMouseEnter, onMouseLeave,
|
|
266
|
-
* onMouseDown, onMouseUp, onTouchStart, onTouchEnd, onTouchCancel,
|
|
267
|
-
* onKeyDown, onKeyUp, onFocus, onBlur, tabIndex) should be passed on to the
|
|
268
|
-
* component that has the ClickableBehavior. You cannot override these handlers
|
|
269
|
-
* without potentially breaking the functionality of ClickableBehavior.
|
|
270
|
-
*
|
|
271
|
-
* There are internal props triggerOnEnter and triggerOnSpace that can be set to
|
|
272
|
-
* false if one of those keys shouldn't count as a click on this component. Be
|
|
273
|
-
* careful about setting those to false -- make certain that the component
|
|
274
|
-
* shouldn't process that key.
|
|
275
|
-
*
|
|
276
|
-
* See [this
|
|
277
|
-
document](https://docs.google.com/document/d/1DG5Rg2f0cawIL5R8UqnPQpd7pbdObk8OyjO5ryYQmBM/edit#)
|
|
278
|
-
for a more thorough explanation of expected behaviors and potential cavaets.
|
|
279
|
-
*
|
|
280
|
-
* `ClickableBehavior` accepts a function as `children` which is passed state
|
|
281
|
-
* and an object containing event handlers and some other props. The `children`
|
|
282
|
-
* function should return a clickable React Element of some sort.
|
|
283
|
-
*
|
|
284
|
-
* Example:
|
|
285
|
-
*
|
|
286
|
-
* ```jsx
|
|
287
|
-
* function MyClickableComponent(props: Props) {
|
|
288
|
-
* const ClickableBehavior = getClickableBehavior();
|
|
289
|
-
*
|
|
290
|
-
* return (
|
|
291
|
-
* <ClickableBehavior
|
|
292
|
-
* disabled={props.disabled}
|
|
293
|
-
* onClick={props.onClick}
|
|
294
|
-
* tabIndex={0}
|
|
295
|
-
* >
|
|
296
|
-
* {({hovered}, childrenProps) => (
|
|
297
|
-
* <RoundRect
|
|
298
|
-
* textcolor="white"
|
|
299
|
-
* backgroundColor={hovered ? "red" : "blue"}
|
|
300
|
-
* {...childrenProps}
|
|
301
|
-
* >
|
|
302
|
-
* {props.children}
|
|
303
|
-
* </RoundRect>
|
|
304
|
-
* )}
|
|
305
|
-
* </ClickableBehavior>
|
|
306
|
-
* );
|
|
307
|
-
* }
|
|
308
|
-
* ```
|
|
309
|
-
*
|
|
310
|
-
* This follows a pattern called [Function as Child
|
|
311
|
-
* Components](https://medium.com/merrickchristensen/function-as-child-components-5f3920a9ace9).
|
|
312
|
-
*
|
|
313
|
-
* **WARNING:** Do not use this component directly, use getClickableBehavior
|
|
314
|
-
* instead. getClickableBehavior takes three arguments (href, directtNav, and
|
|
315
|
-
* router) and returns either the default ClickableBehavior or a react-router
|
|
316
|
-
* aware version.
|
|
317
|
-
*
|
|
318
|
-
* The react-router aware version is returned if `router` is a react-router-dom
|
|
319
|
-
* router, `skipClientNav` is not `true`, and `href` is an internal URL.
|
|
320
|
-
*
|
|
321
|
-
* The `router` can be accessed via __RouterContext (imported from
|
|
322
|
-
'react-router') from a component rendered as a descendant of a BrowserRouter.
|
|
323
|
-
See https://reacttraining.com/react-router/web/guides/basic-components.
|
|
324
|
-
*/
|
|
325
|
-
export default class ClickableBehavior extends React.Component<
|
|
326
|
-
Props,
|
|
327
|
-
ClickableState
|
|
328
|
-
> {
|
|
329
|
-
waitingForClick: boolean;
|
|
330
|
-
enterClick: boolean;
|
|
331
|
-
|
|
332
|
-
static defaultProps: DefaultProps = {
|
|
333
|
-
disabled: false,
|
|
334
|
-
};
|
|
335
|
-
|
|
336
|
-
static getDerivedStateFromProps(
|
|
337
|
-
props: Props,
|
|
338
|
-
state: ClickableState,
|
|
339
|
-
): Partial<ClickableState> | null | undefined {
|
|
340
|
-
// If new props are disabled, reset the hovered/pressed states
|
|
341
|
-
if (props.disabled) {
|
|
342
|
-
// Keep the focused state for enabling keyboard navigation.
|
|
343
|
-
return {...startState, focused: state.focused};
|
|
344
|
-
} else {
|
|
345
|
-
// Cannot return undefined
|
|
346
|
-
return null;
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
constructor(props: Props) {
|
|
351
|
-
super(props);
|
|
352
|
-
|
|
353
|
-
this.state = startState;
|
|
354
|
-
this.waitingForClick = false;
|
|
355
|
-
this.enterClick = false;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
navigateOrReset(shouldNavigate: boolean) {
|
|
359
|
-
if (shouldNavigate) {
|
|
360
|
-
const {
|
|
361
|
-
history,
|
|
362
|
-
href,
|
|
363
|
-
skipClientNav,
|
|
364
|
-
target = undefined,
|
|
365
|
-
} = this.props;
|
|
366
|
-
if (href) {
|
|
367
|
-
if (target === "_blank") {
|
|
368
|
-
window.open(href, "_blank");
|
|
369
|
-
this.setState({waiting: false});
|
|
370
|
-
} else if (history && !skipClientNav) {
|
|
371
|
-
history.push(href);
|
|
372
|
-
this.setState({waiting: false});
|
|
373
|
-
} else {
|
|
374
|
-
window.location.assign(href);
|
|
375
|
-
// We don't bother clearing the waiting state, the full page
|
|
376
|
-
// load navigation will do that for us by loading a new page.
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
} else {
|
|
380
|
-
this.setState({waiting: false});
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
handleSafeWithNav(
|
|
385
|
-
safeWithNav: () => Promise<unknown>,
|
|
386
|
-
shouldNavigate: boolean,
|
|
387
|
-
): Promise<void> {
|
|
388
|
-
const {skipClientNav, history} = this.props;
|
|
389
|
-
|
|
390
|
-
if ((history && !skipClientNav) || this.props.target === "_blank") {
|
|
391
|
-
// client-side nav
|
|
392
|
-
safeWithNav();
|
|
393
|
-
|
|
394
|
-
this.navigateOrReset(shouldNavigate);
|
|
395
|
-
|
|
396
|
-
return Promise.resolve();
|
|
397
|
-
} else {
|
|
398
|
-
if (!this.state.waiting) {
|
|
399
|
-
// We only show the spinner for safeWithNav when doing
|
|
400
|
-
// a full page load navigation since since the spinner is
|
|
401
|
-
// indicating that we're waiting for navigation to occur.
|
|
402
|
-
this.setState({waiting: true});
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
return safeWithNav()
|
|
406
|
-
.then(() => {
|
|
407
|
-
if (!this.state.waiting) {
|
|
408
|
-
// We only show the spinner for safeWithNav when doing
|
|
409
|
-
// a full page load navigation since since the spinner is
|
|
410
|
-
// indicating that we're waiting for navigation to occur.
|
|
411
|
-
this.setState({waiting: true});
|
|
412
|
-
}
|
|
413
|
-
return;
|
|
414
|
-
})
|
|
415
|
-
.catch((error) => {
|
|
416
|
-
// We ignore the error here so that we always
|
|
417
|
-
// navigate when using safeWithNav regardless of
|
|
418
|
-
// whether we're doing a client-side nav or not.
|
|
419
|
-
})
|
|
420
|
-
.finally(() => {
|
|
421
|
-
this.navigateOrReset(shouldNavigate);
|
|
422
|
-
});
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
runCallbackAndMaybeNavigate(
|
|
427
|
-
e: React.SyntheticEvent,
|
|
428
|
-
): Promise<undefined> | null | undefined {
|
|
429
|
-
const {
|
|
430
|
-
onClick = undefined,
|
|
431
|
-
beforeNav = undefined,
|
|
432
|
-
safeWithNav = undefined,
|
|
433
|
-
href,
|
|
434
|
-
type,
|
|
435
|
-
} = this.props;
|
|
436
|
-
let shouldNavigate = true;
|
|
437
|
-
let canSubmit = true;
|
|
438
|
-
|
|
439
|
-
if (onClick) {
|
|
440
|
-
onClick(e);
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
// If onClick() has called e.preventDefault() then we shouldn't
|
|
444
|
-
// navigate.
|
|
445
|
-
if (e.defaultPrevented) {
|
|
446
|
-
shouldNavigate = false;
|
|
447
|
-
canSubmit = false;
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
e.preventDefault();
|
|
451
|
-
|
|
452
|
-
if (!href && type === "submit" && canSubmit) {
|
|
453
|
-
let target = e.currentTarget;
|
|
454
|
-
while (target) {
|
|
455
|
-
if (target instanceof window.HTMLFormElement) {
|
|
456
|
-
// This event must be marked as cancelable otherwise calling
|
|
457
|
-
// e.preventDefault() on it won't do anything in Firefox.
|
|
458
|
-
// Chrome and Safari allow calling e.preventDefault() on
|
|
459
|
-
// non-cancelable events, but really they shouldn't.
|
|
460
|
-
const event = new window.Event("submit", {
|
|
461
|
-
// This is required as event propagation changed in
|
|
462
|
-
// React 17.
|
|
463
|
-
// @see https://legacy.reactjs.org/blog/2020/10/20/react-v17.html#changes-to-event-delegation
|
|
464
|
-
bubbles: true,
|
|
465
|
-
cancelable: true,
|
|
466
|
-
});
|
|
467
|
-
target.dispatchEvent(event);
|
|
468
|
-
break;
|
|
469
|
-
}
|
|
470
|
-
// All events should be typed as SyntheticEvent<HTMLElement>.
|
|
471
|
-
// Updating all of the places will take some time so I'll do
|
|
472
|
-
// this later
|
|
473
|
-
// @ts-expect-error [FEI-5019] - TS2322 - Type 'HTMLElement | null' is not assignable to type 'EventTarget & Element'.
|
|
474
|
-
target = target.parentElement;
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
if (beforeNav) {
|
|
479
|
-
this.setState({waiting: true});
|
|
480
|
-
beforeNav()
|
|
481
|
-
.then(() => {
|
|
482
|
-
if (safeWithNav) {
|
|
483
|
-
return this.handleSafeWithNav(
|
|
484
|
-
safeWithNav,
|
|
485
|
-
shouldNavigate,
|
|
486
|
-
);
|
|
487
|
-
} else {
|
|
488
|
-
return this.navigateOrReset(shouldNavigate);
|
|
489
|
-
}
|
|
490
|
-
})
|
|
491
|
-
.catch(() => {});
|
|
492
|
-
} else if (safeWithNav) {
|
|
493
|
-
// @ts-expect-error [FEI-5019] - TS2322 - Type 'Promise<void>' is not assignable to type 'Promise<undefined>'.
|
|
494
|
-
return this.handleSafeWithNav(safeWithNav, shouldNavigate);
|
|
495
|
-
} else {
|
|
496
|
-
this.navigateOrReset(shouldNavigate);
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
handleClick: (e: React.SyntheticEvent) => void = (e) => {
|
|
501
|
-
const {
|
|
502
|
-
onClick = undefined,
|
|
503
|
-
beforeNav = undefined,
|
|
504
|
-
safeWithNav = undefined,
|
|
505
|
-
} = this.props;
|
|
506
|
-
|
|
507
|
-
if (this.enterClick) {
|
|
508
|
-
return;
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
if (onClick || beforeNav || safeWithNav) {
|
|
512
|
-
this.waitingForClick = false;
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
this.runCallbackAndMaybeNavigate(e);
|
|
516
|
-
};
|
|
517
|
-
|
|
518
|
-
handleMouseEnter: (e: React.MouseEvent) => void = (e) => {
|
|
519
|
-
if (!this.waitingForClick) {
|
|
520
|
-
this.setState({hovered: true});
|
|
521
|
-
}
|
|
522
|
-
};
|
|
523
|
-
|
|
524
|
-
handleMouseLeave: () => void = () => {
|
|
525
|
-
if (!this.waitingForClick) {
|
|
526
|
-
this.setState({hovered: false, pressed: false, focused: false});
|
|
527
|
-
}
|
|
528
|
-
};
|
|
529
|
-
|
|
530
|
-
handleMouseDown: (e: React.MouseEvent) => void = (e) => {
|
|
531
|
-
if (this.props.onMouseDown) {
|
|
532
|
-
this.props.onMouseDown(e);
|
|
533
|
-
}
|
|
534
|
-
this.setState({pressed: true});
|
|
535
|
-
};
|
|
536
|
-
|
|
537
|
-
handleMouseUp: (e: React.MouseEvent) => void = (e) => {
|
|
538
|
-
if (this.props.onMouseUp) {
|
|
539
|
-
this.props.onMouseUp(e);
|
|
540
|
-
}
|
|
541
|
-
this.setState({pressed: false, focused: false});
|
|
542
|
-
};
|
|
543
|
-
|
|
544
|
-
handleTouchStart: () => void = () => {
|
|
545
|
-
this.setState({pressed: true});
|
|
546
|
-
};
|
|
547
|
-
|
|
548
|
-
handleTouchEnd: () => void = () => {
|
|
549
|
-
this.setState({pressed: false});
|
|
550
|
-
this.waitingForClick = true;
|
|
551
|
-
};
|
|
552
|
-
|
|
553
|
-
handleTouchCancel: () => void = () => {
|
|
554
|
-
this.setState({pressed: false});
|
|
555
|
-
this.waitingForClick = true;
|
|
556
|
-
};
|
|
557
|
-
|
|
558
|
-
handleKeyDown: (e: React.KeyboardEvent) => void = (e) => {
|
|
559
|
-
const {onKeyDown, role} = this.props;
|
|
560
|
-
if (onKeyDown) {
|
|
561
|
-
onKeyDown(e);
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
const keyCode = e.which || e.keyCode;
|
|
565
|
-
const {triggerOnEnter, triggerOnSpace} =
|
|
566
|
-
getAppropriateTriggersForRole(role);
|
|
567
|
-
if (
|
|
568
|
-
(triggerOnEnter && keyCode === keyCodes.enter) ||
|
|
569
|
-
(triggerOnSpace && keyCode === keyCodes.space)
|
|
570
|
-
) {
|
|
571
|
-
// This prevents space from scrolling down. It also prevents the
|
|
572
|
-
// space and enter keys from triggering click events. We manually
|
|
573
|
-
// call the supplied onClick and handle potential navigation in
|
|
574
|
-
// handleKeyUp instead.
|
|
575
|
-
e.preventDefault();
|
|
576
|
-
this.setState({pressed: true});
|
|
577
|
-
} else if (!triggerOnEnter && keyCode === keyCodes.enter) {
|
|
578
|
-
// If the component isn't supposed to trigger on enter, we have to
|
|
579
|
-
// keep track of the enter keydown to negate the onClick callback
|
|
580
|
-
this.enterClick = true;
|
|
581
|
-
}
|
|
582
|
-
};
|
|
583
|
-
|
|
584
|
-
handleKeyUp: (e: React.KeyboardEvent) => void = (e) => {
|
|
585
|
-
const {onKeyUp, role} = this.props;
|
|
586
|
-
if (onKeyUp) {
|
|
587
|
-
onKeyUp(e);
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
const keyCode = e.which || e.keyCode;
|
|
591
|
-
const {triggerOnEnter, triggerOnSpace} =
|
|
592
|
-
getAppropriateTriggersForRole(role);
|
|
593
|
-
if (
|
|
594
|
-
(triggerOnEnter && keyCode === keyCodes.enter) ||
|
|
595
|
-
(triggerOnSpace && keyCode === keyCodes.space)
|
|
596
|
-
) {
|
|
597
|
-
this.setState({pressed: false, focused: true});
|
|
598
|
-
|
|
599
|
-
this.runCallbackAndMaybeNavigate(e);
|
|
600
|
-
} else if (!triggerOnEnter && keyCode === keyCodes.enter) {
|
|
601
|
-
this.enterClick = false;
|
|
602
|
-
}
|
|
603
|
-
};
|
|
604
|
-
|
|
605
|
-
handleFocus: (e: React.FocusEvent) => void = (e) => {
|
|
606
|
-
const {onFocus} = this.props;
|
|
607
|
-
this.setState({focused: true}, () => {
|
|
608
|
-
if (onFocus) {
|
|
609
|
-
onFocus(e);
|
|
610
|
-
}
|
|
611
|
-
});
|
|
612
|
-
};
|
|
613
|
-
|
|
614
|
-
handleBlur: (e: React.FocusEvent) => void = (e) => {
|
|
615
|
-
this.setState({focused: false, pressed: false});
|
|
616
|
-
};
|
|
617
|
-
|
|
618
|
-
render(): React.ReactNode {
|
|
619
|
-
// When the link is set to open in a new window, we want to set some
|
|
620
|
-
// `rel` attributes. This is to ensure that the links we're sending folks
|
|
621
|
-
// to can't hijack the existing page. These defaults can be overriden
|
|
622
|
-
// by passing in a different value for the `rel` prop.
|
|
623
|
-
// More info: https://www.jitbit.com/alexblog/256-targetblank---the-most-underestimated-vulnerability-ever/
|
|
624
|
-
const rel =
|
|
625
|
-
this.props.rel ||
|
|
626
|
-
(this.props.target === "_blank"
|
|
627
|
-
? "noopener noreferrer"
|
|
628
|
-
: undefined);
|
|
629
|
-
|
|
630
|
-
const childrenProps: ChildrenProps = this.props.disabled
|
|
631
|
-
? {
|
|
632
|
-
...disabledHandlers,
|
|
633
|
-
// Keep these handlers for keyboard accessibility.
|
|
634
|
-
onFocus: this.handleFocus,
|
|
635
|
-
onBlur: this.handleBlur,
|
|
636
|
-
tabIndex: this.props.tabIndex,
|
|
637
|
-
rel,
|
|
638
|
-
}
|
|
639
|
-
: {
|
|
640
|
-
onClick: this.handleClick,
|
|
641
|
-
onMouseEnter: this.handleMouseEnter,
|
|
642
|
-
onMouseLeave: this.handleMouseLeave,
|
|
643
|
-
onMouseDown: this.handleMouseDown,
|
|
644
|
-
onMouseUp: this.handleMouseUp,
|
|
645
|
-
onTouchStart: this.handleTouchStart,
|
|
646
|
-
onTouchEnd: this.handleTouchEnd,
|
|
647
|
-
onTouchCancel: this.handleTouchCancel,
|
|
648
|
-
onKeyDown: this.handleKeyDown,
|
|
649
|
-
onKeyUp: this.handleKeyUp,
|
|
650
|
-
onFocus: this.handleFocus,
|
|
651
|
-
onBlur: this.handleBlur,
|
|
652
|
-
tabIndex: this.props.tabIndex,
|
|
653
|
-
rel,
|
|
654
|
-
};
|
|
655
|
-
|
|
656
|
-
const {children} = this.props;
|
|
657
|
-
return children && children(this.state, childrenProps);
|
|
658
|
-
}
|
|
659
|
-
}
|