@khanacademy/wonder-blocks-modal 2.1.44 → 2.1.45
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 +5 -2
- package/dist/index.js +27 -16
- package/package.json +13 -13
- package/src/components/__tests__/modal-backdrop.test.js +35 -51
- package/src/components/__tests__/modal-launcher.test.js +36 -6
- package/src/components/modal-backdrop.js +3 -8
- package/src/util/find-focusable-nodes.js +14 -0
- package/src/util/maybe-get-portal-mounted-modal-host-element.test.js +2 -3
package/dist/es/index.js
CHANGED
|
@@ -446,6 +446,10 @@ const ModalLauncherPortalAttributeName = "data-modal-launcher-portal";
|
|
|
446
446
|
* @see https://www.w3.org/TR/html5/editing.html#can-be-focused
|
|
447
447
|
*/
|
|
448
448
|
const FOCUSABLE_ELEMENTS = 'a[href], details, input, textarea, select, button:not([aria-label^="Close"])';
|
|
449
|
+
function findFocusableNodes(root) {
|
|
450
|
+
return Array.from(root.querySelectorAll(FOCUSABLE_ELEMENTS));
|
|
451
|
+
}
|
|
452
|
+
|
|
449
453
|
/**
|
|
450
454
|
* A private component used by ModalLauncher. This is the fixed-position
|
|
451
455
|
* container element that gets mounted outside the DOM. It overlays the modal
|
|
@@ -456,7 +460,6 @@ const FOCUSABLE_ELEMENTS = 'a[href], details, input, textarea, select, button:no
|
|
|
456
460
|
* and adding an `onClose` prop that will call `onCloseModal`. If an
|
|
457
461
|
* `onClose` prop is already provided, the two are merged.
|
|
458
462
|
*/
|
|
459
|
-
|
|
460
463
|
class ModalBackdrop extends Component {
|
|
461
464
|
constructor(...args) {
|
|
462
465
|
super(...args);
|
|
@@ -510,7 +513,7 @@ class ModalBackdrop extends Component {
|
|
|
510
513
|
|
|
511
514
|
_getFirstFocusableElement(node) {
|
|
512
515
|
// get a collection of elements that can be focused
|
|
513
|
-
const focusableElements = node
|
|
516
|
+
const focusableElements = findFocusableNodes(node);
|
|
514
517
|
|
|
515
518
|
if (!focusableElements) {
|
|
516
519
|
return null;
|
package/dist/index.js
CHANGED
|
@@ -82,7 +82,7 @@ module.exports =
|
|
|
82
82
|
/******/
|
|
83
83
|
/******/
|
|
84
84
|
/******/ // Load entry module and return exports
|
|
85
|
-
/******/ return __webpack_require__(__webpack_require__.s =
|
|
85
|
+
/******/ return __webpack_require__(__webpack_require__.s = 25);
|
|
86
86
|
/******/ })
|
|
87
87
|
/************************************************************************/
|
|
88
88
|
/******/ ([
|
|
@@ -477,7 +477,7 @@ const styleSheets = {
|
|
|
477
477
|
/* harmony import */ var _modal_content_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(14);
|
|
478
478
|
/* harmony import */ var _modal_header_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(7);
|
|
479
479
|
/* harmony import */ var _modal_footer_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(8);
|
|
480
|
-
/* harmony import */ var _close_button_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(
|
|
480
|
+
/* harmony import */ var _close_button_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(22);
|
|
481
481
|
|
|
482
482
|
|
|
483
483
|
|
|
@@ -711,7 +711,7 @@ const styleSheets = {
|
|
|
711
711
|
/* harmony import */ var aphrodite__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(aphrodite__WEBPACK_IMPORTED_MODULE_2__);
|
|
712
712
|
/* harmony import */ var _focus_trap_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(18);
|
|
713
713
|
/* harmony import */ var _modal_backdrop_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(19);
|
|
714
|
-
/* harmony import */ var _scroll_disabler_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(
|
|
714
|
+
/* harmony import */ var _scroll_disabler_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(21);
|
|
715
715
|
/* harmony import */ var _modal_context_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(12);
|
|
716
716
|
|
|
717
717
|
|
|
@@ -1261,6 +1261,7 @@ class FocusTrap extends react__WEBPACK_IMPORTED_MODULE_0__["Component"] {
|
|
|
1261
1261
|
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(2);
|
|
1262
1262
|
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_4__);
|
|
1263
1263
|
/* harmony import */ var _util_constants_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(11);
|
|
1264
|
+
/* harmony import */ var _util_find_focusable_nodes_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(20);
|
|
1264
1265
|
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
1265
1266
|
|
|
1266
1267
|
|
|
@@ -1270,11 +1271,7 @@ function _extends() { _extends = Object.assign || function (target) { for (var i
|
|
|
1270
1271
|
|
|
1271
1272
|
|
|
1272
1273
|
|
|
1273
|
-
|
|
1274
|
-
* List of elements that can be focused
|
|
1275
|
-
* @see https://www.w3.org/TR/html5/editing.html#can-be-focused
|
|
1276
|
-
*/
|
|
1277
|
-
const FOCUSABLE_ELEMENTS = 'a[href], details, input, textarea, select, button:not([aria-label^="Close"])';
|
|
1274
|
+
|
|
1278
1275
|
/**
|
|
1279
1276
|
* A private component used by ModalLauncher. This is the fixed-position
|
|
1280
1277
|
* container element that gets mounted outside the DOM. It overlays the modal
|
|
@@ -1285,7 +1282,6 @@ const FOCUSABLE_ELEMENTS = 'a[href], details, input, textarea, select, button:no
|
|
|
1285
1282
|
* and adding an `onClose` prop that will call `onCloseModal`. If an
|
|
1286
1283
|
* `onClose` prop is already provided, the two are merged.
|
|
1287
1284
|
*/
|
|
1288
|
-
|
|
1289
1285
|
class ModalBackdrop extends react__WEBPACK_IMPORTED_MODULE_0__["Component"] {
|
|
1290
1286
|
constructor(...args) {
|
|
1291
1287
|
super(...args);
|
|
@@ -1339,7 +1335,7 @@ class ModalBackdrop extends react__WEBPACK_IMPORTED_MODULE_0__["Component"] {
|
|
|
1339
1335
|
|
|
1340
1336
|
_getFirstFocusableElement(node) {
|
|
1341
1337
|
// get a collection of elements that can be focused
|
|
1342
|
-
const focusableElements =
|
|
1338
|
+
const focusableElements = Object(_util_find_focusable_nodes_js__WEBPACK_IMPORTED_MODULE_6__[/* findFocusableNodes */ "a"])(node);
|
|
1343
1339
|
|
|
1344
1340
|
if (!focusableElements) {
|
|
1345
1341
|
return null;
|
|
@@ -1410,6 +1406,21 @@ const styles = aphrodite__WEBPACK_IMPORTED_MODULE_2__["StyleSheet"].create({
|
|
|
1410
1406
|
/* 20 */
|
|
1411
1407
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
1412
1408
|
|
|
1409
|
+
"use strict";
|
|
1410
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return findFocusableNodes; });
|
|
1411
|
+
/**
|
|
1412
|
+
* List of elements that can be focused
|
|
1413
|
+
* @see https://www.w3.org/TR/html5/editing.html#can-be-focused
|
|
1414
|
+
*/
|
|
1415
|
+
const FOCUSABLE_ELEMENTS = 'a[href], details, input, textarea, select, button:not([aria-label^="Close"])';
|
|
1416
|
+
function findFocusableNodes(root) {
|
|
1417
|
+
return Array.from(root.querySelectorAll(FOCUSABLE_ELEMENTS));
|
|
1418
|
+
}
|
|
1419
|
+
|
|
1420
|
+
/***/ }),
|
|
1421
|
+
/* 21 */
|
|
1422
|
+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
1423
|
+
|
|
1413
1424
|
"use strict";
|
|
1414
1425
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
1415
1426
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
@@ -1504,16 +1515,16 @@ ScrollDisabler.numModalsOpened = 0;
|
|
|
1504
1515
|
/* harmony default export */ __webpack_exports__["a"] = (ScrollDisabler);
|
|
1505
1516
|
|
|
1506
1517
|
/***/ }),
|
|
1507
|
-
/*
|
|
1518
|
+
/* 22 */
|
|
1508
1519
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
1509
1520
|
|
|
1510
1521
|
"use strict";
|
|
1511
1522
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return CloseButton; });
|
|
1512
1523
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
1513
1524
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
1514
|
-
/* harmony import */ var _khanacademy_wonder_blocks_icon__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(
|
|
1525
|
+
/* harmony import */ var _khanacademy_wonder_blocks_icon__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(23);
|
|
1515
1526
|
/* harmony import */ var _khanacademy_wonder_blocks_icon__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_blocks_icon__WEBPACK_IMPORTED_MODULE_1__);
|
|
1516
|
-
/* harmony import */ var _khanacademy_wonder_blocks_icon_button__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(
|
|
1527
|
+
/* harmony import */ var _khanacademy_wonder_blocks_icon_button__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(24);
|
|
1517
1528
|
/* harmony import */ var _khanacademy_wonder_blocks_icon_button__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_blocks_icon_button__WEBPACK_IMPORTED_MODULE_2__);
|
|
1518
1529
|
/* harmony import */ var _modal_context_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(12);
|
|
1519
1530
|
|
|
@@ -1552,19 +1563,19 @@ class CloseButton extends react__WEBPACK_IMPORTED_MODULE_0__["Component"] {
|
|
|
1552
1563
|
}
|
|
1553
1564
|
|
|
1554
1565
|
/***/ }),
|
|
1555
|
-
/*
|
|
1566
|
+
/* 23 */
|
|
1556
1567
|
/***/ (function(module, exports) {
|
|
1557
1568
|
|
|
1558
1569
|
module.exports = require("@khanacademy/wonder-blocks-icon");
|
|
1559
1570
|
|
|
1560
1571
|
/***/ }),
|
|
1561
|
-
/*
|
|
1572
|
+
/* 24 */
|
|
1562
1573
|
/***/ (function(module, exports) {
|
|
1563
1574
|
|
|
1564
1575
|
module.exports = require("@khanacademy/wonder-blocks-icon-button");
|
|
1565
1576
|
|
|
1566
1577
|
/***/ }),
|
|
1567
|
-
/*
|
|
1578
|
+
/* 25 */
|
|
1568
1579
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
1569
1580
|
|
|
1570
1581
|
"use strict";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@khanacademy/wonder-blocks-modal",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.45",
|
|
4
4
|
"design": "v2",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -15,16 +15,16 @@
|
|
|
15
15
|
"author": "",
|
|
16
16
|
"license": "MIT",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@babel/runtime": "^7.
|
|
19
|
-
"@khanacademy/wonder-blocks-breadcrumbs": "^1.0.
|
|
20
|
-
"@khanacademy/wonder-blocks-color": "^1.1.
|
|
21
|
-
"@khanacademy/wonder-blocks-core": "^
|
|
22
|
-
"@khanacademy/wonder-blocks-icon": "^1.2.
|
|
23
|
-
"@khanacademy/wonder-blocks-icon-button": "^3.4.
|
|
24
|
-
"@khanacademy/wonder-blocks-layout": "^1.4.
|
|
25
|
-
"@khanacademy/wonder-blocks-spacing": "^3.0.
|
|
26
|
-
"@khanacademy/wonder-blocks-toolbar": "^2.1.
|
|
27
|
-
"@khanacademy/wonder-blocks-typography": "^1.1.
|
|
18
|
+
"@babel/runtime": "^7.16.3",
|
|
19
|
+
"@khanacademy/wonder-blocks-breadcrumbs": "^1.0.27",
|
|
20
|
+
"@khanacademy/wonder-blocks-color": "^1.1.20",
|
|
21
|
+
"@khanacademy/wonder-blocks-core": "^4.0.0",
|
|
22
|
+
"@khanacademy/wonder-blocks-icon": "^1.2.24",
|
|
23
|
+
"@khanacademy/wonder-blocks-icon-button": "^3.4.1",
|
|
24
|
+
"@khanacademy/wonder-blocks-layout": "^1.4.6",
|
|
25
|
+
"@khanacademy/wonder-blocks-spacing": "^3.0.5",
|
|
26
|
+
"@khanacademy/wonder-blocks-toolbar": "^2.1.28",
|
|
27
|
+
"@khanacademy/wonder-blocks-typography": "^1.1.28"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
30
|
"aphrodite": "^1.2.5",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"react-dom": "16.14.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"wb-dev-build-settings": "^0.
|
|
35
|
+
"wb-dev-build-settings": "^0.2.0"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "9ebea88533e702011165072f090a377e02fa3f0f"
|
|
38
38
|
}
|
|
@@ -5,7 +5,10 @@ import {mount} from "enzyme";
|
|
|
5
5
|
import ModalBackdrop from "../modal-backdrop.js";
|
|
6
6
|
import OnePaneDialog from "../one-pane-dialog.js";
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
import {unmountAll} from "../../../../../utils/testing/enzyme-shim.js";
|
|
9
|
+
import {getElementAttachedToDocument} from "../../../../../utils/testing/get-element-attached-to-document.js";
|
|
10
|
+
|
|
11
|
+
const wait = (duration: number = 0) =>
|
|
9
12
|
new Promise((resolve, reject) => setTimeout(resolve, duration));
|
|
10
13
|
|
|
11
14
|
const exampleModal = (
|
|
@@ -31,6 +34,17 @@ const exampleModalWithButtons = (
|
|
|
31
34
|
);
|
|
32
35
|
|
|
33
36
|
describe("ModalBackdrop", () => {
|
|
37
|
+
beforeEach(() => {
|
|
38
|
+
jest.useRealTimers();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
afterEach(() => {
|
|
42
|
+
unmountAll();
|
|
43
|
+
if (document.body) {
|
|
44
|
+
document.body.innerHTML = "";
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
34
48
|
test("Clicking the backdrop triggers `onCloseModal`", () => {
|
|
35
49
|
const onCloseModal = jest.fn();
|
|
36
50
|
|
|
@@ -80,6 +94,9 @@ describe("ModalBackdrop", () => {
|
|
|
80
94
|
|
|
81
95
|
test("If initialFocusId is set and element is found, we focus that element inside the modal", async () => {
|
|
82
96
|
// Arrange
|
|
97
|
+
// We need the elements in the DOM document, it seems, for this test
|
|
98
|
+
// to work. Changing to testing-library will likely fix this.
|
|
99
|
+
const attachElement = getElementAttachedToDocument("container");
|
|
83
100
|
const initialFocusId = "initial-focus";
|
|
84
101
|
|
|
85
102
|
const wrapper = mount(
|
|
@@ -98,10 +115,11 @@ describe("ModalBackdrop", () => {
|
|
|
98
115
|
footer={<div data-modal-footer />}
|
|
99
116
|
/>
|
|
100
117
|
</ModalBackdrop>,
|
|
118
|
+
{attachTo: attachElement},
|
|
101
119
|
);
|
|
102
120
|
|
|
103
121
|
// Act
|
|
104
|
-
await
|
|
122
|
+
await wait(); // wait for styles to be applied
|
|
105
123
|
const initialFocusElement = wrapper.find(`#${initialFocusId}`);
|
|
106
124
|
|
|
107
125
|
// Assert
|
|
@@ -113,6 +131,9 @@ describe("ModalBackdrop", () => {
|
|
|
113
131
|
|
|
114
132
|
test("If initialFocusId is set but element is NOT found, we focus on the first focusable element instead", async () => {
|
|
115
133
|
// Arrange
|
|
134
|
+
// We need the elements in the DOM document, it seems, for this test
|
|
135
|
+
// to work. Changing to testing-library will likely fix this.
|
|
136
|
+
const attachElement = getElementAttachedToDocument("container");
|
|
116
137
|
const initialFocusId = "initial-focus";
|
|
117
138
|
const firstFocusableElement = "[data-first-button]";
|
|
118
139
|
|
|
@@ -123,10 +144,11 @@ describe("ModalBackdrop", () => {
|
|
|
123
144
|
>
|
|
124
145
|
{exampleModalWithButtons}
|
|
125
146
|
</ModalBackdrop>,
|
|
147
|
+
{attachTo: attachElement},
|
|
126
148
|
);
|
|
127
149
|
|
|
128
150
|
// Act
|
|
129
|
-
await
|
|
151
|
+
await wait(); // wait for styles to be applied
|
|
130
152
|
const initialFocusElement = wrapper.find(`#${initialFocusId}`);
|
|
131
153
|
|
|
132
154
|
// Assert
|
|
@@ -140,14 +162,18 @@ describe("ModalBackdrop", () => {
|
|
|
140
162
|
|
|
141
163
|
test("If no initialFocusId is set, we focus the first button in the modal", async () => {
|
|
142
164
|
// Arrange
|
|
165
|
+
// We need the elements in the DOM document, it seems, for this test
|
|
166
|
+
// to work. Changing to testing-library will likely fix this.
|
|
167
|
+
const attachElement = getElementAttachedToDocument("container");
|
|
143
168
|
const wrapper = mount(
|
|
144
169
|
<ModalBackdrop onCloseModal={() => {}}>
|
|
145
170
|
{exampleModalWithButtons}
|
|
146
171
|
</ModalBackdrop>,
|
|
172
|
+
{attachTo: attachElement},
|
|
147
173
|
);
|
|
148
174
|
|
|
149
175
|
// Act
|
|
150
|
-
await
|
|
176
|
+
await wait(); // wait for styles to be applied
|
|
151
177
|
const focusableElement = wrapper
|
|
152
178
|
.find("[data-first-button]")
|
|
153
179
|
.getDOMNode();
|
|
@@ -158,14 +184,18 @@ describe("ModalBackdrop", () => {
|
|
|
158
184
|
|
|
159
185
|
test("If there are no focusable elements, we focus the Dialog instead", async () => {
|
|
160
186
|
// Arrange
|
|
187
|
+
// We need the elements in the DOM document, it seems, for this test
|
|
188
|
+
// to work. Changing to testing-library will likely fix this.
|
|
189
|
+
const attachElement = getElementAttachedToDocument("container");
|
|
161
190
|
const wrapper = mount(
|
|
162
191
|
<ModalBackdrop onCloseModal={() => {}}>
|
|
163
192
|
{exampleModal}
|
|
164
193
|
</ModalBackdrop>,
|
|
194
|
+
{attachTo: attachElement},
|
|
165
195
|
);
|
|
166
196
|
|
|
167
197
|
// Act
|
|
168
|
-
await
|
|
198
|
+
await wait(); // wait for styles to be applied
|
|
169
199
|
const focusableElement = wrapper
|
|
170
200
|
.find('div[role="dialog"]')
|
|
171
201
|
.getDOMNode();
|
|
@@ -173,50 +203,4 @@ describe("ModalBackdrop", () => {
|
|
|
173
203
|
// Assert
|
|
174
204
|
expect(document.activeElement).toBe(focusableElement);
|
|
175
205
|
});
|
|
176
|
-
|
|
177
|
-
// TODO(mdr): I haven't figured out how to actually simulate tab keystrokes
|
|
178
|
-
// or focus events in a way that JSDOM will recognize, so triggering the
|
|
179
|
-
// global focus handler isn't feasible. I had to do manual testing
|
|
180
|
-
// instead :( Here's what I had, though!
|
|
181
|
-
test.skip("Tabbing inside the modal wraps around", () => {
|
|
182
|
-
const wrapper = mount(
|
|
183
|
-
<div>
|
|
184
|
-
<button data-button-id="A" />
|
|
185
|
-
<ModalBackdrop onCloseModal={() => {}}>
|
|
186
|
-
{exampleModalWithButtons}
|
|
187
|
-
</ModalBackdrop>
|
|
188
|
-
<button data-button-id="Z" />
|
|
189
|
-
</div>,
|
|
190
|
-
);
|
|
191
|
-
|
|
192
|
-
const buttonA = wrapper.find('[data-button-id="A"]').getDOMNode();
|
|
193
|
-
const button1 = wrapper.find('[data-button-id="1"]').getDOMNode();
|
|
194
|
-
const button2 = wrapper.find('[data-button-id="2"]').getDOMNode();
|
|
195
|
-
const button3 = wrapper.find('[data-button-id="3"]').getDOMNode();
|
|
196
|
-
const buttonZ = wrapper.find('[data-button-id="Z"]').getDOMNode();
|
|
197
|
-
|
|
198
|
-
// First, go forward. Confirm that, when we get to button Z, we wrap
|
|
199
|
-
// back to button 1. (I wish we could just simulate tab keypresses!
|
|
200
|
-
// Instead, we depend on the implementation detail that _which_ node you
|
|
201
|
-
// exit from determines where you'll end up.)
|
|
202
|
-
button1.focus();
|
|
203
|
-
expect(document.activeElement).toBe(button1);
|
|
204
|
-
button2.focus();
|
|
205
|
-
expect(document.activeElement).toBe(button2);
|
|
206
|
-
button3.focus();
|
|
207
|
-
expect(document.activeElement).toBe(button3);
|
|
208
|
-
buttonZ.focus();
|
|
209
|
-
expect(document.activeElement).toBe(button1);
|
|
210
|
-
|
|
211
|
-
// Then, go backward. Confirm that, when we get to button A, we wrap
|
|
212
|
-
// back to button 3.
|
|
213
|
-
button3.focus();
|
|
214
|
-
expect(document.activeElement).toBe(button3);
|
|
215
|
-
button2.focus();
|
|
216
|
-
expect(document.activeElement).toBe(button2);
|
|
217
|
-
button1.focus();
|
|
218
|
-
expect(document.activeElement).toBe(button1);
|
|
219
|
-
buttonA.focus();
|
|
220
|
-
expect(document.activeElement).toBe(button3);
|
|
221
|
-
});
|
|
222
206
|
});
|
|
@@ -5,7 +5,10 @@ import {mount, shallow} from "enzyme";
|
|
|
5
5
|
import ModalLauncher from "../modal-launcher.js";
|
|
6
6
|
import OnePaneDialog from "../one-pane-dialog.js";
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
import {unmountAll} from "../../../../../utils/testing/enzyme-shim.js";
|
|
9
|
+
import {getElementAttachedToDocument} from "../../../../../utils/testing/get-element-attached-to-document.js";
|
|
10
|
+
|
|
11
|
+
const wait = (duration: number = 0) =>
|
|
9
12
|
new Promise((resolve, reject) => setTimeout(resolve, duration));
|
|
10
13
|
|
|
11
14
|
const exampleModal = (
|
|
@@ -16,19 +19,41 @@ const exampleModal = (
|
|
|
16
19
|
);
|
|
17
20
|
|
|
18
21
|
describe("ModalLauncher", () => {
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
jest.useRealTimers();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
afterEach(() => {
|
|
27
|
+
unmountAll();
|
|
28
|
+
if (document.body) {
|
|
29
|
+
document.body.innerHTML = "";
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
19
33
|
window.scrollTo = jest.fn();
|
|
20
34
|
|
|
21
|
-
test("Children can launch the modal", () => {
|
|
35
|
+
test("Children can launch the modal", async () => {
|
|
36
|
+
// Arrange
|
|
37
|
+
// We need the elements in the DOM document, it seems, for this test
|
|
38
|
+
// to work. Changing to testing-library will likely fix this.
|
|
39
|
+
const containerDiv = getElementAttachedToDocument("container");
|
|
22
40
|
const wrapper = mount(
|
|
23
41
|
<ModalLauncher modal={exampleModal}>
|
|
24
42
|
{({openModal}) => <button onClick={openModal} />}
|
|
25
43
|
</ModalLauncher>,
|
|
44
|
+
{attachTo: containerDiv},
|
|
26
45
|
);
|
|
46
|
+
|
|
47
|
+
// Act
|
|
27
48
|
wrapper.find("button").simulate("click");
|
|
49
|
+
await wait();
|
|
50
|
+
|
|
28
51
|
const portal = global.document.querySelector(
|
|
29
52
|
"[data-modal-launcher-portal]",
|
|
30
53
|
);
|
|
31
|
-
|
|
54
|
+
|
|
55
|
+
// Assert
|
|
56
|
+
expect(portal).toBeInstanceOf(HTMLDivElement);
|
|
32
57
|
});
|
|
33
58
|
|
|
34
59
|
test("Modal can be manually opened and closed", () => {
|
|
@@ -62,7 +87,7 @@ describe("ModalLauncher", () => {
|
|
|
62
87
|
// this function receives a `closeModal` argument that works.
|
|
63
88
|
const modalFn = ({closeModal}: {|closeModal: () => void|}) => {
|
|
64
89
|
expect(opened).toBe(true);
|
|
65
|
-
|
|
90
|
+
setTimeout(closeModal, 0);
|
|
66
91
|
return exampleModal;
|
|
67
92
|
};
|
|
68
93
|
|
|
@@ -110,6 +135,7 @@ describe("ModalLauncher", () => {
|
|
|
110
135
|
|
|
111
136
|
// Simulate an Escape keypress.
|
|
112
137
|
const event: KeyboardEvent = (document.createEvent("Event"): any);
|
|
138
|
+
// $FlowIgnore[cannot-write]
|
|
113
139
|
event.key = "Escape";
|
|
114
140
|
event.initEvent("keyup", true, true);
|
|
115
141
|
document.dispatchEvent(event);
|
|
@@ -254,7 +280,7 @@ describe("ModalLauncher", () => {
|
|
|
254
280
|
wrapper.find("button").simulate("click");
|
|
255
281
|
|
|
256
282
|
// wait for styles to be applied
|
|
257
|
-
await
|
|
283
|
+
await wait();
|
|
258
284
|
|
|
259
285
|
// Assert
|
|
260
286
|
expect(document.activeElement).not.toBe(lastButton);
|
|
@@ -262,6 +288,9 @@ describe("ModalLauncher", () => {
|
|
|
262
288
|
|
|
263
289
|
test("if modal is closed, return focus to the last element focused outside the modal", async () => {
|
|
264
290
|
// Arrange
|
|
291
|
+
// We need the elements in the DOM document, it seems, for this test
|
|
292
|
+
// to work. Changing to testing-library will likely fix this.
|
|
293
|
+
const containerDiv = getElementAttachedToDocument("container");
|
|
265
294
|
let savedCloseModal = () => {
|
|
266
295
|
throw new Error(`closeModal wasn't saved`);
|
|
267
296
|
};
|
|
@@ -277,6 +306,7 @@ describe("ModalLauncher", () => {
|
|
|
277
306
|
<button onClick={openModal} data-last-focused-button />
|
|
278
307
|
)}
|
|
279
308
|
</ModalLauncher>,
|
|
309
|
+
{attachTo: containerDiv},
|
|
280
310
|
);
|
|
281
311
|
|
|
282
312
|
const lastButton = wrapper
|
|
@@ -289,7 +319,7 @@ describe("ModalLauncher", () => {
|
|
|
289
319
|
wrapper.find("button").simulate("click");
|
|
290
320
|
|
|
291
321
|
// wait for styles to be applied
|
|
292
|
-
await
|
|
322
|
+
await wait();
|
|
293
323
|
|
|
294
324
|
// Act
|
|
295
325
|
savedCloseModal(); // close the modal
|
|
@@ -7,6 +7,8 @@ import Color from "@khanacademy/wonder-blocks-color";
|
|
|
7
7
|
import {View} from "@khanacademy/wonder-blocks-core";
|
|
8
8
|
import {ModalLauncherPortalAttributeName} from "../util/constants.js";
|
|
9
9
|
|
|
10
|
+
import {findFocusableNodes} from "../util/find-focusable-nodes.js";
|
|
11
|
+
|
|
10
12
|
import type {ModalElement} from "../util/types.js";
|
|
11
13
|
|
|
12
14
|
type Props = {|
|
|
@@ -24,13 +26,6 @@ type Props = {|
|
|
|
24
26
|
testId?: string,
|
|
25
27
|
|};
|
|
26
28
|
|
|
27
|
-
/**
|
|
28
|
-
* List of elements that can be focused
|
|
29
|
-
* @see https://www.w3.org/TR/html5/editing.html#can-be-focused
|
|
30
|
-
*/
|
|
31
|
-
const FOCUSABLE_ELEMENTS =
|
|
32
|
-
'a[href], details, input, textarea, select, button:not([aria-label^="Close"])';
|
|
33
|
-
|
|
34
29
|
/**
|
|
35
30
|
* A private component used by ModalLauncher. This is the fixed-position
|
|
36
31
|
* container element that gets mounted outside the DOM. It overlays the modal
|
|
@@ -82,7 +77,7 @@ export default class ModalBackdrop extends React.Component<Props> {
|
|
|
82
77
|
*/
|
|
83
78
|
_getFirstFocusableElement(node: HTMLElement): HTMLElement | null {
|
|
84
79
|
// get a collection of elements that can be focused
|
|
85
|
-
const focusableElements = node
|
|
80
|
+
const focusableElements = findFocusableNodes(node);
|
|
86
81
|
|
|
87
82
|
if (!focusableElements) {
|
|
88
83
|
return null;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* List of elements that can be focused
|
|
5
|
+
* @see https://www.w3.org/TR/html5/editing.html#can-be-focused
|
|
6
|
+
*/
|
|
7
|
+
const FOCUSABLE_ELEMENTS =
|
|
8
|
+
'a[href], details, input, textarea, select, button:not([aria-label^="Close"])';
|
|
9
|
+
|
|
10
|
+
export function findFocusableNodes(
|
|
11
|
+
root: HTMLElement | Document,
|
|
12
|
+
): Array<HTMLElement> {
|
|
13
|
+
return Array.from(root.querySelectorAll(FOCUSABLE_ELEMENTS));
|
|
14
|
+
}
|
|
@@ -108,9 +108,8 @@ describe("maybeGetPortalMountedModalHostElement", () => {
|
|
|
108
108
|
if (node) {
|
|
109
109
|
// Act
|
|
110
110
|
const candidateElement = ReactDOM.findDOMNode(node);
|
|
111
|
-
const result =
|
|
112
|
-
candidateElement
|
|
113
|
-
);
|
|
111
|
+
const result =
|
|
112
|
+
maybeGetPortalMountedModalHostElement(candidateElement);
|
|
114
113
|
|
|
115
114
|
// Assert
|
|
116
115
|
expect(result).toBeTruthy();
|