@targetprocess/popover 12.23.0 → 12.23.1-alpha.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/dist/popover.js DELETED
@@ -1,783 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
-
7
- var React = require('react');
8
- var React__default = _interopDefault(React);
9
- var globalConstants = require('@targetprocess/global-constants');
10
- var classNames = _interopDefault(require('classnames'));
11
- var ReactDOM = _interopDefault(require('react-dom'));
12
- var FocusTrap = _interopDefault(require('focus-trap-react'));
13
-
14
- function _classCallCheck(instance, Constructor) {
15
- if (!(instance instanceof Constructor)) {
16
- throw new TypeError("Cannot call a class as a function");
17
- }
18
- }
19
- function _defineProperties(target, props) {
20
- for (var i = 0; i < props.length; i++) {
21
- var descriptor = props[i];
22
- descriptor.enumerable = descriptor.enumerable || false;
23
- descriptor.configurable = true;
24
- if ("value" in descriptor) descriptor.writable = true;
25
- Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
26
- }
27
- }
28
- function _createClass(Constructor, protoProps, staticProps) {
29
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
30
- if (staticProps) _defineProperties(Constructor, staticProps);
31
- Object.defineProperty(Constructor, "prototype", {
32
- writable: false
33
- });
34
- return Constructor;
35
- }
36
- function _extends() {
37
- _extends = Object.assign ? Object.assign.bind() : function (target) {
38
- for (var i = 1; i < arguments.length; i++) {
39
- var source = arguments[i];
40
- for (var key in source) {
41
- if (Object.prototype.hasOwnProperty.call(source, key)) {
42
- target[key] = source[key];
43
- }
44
- }
45
- }
46
- return target;
47
- };
48
- return _extends.apply(this, arguments);
49
- }
50
- function _inherits(subClass, superClass) {
51
- if (typeof superClass !== "function" && superClass !== null) {
52
- throw new TypeError("Super expression must either be null or a function");
53
- }
54
- subClass.prototype = Object.create(superClass && superClass.prototype, {
55
- constructor: {
56
- value: subClass,
57
- writable: true,
58
- configurable: true
59
- }
60
- });
61
- Object.defineProperty(subClass, "prototype", {
62
- writable: false
63
- });
64
- if (superClass) _setPrototypeOf(subClass, superClass);
65
- }
66
- function _getPrototypeOf(o) {
67
- _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) {
68
- return o.__proto__ || Object.getPrototypeOf(o);
69
- };
70
- return _getPrototypeOf(o);
71
- }
72
- function _setPrototypeOf(o, p) {
73
- _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
74
- o.__proto__ = p;
75
- return o;
76
- };
77
- return _setPrototypeOf(o, p);
78
- }
79
- function _assertThisInitialized(self) {
80
- if (self === void 0) {
81
- throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
82
- }
83
- return self;
84
- }
85
- function _possibleConstructorReturn(self, call) {
86
- if (call && (typeof call === "object" || typeof call === "function")) {
87
- return call;
88
- } else if (call !== void 0) {
89
- throw new TypeError("Derived constructors may only return object or undefined");
90
- }
91
- return _assertThisInitialized(self);
92
- }
93
- function _toPrimitive(input, hint) {
94
- if (typeof input !== "object" || input === null) return input;
95
- var prim = input[Symbol.toPrimitive];
96
- if (prim !== undefined) {
97
- var res = prim.call(input, hint || "default");
98
- if (typeof res !== "object") return res;
99
- throw new TypeError("@@toPrimitive must return a primitive value.");
100
- }
101
- return (hint === "string" ? String : Number)(input);
102
- }
103
- function _toPropertyKey(arg) {
104
- var key = _toPrimitive(arg, "string");
105
- return typeof key === "symbol" ? key : String(key);
106
- }
107
-
108
- /*********************************************************************
109
- * © Copyright IBM Corp. 2024
110
- *********************************************************************/
111
- var PopoverPosition = {
112
- bottomLeft: function bottomLeft(hostRect, popoverRect, containerScroll) {
113
- return {
114
- left: 0,
115
- top: hostRect.height + parseInt(globalConstants.SpaceXS) - containerScroll.top
116
- };
117
- },
118
- bottom: function bottom(hostRect, popoverRect, containerScroll) {
119
- return {
120
- left: '50%',
121
- marginLeft: -popoverRect.width / 2,
122
- top: hostRect.height + parseInt(globalConstants.SpaceXS) - containerScroll.top
123
- };
124
- },
125
- bottomRight: function bottomRight(hostRect, popoverRect, containerScroll) {
126
- return {
127
- right: 0,
128
- top: hostRect.height + parseInt(globalConstants.SpaceXS) - containerScroll.top
129
- };
130
- },
131
- topLeft: function topLeft(hostRect, popoverRect, containerScroll) {
132
- return {
133
- left: 0,
134
- bottom: hostRect.height + parseInt(globalConstants.SpaceXS) + containerScroll.top
135
- };
136
- },
137
- top: function top(hostRect, popoverRect, containerScroll) {
138
- return {
139
- left: '50%',
140
- marginLeft: -popoverRect.width / 2,
141
- bottom: hostRect.height + parseInt(globalConstants.SpaceXS) + containerScroll.top
142
- };
143
- },
144
- topRight: function topRight(hostRect, popoverRect, containerScroll) {
145
- return {
146
- right: 0,
147
- bottom: hostRect.height + parseInt(globalConstants.SpaceXS) + containerScroll.top
148
- };
149
- },
150
- leftTop: function leftTop(hostRect, popoverRect, containerScroll) {
151
- return {
152
- right: hostRect.width + parseInt(globalConstants.SpaceXS) - containerScroll.left,
153
- top: 0
154
- };
155
- },
156
- left: function left(hostRect, popoverRect, containerScroll) {
157
- return {
158
- right: hostRect.width + parseInt(globalConstants.SpaceXS) - containerScroll.left,
159
- marginTop: -popoverRect.height / 2,
160
- top: '50%'
161
- };
162
- },
163
- leftBottom: function leftBottom(hostRect, popoverRect, containerScroll) {
164
- return {
165
- right: hostRect.width + parseInt(globalConstants.SpaceXS) - containerScroll.left,
166
- marginTop: -popoverRect.height / 2,
167
- bottom: 0
168
- };
169
- },
170
- rightTop: function rightTop(hostRect, popoverRect, containerScroll) {
171
- return {
172
- left: hostRect.width + parseInt(globalConstants.SpaceXS) + containerScroll.left,
173
- top: 0
174
- };
175
- },
176
- right: function right(hostRect, popoverRect, containerScroll) {
177
- return {
178
- left: hostRect.width + parseInt(globalConstants.SpaceXS) + containerScroll.left,
179
- marginTop: -popoverRect.height / 2,
180
- top: '50%'
181
- };
182
- },
183
- rightBottom: function rightBottom(hostRect, popoverRect, containerScroll) {
184
- return {
185
- left: hostRect.width + parseInt(globalConstants.SpaceXS) + containerScroll.left,
186
- marginTop: -popoverRect.height / 2,
187
- bottom: 0
188
- };
189
- }
190
- };
191
-
192
- function styleInject(css, ref) {
193
- if ( ref === void 0 ) ref = {};
194
- var insertAt = ref.insertAt;
195
-
196
- if (!css || typeof document === 'undefined') { return; }
197
-
198
- var head = document.head || document.getElementsByTagName('head')[0];
199
- var style = document.createElement('style');
200
- style.type = 'text/css';
201
-
202
- if (insertAt === 'top') {
203
- if (head.firstChild) {
204
- head.insertBefore(style, head.firstChild);
205
- } else {
206
- head.appendChild(style);
207
- }
208
- } else {
209
- head.appendChild(style);
210
- }
211
-
212
- if (style.styleSheet) {
213
- style.styleSheet.cssText = css;
214
- } else {
215
- style.appendChild(document.createTextNode(css));
216
- }
217
- }
218
-
219
- var css = "/*********************************************************************\n* © Copyright IBM Corp. 2024\n*********************************************************************/\n.popover-body-container__popover__12-23-0 {\n -webkit-box-shadow: var(--BoxShadowL, 0 4px 16px 0 rgba(0, 0, 0, 0.12));\n box-shadow: var(--BoxShadowL, 0 4px 16px 0 rgba(0, 0, 0, 0.12));\n border-radius: var(--BorderRadiusL, 4px);\n border: 1px solid var(--HighlightBlockBorderColor, rgba(64, 66, 73, 0.1));\n background: var(--BackgroundDefault, #FFFFFF);\n z-index: var(--ZIndexModal, 10001);\n -webkit-box-sizing: border-box;\n box-sizing: border-box;\n}\n.popover-body__popover__12-23-0 {\n -webkit-box-sizing: border-box;\n box-sizing: border-box;\n padding: var(--SpaceSM, 12px);\n font-size: var(--FontSizeDefault, 13px);\n color: var(--FontColorDefault, #000000);\n}\n.popover-body--no-padding__popover__12-23-0 {\n padding: 0;\n }\n.popover-tip__popover__12-23-0 {\n position: absolute;\n pointer-events: none;\n z-index: calc(var(--ZIndexModal, 10001) + 1);\n overflow: hidden;\n display: -webkit-box;\n display: -ms-flexbox;\n display: flex;\n -webkit-box-align: center;\n -ms-flex-align: center;\n align-items: center;\n -webkit-box-pack: center;\n -ms-flex-pack: center;\n justify-content: center;\n}\n.popover-tip__popover__12-23-0:before {\n content: '';\n position: absolute;\n display: block;\n -webkit-box-sizing: border-box;\n box-sizing: border-box;\n background-color: var(--BackgroundDefault, #FFFFFF);\n border: 1px solid var(--HighlightBlockBorderColor, rgba(64, 66, 73, 0.1));\n /*\n geometry FTW\n\n 2*$popoverTipSize\n _______ _______\n | /|\\ |\n | / | \\ | $popoverTipSize\n | / | \\ |\n |/______|______\\|\n since tip is rotated 45 degrees, its width and height is a\n diagonal of a square with side=$popoverTipSize, thus diagonal\n is sqrt(2) * $popoverTipSize.\n sqrt(2) ~= 1.414, 1.4 used here to prevent too much rounding\n */\n width: 11.2px;\n height: 11.2px;\n -webkit-transform: rotate(45deg);\n transform: rotate(45deg);\n }\n.popover-tip--bottom__popover__12-23-0,\n .popover-tip--top__popover__12-23-0 {\n width: 100%;\n border-top: 1px var(--BackgroundDefault, #FFFFFF);\n border-bottom: 1px var(--BackgroundDefault, #FFFFFF);\n height: 9px;\n }\n.popover-tip--left__popover__12-23-0,\n .popover-tip--right__popover__12-23-0 {\n border-left: 1px var(--BackgroundDefault, #FFFFFF);\n border-right: 1px var(--BackgroundDefault, #FFFFFF);\n height: 100%;\n width: 9px;\n }\n/*\n To determine tip position, we need to rotate it\n along the \"tip\" point, after that, the \"tip\" point\n can be placed with exactly 50% offset from the origin\n so that it is centered.\n */\n.popover-tip--bottom__popover__12-23-0:before {\n -webkit-transform-origin: left top;\n transform-origin: left top;\n left: 50%;\n top: 0;\n }\n.popover-tip--top__popover__12-23-0:before {\n -webkit-transform-origin: right bottom;\n transform-origin: right bottom;\n right: 50%;\n bottom: 0;\n }\n.popover-tip--left__popover__12-23-0:before {\n -webkit-transform-origin: right top;\n transform-origin: right top;\n right: 0;\n top: 50%;\n }\n.popover-tip--right__popover__12-23-0:before {\n -webkit-transform-origin: left bottom;\n transform-origin: left bottom;\n left: 0;\n bottom: 50%;\n }\n";
220
- var styles = {"popover-body-container":"popover-body-container__popover__12-23-0","popover-body":"popover-body__popover__12-23-0","popover-body--no-padding":"popover-body--no-padding__popover__12-23-0","popover-tip":"popover-tip__popover__12-23-0","popover-tip--bottom":"popover-tip--bottom__popover__12-23-0","popover-tip--top":"popover-tip--top__popover__12-23-0","popover-tip--left":"popover-tip--left__popover__12-23-0","popover-tip--right":"popover-tip--right__popover__12-23-0"};
221
- styleInject(css);
222
-
223
- /*********************************************************************
224
- * © Copyright IBM Corp. 2024
225
- *********************************************************************/
226
- var PopoverBody = function PopoverBody(_ref) {
227
- var children = _ref.children,
228
- className = _ref.className,
229
- classNameBody = _ref.classNameBody,
230
- noPadding = _ref.noPadding;
231
- return React__default.createElement("div", {
232
- className: classNames(className, styles['popover-body-container'])
233
- }, React__default.createElement("div", {
234
- className: classNames(classNameBody, styles['popover-body'], noPadding && styles['popover-body--no-padding'])
235
- }, children));
236
- };
237
-
238
- /*********************************************************************
239
- * © Copyright IBM Corp. 2024
240
- *********************************************************************/
241
- var throttle = function throttle(func, limit) {
242
- var lastFunc = null;
243
- var lastRan = null;
244
- return function () {
245
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
246
- args[_key] = arguments[_key];
247
- }
248
- if (lastRan) {
249
- clearTimeout(lastFunc);
250
- lastFunc = setTimeout(function () {
251
- if (Date.now() - lastRan >= limit) {
252
- func.apply(void 0, args);
253
- lastRan = Date.now();
254
- }
255
- }, limit - (Date.now() - lastRan));
256
- } else {
257
- func.apply(void 0, args);
258
- lastRan = Date.now();
259
- }
260
- };
261
- };
262
-
263
- /*********************************************************************
264
- * © Copyright IBM Corp. 2024
265
- *********************************************************************/
266
- var generateId = function generateId(prefix) {
267
- return "".concat(prefix).concat(Math.random().toString(36).substring(7));
268
- };
269
- var isFocusable = function isFocusable(element) {
270
- if (element.tabIndex < 0) {
271
- return false;
272
- }
273
- if ('disabled' in element && element.disabled) {
274
- return false;
275
- }
276
- if (element.tabIndex === 0) {
277
- return true;
278
- }
279
- switch (element.nodeName) {
280
- case 'A':
281
- return element.href;
282
- case 'INPUT':
283
- return element.type !== 'hidden';
284
- case 'BUTTON':
285
- case 'SELECT':
286
- case 'TEXTAREA':
287
- return true;
288
- default:
289
- return false;
290
- }
291
- };
292
- var attemptFocus = function attemptFocus(element) {
293
- if (!isFocusable(element)) {
294
- return false;
295
- }
296
- element.focus();
297
- return document.activeElement === element;
298
- };
299
- var focusFirstDescendant = function focusFirstDescendant(element) {
300
- var _arr = Array.from(element.childNodes);
301
- for (var _i = 0; _i < _arr.length; _i++) {
302
- var child = _arr[_i];
303
- if (attemptFocus(child) || focusFirstDescendant(child)) {
304
- return true;
305
- }
306
- }
307
- return false;
308
- };
309
-
310
- var popoverContainerPrefix = 'popover-container-';
311
- var isScrollParent = function isScrollParent(element) {
312
- // Firefox wants us to check `-x` and `-y` variations as well
313
- var _getComputedStyle = getComputedStyle(element),
314
- overflow = _getComputedStyle.overflow,
315
- overflowX = _getComputedStyle.overflowX,
316
- overflowY = _getComputedStyle.overflowY;
317
- return /auto|scroll|overlay|hidden/.test(overflow + overflowY + overflowX);
318
- };
319
- function getNodeName(element) {
320
- return element ? (element.nodeName || '').toLowerCase() : null;
321
- }
322
- function getNodeScroll(node) {
323
- if (node === window) {
324
- return {
325
- scrollLeft: window.pageXOffset,
326
- scrollTop: window.pageYOffset
327
- };
328
- }
329
- return {
330
- scrollLeft: node.scrollLeft,
331
- scrollTop: node.scrollTop
332
- };
333
- }
334
- var getCompositeRect = function getCompositeRect(elementOrVirtualElement, offsetParent) {
335
- var rect = elementOrVirtualElement.getBoundingClientRect();
336
- var scroll = {
337
- scrollLeft: 0,
338
- scrollTop: 0
339
- };
340
- if (
341
- // https://github.com/popperjs/popper-core/issues/1078
342
- getNodeName(offsetParent) !== 'body' || isScrollParent(document.documentElement)) {
343
- scroll = getNodeScroll(offsetParent);
344
- }
345
- var offsets = offsetParent.getBoundingClientRect();
346
- offsets.x += offsetParent.clientLeft;
347
- offsets.y += offsetParent.clientTop;
348
- return {
349
- left: rect.left + scroll.scrollLeft - offsets.x,
350
- top: rect.top + scroll.scrollTop - offsets.y,
351
- right: rect.right + scroll.scrollLeft - offsets.x,
352
- bottom: rect.bottom + scroll.scrollTop - offsets.y,
353
- width: rect.width,
354
- height: rect.height
355
- };
356
- };
357
- var PopoverContainer = /*#__PURE__*/function (_React$Component) {
358
- _inherits(PopoverContainer, _React$Component);
359
- function PopoverContainer() {
360
- var _this;
361
- _classCallCheck(this, PopoverContainer);
362
- _this = _possibleConstructorReturn(this, _getPrototypeOf(PopoverContainer).apply(this, arguments));
363
- _this.mounted = false;
364
- _this.popoverContentRef = React__default.createRef();
365
- _this.state = {
366
- adjusted: false,
367
- positionX: 0,
368
- positionY: 0
369
- };
370
- _this.popoverContainerId = generateId(popoverContainerPrefix);
371
- _this.onResize = function () {
372
- return _this.adjustPosition();
373
- };
374
- _this.onScroll = throttle(function () {
375
- _this.adjustPosition();
376
- }, _this.props.scrollThrottleIntervalMs || 200);
377
- _this.getPopoverTipPosition = function (popoverPosition, containerPosition) {
378
- if (popoverPosition.bottom && popoverPosition.bottom >= containerPosition.height) {
379
- return 'top';
380
- }
381
- if (popoverPosition.top && popoverPosition.top >= containerPosition.height) {
382
- return 'bottom';
383
- }
384
- if (popoverPosition.left && popoverPosition.left >= containerPosition.width) {
385
- return 'right';
386
- }
387
- if (popoverPosition.right && popoverPosition.right >= containerPosition.width) {
388
- return 'left';
389
- }
390
- return null;
391
- };
392
- _this.handleKeyDown = function (event) {
393
- if (event.key === 'Escape') {
394
- event.stopPropagation();
395
- _this.props.onEscapeAction(event);
396
- }
397
- };
398
- return _this;
399
- }
400
- _createClass(PopoverContainer, [{
401
- key: "adjustPosition",
402
- value: function adjustPosition() {
403
- var _this2 = this;
404
- if (!this.mounted) {
405
- return;
406
- }
407
- var containerElement = this.props.containerElement || document.body;
408
- var positioningBoundingRect = getCompositeRect(this.props.positioningTarget, this.props.containerElement);
409
- this.setState({
410
- hostRect: {
411
- top: this.props.relativeToParent ? 0 : positioningBoundingRect.top,
412
- left: this.props.relativeToParent ? 0 : positioningBoundingRect.left,
413
- right: this.props.relativeToParent ? 0 : positioningBoundingRect.right,
414
- bottom: this.props.relativeToParent ? 0 : positioningBoundingRect.bottom,
415
- width: positioningBoundingRect.width,
416
- height: positioningBoundingRect.height
417
- },
418
- popoverRect: this.popoverContentRef.current.getBoundingClientRect(),
419
- scroll: containerElement === document.body || !this.props.trackScroll ? {
420
- left: 0,
421
- top: 0
422
- } : {
423
- left: containerElement.scrollLeft,
424
- top: containerElement.scrollTop
425
- }
426
- });
427
- window.requestAnimationFrame(function () {
428
- if (_this2.mounted) {
429
- _this2.setState({
430
- adjusted: true
431
- });
432
- }
433
- });
434
- }
435
- }, {
436
- key: "componentDidMount",
437
- value: function componentDidMount() {
438
- this.mounted = true;
439
- this.adjustPosition();
440
- if (this.props.trackScroll) {
441
- document.addEventListener('scroll', this.onScroll);
442
- window.addEventListener('resize', this.onResize);
443
- if (this.props.containerElement && this.props.containerElement !== document.body) {
444
- this.props.containerElement.addEventListener('scroll', this.onScroll);
445
- }
446
- }
447
- if (this.popoverContentRef.current && !this.props.trapFocus) {
448
- if (!focusFirstDescendant(this.popoverContentRef.current)) {
449
- this.popoverContentRef.current.tabIndex = -1;
450
- this.popoverContentRef.current.focus();
451
- }
452
- }
453
- }
454
- }, {
455
- key: "componentDidUpdate",
456
- value: function componentDidUpdate(previousProps) {
457
- if (this.props.containerElement !== previousProps.containerElement || this.props.getPopoverPosition !== previousProps.getPopoverPosition) {
458
- this.adjustPosition();
459
- }
460
- }
461
- }, {
462
- key: "componentWillUnmount",
463
- value: function componentWillUnmount() {
464
- this.mounted = false;
465
- if (this.onScroll && this.props.trackScroll) {
466
- document.body.removeEventListener('scroll', this.onScroll);
467
- window.removeEventListener('resize', this.onResize);
468
- if (this.props.containerElement && this.props.containerElement !== document.body) {
469
- this.props.containerElement.removeEventListener('scroll', this.onScroll);
470
- }
471
- }
472
- }
473
- }, {
474
- key: "render",
475
- value: function render() {
476
- var getPopoverPosition = typeof this.props.getPopoverPosition === 'function' ? this.props.getPopoverPosition : PopoverPosition[this.props.getPopoverPosition] || PopoverPosition.bottomLeft;
477
- var position = this.state.hostRect ? getPopoverPosition(this.state.hostRect, this.state.popoverRect, this.state.scroll, this.props.containerElement) : {};
478
- var containerPosition = {
479
- left: this.state.hostRect ? this.state.hostRect.left : 0,
480
- top: this.state.hostRect ? this.state.hostRect.top : 0,
481
- width: this.state.hostRect && this.state.hostRect.width,
482
- height: this.state.hostRect && this.state.hostRect.height
483
- };
484
- var tipPosition = this.getPopoverTipPosition(position, containerPosition);
485
- // FIXME: position.top cast to number, because value may be not a number
486
- return React__default.createElement("div", {
487
- style: _extends({
488
- position: 'absolute'
489
- }, containerPosition, {
490
- pointerEvents: 'none',
491
- zIndex: this.props.zIndex,
492
- transition: '0.1s',
493
- transitionProperty: 'opacity',
494
- opacity: this.state.adjusted ? 1 : 0
495
- })
496
- }, this.props.showTip && tipPosition && React__default.createElement("div", {
497
- className: classNames(styles['popover-tip'], styles["popover-tip--".concat(tipPosition)], 'popover-tip', this.props.classNameTip),
498
- style: {
499
- top: tipPosition === 'bottom' && position.top - 8,
500
- bottom: tipPosition === 'top' && position.bottom - 8,
501
- left: tipPosition === 'right' && position.left - 8,
502
- right: tipPosition === 'left' && position.right - 8
503
- }
504
- }), this.props.trapFocus && React__default.createElement(FocusTrap, {
505
- focusTrapOptions: {
506
- fallbackFocus: "#".concat(this.popoverContainerId),
507
- clickOutsideDeactivates: true
508
- }
509
- }, React__default.createElement("div", {
510
- id: this.popoverContainerId,
511
- ref: this.popoverContentRef,
512
- style: _extends({
513
- position: 'absolute',
514
- pointerEvents: 'all'
515
- }, position),
516
- tabIndex: -1,
517
- className: this.props.className,
518
- role: this.props.role,
519
- "aria-modal": this.props.trapFocus ? 'true' : 'false',
520
- onKeyDown: this.handleKeyDown
521
- }, this.props.children)), !this.props.trapFocus && React__default.createElement("div", {
522
- id: this.popoverContainerId,
523
- ref: this.popoverContentRef,
524
- style: _extends({
525
- position: 'absolute',
526
- pointerEvents: 'all'
527
- }, position),
528
- tabIndex: -1,
529
- className: this.props.className,
530
- role: this.props.role,
531
- "aria-modal": this.props.trapFocus ? 'true' : 'false',
532
- onKeyDown: this.handleKeyDown
533
- }, this.props.children));
534
- }
535
- }]);
536
- return PopoverContainer;
537
- }(React__default.Component);
538
- PopoverContainer.defaultProps = {
539
- trackScroll: true
540
- };
541
-
542
- var PositioningTarget = /*#__PURE__*/function (_React$Component) {
543
- _inherits(PositioningTarget, _React$Component);
544
- function PositioningTarget() {
545
- var _this;
546
- _classCallCheck(this, PositioningTarget);
547
- _this = _possibleConstructorReturn(this, _getPrototypeOf(PositioningTarget).apply(this, arguments));
548
- _this.state = {
549
- target: null
550
- };
551
- _this.targetRef = React__default.createRef();
552
- return _this;
553
- }
554
- _createClass(PositioningTarget, [{
555
- key: "componentDidMount",
556
- value: function componentDidMount() {
557
- this.setState({
558
- target: this.targetRef.current
559
- });
560
- }
561
- }, {
562
- key: "render",
563
- value: function render() {
564
- return React__default.createElement("div", {
565
- ref: this.targetRef
566
- }, this.props.children(this.state.target));
567
- }
568
- }]);
569
- return PositioningTarget;
570
- }(React__default.Component);
571
-
572
- var BodyDomNode = /*#__PURE__*/function (_React$Component) {
573
- _inherits(BodyDomNode, _React$Component);
574
- function BodyDomNode() {
575
- var _this;
576
- _classCallCheck(this, BodyDomNode);
577
- _this = _possibleConstructorReturn(this, _getPrototypeOf(BodyDomNode).apply(this, arguments));
578
- _this.state = {
579
- node: null
580
- };
581
- _this._onEvent = function (event) {
582
- if (!_this.state.node.contains(event.target) && !_this.props.positioningTarget.contains(event.target)) {
583
- _this.props.onOuterAction(event);
584
- }
585
- };
586
- _this._addDocumentEventListeners = function () {
587
- if (_this.props.outerActionEvents && _this.props.onOuterAction) {
588
- _this.props.outerActionEvents.forEach(function (event) {
589
- return document.addEventListener(event, _this._onEvent);
590
- });
591
- }
592
- };
593
- _this._removeDocumentEventListeners = function () {
594
- var outerActionEvents = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.props.outerActionEvents;
595
- if (outerActionEvents) {
596
- outerActionEvents.forEach(function (event) {
597
- return document.removeEventListener(event, _this._onEvent);
598
- });
599
- }
600
- };
601
- return _this;
602
- }
603
- _createClass(BodyDomNode, [{
604
- key: "componentDidMount",
605
- value: function componentDidMount() {
606
- var node = document.createElement('div');
607
- this.props.containerElement.appendChild(node);
608
- this.setState({
609
- node: node
610
- }, this._addDocumentEventListeners);
611
- }
612
- }, {
613
- key: "componentDidUpdate",
614
- value: function componentDidUpdate(prevProps) {
615
- if (prevProps.outerActionEvents !== this.props.outerActionEvents || prevProps.onOuterAction !== this.props.onOuterAction) {
616
- this._removeDocumentEventListeners(prevProps.outerActionEvents);
617
- this._addDocumentEventListeners();
618
- }
619
- }
620
- }, {
621
- key: "componentWillUnmount",
622
- value: function componentWillUnmount() {
623
- if (this.state.node) {
624
- this.props.containerElement.removeChild(this.state.node);
625
- this._removeDocumentEventListeners();
626
- }
627
- }
628
- }, {
629
- key: "render",
630
- value: function render() {
631
- return this.props.children(this.state.node);
632
- }
633
- }]);
634
- return BodyDomNode;
635
- }(React__default.Component);
636
- BodyDomNode.defaultProps = {
637
- containerElement: document.body
638
- };
639
-
640
- var StatelessPopover = /*#__PURE__*/function (_React$Component) {
641
- _inherits(StatelessPopover, _React$Component);
642
- function StatelessPopover() {
643
- var _this;
644
- _classCallCheck(this, StatelessPopover);
645
- _this = _possibleConstructorReturn(this, _getPrototypeOf(StatelessPopover).apply(this, arguments));
646
- _this.getContainer = function () {
647
- return _this.props.getContainerElement && _this.props.getContainerElement() || document.body;
648
- };
649
- return _this;
650
- }
651
- _createClass(StatelessPopover, [{
652
- key: "renderPortalContent",
653
- value: function renderPortalContent(_ref) {
654
- var props = _ref.props,
655
- container = _ref.container,
656
- positioningTarget = _ref.positioningTarget;
657
- var popoverContent = React__default.createElement(PopoverContainer, {
658
- key: "popover-portal",
659
- getPopoverPosition: this.props.getPopoverPosition,
660
- relativeToParent: props.inline,
661
- containerElement: this.getContainer(),
662
- className: props.className,
663
- positioningTarget: positioningTarget,
664
- showTip: props.showTip,
665
- classNameTip: props.classNameTip,
666
- zIndex: props.zIndex,
667
- trackScroll: props.trackScroll,
668
- role: this.props.role,
669
- trapFocus: this.props.trapFocus,
670
- onEscapeAction: this.props.onEscapeAction
671
- }, props.body);
672
- return ReactDOM.createPortal(popoverContent, container);
673
- }
674
- }, {
675
- key: "renderPortal",
676
- value: function renderPortal(positioningTarget) {
677
- var _this2 = this;
678
- if (!this.props.isOpen) {
679
- return null;
680
- }
681
- var containerElement = this.props.inline ? positioningTarget : this.getContainer();
682
- return React__default.createElement(BodyDomNode, {
683
- onOuterAction: this.props.onOuterAction,
684
- outerActionEvents: this.props.outerActionEvents,
685
- positioningTarget: positioningTarget,
686
- containerElement: containerElement
687
- }, function (container) {
688
- return container ? _this2.renderPortalContent({
689
- props: _this2.props,
690
- container: container,
691
- positioningTarget: positioningTarget
692
- }) : null;
693
- });
694
- }
695
- }, {
696
- key: "render",
697
- value: function render() {
698
- var _this3 = this;
699
- return React__default.createElement(PositioningTarget, null, function (positioningTarget) {
700
- return positioningTarget ? React__default.createElement(React__default.Fragment, null, _this3.props.children, _this3.renderPortal(positioningTarget)) : null;
701
- });
702
- }
703
- }]);
704
- return StatelessPopover;
705
- }(React__default.Component);
706
-
707
- var Popover = /*#__PURE__*/function (_React$Component) {
708
- _inherits(Popover, _React$Component);
709
- function Popover() {
710
- var _this;
711
- _classCallCheck(this, Popover);
712
- _this = _possibleConstructorReturn(this, _getPrototypeOf(Popover).apply(this, arguments));
713
- _this.state = {
714
- isOpen: false
715
- };
716
- _this.setOpened = function (isOpen) {
717
- _this.setState({
718
- isOpen: isOpen
719
- });
720
- };
721
- _this.onOuterAction = function (event) {
722
- if (_this.props.closeOnOuterAction) {
723
- _this.setOpened(false);
724
- }
725
- if (_this.props.onOuterAction) {
726
- _this.props.onOuterAction(event);
727
- }
728
- };
729
- _this.onEscapeAction = function (event) {
730
- var _a;
731
- _this.setOpened(false);
732
- if (_this.props.onOuterAction) {
733
- _this.props.onOuterAction(event);
734
- }
735
- (_a = _this.childrenRef.current) === null || _a === void 0 ? void 0 : _a.focus();
736
- };
737
- _this.childrenRef = React.createRef();
738
- return _this;
739
- }
740
- _createClass(Popover, [{
741
- key: "render",
742
- value: function render() {
743
- var _this2 = this;
744
- var isOpen = typeof this.props.isOpen === 'undefined' ? this.state.isOpen : this.props.isOpen;
745
- var children = typeof this.props.children === 'function' ? this.props.children({
746
- isOpen: isOpen,
747
- setOpened: this.setOpened,
748
- ref: this.childrenRef
749
- }) : this.props.children;
750
- var childrenWithRef = React__default.Children.map(children, function (child) {
751
- return React__default.cloneElement(child, {
752
- ref: _this2.childrenRef
753
- });
754
- });
755
- return React__default.createElement(StatelessPopover, Object.assign({}, this.props, {
756
- onOuterAction: this.onOuterAction,
757
- onEscapeAction: this.onEscapeAction,
758
- isOpen: isOpen,
759
- role: this.props.role,
760
- trapFocus: this.props.trapFocus
761
- }), childrenWithRef);
762
- }
763
- }]);
764
- return Popover;
765
- }(React__default.Component);
766
- Popover.Body = PopoverBody;
767
- Popover.Position = PopoverPosition;
768
- Popover.defaultProps = {
769
- zIndex: 19999,
770
- outerActionEvents: ['mousedown', 'touchstart'],
771
- getPopoverPosition: 'bottomLeft',
772
- role: 'dialog',
773
- trapFocus: 'true'
774
- };
775
-
776
- /*********************************************************************
777
- * © Copyright IBM Corp. 2024
778
- *********************************************************************/
779
-
780
- exports.Popover = Popover;
781
- exports.PopoverBody = PopoverBody;
782
- exports.PopoverPosition = PopoverPosition;
783
- exports.default = Popover;