@targetprocess/popover 12.23.1-alpha.2 → 12.23.1-alpha.4

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