@khanacademy/wonder-blocks-modal 2.1.44 → 2.2.2
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 +29 -0
- package/dist/es/index.js +96 -85
- package/dist/index.js +42 -23
- package/package.json +13 -14
- package/src/__tests__/__snapshots__/generated-snapshot.test.js.snap +11 -11
- package/src/components/__tests__/close-button.test.js +1 -0
- package/src/components/__tests__/modal-backdrop.test.js +115 -57
- package/src/components/__tests__/modal-header.test.js +1 -0
- package/src/components/__tests__/modal-launcher.test.js +37 -6
- package/src/components/__tests__/modal-panel.test.js +1 -0
- package/src/components/__tests__/one-pane-dialog.test.js +1 -0
- package/src/components/modal-backdrop.js +17 -13
- package/src/components/one-pane-dialog.stories.js +4 -4
- package/src/util/find-focusable-nodes.js +14 -0
- package/src/util/maybe-get-portal-mounted-modal-host-element.test.js +3 -3
- package/LICENSE +0 -21
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
// @flow
|
|
2
2
|
import * as React from "react";
|
|
3
3
|
import {mount, shallow} from "enzyme";
|
|
4
|
+
import "jest-enzyme";
|
|
4
5
|
|
|
5
6
|
import ModalLauncher from "../modal-launcher.js";
|
|
6
7
|
import OnePaneDialog from "../one-pane-dialog.js";
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
import {unmountAll} from "../../../../../utils/testing/enzyme-shim.js";
|
|
10
|
+
import {getElementAttachedToDocument} from "../../../../../utils/testing/get-element-attached-to-document.js";
|
|
11
|
+
|
|
12
|
+
const wait = (duration: number = 0) =>
|
|
9
13
|
new Promise((resolve, reject) => setTimeout(resolve, duration));
|
|
10
14
|
|
|
11
15
|
const exampleModal = (
|
|
@@ -16,19 +20,41 @@ const exampleModal = (
|
|
|
16
20
|
);
|
|
17
21
|
|
|
18
22
|
describe("ModalLauncher", () => {
|
|
23
|
+
beforeEach(() => {
|
|
24
|
+
jest.useRealTimers();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
afterEach(() => {
|
|
28
|
+
unmountAll();
|
|
29
|
+
if (document.body) {
|
|
30
|
+
document.body.innerHTML = "";
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
19
34
|
window.scrollTo = jest.fn();
|
|
20
35
|
|
|
21
|
-
test("Children can launch the modal", () => {
|
|
36
|
+
test("Children can launch the modal", async () => {
|
|
37
|
+
// Arrange
|
|
38
|
+
// We need the elements in the DOM document, it seems, for this test
|
|
39
|
+
// to work. Changing to testing-library will likely fix this.
|
|
40
|
+
const containerDiv = getElementAttachedToDocument("container");
|
|
22
41
|
const wrapper = mount(
|
|
23
42
|
<ModalLauncher modal={exampleModal}>
|
|
24
43
|
{({openModal}) => <button onClick={openModal} />}
|
|
25
44
|
</ModalLauncher>,
|
|
45
|
+
{attachTo: containerDiv},
|
|
26
46
|
);
|
|
47
|
+
|
|
48
|
+
// Act
|
|
27
49
|
wrapper.find("button").simulate("click");
|
|
50
|
+
await wait();
|
|
51
|
+
|
|
28
52
|
const portal = global.document.querySelector(
|
|
29
53
|
"[data-modal-launcher-portal]",
|
|
30
54
|
);
|
|
31
|
-
|
|
55
|
+
|
|
56
|
+
// Assert
|
|
57
|
+
expect(portal).toBeInstanceOf(HTMLDivElement);
|
|
32
58
|
});
|
|
33
59
|
|
|
34
60
|
test("Modal can be manually opened and closed", () => {
|
|
@@ -62,7 +88,7 @@ describe("ModalLauncher", () => {
|
|
|
62
88
|
// this function receives a `closeModal` argument that works.
|
|
63
89
|
const modalFn = ({closeModal}: {|closeModal: () => void|}) => {
|
|
64
90
|
expect(opened).toBe(true);
|
|
65
|
-
|
|
91
|
+
setTimeout(closeModal, 0);
|
|
66
92
|
return exampleModal;
|
|
67
93
|
};
|
|
68
94
|
|
|
@@ -110,6 +136,7 @@ describe("ModalLauncher", () => {
|
|
|
110
136
|
|
|
111
137
|
// Simulate an Escape keypress.
|
|
112
138
|
const event: KeyboardEvent = (document.createEvent("Event"): any);
|
|
139
|
+
// $FlowIgnore[cannot-write]
|
|
113
140
|
event.key = "Escape";
|
|
114
141
|
event.initEvent("keyup", true, true);
|
|
115
142
|
document.dispatchEvent(event);
|
|
@@ -254,7 +281,7 @@ describe("ModalLauncher", () => {
|
|
|
254
281
|
wrapper.find("button").simulate("click");
|
|
255
282
|
|
|
256
283
|
// wait for styles to be applied
|
|
257
|
-
await
|
|
284
|
+
await wait();
|
|
258
285
|
|
|
259
286
|
// Assert
|
|
260
287
|
expect(document.activeElement).not.toBe(lastButton);
|
|
@@ -262,6 +289,9 @@ describe("ModalLauncher", () => {
|
|
|
262
289
|
|
|
263
290
|
test("if modal is closed, return focus to the last element focused outside the modal", async () => {
|
|
264
291
|
// Arrange
|
|
292
|
+
// We need the elements in the DOM document, it seems, for this test
|
|
293
|
+
// to work. Changing to testing-library will likely fix this.
|
|
294
|
+
const containerDiv = getElementAttachedToDocument("container");
|
|
265
295
|
let savedCloseModal = () => {
|
|
266
296
|
throw new Error(`closeModal wasn't saved`);
|
|
267
297
|
};
|
|
@@ -277,6 +307,7 @@ describe("ModalLauncher", () => {
|
|
|
277
307
|
<button onClick={openModal} data-last-focused-button />
|
|
278
308
|
)}
|
|
279
309
|
</ModalLauncher>,
|
|
310
|
+
{attachTo: containerDiv},
|
|
280
311
|
);
|
|
281
312
|
|
|
282
313
|
const lastButton = wrapper
|
|
@@ -289,7 +320,7 @@ describe("ModalLauncher", () => {
|
|
|
289
320
|
wrapper.find("button").simulate("click");
|
|
290
321
|
|
|
291
322
|
// wait for styles to be applied
|
|
292
|
-
await
|
|
323
|
+
await wait();
|
|
293
324
|
|
|
294
325
|
// Act
|
|
295
326
|
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
|
|
@@ -62,6 +57,8 @@ export default class ModalBackdrop extends React.Component<Props> {
|
|
|
62
57
|
}, 0);
|
|
63
58
|
}
|
|
64
59
|
|
|
60
|
+
_mousePressedOutside: boolean = false;
|
|
61
|
+
|
|
65
62
|
/**
|
|
66
63
|
* Returns an element specified by the user
|
|
67
64
|
*/
|
|
@@ -82,7 +79,7 @@ export default class ModalBackdrop extends React.Component<Props> {
|
|
|
82
79
|
*/
|
|
83
80
|
_getFirstFocusableElement(node: HTMLElement): HTMLElement | null {
|
|
84
81
|
// get a collection of elements that can be focused
|
|
85
|
-
const focusableElements = node
|
|
82
|
+
const focusableElements = findFocusableNodes(node);
|
|
86
83
|
|
|
87
84
|
if (!focusableElements) {
|
|
88
85
|
return null;
|
|
@@ -112,12 +109,18 @@ export default class ModalBackdrop extends React.Component<Props> {
|
|
|
112
109
|
* _directly_ from the positioner, not bubbled up from its children), close
|
|
113
110
|
* the modal.
|
|
114
111
|
*/
|
|
115
|
-
|
|
116
|
-
//
|
|
117
|
-
|
|
118
|
-
|
|
112
|
+
handleMouseDown: (e: SyntheticEvent<>) => void = (e: SyntheticEvent<>) => {
|
|
113
|
+
// Confirm that it is the backdrop that is being clicked, not the child
|
|
114
|
+
this._mousePressedOutside = e.target === e.currentTarget;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
handleMouseUp: (e: SyntheticEvent<>) => void = (e: SyntheticEvent<>) => {
|
|
118
|
+
// Confirm that it is the backdrop that is being clicked, not the child
|
|
119
|
+
// and that the mouse was pressed in the backdrop first.
|
|
120
|
+
if (e.target === e.currentTarget && this._mousePressedOutside) {
|
|
119
121
|
this.props.onCloseModal();
|
|
120
122
|
}
|
|
123
|
+
this._mousePressedOutside = false;
|
|
121
124
|
};
|
|
122
125
|
|
|
123
126
|
render(): React.Node {
|
|
@@ -129,7 +132,8 @@ export default class ModalBackdrop extends React.Component<Props> {
|
|
|
129
132
|
return (
|
|
130
133
|
<View
|
|
131
134
|
style={styles.modalPositioner}
|
|
132
|
-
|
|
135
|
+
onMouseDown={this.handleMouseDown}
|
|
136
|
+
onMouseUp={this.handleMouseUp}
|
|
133
137
|
testId={testId}
|
|
134
138
|
{...backdropProps}
|
|
135
139
|
>
|
|
@@ -50,7 +50,7 @@ export default {
|
|
|
50
50
|
},
|
|
51
51
|
};
|
|
52
52
|
|
|
53
|
-
export const
|
|
53
|
+
export const Simple: StoryComponentType = () => {
|
|
54
54
|
const modal = (
|
|
55
55
|
<OnePaneDialog
|
|
56
56
|
testId="one-pane-dialog-above"
|
|
@@ -98,7 +98,7 @@ const styles = StyleSheet.create({
|
|
|
98
98
|
},
|
|
99
99
|
});
|
|
100
100
|
|
|
101
|
-
export const
|
|
101
|
+
export const KitchenSink: StoryComponentType = () => {
|
|
102
102
|
const modal = (
|
|
103
103
|
<OnePaneDialog
|
|
104
104
|
title="Single-line title"
|
|
@@ -142,7 +142,7 @@ export const kitchenSink: StoryComponentType = () => {
|
|
|
142
142
|
);
|
|
143
143
|
};
|
|
144
144
|
|
|
145
|
-
export const
|
|
145
|
+
export const WithOpener: StoryComponentType = () => {
|
|
146
146
|
type MyModalProps = {|
|
|
147
147
|
closeModal: () => void,
|
|
148
148
|
|};
|
|
@@ -191,7 +191,7 @@ export const withOpener: StoryComponentType = () => {
|
|
|
191
191
|
);
|
|
192
192
|
};
|
|
193
193
|
|
|
194
|
-
|
|
194
|
+
WithOpener.parameters = {
|
|
195
195
|
viewport: {
|
|
196
196
|
defaultViewport: null,
|
|
197
197
|
},
|
|
@@ -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
|
+
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import * as React from "react";
|
|
3
3
|
import * as ReactDOM from "react-dom";
|
|
4
4
|
import {mount} from "enzyme";
|
|
5
|
+
import "jest-enzyme";
|
|
5
6
|
|
|
6
7
|
import {ModalLauncherPortalAttributeName} from "./constants.js";
|
|
7
8
|
import maybeGetPortalMountedModalHostElement from "./maybe-get-portal-mounted-modal-host-element.js";
|
|
@@ -108,9 +109,8 @@ describe("maybeGetPortalMountedModalHostElement", () => {
|
|
|
108
109
|
if (node) {
|
|
109
110
|
// Act
|
|
110
111
|
const candidateElement = ReactDOM.findDOMNode(node);
|
|
111
|
-
const result =
|
|
112
|
-
candidateElement
|
|
113
|
-
);
|
|
112
|
+
const result =
|
|
113
|
+
maybeGetPortalMountedModalHostElement(candidateElement);
|
|
114
114
|
|
|
115
115
|
// Assert
|
|
116
116
|
expect(result).toBeTruthy();
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2018 Khan Academy
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|