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