@khanacademy/wonder-blocks-popover 1.1.26 → 1.1.28
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 +25 -0
- package/dist/index.js +761 -0
- package/dist/index.js.flow +2 -0
- package/package.json +9 -10
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,30 @@
|
|
|
1
1
|
# @khanacademy/wonder-blocks-popover
|
|
2
2
|
|
|
3
|
+
## 1.1.28
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 496119f2: Cleanup WB interdependencies
|
|
8
|
+
- Updated dependencies [496119f2]
|
|
9
|
+
- @khanacademy/wonder-blocks-core@4.6.2
|
|
10
|
+
- @khanacademy/wonder-blocks-modal@3.0.5
|
|
11
|
+
- @khanacademy/wonder-blocks-icon@1.2.35
|
|
12
|
+
- @khanacademy/wonder-blocks-icon-button@3.4.19
|
|
13
|
+
- @khanacademy/wonder-blocks-tooltip@1.4.4
|
|
14
|
+
- @khanacademy/wonder-blocks-typography@1.1.37
|
|
15
|
+
|
|
16
|
+
## 1.1.27
|
|
17
|
+
|
|
18
|
+
### Patch Changes
|
|
19
|
+
|
|
20
|
+
- @khanacademy/wonder-blocks-core@4.6.1
|
|
21
|
+
- @khanacademy/wonder-blocks-icon@1.2.34
|
|
22
|
+
- @khanacademy/wonder-blocks-icon-button@3.4.18
|
|
23
|
+
- @khanacademy/wonder-blocks-layout@1.4.14
|
|
24
|
+
- @khanacademy/wonder-blocks-modal@3.0.4
|
|
25
|
+
- @khanacademy/wonder-blocks-tooltip@1.4.3
|
|
26
|
+
- @khanacademy/wonder-blocks-typography@1.1.36
|
|
27
|
+
|
|
3
28
|
## 1.1.26
|
|
4
29
|
|
|
5
30
|
### Patch Changes
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,761 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var _extends = require('@babel/runtime/helpers/extends');
|
|
6
|
+
var React = require('react');
|
|
7
|
+
var ReactDOM = require('react-dom');
|
|
8
|
+
var wonderBlocksCore = require('@khanacademy/wonder-blocks-core');
|
|
9
|
+
var wonderBlocksTooltip = require('@khanacademy/wonder-blocks-tooltip');
|
|
10
|
+
var wonderBlocksModal = require('@khanacademy/wonder-blocks-modal');
|
|
11
|
+
var aphrodite = require('aphrodite');
|
|
12
|
+
var Colors = require('@khanacademy/wonder-blocks-color');
|
|
13
|
+
var Spacing = require('@khanacademy/wonder-blocks-spacing');
|
|
14
|
+
var wonderBlocksTypography = require('@khanacademy/wonder-blocks-typography');
|
|
15
|
+
var wonderBlocksIcon = require('@khanacademy/wonder-blocks-icon');
|
|
16
|
+
var IconButton = require('@khanacademy/wonder-blocks-icon-button');
|
|
17
|
+
|
|
18
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
19
|
+
|
|
20
|
+
function _interopNamespace(e) {
|
|
21
|
+
if (e && e.__esModule) return e;
|
|
22
|
+
var n = Object.create(null);
|
|
23
|
+
if (e) {
|
|
24
|
+
Object.keys(e).forEach(function (k) {
|
|
25
|
+
if (k !== 'default') {
|
|
26
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
27
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
get: function () { return e[k]; }
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
n["default"] = e;
|
|
35
|
+
return Object.freeze(n);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
var _extends__default = /*#__PURE__*/_interopDefaultLegacy(_extends);
|
|
39
|
+
var React__namespace = /*#__PURE__*/_interopNamespace(React);
|
|
40
|
+
var ReactDOM__namespace = /*#__PURE__*/_interopNamespace(ReactDOM);
|
|
41
|
+
var ReactDOM__default = /*#__PURE__*/_interopDefaultLegacy(ReactDOM);
|
|
42
|
+
var Colors__default = /*#__PURE__*/_interopDefaultLegacy(Colors);
|
|
43
|
+
var Spacing__default = /*#__PURE__*/_interopDefaultLegacy(Spacing);
|
|
44
|
+
var IconButton__default = /*#__PURE__*/_interopDefaultLegacy(IconButton);
|
|
45
|
+
|
|
46
|
+
const defaultContext = {
|
|
47
|
+
close: undefined,
|
|
48
|
+
placement: "top"
|
|
49
|
+
};
|
|
50
|
+
var PopoverContext = React__namespace.createContext(defaultContext);
|
|
51
|
+
|
|
52
|
+
class PopoverAnchor extends React__namespace.Component {
|
|
53
|
+
componentDidMount() {
|
|
54
|
+
const anchorNode = ReactDOM__namespace.findDOMNode(this);
|
|
55
|
+
|
|
56
|
+
if (anchorNode) {
|
|
57
|
+
this.props.anchorRef(anchorNode);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
render() {
|
|
62
|
+
const {
|
|
63
|
+
children,
|
|
64
|
+
id,
|
|
65
|
+
onClick,
|
|
66
|
+
"aria-controls": ariaControls,
|
|
67
|
+
"aria-expanded": ariaExpanded
|
|
68
|
+
} = this.props;
|
|
69
|
+
const sharedProps = {
|
|
70
|
+
id: id,
|
|
71
|
+
"aria-controls": ariaControls,
|
|
72
|
+
"aria-expanded": ariaExpanded
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
if (typeof children === "function") {
|
|
76
|
+
const renderedChildren = children({
|
|
77
|
+
open: onClick
|
|
78
|
+
});
|
|
79
|
+
return React__namespace.cloneElement(renderedChildren, sharedProps);
|
|
80
|
+
} else {
|
|
81
|
+
return React__namespace.cloneElement(children, _extends__default["default"]({}, children.props, sharedProps, {
|
|
82
|
+
onClick: children.props.onClick ? e => {
|
|
83
|
+
e.stopPropagation();
|
|
84
|
+
children.props.onClick();
|
|
85
|
+
onClick();
|
|
86
|
+
} : onClick
|
|
87
|
+
}));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
class PopoverDialog extends React__namespace.Component {
|
|
94
|
+
componentDidUpdate(prevProps) {
|
|
95
|
+
if (prevProps.placement !== this.props.placement) {
|
|
96
|
+
this.props.onUpdate(this.props.placement);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
render() {
|
|
101
|
+
const {
|
|
102
|
+
placement,
|
|
103
|
+
children,
|
|
104
|
+
id,
|
|
105
|
+
isReferenceHidden,
|
|
106
|
+
updateBubbleRef,
|
|
107
|
+
updateTailRef,
|
|
108
|
+
tailOffset,
|
|
109
|
+
style,
|
|
110
|
+
"aria-describedby": ariaDescribedby
|
|
111
|
+
} = this.props;
|
|
112
|
+
const contentProps = children.props;
|
|
113
|
+
const color = contentProps.emphasized ? "blue" : contentProps.color;
|
|
114
|
+
return React__namespace.createElement(React__namespace.Fragment, null, React__namespace.createElement(wonderBlocksCore.View, {
|
|
115
|
+
"aria-describedby": ariaDescribedby,
|
|
116
|
+
id: id,
|
|
117
|
+
role: "dialog",
|
|
118
|
+
ref: updateBubbleRef,
|
|
119
|
+
"data-placement": placement,
|
|
120
|
+
style: [isReferenceHidden && styles$2.hide, styles$2[`content-${placement}`], style]
|
|
121
|
+
}, children, React__namespace.createElement(wonderBlocksTooltip.TooltipTail, {
|
|
122
|
+
color: color,
|
|
123
|
+
updateRef: updateTailRef,
|
|
124
|
+
placement: placement,
|
|
125
|
+
offset: tailOffset
|
|
126
|
+
})));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
}
|
|
130
|
+
const styles$2 = aphrodite.StyleSheet.create({
|
|
131
|
+
hide: {
|
|
132
|
+
pointerEvents: "none",
|
|
133
|
+
opacity: 0,
|
|
134
|
+
backgroundColor: "transparent",
|
|
135
|
+
color: "transparent"
|
|
136
|
+
},
|
|
137
|
+
"content-top": {
|
|
138
|
+
flexDirection: "column"
|
|
139
|
+
},
|
|
140
|
+
"content-right": {
|
|
141
|
+
flexDirection: "row-reverse"
|
|
142
|
+
},
|
|
143
|
+
"content-bottom": {
|
|
144
|
+
flexDirection: "column-reverse"
|
|
145
|
+
},
|
|
146
|
+
"content-left": {
|
|
147
|
+
flexDirection: "row"
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const FOCUSABLE_ELEMENTS = 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])';
|
|
152
|
+
function findFocusableNodes(root) {
|
|
153
|
+
return Array.from(root.querySelectorAll(FOCUSABLE_ELEMENTS));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
class InitialFocus extends React__namespace.Component {
|
|
157
|
+
constructor(...args) {
|
|
158
|
+
super(...args);
|
|
159
|
+
|
|
160
|
+
this.setInitialFocusableElement = node => {
|
|
161
|
+
const firstFocusableElement = this.maybeGetInitialFocusElement(node) || this.maybeGetFirstFocusableElement(node) || node;
|
|
162
|
+
|
|
163
|
+
if (firstFocusableElement === node) {
|
|
164
|
+
node.tabIndex = -1;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
setTimeout(() => {
|
|
168
|
+
firstFocusableElement.focus();
|
|
169
|
+
}, 0);
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
componentDidMount() {
|
|
174
|
+
const node = ReactDOM__namespace.findDOMNode(this);
|
|
175
|
+
|
|
176
|
+
if (!node) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
this.setInitialFocusableElement(node);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
maybeGetInitialFocusElement(node) {
|
|
184
|
+
const {
|
|
185
|
+
initialFocusId
|
|
186
|
+
} = this.props;
|
|
187
|
+
|
|
188
|
+
if (!initialFocusId) {
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return node.querySelector(`#${initialFocusId}`);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
maybeGetFirstFocusableElement(node) {
|
|
196
|
+
const focusableElements = findFocusableNodes(node);
|
|
197
|
+
|
|
198
|
+
if (!focusableElements.length) {
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return focusableElements[0];
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
render() {
|
|
206
|
+
return this.props.children;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
class FocusManager extends React__namespace.Component {
|
|
212
|
+
constructor(...args) {
|
|
213
|
+
super(...args);
|
|
214
|
+
this.focusableElementsInPopover = [];
|
|
215
|
+
|
|
216
|
+
this.addEventListeners = () => {
|
|
217
|
+
const {
|
|
218
|
+
anchorElement
|
|
219
|
+
} = this.props;
|
|
220
|
+
|
|
221
|
+
if (anchorElement) {
|
|
222
|
+
anchorElement.addEventListener("keydown", this.handleKeydownPreviousFocusableElement, true);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
this.nextElementAfterPopover = this.getNextFocusableElement();
|
|
226
|
+
|
|
227
|
+
if (this.nextElementAfterPopover) {
|
|
228
|
+
this.nextElementAfterPopover.addEventListener("keydown", this.handleKeydownNextFocusableElement, true);
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
this.getNextFocusableElement = () => {
|
|
233
|
+
const {
|
|
234
|
+
anchorElement
|
|
235
|
+
} = this.props;
|
|
236
|
+
|
|
237
|
+
if (!anchorElement || this.nextElementAfterPopover) {
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const focusableElements = findFocusableNodes(document);
|
|
242
|
+
const anchorIndex = focusableElements.indexOf(anchorElement);
|
|
243
|
+
|
|
244
|
+
if (anchorIndex >= 0) {
|
|
245
|
+
const nextElementIndex = anchorIndex < focusableElements.length - 1 ? anchorIndex + 1 : 0;
|
|
246
|
+
return focusableElements[nextElementIndex];
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return;
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
this.getComponentRootNode = node => {
|
|
253
|
+
if (!node) {
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const rootNode = ReactDOM__namespace.findDOMNode(node);
|
|
258
|
+
|
|
259
|
+
if (!rootNode) {
|
|
260
|
+
throw new Error("Assertion error: root node should exist after mount");
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
this.rootNode = rootNode;
|
|
264
|
+
this.focusableElementsInPopover = findFocusableNodes(this.rootNode);
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
this.handleFocusPreviousFocusableElement = () => {
|
|
268
|
+
if (this.props.anchorElement) {
|
|
269
|
+
this.props.anchorElement.focus();
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
this.handleFocusNextFocusableElement = () => {
|
|
274
|
+
if (this.nextElementAfterPopover) {
|
|
275
|
+
this.nextElementAfterPopover.focus();
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
this.handleKeydownPreviousFocusableElement = e => {
|
|
280
|
+
if (e.key === "Tab" && !e.shiftKey) {
|
|
281
|
+
e.preventDefault();
|
|
282
|
+
this.focusableElementsInPopover[0].focus();
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
this.handleKeydownNextFocusableElement = e => {
|
|
287
|
+
if (e.key === "Tab" && e.shiftKey) {
|
|
288
|
+
e.preventDefault();
|
|
289
|
+
const lastElementIndex = this.focusableElementsInPopover.length - 1;
|
|
290
|
+
this.focusableElementsInPopover[lastElementIndex].focus();
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
componentDidMount() {
|
|
296
|
+
this.addEventListeners();
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
componentDidUpdate() {
|
|
300
|
+
this.addEventListeners();
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
componentWillUnmount() {
|
|
304
|
+
const {
|
|
305
|
+
anchorElement
|
|
306
|
+
} = this.props;
|
|
307
|
+
|
|
308
|
+
if (anchorElement) {
|
|
309
|
+
setTimeout(() => anchorElement.focus(), 0);
|
|
310
|
+
anchorElement.removeEventListener("keydown", this.handleKeydownPreviousFocusableElement, true);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (this.nextElementAfterPopover) {
|
|
314
|
+
this.nextElementAfterPopover.removeEventListener("keydown", this.handleKeydownNextFocusableElement, true);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
render() {
|
|
319
|
+
const {
|
|
320
|
+
children
|
|
321
|
+
} = this.props;
|
|
322
|
+
return React__namespace.createElement(React__namespace.Fragment, null, React__namespace.createElement("div", {
|
|
323
|
+
tabIndex: "0",
|
|
324
|
+
onFocus: this.handleFocusPreviousFocusableElement,
|
|
325
|
+
style: {
|
|
326
|
+
position: "fixed"
|
|
327
|
+
}
|
|
328
|
+
}), React__namespace.createElement("div", {
|
|
329
|
+
ref: this.getComponentRootNode
|
|
330
|
+
}, React__namespace.createElement(InitialFocus, {
|
|
331
|
+
initialFocusId: this.props.initialFocusId
|
|
332
|
+
}, children)), React__namespace.createElement("div", {
|
|
333
|
+
tabIndex: "0",
|
|
334
|
+
onFocus: this.handleFocusNextFocusableElement,
|
|
335
|
+
style: {
|
|
336
|
+
position: "fixed"
|
|
337
|
+
}
|
|
338
|
+
}));
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
class PopoverEventListener extends React__namespace.Component {
|
|
344
|
+
constructor(...args) {
|
|
345
|
+
super(...args);
|
|
346
|
+
this.state = {
|
|
347
|
+
isFirstClick: true
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
this._handleKeyup = e => {
|
|
351
|
+
if (e.key === "Escape") {
|
|
352
|
+
e.preventDefault();
|
|
353
|
+
e.stopPropagation();
|
|
354
|
+
this.props.onClose();
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
this._handleClick = e => {
|
|
359
|
+
var _this$props$contentRe;
|
|
360
|
+
|
|
361
|
+
if (this.state.isFirstClick) {
|
|
362
|
+
this.setState({
|
|
363
|
+
isFirstClick: false
|
|
364
|
+
});
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const node = ReactDOM__default["default"].findDOMNode((_this$props$contentRe = this.props.contentRef) == null ? void 0 : _this$props$contentRe.current);
|
|
369
|
+
|
|
370
|
+
if (node && !node.contains(e.target)) {
|
|
371
|
+
e.preventDefault();
|
|
372
|
+
e.stopPropagation();
|
|
373
|
+
this.props.onClose();
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
componentDidMount() {
|
|
379
|
+
window.addEventListener("keyup", this._handleKeyup);
|
|
380
|
+
window.addEventListener("click", this._handleClick);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
componentWillUnmount() {
|
|
384
|
+
window.removeEventListener("keyup", this._handleKeyup);
|
|
385
|
+
window.removeEventListener("click", this._handleClick);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
render() {
|
|
389
|
+
return null;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
class Popover extends React__namespace.Component {
|
|
395
|
+
constructor(...args) {
|
|
396
|
+
super(...args);
|
|
397
|
+
this.state = {
|
|
398
|
+
opened: !!this.props.opened,
|
|
399
|
+
anchorElement: null,
|
|
400
|
+
placement: this.props.placement
|
|
401
|
+
};
|
|
402
|
+
this.contentRef = React__namespace.createRef();
|
|
403
|
+
|
|
404
|
+
this.handleClose = () => {
|
|
405
|
+
this.setState({
|
|
406
|
+
opened: false
|
|
407
|
+
}, () => {
|
|
408
|
+
this.props.onClose && this.props.onClose();
|
|
409
|
+
});
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
this.handleOpen = () => {
|
|
413
|
+
if (this.props.dismissEnabled && this.state.opened) {
|
|
414
|
+
this.setState({
|
|
415
|
+
opened: false
|
|
416
|
+
});
|
|
417
|
+
} else {
|
|
418
|
+
this.setState({
|
|
419
|
+
opened: true
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
this.updateRef = ref => {
|
|
425
|
+
const actualRef = ref && ReactDOM__default["default"].findDOMNode(ref);
|
|
426
|
+
|
|
427
|
+
if (actualRef && this.state.anchorElement !== actualRef) {
|
|
428
|
+
this.setState({
|
|
429
|
+
anchorElement: actualRef
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
static getDerivedStateFromProps(props, state) {
|
|
436
|
+
return {
|
|
437
|
+
opened: typeof props.opened === "boolean" ? props.opened : state.opened
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
renderContent() {
|
|
442
|
+
const {
|
|
443
|
+
content
|
|
444
|
+
} = this.props;
|
|
445
|
+
return React__namespace.cloneElement(typeof content === "function" ? content({
|
|
446
|
+
close: this.handleClose
|
|
447
|
+
}) : content, {
|
|
448
|
+
ref: this.contentRef
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
renderPopper(uniqueId) {
|
|
453
|
+
const {
|
|
454
|
+
initialFocusId,
|
|
455
|
+
placement
|
|
456
|
+
} = this.props;
|
|
457
|
+
const {
|
|
458
|
+
anchorElement
|
|
459
|
+
} = this.state;
|
|
460
|
+
return React__namespace.createElement(FocusManager, {
|
|
461
|
+
anchorElement: anchorElement,
|
|
462
|
+
initialFocusId: initialFocusId
|
|
463
|
+
}, React__namespace.createElement(wonderBlocksTooltip.TooltipPopper, {
|
|
464
|
+
anchorElement: anchorElement,
|
|
465
|
+
placement: placement
|
|
466
|
+
}, props => React__namespace.createElement(PopoverDialog, _extends__default["default"]({}, props, {
|
|
467
|
+
"aria-describedby": `${uniqueId}-anchor`,
|
|
468
|
+
id: uniqueId,
|
|
469
|
+
onUpdate: placement => this.setState({
|
|
470
|
+
placement
|
|
471
|
+
})
|
|
472
|
+
}), this.renderContent())));
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
getHost() {
|
|
476
|
+
return wonderBlocksModal.maybeGetPortalMountedModalHostElement(this.state.anchorElement) || document.body;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
render() {
|
|
480
|
+
const {
|
|
481
|
+
children,
|
|
482
|
+
dismissEnabled,
|
|
483
|
+
id
|
|
484
|
+
} = this.props;
|
|
485
|
+
const {
|
|
486
|
+
opened,
|
|
487
|
+
placement
|
|
488
|
+
} = this.state;
|
|
489
|
+
const popperHost = this.getHost();
|
|
490
|
+
return React__namespace.createElement(PopoverContext.Provider, {
|
|
491
|
+
value: {
|
|
492
|
+
close: this.handleClose,
|
|
493
|
+
placement: placement
|
|
494
|
+
}
|
|
495
|
+
}, React__namespace.createElement(wonderBlocksCore.IDProvider, {
|
|
496
|
+
id: id,
|
|
497
|
+
scope: "popover"
|
|
498
|
+
}, uniqueId => React__namespace.createElement(React__namespace.Fragment, null, React__namespace.createElement(PopoverAnchor, {
|
|
499
|
+
anchorRef: this.updateRef,
|
|
500
|
+
id: `${uniqueId}-anchor`,
|
|
501
|
+
"aria-controls": uniqueId,
|
|
502
|
+
"aria-expanded": opened ? "true" : "false",
|
|
503
|
+
onClick: this.handleOpen
|
|
504
|
+
}, children), popperHost && opened && ReactDOM__default["default"].createPortal(this.renderPopper(uniqueId), popperHost))), dismissEnabled && opened && React__namespace.createElement(PopoverEventListener, {
|
|
505
|
+
onClose: this.handleClose,
|
|
506
|
+
contentRef: this.contentRef
|
|
507
|
+
}));
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
}
|
|
511
|
+
Popover.defaultProps = {
|
|
512
|
+
placement: "top"
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
class CloseButton extends React__namespace.Component {
|
|
516
|
+
render() {
|
|
517
|
+
const {
|
|
518
|
+
light,
|
|
519
|
+
"aria-label": ariaLabel,
|
|
520
|
+
style,
|
|
521
|
+
testId
|
|
522
|
+
} = this.props;
|
|
523
|
+
return React__namespace.createElement(PopoverContext.Consumer, null, ({
|
|
524
|
+
close
|
|
525
|
+
}) => {
|
|
526
|
+
return React__namespace.createElement(IconButton__default["default"], {
|
|
527
|
+
icon: wonderBlocksIcon.icons.dismiss,
|
|
528
|
+
"aria-label": ariaLabel,
|
|
529
|
+
onClick: close,
|
|
530
|
+
kind: light ? "primary" : "tertiary",
|
|
531
|
+
light: light,
|
|
532
|
+
style: style,
|
|
533
|
+
testId: testId
|
|
534
|
+
});
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
}
|
|
539
|
+
CloseButton.defaultProps = {
|
|
540
|
+
light: true,
|
|
541
|
+
"aria-label": "Close Popover"
|
|
542
|
+
};
|
|
543
|
+
|
|
544
|
+
class PopoverContentCore extends React__namespace.Component {
|
|
545
|
+
render() {
|
|
546
|
+
const {
|
|
547
|
+
children,
|
|
548
|
+
closeButtonLight,
|
|
549
|
+
closeButtonLabel,
|
|
550
|
+
closeButtonVisible,
|
|
551
|
+
color,
|
|
552
|
+
style,
|
|
553
|
+
testId
|
|
554
|
+
} = this.props;
|
|
555
|
+
return React__namespace.createElement(wonderBlocksCore.View, {
|
|
556
|
+
testId: testId,
|
|
557
|
+
style: [styles$1.content, color !== "white" && styles$1[color], style]
|
|
558
|
+
}, closeButtonVisible && React__namespace.createElement(CloseButton, {
|
|
559
|
+
"aria-label": closeButtonLabel,
|
|
560
|
+
light: closeButtonLight || color !== "white",
|
|
561
|
+
style: styles$1.closeButton,
|
|
562
|
+
testId: `${testId || "popover"}-close-btn`
|
|
563
|
+
}), children);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
}
|
|
567
|
+
PopoverContentCore.defaultProps = {
|
|
568
|
+
color: "white",
|
|
569
|
+
closeButtonLight: false,
|
|
570
|
+
closeButtonVisible: false
|
|
571
|
+
};
|
|
572
|
+
const styles$1 = aphrodite.StyleSheet.create({
|
|
573
|
+
content: {
|
|
574
|
+
borderRadius: Spacing__default["default"].xxxSmall_4,
|
|
575
|
+
border: `solid 1px ${Colors__default["default"].offBlack16}`,
|
|
576
|
+
backgroundColor: Colors__default["default"].white,
|
|
577
|
+
boxShadow: `0 ${Spacing__default["default"].xSmall_8}px ${Spacing__default["default"].xSmall_8}px 0 ${Colors__default["default"].offBlack8}`,
|
|
578
|
+
margin: 0,
|
|
579
|
+
maxWidth: Spacing__default["default"].medium_16 * 18,
|
|
580
|
+
padding: Spacing__default["default"].large_24,
|
|
581
|
+
overflow: "hidden",
|
|
582
|
+
justifyContent: "center"
|
|
583
|
+
},
|
|
584
|
+
blue: {
|
|
585
|
+
backgroundColor: Colors__default["default"].blue,
|
|
586
|
+
color: Colors__default["default"].white
|
|
587
|
+
},
|
|
588
|
+
darkBlue: {
|
|
589
|
+
backgroundColor: Colors__default["default"].darkBlue,
|
|
590
|
+
color: Colors__default["default"].white
|
|
591
|
+
},
|
|
592
|
+
closeButton: {
|
|
593
|
+
position: "absolute",
|
|
594
|
+
right: Spacing__default["default"].xSmall_8,
|
|
595
|
+
top: Spacing__default["default"].xSmall_8,
|
|
596
|
+
zIndex: 1
|
|
597
|
+
}
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
const StyledImage = wonderBlocksCore.addStyle("img");
|
|
601
|
+
class PopoverContent extends React__namespace.Component {
|
|
602
|
+
constructor(...args) {
|
|
603
|
+
super(...args);
|
|
604
|
+
|
|
605
|
+
this.maybeRenderImage = ({
|
|
606
|
+
placement
|
|
607
|
+
}) => {
|
|
608
|
+
const {
|
|
609
|
+
image
|
|
610
|
+
} = this.props;
|
|
611
|
+
|
|
612
|
+
if (!image) {
|
|
613
|
+
return null;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
return React__namespace.createElement(wonderBlocksCore.View, {
|
|
617
|
+
style: [styles.image, placement === "bottom" && styles.imageToBottom]
|
|
618
|
+
}, image);
|
|
619
|
+
};
|
|
620
|
+
|
|
621
|
+
this.maybeRenderIcon = () => {
|
|
622
|
+
const {
|
|
623
|
+
icon
|
|
624
|
+
} = this.props;
|
|
625
|
+
|
|
626
|
+
if (!icon) {
|
|
627
|
+
return null;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
return React__namespace.createElement(wonderBlocksCore.View, {
|
|
631
|
+
style: styles.iconContainer
|
|
632
|
+
}, typeof icon !== "string" ? icon : React__namespace.createElement(StyledImage, {
|
|
633
|
+
src: icon,
|
|
634
|
+
style: styles.icon
|
|
635
|
+
}));
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
this.maybeRenderActions = close => {
|
|
639
|
+
const {
|
|
640
|
+
actions
|
|
641
|
+
} = this.props;
|
|
642
|
+
|
|
643
|
+
if (!actions) {
|
|
644
|
+
return null;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
return React__namespace.createElement(wonderBlocksCore.View, {
|
|
648
|
+
style: styles.actions
|
|
649
|
+
}, typeof actions === "function" ? actions({
|
|
650
|
+
close: close
|
|
651
|
+
}) : actions);
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
componentDidMount() {
|
|
656
|
+
const {
|
|
657
|
+
icon,
|
|
658
|
+
image
|
|
659
|
+
} = this.props;
|
|
660
|
+
|
|
661
|
+
if (image && icon) {
|
|
662
|
+
throw new Error("'image' and 'icon' cannot be used at the same time. You can fix this by either removing 'image' or 'icon' from your instance.");
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
validateProps({
|
|
667
|
+
placement
|
|
668
|
+
}) {
|
|
669
|
+
if (this.props.image && (placement === "left" || placement === "right")) {
|
|
670
|
+
throw new Error("'image' can only be vertically placed. You can fix this by either changing `placement` to `top` or `bottom` or removing the `image` prop inside `content`.");
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
render() {
|
|
675
|
+
const {
|
|
676
|
+
closeButtonLabel,
|
|
677
|
+
closeButtonVisible,
|
|
678
|
+
content,
|
|
679
|
+
emphasized = undefined,
|
|
680
|
+
icon,
|
|
681
|
+
image,
|
|
682
|
+
style,
|
|
683
|
+
title,
|
|
684
|
+
testId
|
|
685
|
+
} = this.props;
|
|
686
|
+
return React__namespace.createElement(PopoverContext.Consumer, null, ({
|
|
687
|
+
close,
|
|
688
|
+
placement
|
|
689
|
+
}) => {
|
|
690
|
+
this.validateProps({
|
|
691
|
+
close,
|
|
692
|
+
placement
|
|
693
|
+
});
|
|
694
|
+
return React__namespace.createElement(PopoverContentCore, {
|
|
695
|
+
color: emphasized ? "blue" : "white",
|
|
696
|
+
closeButtonLight: image && placement === "top",
|
|
697
|
+
closeButtonLabel: closeButtonLabel,
|
|
698
|
+
closeButtonVisible: closeButtonVisible,
|
|
699
|
+
style: style,
|
|
700
|
+
testId: testId
|
|
701
|
+
}, React__namespace.createElement(wonderBlocksCore.View, {
|
|
702
|
+
style: !!icon && styles.withIcon
|
|
703
|
+
}, this.maybeRenderImage({
|
|
704
|
+
placement
|
|
705
|
+
}), this.maybeRenderIcon(), React__namespace.createElement(wonderBlocksCore.View, {
|
|
706
|
+
style: styles.text
|
|
707
|
+
}, React__namespace.createElement(wonderBlocksTypography.HeadingSmall, {
|
|
708
|
+
style: styles.title
|
|
709
|
+
}, title), React__namespace.createElement(wonderBlocksTypography.Body, null, content))), this.maybeRenderActions(close));
|
|
710
|
+
});
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
}
|
|
714
|
+
PopoverContent.defaultProps = {
|
|
715
|
+
closeButtonVisible: false
|
|
716
|
+
};
|
|
717
|
+
const styles = aphrodite.StyleSheet.create({
|
|
718
|
+
actions: {
|
|
719
|
+
marginTop: Spacing__default["default"].large_24,
|
|
720
|
+
flexDirection: "row",
|
|
721
|
+
alignItems: "center",
|
|
722
|
+
justifyContent: "flex-end"
|
|
723
|
+
},
|
|
724
|
+
text: {
|
|
725
|
+
justifyContent: "center"
|
|
726
|
+
},
|
|
727
|
+
title: {
|
|
728
|
+
marginBottom: Spacing__default["default"].xSmall_8
|
|
729
|
+
},
|
|
730
|
+
iconContainer: {
|
|
731
|
+
alignItems: "center",
|
|
732
|
+
justifyContent: "center",
|
|
733
|
+
height: Spacing__default["default"].xxxLarge_64,
|
|
734
|
+
width: Spacing__default["default"].xxxLarge_64,
|
|
735
|
+
minWidth: Spacing__default["default"].xxxLarge_64,
|
|
736
|
+
marginRight: Spacing__default["default"].medium_16,
|
|
737
|
+
overflow: "hidden"
|
|
738
|
+
},
|
|
739
|
+
icon: {
|
|
740
|
+
width: "100%"
|
|
741
|
+
},
|
|
742
|
+
withIcon: {
|
|
743
|
+
flexDirection: "row"
|
|
744
|
+
},
|
|
745
|
+
image: {
|
|
746
|
+
marginBottom: Spacing__default["default"].large_24,
|
|
747
|
+
marginLeft: -Spacing__default["default"].large_24,
|
|
748
|
+
marginRight: -Spacing__default["default"].large_24,
|
|
749
|
+
marginTop: -Spacing__default["default"].large_24,
|
|
750
|
+
width: `calc(100% + ${Spacing__default["default"].large_24 * 2}px)`
|
|
751
|
+
},
|
|
752
|
+
imageToBottom: {
|
|
753
|
+
marginBottom: -Spacing__default["default"].large_24,
|
|
754
|
+
marginTop: Spacing__default["default"].large_24,
|
|
755
|
+
order: 1
|
|
756
|
+
}
|
|
757
|
+
});
|
|
758
|
+
|
|
759
|
+
exports.Popover = Popover;
|
|
760
|
+
exports.PopoverContent = PopoverContent;
|
|
761
|
+
exports.PopoverContentCore = PopoverContentCore;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@khanacademy/wonder-blocks-popover",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.28",
|
|
4
4
|
"design": "v1",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -17,14 +17,13 @@
|
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"@babel/runtime": "^7.18.6",
|
|
19
19
|
"@khanacademy/wonder-blocks-color": "^1.2.0",
|
|
20
|
-
"@khanacademy/wonder-blocks-core": "^4.6.
|
|
21
|
-
"@khanacademy/wonder-blocks-icon": "^1.2.
|
|
22
|
-
"@khanacademy/wonder-blocks-icon-button": "^3.4.
|
|
23
|
-
"@khanacademy/wonder-blocks-
|
|
24
|
-
"@khanacademy/wonder-blocks-modal": "^3.0.3",
|
|
20
|
+
"@khanacademy/wonder-blocks-core": "^4.6.2",
|
|
21
|
+
"@khanacademy/wonder-blocks-icon": "^1.2.35",
|
|
22
|
+
"@khanacademy/wonder-blocks-icon-button": "^3.4.19",
|
|
23
|
+
"@khanacademy/wonder-blocks-modal": "^3.0.5",
|
|
25
24
|
"@khanacademy/wonder-blocks-spacing": "^3.0.5",
|
|
26
|
-
"@khanacademy/wonder-blocks-tooltip": "^1.4.
|
|
27
|
-
"@khanacademy/wonder-blocks-typography": "^1.1.
|
|
25
|
+
"@khanacademy/wonder-blocks-tooltip": "^1.4.4",
|
|
26
|
+
"@khanacademy/wonder-blocks-typography": "^1.1.37"
|
|
28
27
|
},
|
|
29
28
|
"peerDependencies": {
|
|
30
29
|
"@popperjs/core": "^2.10.1",
|
|
@@ -34,6 +33,6 @@
|
|
|
34
33
|
"react-popper": "^2.0.0"
|
|
35
34
|
},
|
|
36
35
|
"devDependencies": {
|
|
37
|
-
"wb-dev-build-settings": "^0.
|
|
36
|
+
"wb-dev-build-settings": "^0.7.0"
|
|
38
37
|
}
|
|
39
|
-
}
|
|
38
|
+
}
|