@khanacademy/wonder-blocks-clickable 2.1.1 → 2.2.1

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/dist/es/index.js CHANGED
@@ -1,215 +1,13 @@
1
+ import _objectWithoutPropertiesLoose from '@babel/runtime/helpers/objectWithoutPropertiesLoose';
2
+ import _extends from '@babel/runtime/helpers/extends';
1
3
  import { Component, createElement } from 'react';
2
4
  import { StyleSheet } from 'aphrodite';
3
- import { any } from 'prop-types';
4
5
  import { withRouter, Link } from 'react-router-dom';
6
+ import { __RouterContext } from 'react-router';
5
7
  import { addStyle } from '@khanacademy/wonder-blocks-core';
6
8
  import Color from '@khanacademy/wonder-blocks-color';
7
9
 
8
- function _classCallCheck(instance, Constructor) {
9
- if (!(instance instanceof Constructor)) {
10
- throw new TypeError("Cannot call a class as a function");
11
- }
12
- }
13
-
14
- function _defineProperties(target, props) {
15
- for (var i = 0; i < props.length; i++) {
16
- var descriptor = props[i];
17
- descriptor.enumerable = descriptor.enumerable || false;
18
- descriptor.configurable = true;
19
- if ("value" in descriptor) descriptor.writable = true;
20
- Object.defineProperty(target, descriptor.key, descriptor);
21
- }
22
- }
23
-
24
- function _createClass(Constructor, protoProps, staticProps) {
25
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
26
- if (staticProps) _defineProperties(Constructor, staticProps);
27
- return Constructor;
28
- }
29
-
30
- function _defineProperty(obj, key, value) {
31
- if (key in obj) {
32
- Object.defineProperty(obj, key, {
33
- value: value,
34
- enumerable: true,
35
- configurable: true,
36
- writable: true
37
- });
38
- } else {
39
- obj[key] = value;
40
- }
41
-
42
- return obj;
43
- }
44
-
45
- function _extends() {
46
- _extends = Object.assign || function (target) {
47
- for (var i = 1; i < arguments.length; i++) {
48
- var source = arguments[i];
49
-
50
- for (var key in source) {
51
- if (Object.prototype.hasOwnProperty.call(source, key)) {
52
- target[key] = source[key];
53
- }
54
- }
55
- }
56
-
57
- return target;
58
- };
59
-
60
- return _extends.apply(this, arguments);
61
- }
62
-
63
- function ownKeys(object, enumerableOnly) {
64
- var keys = Object.keys(object);
65
-
66
- if (Object.getOwnPropertySymbols) {
67
- var symbols = Object.getOwnPropertySymbols(object);
68
- if (enumerableOnly) symbols = symbols.filter(function (sym) {
69
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
70
- });
71
- keys.push.apply(keys, symbols);
72
- }
73
-
74
- return keys;
75
- }
76
-
77
- function _objectSpread2(target) {
78
- for (var i = 1; i < arguments.length; i++) {
79
- var source = arguments[i] != null ? arguments[i] : {};
80
-
81
- if (i % 2) {
82
- ownKeys(Object(source), true).forEach(function (key) {
83
- _defineProperty(target, key, source[key]);
84
- });
85
- } else if (Object.getOwnPropertyDescriptors) {
86
- Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
87
- } else {
88
- ownKeys(Object(source)).forEach(function (key) {
89
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
90
- });
91
- }
92
- }
93
-
94
- return target;
95
- }
96
-
97
- function _inherits(subClass, superClass) {
98
- if (typeof superClass !== "function" && superClass !== null) {
99
- throw new TypeError("Super expression must either be null or a function");
100
- }
101
-
102
- subClass.prototype = Object.create(superClass && superClass.prototype, {
103
- constructor: {
104
- value: subClass,
105
- writable: true,
106
- configurable: true
107
- }
108
- });
109
- if (superClass) _setPrototypeOf(subClass, superClass);
110
- }
111
-
112
- function _getPrototypeOf(o) {
113
- _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
114
- return o.__proto__ || Object.getPrototypeOf(o);
115
- };
116
- return _getPrototypeOf(o);
117
- }
118
-
119
- function _setPrototypeOf(o, p) {
120
- _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
121
- o.__proto__ = p;
122
- return o;
123
- };
124
-
125
- return _setPrototypeOf(o, p);
126
- }
127
-
128
- function _isNativeReflectConstruct() {
129
- if (typeof Reflect === "undefined" || !Reflect.construct) return false;
130
- if (Reflect.construct.sham) return false;
131
- if (typeof Proxy === "function") return true;
132
-
133
- try {
134
- Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
135
- return true;
136
- } catch (e) {
137
- return false;
138
- }
139
- }
140
-
141
- function _objectWithoutPropertiesLoose(source, excluded) {
142
- if (source == null) return {};
143
- var target = {};
144
- var sourceKeys = Object.keys(source);
145
- var key, i;
146
-
147
- for (i = 0; i < sourceKeys.length; i++) {
148
- key = sourceKeys[i];
149
- if (excluded.indexOf(key) >= 0) continue;
150
- target[key] = source[key];
151
- }
152
-
153
- return target;
154
- }
155
-
156
- function _objectWithoutProperties(source, excluded) {
157
- if (source == null) return {};
158
-
159
- var target = _objectWithoutPropertiesLoose(source, excluded);
160
-
161
- var key, i;
162
-
163
- if (Object.getOwnPropertySymbols) {
164
- var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
165
-
166
- for (i = 0; i < sourceSymbolKeys.length; i++) {
167
- key = sourceSymbolKeys[i];
168
- if (excluded.indexOf(key) >= 0) continue;
169
- if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
170
- target[key] = source[key];
171
- }
172
- }
173
-
174
- return target;
175
- }
176
-
177
- function _assertThisInitialized(self) {
178
- if (self === void 0) {
179
- throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
180
- }
181
-
182
- return self;
183
- }
184
-
185
- function _possibleConstructorReturn(self, call) {
186
- if (call && (typeof call === "object" || typeof call === "function")) {
187
- return call;
188
- }
189
-
190
- return _assertThisInitialized(self);
191
- }
192
-
193
- function _createSuper(Derived) {
194
- var hasNativeReflectConstruct = _isNativeReflectConstruct();
195
-
196
- return function _createSuperInternal() {
197
- var Super = _getPrototypeOf(Derived),
198
- result;
199
-
200
- if (hasNativeReflectConstruct) {
201
- var NewTarget = _getPrototypeOf(this).constructor;
202
-
203
- result = Reflect.construct(Super, arguments, NewTarget);
204
- } else {
205
- result = Super.apply(this, arguments);
206
- }
207
-
208
- return _possibleConstructorReturn(this, result);
209
- };
210
- }
211
-
212
- var getAppropriateTriggersForRole = function getAppropriateTriggersForRole(role) {
10
+ const getAppropriateTriggersForRole = role => {
213
11
  switch (role) {
214
12
  // Triggers on ENTER, but not SPACE
215
13
  case "link":
@@ -240,53 +38,27 @@ var getAppropriateTriggersForRole = function getAppropriateTriggersForRole(role)
240
38
  }
241
39
  };
242
40
 
243
- var disabledHandlers = {
244
- onClick: function onClick() {
245
- return void 0;
246
- },
247
- onMouseEnter: function onMouseEnter() {
248
- return void 0;
249
- },
250
- onMouseLeave: function onMouseLeave() {
251
- return void 0;
252
- },
253
- onMouseDown: function onMouseDown() {
254
- return void 0;
255
- },
256
- onMouseUp: function onMouseUp() {
257
- return void 0;
258
- },
259
- onDragStart: function onDragStart() {
260
- return void 0;
261
- },
262
- onTouchStart: function onTouchStart() {
263
- return void 0;
264
- },
265
- onTouchEnd: function onTouchEnd() {
266
- return void 0;
267
- },
268
- onTouchCancel: function onTouchCancel() {
269
- return void 0;
270
- },
271
- onKeyDown: function onKeyDown() {
272
- return void 0;
273
- },
274
- onKeyUp: function onKeyUp() {
275
- return void 0;
276
- },
277
- onFocus: function onFocus() {
278
- return void 0;
279
- },
280
- onBlur: function onBlur() {
281
- return void 0;
282
- },
41
+ const disabledHandlers = {
42
+ onClick: () => void 0,
43
+ onMouseEnter: () => void 0,
44
+ onMouseLeave: () => void 0,
45
+ onMouseDown: () => void 0,
46
+ onMouseUp: () => void 0,
47
+ onDragStart: () => void 0,
48
+ onTouchStart: () => void 0,
49
+ onTouchEnd: () => void 0,
50
+ onTouchCancel: () => void 0,
51
+ onKeyDown: () => void 0,
52
+ onKeyUp: () => void 0,
53
+ onFocus: () => void 0,
54
+ onBlur: () => void 0,
283
55
  tabIndex: -1
284
56
  };
285
- var keyCodes = {
57
+ const keyCodes = {
286
58
  enter: 13,
287
59
  space: 32
288
60
  };
289
- var startState = {
61
+ const startState = {
290
62
  hovered: false,
291
63
  focused: false,
292
64
  pressed: false,
@@ -366,149 +138,126 @@ var startState = {
366
138
  * The react-router aware version is returned if `router` is a react-router-dom
367
139
  * router, `skipClientNav` is not `true`, and `href` is an internal URL.
368
140
  *
369
- * The `router` can be accessed via this.context.router from a component
370
- * rendered as a descendant of a BrowserRouter.
141
+ * The `router` can be accessed via __RouterContext (imported from 'react-router')
142
+ * from a component rendered as a descendant of a BrowserRouter.
371
143
  * See https://reacttraining.com/react-router/web/guides/basic-components.
372
144
  */
373
145
 
374
- var ClickableBehavior = /*#__PURE__*/function (_React$Component) {
375
- _inherits(ClickableBehavior, _React$Component);
376
-
377
- var _super = _createSuper(ClickableBehavior);
378
-
379
- _createClass(ClickableBehavior, null, [{
380
- key: "getDerivedStateFromProps",
381
- value: function getDerivedStateFromProps(props, state) {
382
- // If new props are disabled, reset the hovered/focused/pressed states
383
- if (props.disabled) {
384
- return startState;
385
- } else {
386
- // Cannot return undefined
387
- return null;
388
- }
146
+ class ClickableBehavior extends Component {
147
+ static getDerivedStateFromProps(props, state) {
148
+ // If new props are disabled, reset the hovered/focused/pressed states
149
+ if (props.disabled) {
150
+ return startState;
151
+ } else {
152
+ // Cannot return undefined
153
+ return null;
389
154
  }
390
- }]);
391
-
392
- function ClickableBehavior(props) {
393
- var _this;
394
-
395
- _classCallCheck(this, ClickableBehavior);
396
-
397
- _this = _super.call(this, props);
398
-
399
- _defineProperty(_assertThisInitialized(_this), "waitingForClick", void 0);
400
-
401
- _defineProperty(_assertThisInitialized(_this), "enterClick", void 0);
155
+ }
402
156
 
403
- _defineProperty(_assertThisInitialized(_this), "dragging", void 0);
157
+ constructor(props) {
158
+ super(props);
404
159
 
405
- _defineProperty(_assertThisInitialized(_this), "handleClick", function (e) {
406
- var _this$props = _this.props,
407
- _this$props$onClick = _this$props.onClick,
408
- onClick = _this$props$onClick === void 0 ? undefined : _this$props$onClick,
409
- _this$props$beforeNav = _this$props.beforeNav,
410
- beforeNav = _this$props$beforeNav === void 0 ? undefined : _this$props$beforeNav,
411
- _this$props$safeWithN = _this$props.safeWithNav,
412
- safeWithNav = _this$props$safeWithN === void 0 ? undefined : _this$props$safeWithN;
160
+ this.handleClick = e => {
161
+ const {
162
+ onClick = undefined,
163
+ beforeNav = undefined,
164
+ safeWithNav = undefined
165
+ } = this.props;
413
166
 
414
- if (_this.enterClick) {
167
+ if (this.enterClick) {
415
168
  return;
416
169
  }
417
170
 
418
171
  if (onClick || beforeNav || safeWithNav) {
419
- _this.waitingForClick = false;
172
+ this.waitingForClick = false;
420
173
  }
421
174
 
422
- _this.runCallbackAndMaybeNavigate(e);
423
- });
175
+ this.runCallbackAndMaybeNavigate(e);
176
+ };
424
177
 
425
- _defineProperty(_assertThisInitialized(_this), "handleMouseEnter", function (e) {
178
+ this.handleMouseEnter = e => {
426
179
  // When the left button is pressed already, we want it to be pressed
427
180
  if (e.buttons === 1) {
428
- _this.dragging = true;
429
-
430
- _this.setState({
181
+ this.dragging = true;
182
+ this.setState({
431
183
  pressed: true
432
184
  });
433
- } else if (!_this.waitingForClick) {
434
- _this.setState({
185
+ } else if (!this.waitingForClick) {
186
+ this.setState({
435
187
  hovered: true
436
188
  });
437
189
  }
438
- });
439
-
440
- _defineProperty(_assertThisInitialized(_this), "handleMouseLeave", function () {
441
- if (!_this.waitingForClick) {
442
- _this.dragging = false;
190
+ };
443
191
 
444
- _this.setState({
192
+ this.handleMouseLeave = () => {
193
+ if (!this.waitingForClick) {
194
+ this.dragging = false;
195
+ this.setState({
445
196
  hovered: false,
446
197
  pressed: false,
447
198
  focused: false
448
199
  });
449
200
  }
450
- });
201
+ };
451
202
 
452
- _defineProperty(_assertThisInitialized(_this), "handleMouseDown", function () {
453
- _this.setState({
203
+ this.handleMouseDown = () => {
204
+ this.setState({
454
205
  pressed: true
455
206
  });
456
- });
457
-
458
- _defineProperty(_assertThisInitialized(_this), "handleMouseUp", function (e) {
459
- if (_this.dragging) {
460
- _this.dragging = false;
207
+ };
461
208
 
462
- _this.handleClick(e);
209
+ this.handleMouseUp = e => {
210
+ if (this.dragging) {
211
+ this.dragging = false;
212
+ this.handleClick(e);
463
213
  }
464
214
 
465
- _this.setState({
215
+ this.setState({
466
216
  pressed: false,
467
217
  focused: false
468
218
  });
469
- });
219
+ };
470
220
 
471
- _defineProperty(_assertThisInitialized(_this), "handleDragStart", function (e) {
472
- _this.dragging = true;
221
+ this.handleDragStart = e => {
222
+ this.dragging = true;
473
223
  e.preventDefault();
474
- });
224
+ };
475
225
 
476
- _defineProperty(_assertThisInitialized(_this), "handleTouchStart", function () {
477
- _this.setState({
226
+ this.handleTouchStart = () => {
227
+ this.setState({
478
228
  pressed: true
479
229
  });
480
- });
230
+ };
481
231
 
482
- _defineProperty(_assertThisInitialized(_this), "handleTouchEnd", function () {
483
- _this.setState({
232
+ this.handleTouchEnd = () => {
233
+ this.setState({
484
234
  pressed: false
485
235
  });
236
+ this.waitingForClick = true;
237
+ };
486
238
 
487
- _this.waitingForClick = true;
488
- });
489
-
490
- _defineProperty(_assertThisInitialized(_this), "handleTouchCancel", function () {
491
- _this.setState({
239
+ this.handleTouchCancel = () => {
240
+ this.setState({
492
241
  pressed: false
493
242
  });
243
+ this.waitingForClick = true;
244
+ };
494
245
 
495
- _this.waitingForClick = true;
496
- });
497
-
498
- _defineProperty(_assertThisInitialized(_this), "handleKeyDown", function (e) {
499
- var _this$props2 = _this.props,
500
- onKeyDown = _this$props2.onKeyDown,
501
- role = _this$props2.role;
246
+ this.handleKeyDown = e => {
247
+ const {
248
+ onKeyDown,
249
+ role
250
+ } = this.props;
502
251
 
503
252
  if (onKeyDown) {
504
253
  onKeyDown(e);
505
254
  }
506
255
 
507
- var keyCode = e.which || e.keyCode;
508
-
509
- var _getAppropriateTrigge = getAppropriateTriggersForRole(role),
510
- triggerOnEnter = _getAppropriateTrigge.triggerOnEnter,
511
- triggerOnSpace = _getAppropriateTrigge.triggerOnSpace;
256
+ const keyCode = e.which || e.keyCode;
257
+ const {
258
+ triggerOnEnter,
259
+ triggerOnSpace
260
+ } = getAppropriateTriggersForRole(role);
512
261
 
513
262
  if (triggerOnEnter && keyCode === keyCodes.enter || triggerOnSpace && keyCode === keyCodes.space) {
514
263
  // This prevents space from scrolling down. It also prevents the
@@ -516,112 +265,116 @@ var ClickableBehavior = /*#__PURE__*/function (_React$Component) {
516
265
  // call the supplied onClick and handle potential navigation in
517
266
  // handleKeyUp instead.
518
267
  e.preventDefault();
519
-
520
- _this.setState({
268
+ this.setState({
521
269
  pressed: true
522
270
  });
523
271
  } else if (!triggerOnEnter && keyCode === keyCodes.enter) {
524
272
  // If the component isn't supposed to trigger on enter, we have to
525
273
  // keep track of the enter keydown to negate the onClick callback
526
- _this.enterClick = true;
274
+ this.enterClick = true;
527
275
  }
528
- });
276
+ };
529
277
 
530
- _defineProperty(_assertThisInitialized(_this), "handleKeyUp", function (e) {
531
- var _this$props3 = _this.props,
532
- onKeyUp = _this$props3.onKeyUp,
533
- role = _this$props3.role;
278
+ this.handleKeyUp = e => {
279
+ const {
280
+ onKeyUp,
281
+ role
282
+ } = this.props;
534
283
 
535
284
  if (onKeyUp) {
536
285
  onKeyUp(e);
537
286
  }
538
287
 
539
- var keyCode = e.which || e.keyCode;
540
-
541
- var _getAppropriateTrigge2 = getAppropriateTriggersForRole(role),
542
- triggerOnEnter = _getAppropriateTrigge2.triggerOnEnter,
543
- triggerOnSpace = _getAppropriateTrigge2.triggerOnSpace;
288
+ const keyCode = e.which || e.keyCode;
289
+ const {
290
+ triggerOnEnter,
291
+ triggerOnSpace
292
+ } = getAppropriateTriggersForRole(role);
544
293
 
545
294
  if (triggerOnEnter && keyCode === keyCodes.enter || triggerOnSpace && keyCode === keyCodes.space) {
546
- _this.setState({
295
+ this.setState({
547
296
  pressed: false,
548
297
  focused: true
549
298
  });
550
-
551
- _this.runCallbackAndMaybeNavigate(e);
299
+ this.runCallbackAndMaybeNavigate(e);
552
300
  } else if (!triggerOnEnter && keyCode === keyCodes.enter) {
553
- _this.enterClick = false;
301
+ this.enterClick = false;
554
302
  }
555
- });
303
+ };
556
304
 
557
- _defineProperty(_assertThisInitialized(_this), "handleFocus", function (e) {
558
- _this.setState({
305
+ this.handleFocus = e => {
306
+ this.setState({
559
307
  focused: true
560
308
  });
561
- });
309
+ };
562
310
 
563
- _defineProperty(_assertThisInitialized(_this), "handleBlur", function (e) {
564
- _this.setState({
311
+ this.handleBlur = e => {
312
+ this.setState({
565
313
  focused: false,
566
314
  pressed: false
567
315
  });
568
- });
316
+ };
569
317
 
570
- _this.state = startState;
571
- _this.waitingForClick = false;
572
- _this.enterClick = false;
573
- _this.dragging = false;
574
- return _this;
318
+ this.state = startState;
319
+ this.waitingForClick = false;
320
+ this.enterClick = false;
321
+ this.dragging = false;
575
322
  }
576
323
 
577
- _createClass(ClickableBehavior, [{
578
- key: "navigateOrReset",
579
- value: function navigateOrReset(shouldNavigate) {
580
- if (shouldNavigate) {
581
- var _this$props4 = this.props,
582
- history = _this$props4.history,
583
- href = _this$props4.href,
584
- skipClientNav = _this$props4.skipClientNav,
585
- _this$props4$target = _this$props4.target,
586
- target = _this$props4$target === void 0 ? undefined : _this$props4$target;
587
-
588
- if (href) {
589
- if (target === "_blank") {
590
- window.open(href, "_blank");
591
- this.setState({
592
- waiting: false
593
- });
594
- } else if (history && !skipClientNav) {
595
- history.push(href);
596
- this.setState({
597
- waiting: false
598
- });
599
- } else {
600
- window.location.assign(href); // We don't bother clearing the waiting state, the full page
601
- // load navigation will do that for us by loading a new page.
602
- }
324
+ navigateOrReset(shouldNavigate) {
325
+ if (shouldNavigate) {
326
+ const {
327
+ history,
328
+ href,
329
+ skipClientNav,
330
+ target = undefined
331
+ } = this.props;
332
+
333
+ if (href) {
334
+ if (target === "_blank") {
335
+ window.open(href, "_blank");
336
+ this.setState({
337
+ waiting: false
338
+ });
339
+ } else if (history && !skipClientNav) {
340
+ history.push(href);
341
+ this.setState({
342
+ waiting: false
343
+ });
344
+ } else {
345
+ window.location.assign(href); // We don't bother clearing the waiting state, the full page
346
+ // load navigation will do that for us by loading a new page.
603
347
  }
604
- } else {
348
+ }
349
+ } else {
350
+ this.setState({
351
+ waiting: false
352
+ });
353
+ }
354
+ }
355
+
356
+ handleSafeWithNav(safeWithNav, shouldNavigate) {
357
+ const {
358
+ skipClientNav,
359
+ history
360
+ } = this.props;
361
+
362
+ if (history && !skipClientNav || this.props.target === "_blank") {
363
+ // client-side nav
364
+ safeWithNav();
365
+ this.navigateOrReset(shouldNavigate);
366
+ return Promise.resolve();
367
+ } else {
368
+ if (!this.state.waiting) {
369
+ // We only show the spinner for safeWithNav when doing
370
+ // a full page load navigation since since the spinner is
371
+ // indicating that we're waiting for navigation to occur.
605
372
  this.setState({
606
- waiting: false
373
+ waiting: true
607
374
  });
608
375
  }
609
- }
610
- }, {
611
- key: "handleSafeWithNav",
612
- value: function handleSafeWithNav(safeWithNav, shouldNavigate) {
613
- var _this2 = this;
614
-
615
- var _this$props5 = this.props,
616
- skipClientNav = _this$props5.skipClientNav,
617
- history = _this$props5.history;
618
-
619
- if (history && !skipClientNav || this.props.target === "_blank") {
620
- // client-side nav
621
- safeWithNav();
622
- this.navigateOrReset(shouldNavigate);
623
- return Promise.resolve();
624
- } else {
376
+
377
+ return safeWithNav().then(() => {
625
378
  if (!this.state.waiting) {
626
379
  // We only show the spinner for safeWithNav when doing
627
380
  // a full page load navigation since since the spinner is
@@ -631,134 +384,117 @@ var ClickableBehavior = /*#__PURE__*/function (_React$Component) {
631
384
  });
632
385
  }
633
386
 
634
- return safeWithNav().then(function () {
635
- if (!_this2.state.waiting) {
636
- // We only show the spinner for safeWithNav when doing
637
- // a full page load navigation since since the spinner is
638
- // indicating that we're waiting for navigation to occur.
639
- _this2.setState({
640
- waiting: true
641
- });
642
- }
643
-
644
- return;
645
- }).catch(function (error) {// We ignore the error here so that we always
646
- // navigate when using safeWithNav regardless of
647
- // whether we're doing a client-side nav or not.
648
- }).finally(function () {
649
- _this2.navigateOrReset(shouldNavigate);
650
- });
651
- }
387
+ return;
388
+ }).catch(error => {// We ignore the error here so that we always
389
+ // navigate when using safeWithNav regardless of
390
+ // whether we're doing a client-side nav or not.
391
+ }).finally(() => {
392
+ this.navigateOrReset(shouldNavigate);
393
+ });
652
394
  }
653
- }, {
654
- key: "runCallbackAndMaybeNavigate",
655
- value: function runCallbackAndMaybeNavigate(e) {
656
- var _this3 = this;
657
-
658
- var _this$props6 = this.props,
659
- _this$props6$onClick = _this$props6.onClick,
660
- onClick = _this$props6$onClick === void 0 ? undefined : _this$props6$onClick,
661
- _this$props6$beforeNa = _this$props6.beforeNav,
662
- beforeNav = _this$props6$beforeNa === void 0 ? undefined : _this$props6$beforeNa,
663
- _this$props6$safeWith = _this$props6.safeWithNav,
664
- safeWithNav = _this$props6$safeWith === void 0 ? undefined : _this$props6$safeWith,
665
- href = _this$props6.href,
666
- type = _this$props6.type;
667
- var shouldNavigate = true;
668
- var canSubmit = true;
669
-
670
- if (onClick) {
671
- onClick(e);
672
- } // If onClick() has called e.preventDefault() then we shouldn't
673
- // navigate.
674
-
675
-
676
- if (e.defaultPrevented) {
677
- shouldNavigate = false;
678
- canSubmit = false;
679
- }
395
+ }
680
396
 
681
- e.preventDefault();
397
+ runCallbackAndMaybeNavigate(e) {
398
+ const {
399
+ onClick = undefined,
400
+ beforeNav = undefined,
401
+ safeWithNav = undefined,
402
+ href,
403
+ type
404
+ } = this.props;
405
+ let shouldNavigate = true;
406
+ let canSubmit = true;
407
+
408
+ if (onClick) {
409
+ onClick(e);
410
+ } // If onClick() has called e.preventDefault() then we shouldn't
411
+ // navigate.
412
+
413
+
414
+ if (e.defaultPrevented) {
415
+ shouldNavigate = false;
416
+ canSubmit = false;
417
+ }
682
418
 
683
- if (!href && type === "submit" && canSubmit) {
684
- var target = e.currentTarget;
685
-
686
- while (target) {
687
- if (target instanceof window.HTMLFormElement) {
688
- // This event must be marked as cancelable otherwise calling
689
- // e.preventDefault() on it won't do anything in Firefox.
690
- // Chrome and Safari allow calling e.preventDefault() on
691
- // non-cancelable events, but really they shouldn't.
692
- var event = new window.Event("submit", {
693
- cancelable: true
694
- });
695
- target.dispatchEvent(event);
696
- break;
697
- } // All events should be typed as SyntheticEvent<HTMLElement>.
698
- // Updating all of the places will take some time so I'll do
699
- // this later
700
- // $FlowFixMe[prop-missing]
701
-
702
-
703
- target = target.parentElement;
704
- }
705
- }
419
+ e.preventDefault();
706
420
 
707
- if (beforeNav) {
708
- this.setState({
709
- waiting: true
710
- });
711
- beforeNav().then(function () {
712
- if (safeWithNav) {
713
- return _this3.handleSafeWithNav(safeWithNav, shouldNavigate);
714
- } else {
715
- return _this3.navigateOrReset(shouldNavigate);
716
- }
717
- }).catch(function () {});
718
- } else if (safeWithNav) {
719
- return this.handleSafeWithNav(safeWithNav, shouldNavigate);
720
- } else {
721
- this.navigateOrReset(shouldNavigate);
421
+ if (!href && type === "submit" && canSubmit) {
422
+ let target = e.currentTarget;
423
+
424
+ while (target) {
425
+ if (target instanceof window.HTMLFormElement) {
426
+ // This event must be marked as cancelable otherwise calling
427
+ // e.preventDefault() on it won't do anything in Firefox.
428
+ // Chrome and Safari allow calling e.preventDefault() on
429
+ // non-cancelable events, but really they shouldn't.
430
+ const event = new window.Event("submit", {
431
+ cancelable: true
432
+ });
433
+ target.dispatchEvent(event);
434
+ break;
435
+ } // All events should be typed as SyntheticEvent<HTMLElement>.
436
+ // Updating all of the places will take some time so I'll do
437
+ // this later
438
+ // $FlowFixMe[prop-missing]
439
+
440
+
441
+ target = target.parentElement;
722
442
  }
723
443
  }
724
- }, {
725
- key: "render",
726
- value: function render() {
727
- var childrenProps = this.props.disabled ? disabledHandlers : {
728
- onClick: this.handleClick,
729
- onMouseEnter: this.handleMouseEnter,
730
- onMouseLeave: this.handleMouseLeave,
731
- onMouseDown: this.handleMouseDown,
732
- onMouseUp: this.handleMouseUp,
733
- onDragStart: this.handleDragStart,
734
- onTouchStart: this.handleTouchStart,
735
- onTouchEnd: this.handleTouchEnd,
736
- onTouchCancel: this.handleTouchCancel,
737
- onKeyDown: this.handleKeyDown,
738
- onKeyUp: this.handleKeyUp,
739
- onFocus: this.handleFocus,
740
- onBlur: this.handleBlur,
741
- // We set tabIndex to 0 so that users can tab to clickable
742
- // things that aren't buttons or anchors.
743
- tabIndex: 0
744
- }; // When the link is set to open in a new window, we want to set some
745
- // `rel` attributes. This is to ensure that the links we're sending folks
746
- // to can't hijack the existing page. These defaults can be overriden
747
- // by passing in a different value for the `rel` prop.
748
- // More info: https://www.jitbit.com/alexblog/256-targetblank---the-most-underestimated-vulnerability-ever/
749
-
750
- childrenProps.rel = this.props.rel || (this.props.target === "_blank" ? "noopener noreferrer" : undefined);
751
- var children = this.props.children;
752
- return children && children(this.state, childrenProps);
444
+
445
+ if (beforeNav) {
446
+ this.setState({
447
+ waiting: true
448
+ });
449
+ beforeNav().then(() => {
450
+ if (safeWithNav) {
451
+ return this.handleSafeWithNav(safeWithNav, shouldNavigate);
452
+ } else {
453
+ return this.navigateOrReset(shouldNavigate);
454
+ }
455
+ }).catch(() => {});
456
+ } else if (safeWithNav) {
457
+ return this.handleSafeWithNav(safeWithNav, shouldNavigate);
458
+ } else {
459
+ this.navigateOrReset(shouldNavigate);
753
460
  }
754
- }]);
461
+ }
755
462
 
756
- return ClickableBehavior;
757
- }(Component);
463
+ render() {
464
+ const childrenProps = this.props.disabled ? disabledHandlers : {
465
+ onClick: this.handleClick,
466
+ onMouseEnter: this.handleMouseEnter,
467
+ onMouseLeave: this.handleMouseLeave,
468
+ onMouseDown: this.handleMouseDown,
469
+ onMouseUp: this.handleMouseUp,
470
+ onDragStart: this.handleDragStart,
471
+ onTouchStart: this.handleTouchStart,
472
+ onTouchEnd: this.handleTouchEnd,
473
+ onTouchCancel: this.handleTouchCancel,
474
+ onKeyDown: this.handleKeyDown,
475
+ onKeyUp: this.handleKeyUp,
476
+ onFocus: this.handleFocus,
477
+ onBlur: this.handleBlur,
478
+ // We set tabIndex to 0 so that users can tab to clickable
479
+ // things that aren't buttons or anchors.
480
+ tabIndex: 0
481
+ }; // When the link is set to open in a new window, we want to set some
482
+ // `rel` attributes. This is to ensure that the links we're sending folks
483
+ // to can't hijack the existing page. These defaults can be overriden
484
+ // by passing in a different value for the `rel` prop.
485
+ // More info: https://www.jitbit.com/alexblog/256-targetblank---the-most-underestimated-vulnerability-ever/
486
+
487
+ childrenProps.rel = this.props.rel || (this.props.target === "_blank" ? "noopener noreferrer" : undefined);
488
+ const {
489
+ children
490
+ } = this.props;
491
+ return children && children(this.state, childrenProps);
492
+ }
758
493
 
759
- _defineProperty(ClickableBehavior, "defaultProps", {
494
+ }
495
+ ClickableBehavior.defaultProps = {
760
496
  disabled: false
761
- });
497
+ };
762
498
 
763
499
  /**
764
500
  * Returns:
@@ -766,7 +502,7 @@ _defineProperty(ClickableBehavior, "defaultProps", {
766
502
  * - false for '#', 'javascript:...', 'mailto:...', 'tel:...', etc.
767
503
  * - true for all other values, e.g. /foo/bar
768
504
  */
769
- var isClientSideUrl = function isClientSideUrl(href) {
505
+ const isClientSideUrl = href => {
770
506
  if (typeof href !== "string") {
771
507
  return false;
772
508
  }
@@ -780,11 +516,11 @@ var isClientSideUrl = function isClientSideUrl(href) {
780
516
  * The react-router aware version is returned if `router` is a react-router-dom
781
517
  * router, `skipClientNav` is not `true`, and `href` is an internal URL.
782
518
  *
783
- * The `router` can be accessed via this.context.router from a component rendered
784
- * as a descendant of a BrowserRouter.
519
+ * The `router` can be accessed via __RouterContext (imported from 'react-router')
520
+ * from a component rendered as a descendant of a BrowserRouter.
785
521
  * See https://reacttraining.com/react-router/web/guides/basic-components.
786
522
  */
787
- var ClickableBehaviorWithRouter = withRouter(ClickableBehavior);
523
+ const ClickableBehaviorWithRouter = withRouter(ClickableBehavior);
788
524
  function getClickableBehavior(
789
525
  /**
790
526
  * The URL to navigate to.
@@ -809,9 +545,10 @@ router) {
809
545
  return ClickableBehavior;
810
546
  }
811
547
 
812
- var StyledAnchor = addStyle("a");
813
- var StyledButton = addStyle("button");
814
- var StyledLink = addStyle(Link);
548
+ const _excluded = ["href", "onClick", "skipClientNav", "beforeNav", "safeWithNav", "style", "target", "testId", "onKeyDown", "onKeyUp", "hideDefaultFocusRing", "light", "disabled"];
549
+ const StyledAnchor = addStyle("a");
550
+ const StyledButton = addStyle("button");
551
+ const StyledLink = addStyle(Link);
815
552
  /**
816
553
  * A component to turn any custom component into a clickable one.
817
554
  *
@@ -837,130 +574,102 @@ var StyledLink = addStyle(Link);
837
574
  * ```
838
575
  */
839
576
 
840
- var Clickable = /*#__PURE__*/function (_React$Component) {
841
- _inherits(Clickable, _React$Component);
842
-
843
- var _super = _createSuper(Clickable);
844
-
845
- function Clickable() {
846
- var _this;
847
-
848
- _classCallCheck(this, Clickable);
577
+ class Clickable extends Component {
578
+ constructor(...args) {
579
+ super(...args);
849
580
 
850
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
851
- args[_key] = arguments[_key];
852
- }
853
-
854
- _this = _super.call.apply(_super, [this].concat(args));
855
-
856
- _defineProperty(_assertThisInitialized(_this), "getCorrectTag", function (clickableState, commonProps) {
857
- var activeHref = _this.props.href && !_this.props.disabled;
858
- var useClient = _this.context.router && !_this.props.skipClientNav && isClientSideUrl(_this.props.href || ""); // NOTE: checking this.props.href here is redundant, but flow
581
+ this.getCorrectTag = (clickableState, router, commonProps) => {
582
+ const activeHref = this.props.href && !this.props.disabled;
583
+ const useClient = router && !this.props.skipClientNav && isClientSideUrl(this.props.href || ""); // NOTE: checking this.props.href here is redundant, but flow
859
584
  // needs it to refine this.props.href to a string.
860
585
 
861
- if (activeHref && useClient && _this.props.href) {
586
+ if (activeHref && useClient && this.props.href) {
862
587
  return /*#__PURE__*/createElement(StyledLink, _extends({}, commonProps, {
863
- to: _this.props.href,
864
- role: _this.props.role,
865
- target: _this.props.target || undefined,
866
- "aria-disabled": _this.props.disabled ? "true" : undefined
867
- }), _this.props.children(clickableState));
588
+ to: this.props.href,
589
+ role: this.props.role,
590
+ target: this.props.target || undefined,
591
+ "aria-disabled": this.props.disabled ? "true" : undefined
592
+ }), this.props.children(clickableState));
868
593
  } else if (activeHref && !useClient) {
869
594
  return /*#__PURE__*/createElement(StyledAnchor, _extends({}, commonProps, {
870
- href: _this.props.href,
871
- role: _this.props.role,
872
- target: _this.props.target || undefined,
873
- "aria-disabled": _this.props.disabled ? "true" : undefined
874
- }), _this.props.children(clickableState));
595
+ href: this.props.href,
596
+ role: this.props.role,
597
+ target: this.props.target || undefined,
598
+ "aria-disabled": this.props.disabled ? "true" : undefined
599
+ }), this.props.children(clickableState));
875
600
  } else {
876
601
  return /*#__PURE__*/createElement(StyledButton, _extends({}, commonProps, {
877
602
  type: "button",
878
- disabled: _this.props.disabled
879
- }), _this.props.children(clickableState));
603
+ disabled: this.props.disabled
604
+ }), this.props.children(clickableState));
880
605
  }
881
- });
882
-
883
- return _this;
606
+ };
884
607
  }
885
608
 
886
- _createClass(Clickable, [{
887
- key: "render",
888
- value: function render() {
889
- var _this2 = this;
890
-
891
- var _this$props = this.props,
892
- href = _this$props.href,
893
- onClick = _this$props.onClick,
894
- skipClientNav = _this$props.skipClientNav,
895
- _this$props$beforeNav = _this$props.beforeNav,
896
- beforeNav = _this$props$beforeNav === void 0 ? undefined : _this$props$beforeNav,
897
- _this$props$safeWithN = _this$props.safeWithNav,
898
- safeWithNav = _this$props$safeWithN === void 0 ? undefined : _this$props$safeWithN,
899
- style = _this$props.style,
900
- _this$props$target = _this$props.target,
901
- target = _this$props$target === void 0 ? undefined : _this$props$target,
902
- testId = _this$props.testId,
903
- onKeyDown = _this$props.onKeyDown,
904
- onKeyUp = _this$props.onKeyUp,
905
- hideDefaultFocusRing = _this$props.hideDefaultFocusRing,
906
- light = _this$props.light,
907
- disabled = _this$props.disabled,
908
- restProps = _objectWithoutProperties(_this$props, ["href", "onClick", "skipClientNav", "beforeNav", "safeWithNav", "style", "target", "testId", "onKeyDown", "onKeyUp", "hideDefaultFocusRing", "light", "disabled"]);
909
-
910
- var ClickableBehavior = getClickableBehavior(href, skipClientNav, this.context.router);
911
-
912
- var getStyle = function getStyle(state) {
913
- return [styles.reset, styles.link, !hideDefaultFocusRing && state.focused && (light ? styles.focusedLight : styles.focused), style];
914
- };
915
-
916
- if (beforeNav) {
917
- return /*#__PURE__*/createElement(ClickableBehavior, {
918
- href: href,
919
- onClick: onClick,
920
- beforeNav: beforeNav,
921
- safeWithNav: safeWithNav,
922
- onKeyDown: onKeyDown,
923
- onKeyUp: onKeyUp,
924
- disabled: disabled
925
- }, function (state, childrenProps) {
926
- return _this2.getCorrectTag(state, _objectSpread2(_objectSpread2({}, restProps), {}, {
927
- "data-test-id": testId,
928
- style: getStyle(state)
929
- }, childrenProps));
930
- });
931
- } else {
932
- return /*#__PURE__*/createElement(ClickableBehavior, {
933
- href: href,
934
- onClick: onClick,
935
- safeWithNav: safeWithNav,
936
- onKeyDown: onKeyDown,
937
- onKeyUp: onKeyUp,
938
- target: target,
939
- disabled: disabled
940
- }, function (state, childrenProps) {
941
- return _this2.getCorrectTag(state, _objectSpread2(_objectSpread2({}, restProps), {}, {
942
- "data-test-id": testId,
943
- style: getStyle(state)
944
- }, childrenProps));
945
- });
946
- }
609
+ renderClickableBehavior(router) {
610
+ const _this$props = this.props,
611
+ {
612
+ href,
613
+ onClick,
614
+ skipClientNav,
615
+ beforeNav = undefined,
616
+ safeWithNav = undefined,
617
+ style,
618
+ target = undefined,
619
+ testId,
620
+ onKeyDown,
621
+ onKeyUp,
622
+ hideDefaultFocusRing,
623
+ light,
624
+ disabled
625
+ } = _this$props,
626
+ restProps = _objectWithoutPropertiesLoose(_this$props, _excluded);
627
+
628
+ const ClickableBehavior = getClickableBehavior(href, skipClientNav, router);
629
+
630
+ const getStyle = state => [styles.reset, styles.link, !hideDefaultFocusRing && state.focused && (light ? styles.focusedLight : styles.focused), style];
631
+
632
+ if (beforeNav) {
633
+ return /*#__PURE__*/createElement(ClickableBehavior, {
634
+ href: href,
635
+ onClick: onClick,
636
+ beforeNav: beforeNav,
637
+ safeWithNav: safeWithNav,
638
+ onKeyDown: onKeyDown,
639
+ onKeyUp: onKeyUp,
640
+ disabled: disabled
641
+ }, (state, childrenProps) => this.getCorrectTag(state, router, _extends({}, restProps, {
642
+ "data-test-id": testId,
643
+ style: getStyle(state)
644
+ }, childrenProps)));
645
+ } else {
646
+ return /*#__PURE__*/createElement(ClickableBehavior, {
647
+ href: href,
648
+ onClick: onClick,
649
+ safeWithNav: safeWithNav,
650
+ onKeyDown: onKeyDown,
651
+ onKeyUp: onKeyUp,
652
+ target: target,
653
+ disabled: disabled
654
+ }, (state, childrenProps) => this.getCorrectTag(state, router, _extends({}, restProps, {
655
+ "data-test-id": testId,
656
+ style: getStyle(state)
657
+ }, childrenProps)));
947
658
  }
948
- }]);
949
-
950
- return Clickable;
951
- }(Component); // Source: https://gist.github.com/MoOx/9137295
659
+ }
952
660
 
661
+ render() {
662
+ return /*#__PURE__*/createElement(__RouterContext.Consumer, null, router => this.renderClickableBehavior(router));
663
+ }
953
664
 
954
- _defineProperty(Clickable, "contextTypes", {
955
- router: any
956
- });
665
+ } // Source: https://gist.github.com/MoOx/9137295
957
666
 
958
- _defineProperty(Clickable, "defaultProps", {
667
+ Clickable.defaultProps = {
959
668
  light: false,
960
669
  disabled: false,
961
670
  "aria-label": ""
962
- });
963
- var styles = StyleSheet.create({
671
+ };
672
+ const styles = StyleSheet.create({
964
673
  reset: {
965
674
  border: "none",
966
675
  margin: 0,
@@ -993,10 +702,10 @@ var styles = StyleSheet.create({
993
702
  cursor: "pointer"
994
703
  },
995
704
  focused: {
996
- outline: "solid 2px ".concat(Color.blue)
705
+ outline: `solid 2px ${Color.blue}`
997
706
  },
998
707
  focusedLight: {
999
- outline: "solid 2px ".concat(Color.white)
708
+ outline: `solid 2px ${Color.white}`
1000
709
  }
1001
710
  });
1002
711