diginet-core-ui 1.3.35 → 1.3.36

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.
@@ -6,24 +6,118 @@ import { createPortal } from 'react-dom';
6
6
  import PropTypes from 'prop-types';
7
7
  import { jsx, css } from '@emotion/core';
8
8
  import { typography } from '../../styles/typography';
9
- import { borderRadius4px, displayBlock, flexRow, positionAbsolute, positionFixed, positionRelative } from '../../styles/general';
9
+ import { borderRadius4px, displayBlock, flexRow, parseWidthHeight, positionAbsolute, positionFixed, positionRelative } from '../../styles/general';
10
10
  const {
11
11
  paragraph1
12
12
  } = typography;
13
+
14
+ const getArrowPosition = (anchorOrigin, transformOrigin, translate) => {
15
+ if (anchorOrigin.horizontal === 'right' && transformOrigin.horizontal === 'left') {
16
+ translate.left = -6;
17
+ translate.transform = 'rotateZ(270deg)';
18
+ return 'left';
19
+ } else if (anchorOrigin.horizontal === 'left' && transformOrigin.horizontal === 'right') {
20
+ translate.left = 6;
21
+ translate.transform = 'rotateZ(90deg)';
22
+ return 'right';
23
+ } else if (transformOrigin.vertical === 'bottom') {
24
+ translate.top = 6;
25
+ translate.transform = 'rotateZ(180deg)';
26
+ return 'bottom';
27
+ } else {
28
+ translate.top = -6;
29
+ return 'top';
30
+ }
31
+ };
32
+
33
+ const calPosition = (position, originBounding) => {
34
+ const {
35
+ width,
36
+ height
37
+ } = originBounding;
38
+
39
+ switch (position) {
40
+ case 'bottom':
41
+ case 'right':
42
+ {
43
+ return [height, width];
44
+ }
45
+
46
+ case 'center':
47
+ {
48
+ return [height / 2, width / 2];
49
+ }
50
+
51
+ default:
52
+ return [0, 0];
53
+ }
54
+ };
55
+
56
+ const getDirectionPopover = direction => {
57
+ switch (direction) {
58
+ case 'top':
59
+ return {
60
+ anchorOrigin: {
61
+ vertical: 'top',
62
+ horizontal: 'center'
63
+ },
64
+ transformOrigin: {
65
+ vertical: 'bottom',
66
+ horizontal: 'center'
67
+ }
68
+ };
69
+
70
+ case 'left':
71
+ return {
72
+ anchorOrigin: {
73
+ vertical: 'center',
74
+ horizontal: 'left'
75
+ },
76
+ transformOrigin: {
77
+ vertical: 'center',
78
+ horizontal: 'right'
79
+ }
80
+ };
81
+
82
+ case 'right':
83
+ return {
84
+ anchorOrigin: {
85
+ vertical: 'center',
86
+ horizontal: 'right'
87
+ },
88
+ transformOrigin: {
89
+ vertical: 'center',
90
+ horizontal: 'left'
91
+ }
92
+ };
93
+
94
+ default:
95
+ return {
96
+ anchorOrigin: {
97
+ vertical: 'bottom',
98
+ horizontal: 'center'
99
+ },
100
+ transformOrigin: {
101
+ vertical: 'top',
102
+ horizontal: 'center'
103
+ }
104
+ };
105
+ }
106
+ };
107
+
13
108
  const Popover = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(({
14
109
  open,
15
110
  pressESCToClose,
16
- removeAfterClose,
17
111
  fullScreen,
18
112
  arrow,
19
113
  style,
20
114
  anchor,
21
115
  anchorOrigin,
22
116
  transformOrigin,
117
+ direction,
23
118
  width,
24
119
  height,
25
120
  bgColor,
26
- refs,
27
121
  onClose,
28
122
  zIndex,
29
123
  children,
@@ -32,47 +126,16 @@ const Popover = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(({
32
126
  const ref = useRef(null);
33
127
  const [openState, setOpenState] = useState(open);
34
128
  const [element, setElement] = useState(null);
35
- const PopoverContainer = css`
36
- ${flexRow}
37
- ${positionFixed}
38
- width: ${isNaN(width) ? width : width + 'px'};
39
- max-width: 100%;
40
- max-height: 100%;
41
- height: ${isNaN(height) ? height : height + 'px'};
42
- z-index: 1000;
43
- transform: scale(0);
44
- transition: transform 0.2s linear;
45
- pointer-events: initial;
46
- &.open {
47
- transform: scale(1);
48
- }
49
- `;
50
- const PopoverRoot = css`
51
- ${displayBlock}
52
- ${positionRelative}
53
- ${borderRadius4px}
54
- ${paragraph1};
55
- background-color: ${bgColor};
56
- width: 100%;
57
- height: 100%;
58
- box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25);
59
- `;
60
- const PopoverArrow = css`
61
- ${displayBlock}
62
- ${positionAbsolute}
63
- height: 0;
64
- width: 0;
65
- border-left: 6px solid transparent;
66
- border-right: 6px solid transparent;
67
- border-bottom: 6px solid ${bgColor};
68
- `;
129
+
130
+ if (direction) {
131
+ const directionObject = getDirectionPopover(direction);
132
+ anchorOrigin = directionObject.anchorOrigin;
133
+ transformOrigin = directionObject.transformOrigin;
134
+ }
69
135
 
70
136
  const onClosePopover = () => {
71
- // const popovers = document.querySelectorAll('.DGN-UI-Portal');
72
- // if (popovers && popovers.length > 1 && Array.from(popovers)[popovers.length - 1] && Array.from(popovers)[popovers.length - 1].contains(ref.current)) {
73
- // return;
74
- // }
75
137
  if (openState) {
138
+ ref.current.style.opacity = 0;
76
139
  onClose && onClose();
77
140
  setOpenState(false);
78
141
  }
@@ -95,53 +158,11 @@ const Popover = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(({
95
158
  }
96
159
  };
97
160
 
98
- const getArrowPosition = (anchorOrigin, transformOrigin, translate) => {
99
- if (anchorOrigin.horizontal === 'right' && transformOrigin.horizontal === 'left') {
100
- translate.left = -6;
101
- translate.transform = 'rotateZ(270deg)';
102
- return 'left';
103
- } else if (anchorOrigin.horizontal === 'left' && transformOrigin.horizontal === 'right') {
104
- translate.left = 6;
105
- translate.transform = 'rotateZ(90deg)';
106
- return 'right';
107
- } else if (transformOrigin.vertical === 'bottom') {
108
- translate.top = 6;
109
- translate.transform = 'rotateZ(180deg)';
110
- return 'bottom';
111
- } else {
112
- translate.top = -6;
113
- return 'top';
114
- }
115
- };
116
-
117
- const calPosition = (position, originBounding) => {
118
- const {
119
- width,
120
- height
121
- } = originBounding;
122
-
123
- switch (position) {
124
- case 'bottom':
125
- case 'right':
126
- {
127
- return [height, width];
128
- }
129
-
130
- case 'center':
131
- {
132
- return [height / 2, width / 2];
133
- }
134
-
135
- default:
136
- return [0, 0];
137
- }
138
- };
139
-
140
161
  const updatePositionPopover = (el = null) => {
141
162
  setElement(el instanceof Element ? el : null);
142
163
 
143
164
  if (!ref.current) {
144
- window.removeEventListener('scroll', updatePositionPopover);
165
+ window.removeEventListener('resize', updatePositionPopover);
145
166
  return;
146
167
  }
147
168
 
@@ -247,7 +268,7 @@ const Popover = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(({
247
268
  break;
248
269
  }
249
270
 
250
- Object.assign(ref.current.querySelector(`.css-${PopoverArrow.name}`).style, arrowPosition, {
271
+ Object.assign(ref.current.querySelector(`.css-${PopoverArrowCSS(bgColor).name}`).style, arrowPosition, {
251
272
  transform: translate.transform
252
273
  });
253
274
  }
@@ -255,17 +276,17 @@ const Popover = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(({
255
276
  ref.current.style.top = top + 'px';
256
277
  ref.current.style.left = left + 'px';
257
278
  ref.current.style.transformOrigin = `${transformOrigin.horizontal || 'left'} ${transformOrigin.vertical || 'top'}`;
279
+ ref.current.style.opacity = 1;
258
280
  };
259
281
 
260
282
  useEffect(() => {
261
- if (refs) refs(ref);
262
283
  return () => {
263
284
  if (pressESCToClose) {
264
285
  document.removeEventListener('keydown', pressESCHandler);
265
286
  }
266
287
 
267
288
  document.removeEventListener('click', clickOutOfPopover);
268
- window.removeEventListener('scroll', updatePositionPopover);
289
+ window.removeEventListener('resize', updatePositionPopover);
269
290
  };
270
291
  }, []);
271
292
  useEffect(() => {
@@ -274,33 +295,26 @@ const Popover = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(({
274
295
  useEffect(() => {
275
296
  if (openState && ref.current) {
276
297
  // Position's anchor
277
- updatePositionPopover();
278
-
279
- if (fullScreen && !removeAfterClose) {
280
- ref.current.parentNode.style.pointerEvents = 'initial';
281
- } // Allow press ESC to close popup
282
-
298
+ updatePositionPopover(); // Allow press ESC to close popup
283
299
 
284
300
  if (pressESCToClose) {
285
301
  document.addEventListener('keydown', pressESCHandler);
286
302
  } // Click out popover to close popover
287
303
 
288
304
 
289
- document.addEventListener('click', clickOutOfPopover); // Update position popover on scroll
305
+ document.addEventListener('click', clickOutOfPopover); // Update position popover on resize
290
306
 
291
- window.addEventListener('scroll', updatePositionPopover);
307
+ window.addEventListener('resize', updatePositionPopover);
292
308
  return () => {
293
309
  if (pressESCToClose) {
294
310
  document.removeEventListener('keydown', pressESCHandler);
295
311
  }
296
312
 
297
313
  document.removeEventListener('click', clickOutOfPopover);
298
- window.removeEventListener('scroll', updatePositionPopover);
314
+ window.removeEventListener('resize', updatePositionPopover);
299
315
  };
300
- } else if (ref.current) {
301
- if (fullScreen && !removeAfterClose) {
302
- ref.current.parentNode.style.pointerEvents = 'none';
303
- }
316
+ } else if (!openState) {
317
+ document.documentElement.style.overflow = 'auto';
304
318
  }
305
319
  }, [openState]);
306
320
  useImperativeHandle(reference, () => {
@@ -324,36 +338,70 @@ const Popover = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(({
324
338
  return currentRef;
325
339
  });
326
340
  const PopoverView = useMemo(() => {
327
- if (openState || !removeAfterClose) {
341
+ if (openState) {
328
342
  const node = jsx("div", {
329
343
  className: 'DGN-UI-Portal DGN-UI-Popover',
330
- style: {
331
- position: 'fixed',
332
- zIndex,
333
- inset: 0,
334
- pointerEvents: fullScreen ? 'initial' : 'none'
335
- }
344
+ css: PortalPopoverCSS(zIndex, fullScreen)
336
345
  }, jsx("div", {
337
- css: PopoverContainer,
346
+ css: PopoverContainerCSS(width, height),
338
347
  ref: ref,
339
- className: openState ? 'open' : '',
340
348
  ...props
341
349
  }, jsx("div", {
342
- css: PopoverRoot,
350
+ css: PopoverRootCSS(bgColor),
343
351
  style: style
344
352
  }, arrow && jsx("span", {
345
- css: PopoverArrow
353
+ css: PopoverArrowCSS(bgColor)
346
354
  }), children)));
347
355
  return /*#__PURE__*/createPortal(node, document.body);
348
356
  }
349
357
 
350
358
  return null;
351
- }, [openState, fullScreen, style, arrow, children]);
359
+ }, [openState, zIndex, fullScreen, style, arrow, children, width, height, bgColor]);
352
360
  return PopoverView;
353
361
  }));
362
+
363
+ const PortalPopoverCSS = (zIndex, fullScreen) => css`
364
+ ${positionFixed};
365
+ z-index: ${zIndex};
366
+ pointer-events: ${fullScreen ? 'initial' : 'none'};
367
+ inset: 0;
368
+ `;
369
+
370
+ const PopoverContainerCSS = (width, height) => css`
371
+ ${flexRow};
372
+ ${positionFixed};
373
+ ${parseWidthHeight(width, height)};
374
+ max-width: 100%;
375
+ max-height: 100%;
376
+ z-index: 1000;
377
+ transition: opacity 0.3s ease;
378
+ pointer-events: initial;
379
+ opacity: 0;
380
+ `;
381
+
382
+ const PopoverRootCSS = bgColor => css`
383
+ ${displayBlock};
384
+ ${positionRelative};
385
+ ${borderRadius4px};
386
+ ${paragraph1};
387
+ background-color: ${bgColor};
388
+ width: 100%;
389
+ height: 100%;
390
+ box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25);
391
+ `;
392
+
393
+ const PopoverArrowCSS = bgColor => css`
394
+ ${displayBlock};
395
+ ${positionAbsolute};
396
+ height: 0;
397
+ width: 0;
398
+ border-left: 6px solid transparent;
399
+ border-right: 6px solid transparent;
400
+ border-bottom: 6px solid ${bgColor};
401
+ `;
402
+
354
403
  Popover.defaultProps = {
355
404
  pressESCToClose: true,
356
- removeAfterClose: false,
357
405
  fullScreen: true,
358
406
  arrow: false,
359
407
  width: 150,
@@ -367,25 +415,82 @@ Popover.defaultProps = {
367
415
  transformOrigin: {
368
416
  vertical: 'top',
369
417
  horizontal: 'center'
370
- }
418
+ },
419
+ style: {}
371
420
  };
372
421
  Popover.propTypes = {
422
+ /** If true, the component is shown. */
373
423
  open: PropTypes.bool,
424
+
425
+ /** If true, arrow is shown. */
374
426
  arrow: PropTypes.bool,
427
+
428
+ /** allow close Popover when press ESC. */
375
429
  pressESCToClose: PropTypes.bool,
376
- removeAfterClose: PropTypes.bool,
430
+
431
+ /** background color of Popover. */
377
432
  bgColor: PropTypes.string,
433
+
434
+ /** style inline of component. */
378
435
  style: PropTypes.object,
379
- anchor: PropTypes.object,
380
- anchorOrigin: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
381
- // {vertical: 'top', horizontal: 'left'}
382
- transformOrigin: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
383
- // {vertical: 'bottom', horizontal: 'right'}
436
+
437
+ /** An HTML element, or a function that returns one. It's used to set the position of the popover. */
438
+ anchor: PropTypes.oneOfType([PropTypes.instanceOf(Element), PropTypes.func, PropTypes.object]),
439
+
440
+ /**
441
+ * This is the point on the anchor where the popover's anchor will attach to.
442
+ * Options: vertical: [top, center, bottom]; horizontal: [left, center, right].
443
+ */
444
+ anchorOrigin: PropTypes.shape({
445
+ horizontal: PropTypes.oneOf(['center', 'left', 'right']),
446
+ vertical: PropTypes.oneOf(['bottom', 'center', 'top'])
447
+ }),
448
+
449
+ /**
450
+ * This is the point on the popover which will attach to the anchor's origin.
451
+ * Options: vertical: [top, center, bottom]; horizontal: [left, center, right].
452
+ */
453
+ transformOrigin: PropTypes.shape({
454
+ horizontal: PropTypes.oneOf(['center', 'left', 'right']),
455
+ vertical: PropTypes.oneOf(['bottom', 'center', 'top'])
456
+ }),
457
+
458
+ /**
459
+ * Direction when Popover shown.
460
+ * Note: This prop will overwrite anchorOrigin & transformOrigin.
461
+ *
462
+ * * top: anchorOrigin: { vertical: 'top', horizontal: 'center' }, transformOrigin: { vertical: 'bottom', horizontal: 'center' }
463
+ * * left: anchorOrigin: { vertical: 'center', horizontal: 'left' }, transformOrigin: { vertical: 'center', horizontal: 'right' }
464
+ * * right: anchorOrigin: { vertical: 'center', horizontal: 'right' }, transformOrigin: { vertical: 'center', horizontal: 'left' }
465
+ * * right: anchorOrigin: { vertical: 'bottom', horizontal: 'center' }, transformOrigin: { vertical: 'top', horizontal: 'center' }
466
+ */
467
+ direction: PropTypes.oneOf(['top', 'left', 'right', 'bottom']),
468
+
469
+ /** width of popover */
384
470
  width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
471
+
472
+ /** height of popover */
385
473
  height: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
474
+
475
+ /** z-index of popover */
386
476
  zIndex: PropTypes.number,
387
- refs: PropTypes.func,
477
+
478
+ /** Callback fired when the component requests to be closed. */
388
479
  onClose: PropTypes.func,
389
- children: PropTypes.node
480
+
481
+ /** The content of the component. */
482
+ children: PropTypes.node,
483
+
484
+ /**
485
+ * ref methods (ref.current.instance.*method*)
486
+ *
487
+ * * show: Show popover
488
+ * * close: Close popover
489
+ * * setPosition(element): Set position of popover
490
+ * * @param {element} - element
491
+ */
492
+ reference: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({
493
+ current: PropTypes.instanceOf(Element)
494
+ })])
390
495
  };
391
496
  export default Popover;
@@ -461,13 +461,13 @@ Popup.propTypes = {
461
461
  /** the function will run when click on cancel button */
462
462
  onCancel: func,
463
463
 
464
- /**
465
- * named ref: get ref of Popup component, insist:<br />
466
- * * element<br />
467
- * * show: the method to show Popup (ref.current.show(options, callback))<br />
468
- * &nbsp;&nbsp; + options: {type, title, icon, yesNo, description, confirmProps, cancelProps, onCancel, onConfirm}<br />
469
- * &nbsp;&nbsp; + callback: the function will run after click on button (callback(true) with confirm Button and callback(false) with cancel Button). Alternate both onCancel, onConfirm methods of options<br />
470
- * * close: the method to close Popup<br />
464
+ /**
465
+ * named ref: get ref of Popup component, insist:<br />
466
+ * * element<br />
467
+ * * show: the method to show Popup (ref.current.show(options, callback))<br />
468
+ * &nbsp;&nbsp; + options: {type, title, icon, yesNo, description, confirmProps, cancelProps, onCancel, onConfirm}<br />
469
+ * &nbsp;&nbsp; + callback: the function will run after click on button (callback(true) with confirm Button and callback(false) with cancel Button). Alternate both onCancel, onConfirm methods of options<br />
470
+ * * close: the method to close Popup<br />
471
471
  */
472
472
  reference: oneOfType([func, shape({
473
473
  current: instanceOf(elementType)
@@ -63,8 +63,8 @@ const getDimension = size => {
63
63
  strokeWidth
64
64
  };
65
65
  };
66
- /**
67
- * Determinate circular progress
66
+ /**
67
+ * Determinate circular progress
68
68
  */
69
69
 
70
70
 
@@ -113,8 +113,8 @@ const Determinate = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(({
113
113
 
114
114
  return angle;
115
115
  };
116
- /**
117
- * style
116
+ /**
117
+ * style
118
118
  */
119
119
 
120
120
 
@@ -216,8 +216,8 @@ Determinate.propTypes = {
216
216
  width: PropTypes.number
217
217
  };
218
218
  export const DeterminateCircularProgress = Determinate;
219
- /**
220
- * Indeterminate circular progress
219
+ /**
220
+ * Indeterminate circular progress
221
221
  */
222
222
 
223
223
  const Indeterminate = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(({
@@ -254,8 +254,8 @@ const Indeterminate = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(({
254
254
  stroke-dashoffset: -${r * 124 / 200};
255
255
  }
256
256
  `;
257
- /**
258
- * style
257
+ /**
258
+ * style
259
259
  */
260
260
 
261
261
 
@@ -286,8 +286,8 @@ const Indeterminate = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(({
286
286
  stroke-linecap: round;
287
287
  stroke-width: ${circleStrokeW};
288
288
  `;
289
- /**
290
- * component
289
+ /**
290
+ * component
291
291
  */
292
292
 
293
293
  const CircleBackgroundMemo = useMemo(() => jsx("circle", {
@@ -330,8 +330,8 @@ Indeterminate.propTypes = {
330
330
  width: PropTypes.number
331
331
  };
332
332
  export const IndeterminateCircularProgress = Indeterminate;
333
- /**
334
- * Circular progress
333
+ /**
334
+ * Circular progress
335
335
  */
336
336
 
337
337
  const CircularProgress = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(({
@@ -93,8 +93,8 @@ TabContainer.propTypes = {
93
93
  /** any props else */
94
94
  props: PropTypes.any,
95
95
 
96
- /**
97
- * ref methods
96
+ /**
97
+ * ref methods
98
98
  */
99
99
  reference: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({
100
100
  current: PropTypes.instanceOf(Element)
@@ -157,8 +157,8 @@ TabHeader.propTypes = {
157
157
  /** any props else */
158
158
  props: PropTypes.any,
159
159
 
160
- /**
161
- * ref methods
160
+ /**
161
+ * ref methods
162
162
  */
163
163
  reference: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({
164
164
  current: PropTypes.instanceOf(Element)
@@ -87,8 +87,8 @@ TabPanel.propTypes = {
87
87
  /** any props else */
88
88
  props: PropTypes.any,
89
89
 
90
- /**
91
- * ref methods
90
+ /**
91
+ * ref methods
92
92
  */
93
93
  reference: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({
94
94
  current: PropTypes.instanceOf(Element)
@@ -213,8 +213,8 @@ TabItem.propTypes = {
213
213
  /** any props else */
214
214
  props: PropTypes.any,
215
215
 
216
- /**
217
- * ref methods
216
+ /**
217
+ * ref methods
218
218
  */
219
219
  reference: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({
220
220
  current: PropTypes.instanceOf(Element)