@redsift/charts 6.2.0 → 6.3.1
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/index.d.ts +62 -42
- package/index.js +898 -72
- package/index.js.map +1 -1
- package/package.json +4 -3
package/index.js
CHANGED
|
@@ -1,20 +1,41 @@
|
|
|
1
|
-
import React, { forwardRef } from 'react';
|
|
2
1
|
import { scaleOrdinal, pie, arc, sum } from 'd3';
|
|
2
|
+
import React, { forwardRef, useState, useRef, useEffect, useImperativeHandle, useLayoutEffect, useId } from 'react';
|
|
3
3
|
import styled, { css } from 'styled-components';
|
|
4
|
+
import ReactDOM from 'react-dom';
|
|
4
5
|
|
|
5
|
-
const
|
|
6
|
+
const ColorTheme = {
|
|
7
|
+
default: 'default',
|
|
8
|
+
dark: 'dark',
|
|
9
|
+
darker: 'darker',
|
|
10
|
+
monochrome: 'monochrome'
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const monochrome = '#B6DAEE';
|
|
6
14
|
const scheme = {
|
|
7
15
|
default: ['#C1AAEA', '#B6DAEE', '#B3E3C5', '#EAADAC', '#E2A7D7', '#AFF5F3', '#F2D39E', '#F6EFC7', '#E6C2A0', '#E5E5E5'],
|
|
8
16
|
dark: ['#A78ADB', '#97C5DE', '#90D2A9', '#D18785', '#D185C3', '#8CEAE6', '#D8B475', '#DED4A0', '#CDA279', '#BDBDBD'],
|
|
9
17
|
darker: ['#8D6CC9', '#73ADCC', '#6DBE8B', '#BA6361', '#BD63AC', '#6CDCD8', '#BD954F', '#C6BB7A', '#B58556', '#666666'],
|
|
10
18
|
light: ['#D9CAF5', '#D8EDF8', '#D3F0DE', '#FFD3D2', '#EFC8E8', '#D4FCFA', '#FFE8C0', '#FFFBE1', '#FFE3C9', '#F5F5F5'],
|
|
11
19
|
lighter: ['#F0E9FC', '#F6FBFE', '#EDFAF1', '#FFE5E5', '#F9E6F6', '#E1FFFF', '#FFF0D7', '#FFFDF0', '#FFEEDE', '#FFFFFF'],
|
|
12
|
-
monochrome: [
|
|
20
|
+
monochrome: [monochrome],
|
|
21
|
+
empty: ['#BDBDBD']
|
|
13
22
|
};
|
|
14
23
|
const successDangerScheme = {
|
|
15
24
|
success: '#90D2A9',
|
|
16
25
|
warning: '#D8B475',
|
|
17
|
-
danger: '#D18785'
|
|
26
|
+
danger: '#D18785',
|
|
27
|
+
neutral: '#BDBDBD'
|
|
28
|
+
};
|
|
29
|
+
const getColorScale = (theme, isEmpty) => {
|
|
30
|
+
let d3colors = scaleOrdinal(scheme.default);
|
|
31
|
+
if (isEmpty) {
|
|
32
|
+
d3colors = scaleOrdinal(scheme.empty);
|
|
33
|
+
} else if (typeof theme === 'string') {
|
|
34
|
+
d3colors = scaleOrdinal(scheme[theme]);
|
|
35
|
+
} else if (typeof theme === 'object') {
|
|
36
|
+
d3colors = scaleOrdinal().domain([theme.success, theme.warning, theme.danger, ...(theme.neutral ? [theme.neutral] : [])]).range([successDangerScheme.success, successDangerScheme.warning, successDangerScheme.danger, ...(theme.neutral ? [successDangerScheme.neutral] : [])]).unknown(monochrome);
|
|
37
|
+
}
|
|
38
|
+
return d3colors;
|
|
18
39
|
};
|
|
19
40
|
|
|
20
41
|
function ownKeys(object, enumerableOnly) {
|
|
@@ -51,8 +72,8 @@ function _defineProperty(obj, key, value) {
|
|
|
51
72
|
}
|
|
52
73
|
return obj;
|
|
53
74
|
}
|
|
54
|
-
function _extends() {
|
|
55
|
-
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
75
|
+
function _extends$1() {
|
|
76
|
+
_extends$1 = Object.assign ? Object.assign.bind() : function (target) {
|
|
56
77
|
for (var i = 1; i < arguments.length; i++) {
|
|
57
78
|
var source = arguments[i];
|
|
58
79
|
for (var key in source) {
|
|
@@ -63,7 +84,7 @@ function _extends() {
|
|
|
63
84
|
}
|
|
64
85
|
return target;
|
|
65
86
|
};
|
|
66
|
-
return _extends.apply(this, arguments);
|
|
87
|
+
return _extends$1.apply(this, arguments);
|
|
67
88
|
}
|
|
68
89
|
function _objectWithoutPropertiesLoose(source, excluded) {
|
|
69
90
|
if (source == null) return {};
|
|
@@ -123,11 +144,15 @@ const PieChartLabelVariant = {
|
|
|
123
144
|
/**
|
|
124
145
|
* Component theme.
|
|
125
146
|
*/
|
|
126
|
-
const PieChartTheme =
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
147
|
+
const PieChartTheme = ColorTheme;
|
|
148
|
+
/**
|
|
149
|
+
* Tooltip label variant.
|
|
150
|
+
*/
|
|
151
|
+
const PieChartTooltipVariant = {
|
|
152
|
+
none: 'none',
|
|
153
|
+
label: 'label',
|
|
154
|
+
value: 'value',
|
|
155
|
+
percent: 'percent'
|
|
131
156
|
};
|
|
132
157
|
|
|
133
158
|
/**
|
|
@@ -151,6 +176,9 @@ const StyledPieChart = styled.div`
|
|
|
151
176
|
position: relative;
|
|
152
177
|
}
|
|
153
178
|
}
|
|
179
|
+
svg {
|
|
180
|
+
display: block;
|
|
181
|
+
}
|
|
154
182
|
`;
|
|
155
183
|
const StyledPieChartTitle = styled.div`
|
|
156
184
|
font-family: var(--redsift-typography-h4-font-family);
|
|
@@ -174,6 +202,7 @@ const StyledPieChartCenterText = styled.div`
|
|
|
174
202
|
flex-direction: column;
|
|
175
203
|
justify-content: center;
|
|
176
204
|
align-items: center;
|
|
205
|
+
pointer-events: none;
|
|
177
206
|
|
|
178
207
|
> * {
|
|
179
208
|
max-width: ${_ref => {
|
|
@@ -185,19 +214,121 @@ const StyledPieChartCenterText = styled.div`
|
|
|
185
214
|
}
|
|
186
215
|
|
|
187
216
|
> b {
|
|
188
|
-
margin-bottom: 8px;
|
|
189
217
|
font-family: var(--redsift-typography-font-family-source-code-pro);
|
|
190
218
|
color: var(--redsift-color-text-primary);
|
|
191
|
-
font-weight: var(--redsift-typography-font-weight-
|
|
192
|
-
font-size:
|
|
193
|
-
|
|
219
|
+
font-weight: var(--redsift-typography-font-weight-medium);
|
|
220
|
+
font-size: ${_ref2 => {
|
|
221
|
+
let {
|
|
222
|
+
$textSize
|
|
223
|
+
} = _ref2;
|
|
224
|
+
return $textSize;
|
|
225
|
+
}}px;
|
|
226
|
+
line-height: ${_ref3 => {
|
|
227
|
+
let {
|
|
228
|
+
$textSize
|
|
229
|
+
} = _ref3;
|
|
230
|
+
return $textSize;
|
|
231
|
+
}}px;
|
|
232
|
+
|
|
233
|
+
&:nth-child(2) {
|
|
234
|
+
font-weight: normal;
|
|
235
|
+
margin-top: 8px;
|
|
236
|
+
font-family: var(--redsift-typography-font-family-raleway);
|
|
237
|
+
font-size: ${_ref4 => {
|
|
238
|
+
let {
|
|
239
|
+
$smallTextSize
|
|
240
|
+
} = _ref4;
|
|
241
|
+
return $smallTextSize;
|
|
242
|
+
}}px;
|
|
243
|
+
line-height: ${_ref5 => {
|
|
244
|
+
let {
|
|
245
|
+
$smallTextSize
|
|
246
|
+
} = _ref5;
|
|
247
|
+
return $smallTextSize;
|
|
248
|
+
}}px;
|
|
249
|
+
}
|
|
194
250
|
}
|
|
195
251
|
> span {
|
|
252
|
+
color: var(--redsift-color-text-primary);
|
|
253
|
+
font-size: ${_ref6 => {
|
|
254
|
+
let {
|
|
255
|
+
$textSize
|
|
256
|
+
} = _ref6;
|
|
257
|
+
return $textSize;
|
|
258
|
+
}}px;
|
|
259
|
+
line-height: ${_ref7 => {
|
|
260
|
+
let {
|
|
261
|
+
$textSize
|
|
262
|
+
} = _ref7;
|
|
263
|
+
return $textSize;
|
|
264
|
+
}}px;
|
|
265
|
+
&:nth-child(3) {
|
|
266
|
+
margin-top: 4px;
|
|
267
|
+
font-size: ${_ref8 => {
|
|
268
|
+
let {
|
|
269
|
+
$smallTextSize
|
|
270
|
+
} = _ref8;
|
|
271
|
+
return $smallTextSize;
|
|
272
|
+
}}px;
|
|
273
|
+
line-height: ${_ref9 => {
|
|
274
|
+
let {
|
|
275
|
+
$smallTextSize
|
|
276
|
+
} = _ref9;
|
|
277
|
+
return $smallTextSize;
|
|
278
|
+
}}px;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
> b + span {
|
|
196
282
|
font-size: var(--redsift-typography-body-font-size);
|
|
283
|
+
font-family: var(--redsift-typography-font-family-raleway);
|
|
284
|
+
line-height: var(--redsift-typography-badge-line-height);
|
|
197
285
|
color: rgba(0, 0, 0, 0.6);
|
|
198
286
|
text-align: center;
|
|
199
287
|
}
|
|
200
288
|
`;
|
|
289
|
+
const StyledPieChartEmptyText = styled.div`
|
|
290
|
+
position: absolute;
|
|
291
|
+
top: 0;
|
|
292
|
+
left: 0;
|
|
293
|
+
height: 100%;
|
|
294
|
+
width: 100%;
|
|
295
|
+
display: flex;
|
|
296
|
+
flex-direction: column;
|
|
297
|
+
justify-content: center;
|
|
298
|
+
align-items: center;
|
|
299
|
+
pointer-events: none;
|
|
300
|
+
|
|
301
|
+
> * {
|
|
302
|
+
max-width: ${_ref10 => {
|
|
303
|
+
let {
|
|
304
|
+
$maxWidth
|
|
305
|
+
} = _ref10;
|
|
306
|
+
return $maxWidth;
|
|
307
|
+
}}px;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
> span {
|
|
311
|
+
font-family: var(--redsift-typography-font-family-raleway);
|
|
312
|
+
color: ${_ref11 => {
|
|
313
|
+
let {
|
|
314
|
+
$isDonut
|
|
315
|
+
} = _ref11;
|
|
316
|
+
return $isDonut ? css`var(--redsift-color-text-secondary)` : css`var(--redsift-color-text-primary)`;
|
|
317
|
+
}};
|
|
318
|
+
font-size: ${_ref12 => {
|
|
319
|
+
let {
|
|
320
|
+
$textSize
|
|
321
|
+
} = _ref12;
|
|
322
|
+
return $textSize;
|
|
323
|
+
}}px;
|
|
324
|
+
line-height: ${_ref13 => {
|
|
325
|
+
let {
|
|
326
|
+
$textSize
|
|
327
|
+
} = _ref13;
|
|
328
|
+
return $textSize;
|
|
329
|
+
}}px;
|
|
330
|
+
}
|
|
331
|
+
`;
|
|
201
332
|
const StyledPieChartLabel = styled.li`
|
|
202
333
|
display: flex;
|
|
203
334
|
align-items: center;
|
|
@@ -212,10 +343,10 @@ const StyledPieChartLabel = styled.li`
|
|
|
212
343
|
height: 16px;
|
|
213
344
|
width: 16px;
|
|
214
345
|
min-width: 16px;
|
|
215
|
-
background-color: ${
|
|
346
|
+
background-color: ${_ref14 => {
|
|
216
347
|
let {
|
|
217
348
|
$color
|
|
218
|
-
} =
|
|
349
|
+
} = _ref14;
|
|
219
350
|
return $color;
|
|
220
351
|
}};
|
|
221
352
|
}
|
|
@@ -227,10 +358,10 @@ const StyledPieChartLabel = styled.li`
|
|
|
227
358
|
}
|
|
228
359
|
`;
|
|
229
360
|
const StyledPieChartArc = styled.g`
|
|
230
|
-
${
|
|
361
|
+
${_ref15 => {
|
|
231
362
|
let {
|
|
232
363
|
$spaced
|
|
233
|
-
} =
|
|
364
|
+
} = _ref15;
|
|
234
365
|
return $spaced ? css`
|
|
235
366
|
path {
|
|
236
367
|
stroke-width: 2px;
|
|
@@ -239,20 +370,652 @@ const StyledPieChartArc = styled.g`
|
|
|
239
370
|
` : '';
|
|
240
371
|
}}
|
|
241
372
|
|
|
242
|
-
${
|
|
373
|
+
${_ref16 => {
|
|
243
374
|
let {
|
|
244
|
-
$
|
|
245
|
-
} =
|
|
246
|
-
return $
|
|
375
|
+
$clickable
|
|
376
|
+
} = _ref16;
|
|
377
|
+
return $clickable ? css`
|
|
247
378
|
cursor: pointer;
|
|
248
379
|
&:hover {
|
|
249
380
|
opacity: 0.8;
|
|
250
381
|
}
|
|
251
382
|
` : '';
|
|
252
|
-
}}
|
|
383
|
+
}}}
|
|
384
|
+
`;
|
|
385
|
+
const Tooltip = styled.div`
|
|
386
|
+
font-family: var(--redsift-typography-font-family-raleway);
|
|
387
|
+
border: 1px solid rgb(0 0 0 / 20%);
|
|
388
|
+
padding: 10px;
|
|
389
|
+
box-shadow: 0 2px 1px -1px rgb(0 0 0 / 20%), 0 1px 1px 0 rgb(0 0 0 / 14%),
|
|
390
|
+
0 1px 3px 0 rgb(0 0 0 / 12%);
|
|
391
|
+
background-color: var(--redsift-color-primary-contrast);
|
|
253
392
|
`;
|
|
254
393
|
|
|
255
|
-
|
|
394
|
+
function _extends() {
|
|
395
|
+
_extends = Object.assign || function (target) {
|
|
396
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
397
|
+
var source = arguments[i];
|
|
398
|
+
|
|
399
|
+
for (var key in source) {
|
|
400
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
401
|
+
target[key] = source[key];
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
return target;
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
return _extends.apply(this, arguments);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
var useOnEscape = function useOnEscape(handler, active) {
|
|
413
|
+
if (active === void 0) {
|
|
414
|
+
active = true;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
useEffect(function () {
|
|
418
|
+
if (!active) return;
|
|
419
|
+
|
|
420
|
+
var listener = function listener(event) {
|
|
421
|
+
// check if key is an Escape
|
|
422
|
+
if (event.key === 'Escape') handler(event);
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
document.addEventListener('keyup', listener);
|
|
426
|
+
return function () {
|
|
427
|
+
if (!active) return;
|
|
428
|
+
document.removeEventListener('keyup', listener);
|
|
429
|
+
};
|
|
430
|
+
}, [handler, active]);
|
|
431
|
+
};
|
|
432
|
+
var useRepositionOnResize = function useRepositionOnResize(handler, active) {
|
|
433
|
+
if (active === void 0) {
|
|
434
|
+
active = true;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
useEffect(function () {
|
|
438
|
+
if (!active) return;
|
|
439
|
+
|
|
440
|
+
var listener = function listener() {
|
|
441
|
+
handler();
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
window.addEventListener('resize', listener);
|
|
445
|
+
return function () {
|
|
446
|
+
if (!active) return;
|
|
447
|
+
window.removeEventListener('resize', listener);
|
|
448
|
+
};
|
|
449
|
+
}, [handler, active]);
|
|
450
|
+
};
|
|
451
|
+
var useOnClickOutside = function useOnClickOutside(ref, handler, active) {
|
|
452
|
+
if (active === void 0) {
|
|
453
|
+
active = true;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
useEffect(function () {
|
|
457
|
+
if (!active) return;
|
|
458
|
+
|
|
459
|
+
var listener = function listener(event) {
|
|
460
|
+
// Do nothing if clicking ref's element or descendent elements
|
|
461
|
+
var refs = Array.isArray(ref) ? ref : [ref];
|
|
462
|
+
var contains = false;
|
|
463
|
+
refs.forEach(function (r) {
|
|
464
|
+
if (!r.current || r.current.contains(event.target)) {
|
|
465
|
+
contains = true;
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
});
|
|
469
|
+
event.stopPropagation();
|
|
470
|
+
if (!contains) handler(event);
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
document.addEventListener('mousedown', listener);
|
|
474
|
+
document.addEventListener('touchstart', listener);
|
|
475
|
+
return function () {
|
|
476
|
+
if (!active) return;
|
|
477
|
+
document.removeEventListener('mousedown', listener);
|
|
478
|
+
document.removeEventListener('touchstart', listener);
|
|
479
|
+
};
|
|
480
|
+
}, [ref, handler, active]);
|
|
481
|
+
}; // Make sure that user is not able TAB out of the Modal content on Open
|
|
482
|
+
|
|
483
|
+
var useTabbing = function useTabbing(contentRef, active) {
|
|
484
|
+
if (active === void 0) {
|
|
485
|
+
active = true;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
useEffect(function () {
|
|
489
|
+
if (!active) return;
|
|
490
|
+
|
|
491
|
+
var listener = function listener(event) {
|
|
492
|
+
// check if key is an Tab
|
|
493
|
+
if (event.keyCode === 9) {
|
|
494
|
+
var _contentRef$current;
|
|
495
|
+
|
|
496
|
+
var els = contentRef === null || contentRef === void 0 ? void 0 : (_contentRef$current = contentRef.current) === null || _contentRef$current === void 0 ? void 0 : _contentRef$current.querySelectorAll('a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), [tabindex="0"]');
|
|
497
|
+
var focusableEls = Array.prototype.slice.call(els);
|
|
498
|
+
|
|
499
|
+
if (focusableEls.length === 1) {
|
|
500
|
+
event.preventDefault();
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
var firstFocusableEl = focusableEls[0];
|
|
505
|
+
var lastFocusableEl = focusableEls[focusableEls.length - 1];
|
|
506
|
+
|
|
507
|
+
if (event.shiftKey && document.activeElement === firstFocusableEl) {
|
|
508
|
+
event.preventDefault();
|
|
509
|
+
lastFocusableEl.focus();
|
|
510
|
+
} else if (document.activeElement === lastFocusableEl) {
|
|
511
|
+
event.preventDefault();
|
|
512
|
+
firstFocusableEl.focus();
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
};
|
|
516
|
+
|
|
517
|
+
document.addEventListener('keydown', listener);
|
|
518
|
+
return function () {
|
|
519
|
+
if (!active) return;
|
|
520
|
+
document.removeEventListener('keydown', listener);
|
|
521
|
+
};
|
|
522
|
+
}, [contentRef, active]);
|
|
523
|
+
};
|
|
524
|
+
var useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
|
|
525
|
+
|
|
526
|
+
var Style = {
|
|
527
|
+
popupContent: {
|
|
528
|
+
tooltip: {
|
|
529
|
+
position: 'absolute',
|
|
530
|
+
zIndex: 999
|
|
531
|
+
},
|
|
532
|
+
modal: {
|
|
533
|
+
position: 'relative',
|
|
534
|
+
margin: 'auto'
|
|
535
|
+
}
|
|
536
|
+
},
|
|
537
|
+
popupArrow: {
|
|
538
|
+
height: '8px',
|
|
539
|
+
width: '16px',
|
|
540
|
+
position: 'absolute',
|
|
541
|
+
background: 'transparent',
|
|
542
|
+
color: '#FFF',
|
|
543
|
+
zIndex: -1
|
|
544
|
+
},
|
|
545
|
+
overlay: {
|
|
546
|
+
tooltip: {
|
|
547
|
+
position: 'fixed',
|
|
548
|
+
top: '0',
|
|
549
|
+
bottom: '0',
|
|
550
|
+
left: '0',
|
|
551
|
+
right: '0',
|
|
552
|
+
zIndex: 999
|
|
553
|
+
},
|
|
554
|
+
modal: {
|
|
555
|
+
position: 'fixed',
|
|
556
|
+
top: '0',
|
|
557
|
+
bottom: '0',
|
|
558
|
+
left: '0',
|
|
559
|
+
right: '0',
|
|
560
|
+
display: 'flex',
|
|
561
|
+
zIndex: 999
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
|
|
566
|
+
var POSITION_TYPES = ['top left', 'top center', 'top right', 'right top', 'right center', 'right bottom', 'bottom left', 'bottom center', 'bottom right', 'left top', 'left center', 'left bottom'];
|
|
567
|
+
|
|
568
|
+
var getCoordinatesForPosition = function getCoordinatesForPosition(triggerBounding, ContentBounding, position, //PopupPosition | PopupPosition[],
|
|
569
|
+
arrow, _ref) {
|
|
570
|
+
var offsetX = _ref.offsetX,
|
|
571
|
+
offsetY = _ref.offsetY;
|
|
572
|
+
var margin = arrow ? 8 : 0;
|
|
573
|
+
var args = position.split(' '); // the step N 1 : center the popup content => ok
|
|
574
|
+
|
|
575
|
+
var CenterTop = triggerBounding.top + triggerBounding.height / 2;
|
|
576
|
+
var CenterLeft = triggerBounding.left + triggerBounding.width / 2;
|
|
577
|
+
var height = ContentBounding.height,
|
|
578
|
+
width = ContentBounding.width;
|
|
579
|
+
var top = CenterTop - height / 2;
|
|
580
|
+
var left = CenterLeft - width / 2;
|
|
581
|
+
var transform = '';
|
|
582
|
+
var arrowTop = '0%';
|
|
583
|
+
var arrowLeft = '0%'; // the step N 2 : => ok
|
|
584
|
+
|
|
585
|
+
switch (args[0]) {
|
|
586
|
+
case 'top':
|
|
587
|
+
top -= height / 2 + triggerBounding.height / 2 + margin;
|
|
588
|
+
transform = "rotate(180deg) translateX(50%)";
|
|
589
|
+
arrowTop = '100%';
|
|
590
|
+
arrowLeft = '50%';
|
|
591
|
+
break;
|
|
592
|
+
|
|
593
|
+
case 'bottom':
|
|
594
|
+
top += height / 2 + triggerBounding.height / 2 + margin;
|
|
595
|
+
transform = "rotate(0deg) translateY(-100%) translateX(-50%)";
|
|
596
|
+
arrowLeft = '50%';
|
|
597
|
+
break;
|
|
598
|
+
|
|
599
|
+
case 'left':
|
|
600
|
+
left -= width / 2 + triggerBounding.width / 2 + margin;
|
|
601
|
+
transform = " rotate(90deg) translateY(50%) translateX(-25%)";
|
|
602
|
+
arrowLeft = '100%';
|
|
603
|
+
arrowTop = '50%';
|
|
604
|
+
break;
|
|
605
|
+
|
|
606
|
+
case 'right':
|
|
607
|
+
left += width / 2 + triggerBounding.width / 2 + margin;
|
|
608
|
+
transform = "rotate(-90deg) translateY(-150%) translateX(25%)";
|
|
609
|
+
arrowTop = '50%';
|
|
610
|
+
break;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
switch (args[1]) {
|
|
614
|
+
case 'top':
|
|
615
|
+
top = triggerBounding.top;
|
|
616
|
+
arrowTop = triggerBounding.height / 2 + "px";
|
|
617
|
+
break;
|
|
618
|
+
|
|
619
|
+
case 'bottom':
|
|
620
|
+
top = triggerBounding.top - height + triggerBounding.height;
|
|
621
|
+
arrowTop = height - triggerBounding.height / 2 + "px";
|
|
622
|
+
break;
|
|
623
|
+
|
|
624
|
+
case 'left':
|
|
625
|
+
left = triggerBounding.left;
|
|
626
|
+
arrowLeft = triggerBounding.width / 2 + "px";
|
|
627
|
+
break;
|
|
628
|
+
|
|
629
|
+
case 'right':
|
|
630
|
+
left = triggerBounding.left - width + triggerBounding.width;
|
|
631
|
+
arrowLeft = width - triggerBounding.width / 2 + "px";
|
|
632
|
+
break;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
top = args[0] === 'top' ? top - offsetY : top + offsetY;
|
|
636
|
+
left = args[0] === 'left' ? left - offsetX : left + offsetX;
|
|
637
|
+
return {
|
|
638
|
+
top: top,
|
|
639
|
+
left: left,
|
|
640
|
+
transform: transform,
|
|
641
|
+
arrowLeft: arrowLeft,
|
|
642
|
+
arrowTop: arrowTop
|
|
643
|
+
};
|
|
644
|
+
};
|
|
645
|
+
|
|
646
|
+
var getTooltipBoundary = function getTooltipBoundary(keepTooltipInside) {
|
|
647
|
+
// add viewport
|
|
648
|
+
var boundingBox = {
|
|
649
|
+
top: 0,
|
|
650
|
+
left: 0,
|
|
651
|
+
|
|
652
|
+
/* eslint-disable-next-line no-undef */
|
|
653
|
+
width: window.innerWidth,
|
|
654
|
+
|
|
655
|
+
/* eslint-disable-next-line no-undef */
|
|
656
|
+
height: window.innerHeight
|
|
657
|
+
};
|
|
658
|
+
|
|
659
|
+
if (typeof keepTooltipInside === 'string') {
|
|
660
|
+
/* eslint-disable-next-line no-undef */
|
|
661
|
+
var selector = document.querySelector(keepTooltipInside);
|
|
662
|
+
|
|
663
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
664
|
+
if (selector === null) throw new Error(keepTooltipInside + " selector does not exist : keepTooltipInside must be a valid html selector 'class' or 'Id' or a boolean value");
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
if (selector !== null) boundingBox = selector.getBoundingClientRect();
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
return boundingBox;
|
|
671
|
+
};
|
|
672
|
+
|
|
673
|
+
var calculatePosition = function calculatePosition(triggerBounding, ContentBounding, position, arrow, _ref2, keepTooltipInside) {
|
|
674
|
+
var offsetX = _ref2.offsetX,
|
|
675
|
+
offsetY = _ref2.offsetY;
|
|
676
|
+
var bestCoords = {
|
|
677
|
+
arrowLeft: '0%',
|
|
678
|
+
arrowTop: '0%',
|
|
679
|
+
left: 0,
|
|
680
|
+
top: 0,
|
|
681
|
+
transform: 'rotate(135deg)'
|
|
682
|
+
};
|
|
683
|
+
var i = 0;
|
|
684
|
+
var wrapperBox = getTooltipBoundary(keepTooltipInside);
|
|
685
|
+
var positions = Array.isArray(position) ? position : [position]; // keepTooltipInside would be activated if the keepTooltipInside exist or the position is Array
|
|
686
|
+
|
|
687
|
+
if (keepTooltipInside || Array.isArray(position)) positions = [].concat(positions, POSITION_TYPES); // add viewPort for WarpperBox
|
|
688
|
+
// wrapperBox.top = wrapperBox.top + window.scrollY;
|
|
689
|
+
// wrapperBox.left = wrapperBox.left + window.scrollX;
|
|
690
|
+
|
|
691
|
+
while (i < positions.length) {
|
|
692
|
+
bestCoords = getCoordinatesForPosition(triggerBounding, ContentBounding, positions[i], arrow, {
|
|
693
|
+
offsetX: offsetX,
|
|
694
|
+
offsetY: offsetY
|
|
695
|
+
});
|
|
696
|
+
var contentBox = {
|
|
697
|
+
top: bestCoords.top,
|
|
698
|
+
left: bestCoords.left,
|
|
699
|
+
width: ContentBounding.width,
|
|
700
|
+
height: ContentBounding.height
|
|
701
|
+
};
|
|
702
|
+
|
|
703
|
+
if (contentBox.top <= wrapperBox.top || contentBox.left <= wrapperBox.left || contentBox.top + contentBox.height >= wrapperBox.top + wrapperBox.height || contentBox.left + contentBox.width >= wrapperBox.left + wrapperBox.width) {
|
|
704
|
+
i++;
|
|
705
|
+
} else {
|
|
706
|
+
break;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
return bestCoords;
|
|
711
|
+
};
|
|
712
|
+
|
|
713
|
+
var popupIdCounter = 0;
|
|
714
|
+
|
|
715
|
+
var getRootPopup = function getRootPopup() {
|
|
716
|
+
var PopupRoot = document.getElementById('popup-root');
|
|
717
|
+
|
|
718
|
+
if (PopupRoot === null) {
|
|
719
|
+
PopupRoot = document.createElement('div');
|
|
720
|
+
PopupRoot.setAttribute('id', 'popup-root');
|
|
721
|
+
document.body.appendChild(PopupRoot);
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
return PopupRoot;
|
|
725
|
+
};
|
|
726
|
+
|
|
727
|
+
var Popup = /*#__PURE__*/forwardRef(function (_ref, ref) {
|
|
728
|
+
var _ref$trigger = _ref.trigger,
|
|
729
|
+
trigger = _ref$trigger === void 0 ? null : _ref$trigger,
|
|
730
|
+
_ref$onOpen = _ref.onOpen,
|
|
731
|
+
onOpen = _ref$onOpen === void 0 ? function () {} : _ref$onOpen,
|
|
732
|
+
_ref$onClose = _ref.onClose,
|
|
733
|
+
onClose = _ref$onClose === void 0 ? function () {} : _ref$onClose,
|
|
734
|
+
_ref$defaultOpen = _ref.defaultOpen,
|
|
735
|
+
defaultOpen = _ref$defaultOpen === void 0 ? false : _ref$defaultOpen,
|
|
736
|
+
_ref$open = _ref.open,
|
|
737
|
+
open = _ref$open === void 0 ? undefined : _ref$open,
|
|
738
|
+
_ref$disabled = _ref.disabled,
|
|
739
|
+
disabled = _ref$disabled === void 0 ? false : _ref$disabled,
|
|
740
|
+
_ref$nested = _ref.nested,
|
|
741
|
+
nested = _ref$nested === void 0 ? false : _ref$nested,
|
|
742
|
+
_ref$closeOnDocumentC = _ref.closeOnDocumentClick,
|
|
743
|
+
closeOnDocumentClick = _ref$closeOnDocumentC === void 0 ? true : _ref$closeOnDocumentC,
|
|
744
|
+
_ref$repositionOnResi = _ref.repositionOnResize,
|
|
745
|
+
repositionOnResize = _ref$repositionOnResi === void 0 ? true : _ref$repositionOnResi,
|
|
746
|
+
_ref$closeOnEscape = _ref.closeOnEscape,
|
|
747
|
+
closeOnEscape = _ref$closeOnEscape === void 0 ? true : _ref$closeOnEscape,
|
|
748
|
+
_ref$on = _ref.on,
|
|
749
|
+
on = _ref$on === void 0 ? ['click'] : _ref$on,
|
|
750
|
+
_ref$contentStyle = _ref.contentStyle,
|
|
751
|
+
contentStyle = _ref$contentStyle === void 0 ? {} : _ref$contentStyle,
|
|
752
|
+
_ref$arrowStyle = _ref.arrowStyle,
|
|
753
|
+
arrowStyle = _ref$arrowStyle === void 0 ? {} : _ref$arrowStyle,
|
|
754
|
+
_ref$overlayStyle = _ref.overlayStyle,
|
|
755
|
+
overlayStyle = _ref$overlayStyle === void 0 ? {} : _ref$overlayStyle,
|
|
756
|
+
_ref$className = _ref.className,
|
|
757
|
+
className = _ref$className === void 0 ? '' : _ref$className,
|
|
758
|
+
_ref$position = _ref.position,
|
|
759
|
+
position = _ref$position === void 0 ? 'bottom center' : _ref$position,
|
|
760
|
+
_ref$modal = _ref.modal,
|
|
761
|
+
modal = _ref$modal === void 0 ? false : _ref$modal,
|
|
762
|
+
_ref$lockScroll = _ref.lockScroll,
|
|
763
|
+
lockScroll = _ref$lockScroll === void 0 ? false : _ref$lockScroll,
|
|
764
|
+
_ref$arrow = _ref.arrow,
|
|
765
|
+
arrow = _ref$arrow === void 0 ? true : _ref$arrow,
|
|
766
|
+
_ref$offsetX = _ref.offsetX,
|
|
767
|
+
offsetX = _ref$offsetX === void 0 ? 0 : _ref$offsetX,
|
|
768
|
+
_ref$offsetY = _ref.offsetY,
|
|
769
|
+
offsetY = _ref$offsetY === void 0 ? 0 : _ref$offsetY,
|
|
770
|
+
_ref$mouseEnterDelay = _ref.mouseEnterDelay,
|
|
771
|
+
mouseEnterDelay = _ref$mouseEnterDelay === void 0 ? 100 : _ref$mouseEnterDelay,
|
|
772
|
+
_ref$mouseLeaveDelay = _ref.mouseLeaveDelay,
|
|
773
|
+
mouseLeaveDelay = _ref$mouseLeaveDelay === void 0 ? 100 : _ref$mouseLeaveDelay,
|
|
774
|
+
_ref$keepTooltipInsid = _ref.keepTooltipInside,
|
|
775
|
+
keepTooltipInside = _ref$keepTooltipInsid === void 0 ? false : _ref$keepTooltipInsid,
|
|
776
|
+
children = _ref.children;
|
|
777
|
+
|
|
778
|
+
var _useState = useState(open || defaultOpen),
|
|
779
|
+
isOpen = _useState[0],
|
|
780
|
+
setIsOpen = _useState[1];
|
|
781
|
+
|
|
782
|
+
var triggerRef = useRef(null);
|
|
783
|
+
var contentRef = useRef(null);
|
|
784
|
+
var arrowRef = useRef(null);
|
|
785
|
+
var focusedElBeforeOpen = useRef(null);
|
|
786
|
+
var popupId = useRef("popup-" + ++popupIdCounter);
|
|
787
|
+
var isModal = modal ? true : !trigger;
|
|
788
|
+
var timeOut = useRef(0);
|
|
789
|
+
useIsomorphicLayoutEffect(function () {
|
|
790
|
+
if (isOpen) {
|
|
791
|
+
focusedElBeforeOpen.current = document.activeElement;
|
|
792
|
+
setPosition();
|
|
793
|
+
focusContentOnOpen(); // for accessibility
|
|
794
|
+
|
|
795
|
+
lockScrolll();
|
|
796
|
+
} else {
|
|
797
|
+
resetScroll();
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
return function () {
|
|
801
|
+
clearTimeout(timeOut.current);
|
|
802
|
+
};
|
|
803
|
+
}, [isOpen]); // for uncontrolled popup we need to sync isOpen with open prop
|
|
804
|
+
|
|
805
|
+
useEffect(function () {
|
|
806
|
+
if (typeof open === 'boolean') {
|
|
807
|
+
if (open) openPopup();else closePopup();
|
|
808
|
+
}
|
|
809
|
+
}, [open, disabled]);
|
|
810
|
+
|
|
811
|
+
var openPopup = function openPopup(event) {
|
|
812
|
+
if (isOpen || disabled) return;
|
|
813
|
+
setIsOpen(true);
|
|
814
|
+
setTimeout(function () {
|
|
815
|
+
return onOpen(event);
|
|
816
|
+
}, 0);
|
|
817
|
+
};
|
|
818
|
+
|
|
819
|
+
var closePopup = function closePopup(event) {
|
|
820
|
+
var _focusedElBeforeOpen$;
|
|
821
|
+
|
|
822
|
+
if (!isOpen || disabled) return;
|
|
823
|
+
setIsOpen(false);
|
|
824
|
+
if (isModal) (_focusedElBeforeOpen$ = focusedElBeforeOpen.current) === null || _focusedElBeforeOpen$ === void 0 ? void 0 : _focusedElBeforeOpen$.focus();
|
|
825
|
+
setTimeout(function () {
|
|
826
|
+
return onClose(event);
|
|
827
|
+
}, 0);
|
|
828
|
+
};
|
|
829
|
+
|
|
830
|
+
var togglePopup = function togglePopup(event) {
|
|
831
|
+
event === null || event === void 0 ? void 0 : event.stopPropagation();
|
|
832
|
+
if (!isOpen) openPopup(event);else closePopup(event);
|
|
833
|
+
};
|
|
834
|
+
|
|
835
|
+
var onMouseEnter = function onMouseEnter(event) {
|
|
836
|
+
clearTimeout(timeOut.current);
|
|
837
|
+
timeOut.current = setTimeout(function () {
|
|
838
|
+
return openPopup(event);
|
|
839
|
+
}, mouseEnterDelay);
|
|
840
|
+
};
|
|
841
|
+
|
|
842
|
+
var onContextMenu = function onContextMenu(event) {
|
|
843
|
+
event === null || event === void 0 ? void 0 : event.preventDefault();
|
|
844
|
+
togglePopup();
|
|
845
|
+
};
|
|
846
|
+
|
|
847
|
+
var onMouseLeave = function onMouseLeave(event) {
|
|
848
|
+
clearTimeout(timeOut.current);
|
|
849
|
+
timeOut.current = setTimeout(function () {
|
|
850
|
+
return closePopup(event);
|
|
851
|
+
}, mouseLeaveDelay);
|
|
852
|
+
};
|
|
853
|
+
|
|
854
|
+
var lockScrolll = function lockScrolll() {
|
|
855
|
+
if (isModal && lockScroll) document.getElementsByTagName('body')[0].style.overflow = 'hidden'; // migrate to document.body
|
|
856
|
+
};
|
|
857
|
+
|
|
858
|
+
var resetScroll = function resetScroll() {
|
|
859
|
+
if (isModal && lockScroll) document.getElementsByTagName('body')[0].style.overflow = 'auto';
|
|
860
|
+
};
|
|
861
|
+
|
|
862
|
+
var focusContentOnOpen = function focusContentOnOpen() {
|
|
863
|
+
var _contentRef$current;
|
|
864
|
+
|
|
865
|
+
var focusableEls = contentRef === null || contentRef === void 0 ? void 0 : (_contentRef$current = contentRef.current) === null || _contentRef$current === void 0 ? void 0 : _contentRef$current.querySelectorAll('a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), [tabindex="0"]');
|
|
866
|
+
var firstEl = Array.prototype.slice.call(focusableEls)[0];
|
|
867
|
+
firstEl === null || firstEl === void 0 ? void 0 : firstEl.focus();
|
|
868
|
+
};
|
|
869
|
+
|
|
870
|
+
useImperativeHandle(ref, function () {
|
|
871
|
+
return {
|
|
872
|
+
open: function open() {
|
|
873
|
+
openPopup();
|
|
874
|
+
},
|
|
875
|
+
close: function close() {
|
|
876
|
+
closePopup();
|
|
877
|
+
},
|
|
878
|
+
toggle: function toggle() {
|
|
879
|
+
togglePopup();
|
|
880
|
+
}
|
|
881
|
+
};
|
|
882
|
+
}); // set Position
|
|
883
|
+
|
|
884
|
+
var setPosition = function setPosition() {
|
|
885
|
+
if (isModal || !isOpen) return;
|
|
886
|
+
if (!(triggerRef === null || triggerRef === void 0 ? void 0 : triggerRef.current) || !(triggerRef === null || triggerRef === void 0 ? void 0 : triggerRef.current) || !(contentRef === null || contentRef === void 0 ? void 0 : contentRef.current)) return; /// show error as one of ref is undefined
|
|
887
|
+
|
|
888
|
+
var trigger = triggerRef.current.getBoundingClientRect();
|
|
889
|
+
var content = contentRef.current.getBoundingClientRect();
|
|
890
|
+
var cords = calculatePosition(trigger, content, position, arrow, {
|
|
891
|
+
offsetX: offsetX,
|
|
892
|
+
offsetY: offsetY
|
|
893
|
+
}, keepTooltipInside);
|
|
894
|
+
contentRef.current.style.top = cords.top + window.scrollY + "px";
|
|
895
|
+
contentRef.current.style.left = cords.left + window.scrollX + "px";
|
|
896
|
+
|
|
897
|
+
if (arrow && !!arrowRef.current) {
|
|
898
|
+
var _arrowStyle$top, _arrowStyle$left;
|
|
899
|
+
|
|
900
|
+
arrowRef.current.style.transform = cords.transform;
|
|
901
|
+
arrowRef.current.style.setProperty('-ms-transform', cords.transform);
|
|
902
|
+
arrowRef.current.style.setProperty('-webkit-transform', cords.transform);
|
|
903
|
+
arrowRef.current.style.top = ((_arrowStyle$top = arrowStyle.top) === null || _arrowStyle$top === void 0 ? void 0 : _arrowStyle$top.toString()) || cords.arrowTop;
|
|
904
|
+
arrowRef.current.style.left = ((_arrowStyle$left = arrowStyle.left) === null || _arrowStyle$left === void 0 ? void 0 : _arrowStyle$left.toString()) || cords.arrowLeft;
|
|
905
|
+
}
|
|
906
|
+
}; // hooks
|
|
907
|
+
|
|
908
|
+
|
|
909
|
+
useOnEscape(closePopup, closeOnEscape); // can be optimized if we disabled for hover
|
|
910
|
+
|
|
911
|
+
useTabbing(contentRef, isOpen && isModal);
|
|
912
|
+
useRepositionOnResize(setPosition, repositionOnResize);
|
|
913
|
+
useOnClickOutside(!!trigger ? [contentRef, triggerRef] : [contentRef], closePopup, closeOnDocumentClick && !nested); // we need to add a ne
|
|
914
|
+
// render the trigger element and add events
|
|
915
|
+
|
|
916
|
+
var renderTrigger = function renderTrigger() {
|
|
917
|
+
var triggerProps = {
|
|
918
|
+
key: 'T',
|
|
919
|
+
ref: triggerRef,
|
|
920
|
+
'aria-describedby': popupId.current
|
|
921
|
+
};
|
|
922
|
+
var onAsArray = Array.isArray(on) ? on : [on];
|
|
923
|
+
|
|
924
|
+
for (var i = 0, len = onAsArray.length; i < len; i++) {
|
|
925
|
+
switch (onAsArray[i]) {
|
|
926
|
+
case 'click':
|
|
927
|
+
triggerProps.onClick = togglePopup;
|
|
928
|
+
break;
|
|
929
|
+
|
|
930
|
+
case 'right-click':
|
|
931
|
+
triggerProps.onContextMenu = onContextMenu;
|
|
932
|
+
break;
|
|
933
|
+
|
|
934
|
+
case 'hover':
|
|
935
|
+
triggerProps.onMouseEnter = onMouseEnter;
|
|
936
|
+
triggerProps.onMouseLeave = onMouseLeave;
|
|
937
|
+
break;
|
|
938
|
+
|
|
939
|
+
case 'focus':
|
|
940
|
+
triggerProps.onFocus = onMouseEnter;
|
|
941
|
+
triggerProps.onBlur = onMouseLeave;
|
|
942
|
+
break;
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
if (typeof trigger === 'function') {
|
|
947
|
+
var comp = trigger(isOpen);
|
|
948
|
+
return !!trigger && React.cloneElement(comp, triggerProps);
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
return !!trigger && React.cloneElement(trigger, triggerProps);
|
|
952
|
+
};
|
|
953
|
+
|
|
954
|
+
var addWarperAction = function addWarperAction() {
|
|
955
|
+
var popupContentStyle = isModal ? Style.popupContent.modal : Style.popupContent.tooltip;
|
|
956
|
+
var childrenElementProps = {
|
|
957
|
+
className: "popup-content " + (className !== '' ? className.split(' ').map(function (c) {
|
|
958
|
+
return c + "-content";
|
|
959
|
+
}).join(' ') : ''),
|
|
960
|
+
style: _extends({}, popupContentStyle, contentStyle, {
|
|
961
|
+
pointerEvents: 'auto'
|
|
962
|
+
}),
|
|
963
|
+
ref: contentRef,
|
|
964
|
+
onClick: function onClick(e) {
|
|
965
|
+
e.stopPropagation();
|
|
966
|
+
}
|
|
967
|
+
};
|
|
968
|
+
|
|
969
|
+
if (!modal && on.indexOf('hover') >= 0) {
|
|
970
|
+
childrenElementProps.onMouseEnter = onMouseEnter;
|
|
971
|
+
childrenElementProps.onMouseLeave = onMouseLeave;
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
return childrenElementProps;
|
|
975
|
+
};
|
|
976
|
+
|
|
977
|
+
var renderContent = function renderContent() {
|
|
978
|
+
return React.createElement("div", Object.assign({}, addWarperAction(), {
|
|
979
|
+
key: "C",
|
|
980
|
+
role: isModal ? 'dialog' : 'tooltip',
|
|
981
|
+
id: popupId.current
|
|
982
|
+
}), arrow && !isModal && React.createElement("div", {
|
|
983
|
+
ref: arrowRef,
|
|
984
|
+
style: Style.popupArrow
|
|
985
|
+
}, React.createElement("svg", {
|
|
986
|
+
"data-testid": "arrow",
|
|
987
|
+
className: "popup-arrow " + (className !== '' ? className.split(' ').map(function (c) {
|
|
988
|
+
return c + "-arrow";
|
|
989
|
+
}).join(' ') : ''),
|
|
990
|
+
viewBox: "0 0 32 16",
|
|
991
|
+
style: _extends({
|
|
992
|
+
position: 'absolute'
|
|
993
|
+
}, arrowStyle)
|
|
994
|
+
}, React.createElement("path", {
|
|
995
|
+
d: "M16 0l16 16H0z",
|
|
996
|
+
fill: "currentcolor"
|
|
997
|
+
}))), children && typeof children === 'function' ? children(closePopup, isOpen) : children);
|
|
998
|
+
};
|
|
999
|
+
|
|
1000
|
+
var overlay = !(on.indexOf('hover') >= 0);
|
|
1001
|
+
var ovStyle = isModal ? Style.overlay.modal : Style.overlay.tooltip;
|
|
1002
|
+
var content = [overlay && React.createElement("div", {
|
|
1003
|
+
key: "O",
|
|
1004
|
+
"data-testid": "overlay",
|
|
1005
|
+
"data-popup": isModal ? 'modal' : 'tooltip',
|
|
1006
|
+
className: "popup-overlay " + (className !== '' ? className.split(' ').map(function (c) {
|
|
1007
|
+
return c + "-overlay";
|
|
1008
|
+
}).join(' ') : ''),
|
|
1009
|
+
style: _extends({}, ovStyle, overlayStyle, {
|
|
1010
|
+
pointerEvents: closeOnDocumentClick && nested || isModal ? 'auto' : 'none'
|
|
1011
|
+
}),
|
|
1012
|
+
onClick: closeOnDocumentClick && nested ? closePopup : undefined,
|
|
1013
|
+
tabIndex: -1
|
|
1014
|
+
}, isModal && renderContent()), !isModal && renderContent()];
|
|
1015
|
+
return React.createElement(React.Fragment, null, renderTrigger(), isOpen && ReactDOM.createPortal(content, getRootPopup()));
|
|
1016
|
+
});
|
|
1017
|
+
|
|
1018
|
+
const _excluded$1 = ["color", "data", "index", "onClick", "path", "spaced", "showTooltip", "tooltipPercent", "tooltipLabelOnly", "isEmpty"];
|
|
256
1019
|
const COMPONENT_NAME$1 = 'RedSiftPieChartArc';
|
|
257
1020
|
const CLASSNAME$1 = 'redsift-piechart-arc';
|
|
258
1021
|
const DEFAULT_PROPS$1 = {};
|
|
@@ -262,39 +1025,54 @@ const PieChartArc = /*#__PURE__*/forwardRef((props, ref) => {
|
|
|
262
1025
|
data,
|
|
263
1026
|
index,
|
|
264
1027
|
onClick,
|
|
265
|
-
onMouseOver,
|
|
266
1028
|
path,
|
|
267
|
-
spaced
|
|
1029
|
+
spaced,
|
|
1030
|
+
showTooltip,
|
|
1031
|
+
tooltipPercent,
|
|
1032
|
+
tooltipLabelOnly,
|
|
1033
|
+
isEmpty
|
|
268
1034
|
} = props,
|
|
269
1035
|
forwardedProps = _objectWithoutProperties(props, _excluded$1);
|
|
270
|
-
|
|
1036
|
+
const Arc = /*#__PURE__*/React.createElement(StyledPieChartArc, _extends$1({
|
|
271
1037
|
ref: ref
|
|
272
1038
|
}, forwardedProps, {
|
|
273
1039
|
role: onClick ? 'button' : undefined,
|
|
274
1040
|
className: `pie-slice _${index}`,
|
|
275
1041
|
onClick: onClick,
|
|
276
|
-
|
|
277
|
-
$hover: Boolean(onMouseOver),
|
|
1042
|
+
$clickable: Boolean(onClick),
|
|
278
1043
|
$spaced: Boolean(spaced)
|
|
279
1044
|
}), /*#__PURE__*/React.createElement("path", {
|
|
280
1045
|
d: path,
|
|
281
1046
|
fill: color
|
|
282
|
-
}), /*#__PURE__*/React.createElement("title", null, `${data.name}: ${data.value}`));
|
|
1047
|
+
}), !showTooltip && !isEmpty && /*#__PURE__*/React.createElement("title", null, `${data.name}: ${data.value}`));
|
|
1048
|
+
if (showTooltip) {
|
|
1049
|
+
const tooltipValue = tooltipPercent !== null ? `${tooltipPercent}%` : data.value;
|
|
1050
|
+
return /*#__PURE__*/React.createElement(Popup, {
|
|
1051
|
+
on: "hover",
|
|
1052
|
+
arrow: false,
|
|
1053
|
+
position: "right center",
|
|
1054
|
+
trigger: Arc
|
|
1055
|
+
}, /*#__PURE__*/React.createElement(Tooltip, null, `${data.name}${tooltipLabelOnly ? '' : ` - ${tooltipValue}`}`));
|
|
1056
|
+
}
|
|
1057
|
+
return Arc;
|
|
283
1058
|
});
|
|
284
1059
|
PieChartArc.className = CLASSNAME$1;
|
|
285
1060
|
PieChartArc.defaultProps = DEFAULT_PROPS$1;
|
|
286
1061
|
PieChartArc.displayName = COMPONENT_NAME$1;
|
|
287
1062
|
|
|
288
|
-
const _excluded = ["caping", "caption", "className", "data", "labelVariant", "onSliceClick", "
|
|
1063
|
+
const _excluded = ["aria-label", "aria-labelledby", "caping", "caption", "className", "data", "labelVariant", "onSliceClick", "others", "size", "text", "middleText", "subtext", "theme", "title", "variant", "tooltipVariant", "localeText"];
|
|
289
1064
|
const COMPONENT_NAME = 'RedSiftPieChart';
|
|
290
1065
|
const CLASSNAME = 'redsift-piechart';
|
|
291
1066
|
const DEFAULT_PROPS = {
|
|
292
|
-
labelVariant: PieChartLabelVariant.
|
|
1067
|
+
labelVariant: PieChartLabelVariant.none,
|
|
293
1068
|
others: true,
|
|
294
1069
|
size: PieChartSize.medium,
|
|
295
1070
|
theme: PieChartTheme.default,
|
|
296
|
-
|
|
297
|
-
|
|
1071
|
+
variant: PieChartVariant.plain,
|
|
1072
|
+
tooltipVariant: PieChartTooltipVariant.value,
|
|
1073
|
+
localeText: {
|
|
1074
|
+
emptyChartText: 'No Data'
|
|
1075
|
+
}
|
|
298
1076
|
};
|
|
299
1077
|
const sizeToDimension = size => {
|
|
300
1078
|
switch (size) {
|
|
@@ -311,40 +1089,72 @@ const sizeToDimension = size => {
|
|
|
311
1089
|
case PieChartSize.medium:
|
|
312
1090
|
default:
|
|
313
1091
|
return {
|
|
314
|
-
width:
|
|
315
|
-
height:
|
|
1092
|
+
width: 240,
|
|
1093
|
+
height: 240
|
|
316
1094
|
};
|
|
317
1095
|
}
|
|
318
1096
|
};
|
|
1097
|
+
const sizeToSmallFontSize = size => {
|
|
1098
|
+
switch (size) {
|
|
1099
|
+
case PieChartSize.small:
|
|
1100
|
+
return 13;
|
|
1101
|
+
case PieChartSize.large:
|
|
1102
|
+
return 18;
|
|
1103
|
+
case PieChartSize.medium:
|
|
1104
|
+
default:
|
|
1105
|
+
return 14;
|
|
1106
|
+
}
|
|
1107
|
+
};
|
|
1108
|
+
const sizeToFontSize = size => {
|
|
1109
|
+
switch (size) {
|
|
1110
|
+
case PieChartSize.small:
|
|
1111
|
+
return 30;
|
|
1112
|
+
case PieChartSize.large:
|
|
1113
|
+
return 38;
|
|
1114
|
+
case PieChartSize.medium:
|
|
1115
|
+
default:
|
|
1116
|
+
return 34;
|
|
1117
|
+
}
|
|
1118
|
+
};
|
|
319
1119
|
const sizeToInnerRadius = size => {
|
|
320
1120
|
switch (size) {
|
|
321
1121
|
case PieChartSize.small:
|
|
322
|
-
return
|
|
1122
|
+
return 55;
|
|
323
1123
|
case PieChartSize.large:
|
|
324
|
-
return
|
|
1124
|
+
return 80;
|
|
325
1125
|
case PieChartSize.medium:
|
|
326
1126
|
default:
|
|
327
|
-
return
|
|
1127
|
+
return 65;
|
|
328
1128
|
}
|
|
329
1129
|
};
|
|
330
1130
|
const PieChart = /*#__PURE__*/forwardRef((props, ref) => {
|
|
1131
|
+
const id = useId();
|
|
331
1132
|
const {
|
|
1133
|
+
'aria-label': ariaLabel,
|
|
1134
|
+
'aria-labelledby': ariaLabelledby,
|
|
332
1135
|
caping,
|
|
333
1136
|
caption,
|
|
334
1137
|
className,
|
|
335
1138
|
data: propData,
|
|
336
1139
|
labelVariant,
|
|
337
1140
|
onSliceClick,
|
|
338
|
-
onSliceHover,
|
|
339
1141
|
others,
|
|
340
1142
|
size,
|
|
341
|
-
subtext,
|
|
342
1143
|
text,
|
|
1144
|
+
middleText,
|
|
1145
|
+
subtext,
|
|
343
1146
|
theme,
|
|
344
1147
|
title,
|
|
345
|
-
variant
|
|
1148
|
+
variant,
|
|
1149
|
+
tooltipVariant,
|
|
1150
|
+
localeText
|
|
346
1151
|
} = props,
|
|
347
1152
|
forwardedProps = _objectWithoutProperties(props, _excluded);
|
|
1153
|
+
const {
|
|
1154
|
+
emptyChartText
|
|
1155
|
+
} = _objectSpread2(_objectSpread2({}, DEFAULT_PROPS.localeText), localeText);
|
|
1156
|
+
const isEmpty = propData.every(d => d.value === 0);
|
|
1157
|
+
const isDonut = variant === PieChartVariant.donut || variant === PieChartVariant.spacedDonut;
|
|
348
1158
|
let data = propData.sort((a, b) => a.value < b.value ? 1 : -1);
|
|
349
1159
|
if (caping) {
|
|
350
1160
|
if (typeof others === 'boolean' && !others) {
|
|
@@ -367,6 +1177,12 @@ const PieChart = /*#__PURE__*/forwardRef((props, ref) => {
|
|
|
367
1177
|
}, []);
|
|
368
1178
|
}
|
|
369
1179
|
}
|
|
1180
|
+
if (isEmpty) {
|
|
1181
|
+
data = [{
|
|
1182
|
+
name: emptyChartText,
|
|
1183
|
+
value: 100
|
|
1184
|
+
}];
|
|
1185
|
+
}
|
|
370
1186
|
|
|
371
1187
|
// Get charts dimensions based on the selected size.
|
|
372
1188
|
const chartDimensions = sizeToDimension(size);
|
|
@@ -374,54 +1190,64 @@ const PieChart = /*#__PURE__*/forwardRef((props, ref) => {
|
|
|
374
1190
|
const innerRadius = sizeToInnerRadius(size);
|
|
375
1191
|
|
|
376
1192
|
// Set color scheme.
|
|
377
|
-
|
|
378
|
-
if (typeof theme === 'string') {
|
|
379
|
-
d3colors = scaleOrdinal(scheme[theme]);
|
|
380
|
-
} else if (typeof theme === 'object') {
|
|
381
|
-
d3colors = scaleOrdinal().domain([theme.success, theme.warning, theme.danger]).range([successDangerScheme.success, successDangerScheme.warning, successDangerScheme.danger]).unknown(neutral);
|
|
382
|
-
}
|
|
1193
|
+
const d3colors = getColorScale(theme, isEmpty);
|
|
383
1194
|
|
|
384
1195
|
// Initialize the chart.
|
|
385
1196
|
const createPie = pie().value(d => d.value).sort(null);
|
|
386
|
-
const createArc = arc().innerRadius(
|
|
1197
|
+
const createArc = arc().innerRadius(isDonut ? innerRadius : 0).outerRadius(chartDimensions.width / 2 - externalRadiusPadding);
|
|
387
1198
|
const pieData = createPie(data);
|
|
388
1199
|
const total = sum(data, d => d.value);
|
|
389
|
-
return /*#__PURE__*/React.createElement(StyledPieChart, _extends({
|
|
1200
|
+
return /*#__PURE__*/React.createElement(StyledPieChart, _extends$1({
|
|
390
1201
|
ref: ref
|
|
391
1202
|
}, forwardedProps, {
|
|
392
1203
|
className: className
|
|
393
|
-
}), /*#__PURE__*/React.createElement(StyledPieChartTitle, {
|
|
394
|
-
className: `${PieChart.className}__title
|
|
395
|
-
|
|
1204
|
+
}), title ? /*#__PURE__*/React.createElement(StyledPieChartTitle, {
|
|
1205
|
+
className: `${PieChart.className}__title`,
|
|
1206
|
+
id: `id${id}__title`
|
|
1207
|
+
}, title) : null, /*#__PURE__*/React.createElement("div", {
|
|
396
1208
|
className: `${PieChart.className}__container`
|
|
397
1209
|
}, /*#__PURE__*/React.createElement("div", {
|
|
398
|
-
className: `${PieChart.className}-container__chart
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
},
|
|
1210
|
+
className: `${PieChart.className}-container__chart`,
|
|
1211
|
+
"aria-label": ariaLabel ? ariaLabel : ariaLabelledby || title ? undefined : 'Pie Chart',
|
|
1212
|
+
"aria-labelledby": ariaLabelledby ? ariaLabelledby : title ? `id${id}__title` : undefined
|
|
1213
|
+
}, text && !isEmpty ? /*#__PURE__*/React.createElement(StyledPieChartCenterText, {
|
|
1214
|
+
$maxWidth: innerRadius * 2,
|
|
1215
|
+
$textSize: sizeToFontSize(size),
|
|
1216
|
+
$smallTextSize: sizeToSmallFontSize(size)
|
|
1217
|
+
}, subtext ? /*#__PURE__*/React.createElement("b", null, text) : /*#__PURE__*/React.createElement("span", null, text), middleText ? /*#__PURE__*/React.createElement("b", null, middleText) : null, /*#__PURE__*/React.createElement("span", null, subtext)) : null, isEmpty ? /*#__PURE__*/React.createElement(StyledPieChartEmptyText, {
|
|
1218
|
+
$maxWidth: innerRadius * 2,
|
|
1219
|
+
$textSize: sizeToFontSize(size) / 2,
|
|
1220
|
+
$isDonut: isDonut
|
|
1221
|
+
}, /*#__PURE__*/React.createElement("span", null, emptyChartText)) : null, /*#__PURE__*/React.createElement("svg", {
|
|
402
1222
|
width: chartDimensions.width,
|
|
403
1223
|
height: chartDimensions.height
|
|
404
1224
|
}, /*#__PURE__*/React.createElement("g", {
|
|
405
1225
|
transform: `translate(${chartDimensions.width / 2} ${chartDimensions.height / 2})`
|
|
406
1226
|
}, /*#__PURE__*/React.createElement("g", {
|
|
407
1227
|
className: "pie-slice-group"
|
|
408
|
-
}, pieData.map((datum, index) =>
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
1228
|
+
}, pieData.map((datum, index) => {
|
|
1229
|
+
const percent = (datum.data.value / total * 100).toFixed(2);
|
|
1230
|
+
return /*#__PURE__*/React.createElement(PieChartArc, {
|
|
1231
|
+
color: d3colors(datum.data.name),
|
|
1232
|
+
data: datum.data,
|
|
1233
|
+
isEmpty: isEmpty,
|
|
1234
|
+
index: index,
|
|
1235
|
+
key: `pie-slice _${index}`,
|
|
1236
|
+
onClick: onSliceClick ? () => onSliceClick(data[index]) : undefined,
|
|
1237
|
+
showTooltip: tooltipVariant !== PieChartTooltipVariant.none && !isEmpty,
|
|
1238
|
+
tooltipLabelOnly: tooltipVariant == PieChartTooltipVariant.label,
|
|
1239
|
+
tooltipPercent: tooltipVariant === PieChartTooltipVariant.percent ? percent : null,
|
|
1240
|
+
path: createArc(datum),
|
|
1241
|
+
spaced: variant === PieChartVariant.spaced || variant === PieChartVariant.spacedDonut
|
|
1242
|
+
});
|
|
1243
|
+
})), labelVariant === PieChartLabelVariant.internal ? /*#__PURE__*/React.createElement("g", {
|
|
418
1244
|
className: "pie-label-group"
|
|
419
1245
|
}, pieData.map((datum, index) => /*#__PURE__*/React.createElement("text", {
|
|
420
1246
|
className: `pie-slice pie-label _${index}`,
|
|
421
1247
|
key: `pie-slice pie-label _${index}`,
|
|
422
1248
|
textAnchor: "middle",
|
|
423
1249
|
transform: `translate(${createArc.centroid(datum)})`
|
|
424
|
-
}, index <
|
|
1250
|
+
}, index < 2 || caping ? datum.data.name : null))) : null))), labelVariant !== PieChartLabelVariant.none && labelVariant !== PieChartLabelVariant.internal ? /*#__PURE__*/React.createElement("ul", null, data.map((_ref, index) => {
|
|
425
1251
|
let {
|
|
426
1252
|
name,
|
|
427
1253
|
value
|
|
@@ -429,7 +1255,7 @@ const PieChart = /*#__PURE__*/forwardRef((props, ref) => {
|
|
|
429
1255
|
return /*#__PURE__*/React.createElement(StyledPieChartLabel, {
|
|
430
1256
|
key: `pie-external-label _${index}`,
|
|
431
1257
|
$color: d3colors(name)
|
|
432
|
-
}, /*#__PURE__*/React.createElement("div", null),
|
|
1258
|
+
}, /*#__PURE__*/React.createElement("div", null), labelVariant === PieChartLabelVariant.externalLabelValue ? /*#__PURE__*/React.createElement("b", null, value) : labelVariant === PieChartLabelVariant.externalLabelPercent ? /*#__PURE__*/React.createElement("b", null, `${(value * 100 / total).toFixed(2)}%`) : null, /*#__PURE__*/React.createElement("span", null, name));
|
|
433
1259
|
})) : null), caption ? /*#__PURE__*/React.createElement(StyledPieChartCaption, {
|
|
434
1260
|
className: `${PieChart.className}__caption`
|
|
435
1261
|
}, caption) : null);
|
|
@@ -438,5 +1264,5 @@ PieChart.className = CLASSNAME;
|
|
|
438
1264
|
PieChart.defaultProps = DEFAULT_PROPS;
|
|
439
1265
|
PieChart.displayName = COMPONENT_NAME;
|
|
440
1266
|
|
|
441
|
-
export { PieChart, PieChartLabelVariant, PieChartSize, PieChartTheme, PieChartVariant, StyledPieChart, StyledPieChartArc, StyledPieChartCaption, StyledPieChartCenterText, StyledPieChartLabel, StyledPieChartTitle,
|
|
1267
|
+
export { ColorTheme, PieChart, PieChartLabelVariant, PieChartSize, PieChartTheme, PieChartTooltipVariant, PieChartVariant, StyledPieChart, StyledPieChartArc, StyledPieChartCaption, StyledPieChartCenterText, StyledPieChartEmptyText, StyledPieChartLabel, StyledPieChartTitle, Tooltip, getColorScale, monochrome, scheme, successDangerScheme };
|
|
442
1268
|
//# sourceMappingURL=index.js.map
|