@react-magma/charts 13.3.0 → 13.3.2-v4-next.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-magma/charts",
3
- "version": "13.3.0",
3
+ "version": "13.3.2-v4-next.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -47,7 +47,7 @@
47
47
  "identity-obj-proxy": "^3.0.0",
48
48
  "react": "^17.0.2",
49
49
  "react-dom": "^17.0.2",
50
- "react-magma-dom": "^4.16.0",
50
+ "react-magma-dom": "^4.17.0",
51
51
  "react-magma-icons": "^3.2.5",
52
52
  "rollup": "^4.52.4",
53
53
  "rollup-plugin-postcss": "^4.0.2"
@@ -979,6 +979,77 @@ describe('CarbonChart', () => {
979
979
  });
980
980
  });
981
981
 
982
+ describe('fullscreen', () => {
983
+ const toolbarProps = {
984
+ dataSet,
985
+ options: chartOptions,
986
+ type: CarbonChartType.bar,
987
+ chartToolbar: {},
988
+ };
989
+
990
+ let setOptions;
991
+ let createChartSpy;
992
+
993
+ const setFullscreenElement = element => {
994
+ Object.defineProperty(document, 'fullscreenElement', {
995
+ configurable: true,
996
+ value: element,
997
+ });
998
+ act(() => {
999
+ document.dispatchEvent(new Event('fullscreenchange'));
1000
+ });
1001
+ };
1002
+
1003
+ beforeEach(() => {
1004
+ setOptions = jest.fn();
1005
+ createChartSpy = jest
1006
+ .spyOn(SimpleBarChart.prototype, 'createChart')
1007
+ .mockImplementation(holder => {
1008
+ holder.classList.add('cds--chart-holder');
1009
+ holder.style.height = chartOptions.height;
1010
+
1011
+ return { model: { setOptions, setData: jest.fn() } };
1012
+ });
1013
+ });
1014
+
1015
+ afterEach(() => {
1016
+ createChartSpy.mockRestore();
1017
+ delete document.fullscreenElement;
1018
+ });
1019
+
1020
+ it('does not update chart options when entering and exiting fullscreen', () => {
1021
+ const { container } = render(<CarbonChart {...toolbarProps} />);
1022
+ const holder = container.querySelector('.cds--chart-holder');
1023
+
1024
+ setFullscreenElement(container.firstChild);
1025
+
1026
+ expect(
1027
+ screen.getByRole('button', {
1028
+ name: `Exit ${chartOptions.title} full screen`,
1029
+ })
1030
+ ).toBeInTheDocument();
1031
+ expect(holder.style.height).toBe('100vh');
1032
+
1033
+ setFullscreenElement(null);
1034
+
1035
+ expect(
1036
+ screen.getByRole('button', {
1037
+ name: `View ${chartOptions.title} full screen`,
1038
+ })
1039
+ ).toBeInTheDocument();
1040
+ expect(holder.style.height).toBe(chartOptions.height);
1041
+ expect(setOptions).not.toHaveBeenCalled();
1042
+ });
1043
+
1044
+ it('does not update chart options on re-render with the same props', () => {
1045
+ const { rerender } = render(<CarbonChart {...toolbarProps} />);
1046
+
1047
+ rerender(<CarbonChart {...toolbarProps} chartToolbar={{}} />);
1048
+
1049
+ expect(setOptions).not.toHaveBeenCalled();
1050
+ });
1051
+ });
1052
+
982
1053
  describe('image export', () => {
983
1054
  const toolbarProps = {
984
1055
  dataSet,
@@ -1144,15 +1215,6 @@ describe('CarbonChart', () => {
1144
1215
  });
1145
1216
 
1146
1217
  describe('focus on dot content', () => {
1147
- it('should set opacity to 1 when a dot receives focus', () => {
1148
- const wrapper = renderChart();
1149
- const dot = addDot(wrapper);
1150
-
1151
- fireEvent.focusIn(dot);
1152
-
1153
- expect(dot.style.opacity).toBe('1');
1154
- });
1155
-
1156
1218
  it('should dispatch mouseover on dot focusin to reveal tooltip data', () => {
1157
1219
  const wrapper = renderChart();
1158
1220
  const dot = addDot(wrapper);
@@ -1192,28 +1254,45 @@ describe('CarbonChart', () => {
1192
1254
  });
1193
1255
 
1194
1256
  describe('data visibility', () => {
1195
- it('should reset dot opacity when dot loses focus', () => {
1257
+ it('should dispatch mouseout on dot focusout to hide tooltip', () => {
1196
1258
  const wrapper = renderChart();
1197
1259
  const dot = addDot(wrapper);
1198
1260
 
1199
- fireEvent.focusIn(dot);
1200
- expect(dot.style.opacity).toBe('1');
1261
+ const mouseoutSpy = jest.fn();
1262
+ dot.addEventListener('mouseout', mouseoutSpy);
1201
1263
 
1264
+ fireEvent.focusIn(dot);
1202
1265
  fireEvent.focusOut(dot);
1203
- expect(dot.style.opacity).toBe('');
1266
+
1267
+ expect(mouseoutSpy).toHaveBeenCalledTimes(1);
1204
1268
  });
1205
1269
 
1206
- it('should dispatch mouseout on dot focusout to hide tooltip', () => {
1270
+ it('should not dispatch mouseout when focus moves from one dot to another', () => {
1207
1271
  const wrapper = renderChart();
1208
- const dot = addDot(wrapper);
1272
+ const firstDot = addDot(wrapper);
1273
+ const secondDot = addDot(wrapper);
1209
1274
 
1210
1275
  const mouseoutSpy = jest.fn();
1211
- dot.addEventListener('mouseout', mouseoutSpy);
1276
+ firstDot.addEventListener('mouseout', mouseoutSpy);
1212
1277
 
1213
- fireEvent.focusIn(dot);
1214
- fireEvent.focusOut(dot);
1278
+ fireEvent.focusOut(firstDot, { relatedTarget: secondDot });
1215
1279
 
1216
- expect(mouseoutSpy).toHaveBeenCalledTimes(1);
1280
+ expect(mouseoutSpy).not.toHaveBeenCalled();
1281
+ });
1282
+ });
1283
+
1284
+ describe('dynamic dot rendering', () => {
1285
+ it('should stamp tabindex="0" on dots added after initial raf pass', () => {
1286
+ const wrapper = renderChart();
1287
+
1288
+ // Run initial pass before adding a dot so the observer must handle it.
1289
+ act(() => rafCallbacks[rafCallbacks.length - 1](0));
1290
+
1291
+ const lateDot = addDot(wrapper);
1292
+
1293
+ act(() => mutationObserverCallback([]));
1294
+
1295
+ expect(lateDot).toHaveAttribute('tabindex', '0');
1217
1296
  });
1218
1297
  });
1219
1298
  });
@@ -1373,28 +1373,32 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1373
1373
  isInverse,
1374
1374
  ]);
1375
1375
 
1376
- const newOptions = {
1377
- ...options,
1378
- theme: isInverse ? ChartTheme.G100 : ChartTheme.WHITE,
1379
- color: {
1380
- scale: colorScale,
1381
- },
1382
- tooltip: {
1383
- ...(options?.tooltip || {}),
1384
- truncation: {
1385
- type: 'none',
1376
+ const hasChartToolbar = Boolean(chartToolbar);
1377
+ const newOptions = React.useMemo(
1378
+ () => ({
1379
+ ...options,
1380
+ theme: isInverse ? ChartTheme.G100 : ChartTheme.WHITE,
1381
+ color: {
1382
+ scale: colorScale,
1386
1383
  },
1387
- },
1388
- ...(chartToolbar
1389
- ? {
1390
- toolbar: { enabled: false },
1391
- title: chartTitle,
1392
- ...(options.fileDownload
1393
- ? {}
1394
- : { fileDownload: { fileName: chartTitle } }),
1395
- }
1396
- : {}),
1397
- };
1384
+ tooltip: {
1385
+ ...(options?.tooltip || {}),
1386
+ truncation: {
1387
+ type: 'none',
1388
+ },
1389
+ },
1390
+ ...(hasChartToolbar
1391
+ ? {
1392
+ toolbar: { enabled: false },
1393
+ title: chartTitle,
1394
+ ...(options.fileDownload
1395
+ ? {}
1396
+ : { fileDownload: { fileName: chartTitle } }),
1397
+ }
1398
+ : {}),
1399
+ }),
1400
+ [options, isInverse, colorScale, hasChartToolbar, chartTitle]
1401
+ );
1398
1402
 
1399
1403
  const ChartType = ALL_CHARTS[type];
1400
1404
 
@@ -1408,94 +1412,17 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1408
1412
  return () => cancelAnimationFrame(rafId);
1409
1413
  }, [ariaLabel]);
1410
1414
 
1411
- // Make Carbon Charts data points keyboard-focusable.
1412
- React.useEffect(() => {
1413
- const wrapper = internalRef.current;
1414
- if (!wrapper) return;
1415
-
1416
- const isDot = (el: EventTarget | null): el is SVGCircleElement =>
1417
- el instanceof Element &&
1418
- el.nodeName.toLowerCase() === 'circle' &&
1419
- el.classList.contains('dot');
1420
-
1421
- const onFocusIn = (e: FocusEvent) => {
1422
- if (!isDot(e.target)) return;
1423
- const dot = e.target as SVGCircleElement;
1424
- dot.style.opacity = '1';
1425
- const { left, top, width, height } = dot.getBoundingClientRect();
1426
- const cx = left + width / 2;
1427
- const cy = top + height / 2;
1428
- dot.dispatchEvent(
1429
- new MouseEvent('mouseover', {
1430
- bubbles: true,
1431
- clientX: cx,
1432
- clientY: cy,
1433
- screenX: cx,
1434
- screenY: cy,
1435
- })
1436
- );
1437
- dot.dispatchEvent(
1438
- new MouseEvent('mousemove', {
1439
- bubbles: true,
1440
- clientX: cx,
1441
- clientY: cy,
1442
- screenX: cx,
1443
- screenY: cy,
1444
- })
1445
- );
1446
- };
1447
-
1448
- const onFocusOut = (e: FocusEvent) => {
1449
- if (!isDot(e.target)) return;
1450
- const dot = e.target;
1451
- dot.style.opacity = '';
1452
- dot.dispatchEvent(new MouseEvent('mouseout', { bubbles: true }));
1453
- };
1454
-
1455
- const onKeyDown = (e: KeyboardEvent) => {
1456
- if (!isDot(e.target)) return;
1457
- if (e.key !== 'Enter' && e.key !== ' ') return;
1458
- e.preventDefault();
1459
- const dot = e.target;
1460
- const { left, top, width, height } = dot.getBoundingClientRect();
1461
- const cx = left + width / 2;
1462
- const cy = top + height / 2;
1463
- dot.dispatchEvent(
1464
- new MouseEvent('click', {
1465
- bubbles: true,
1466
- clientX: cx,
1467
- clientY: cy,
1468
- })
1469
- );
1470
- };
1471
-
1472
- const rafId = requestAnimationFrame(() => {
1473
- wrapper
1474
- .querySelectorAll<SVGCircleElement>('circle.dot')
1475
- .forEach(dot => {
1476
- if (!dot.hasAttribute('tabindex')) {
1477
- dot.setAttribute('tabindex', '0');
1478
- }
1479
- });
1480
- });
1481
-
1482
- wrapper.addEventListener('focusin', onFocusIn);
1483
- wrapper.addEventListener('focusout', onFocusOut);
1484
- wrapper.addEventListener('keydown', onKeyDown);
1485
-
1486
- return () => {
1487
- cancelAnimationFrame(rafId);
1488
- wrapper.removeEventListener('focusin', onFocusIn);
1489
- wrapper.removeEventListener('focusout', onFocusOut);
1490
- wrapper.removeEventListener('keydown', onKeyDown);
1491
- };
1492
- }, [type]);
1493
-
1494
1415
  React.useEffect(() => {
1495
1416
  const timer = setTimeout(() => {
1496
1417
  if (internalRef.current) {
1497
1418
  internalRef.current
1498
- .querySelectorAll<SVGGElement>('g[aria-label]')
1419
+ .querySelectorAll<SVGGElement>('g.ruler[aria-label]')
1420
+ .forEach(g => {
1421
+ g.setAttribute('aria-hidden', 'true');
1422
+ });
1423
+
1424
+ internalRef.current
1425
+ .querySelectorAll<SVGGElement>('g[aria-label]:not(.ruler)')
1499
1426
  .forEach(g => {
1500
1427
  const role = g.getAttribute('role');
1501
1428
 
@@ -1511,6 +1438,30 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1511
1438
  return () => clearTimeout(timer);
1512
1439
  }, [type, dataSet]);
1513
1440
 
1441
+ React.useEffect(() => {
1442
+ const container = internalRef.current;
1443
+
1444
+ if (!container) return;
1445
+
1446
+ const hideRulerFromAT = () => {
1447
+ container
1448
+ .querySelectorAll<SVGGElement>('g.ruler[aria-label]')
1449
+ .forEach(g => {
1450
+ if (!g.hasAttribute('aria-hidden')) {
1451
+ g.setAttribute('aria-hidden', 'true');
1452
+ }
1453
+ });
1454
+ };
1455
+
1456
+ hideRulerFromAT();
1457
+
1458
+ const observer = new MutationObserver(hideRulerFromAT);
1459
+
1460
+ observer.observe(container, { childList: true, subtree: true });
1461
+
1462
+ return () => observer.disconnect();
1463
+ }, [type, dataSet]);
1464
+
1514
1465
  React.useEffect(() => {
1515
1466
  const wrapper = internalRef.current;
1516
1467
 
@@ -1572,6 +1523,113 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1572
1523
  return () => observer.disconnect();
1573
1524
  }, [type, dataSet]);
1574
1525
 
1526
+ // Make Carbon Charts data points keyboard-focusable.
1527
+ React.useEffect(() => {
1528
+ const wrapper = internalRef.current;
1529
+
1530
+ if (!wrapper) return;
1531
+
1532
+ const stampDotTabIndex = () => {
1533
+ wrapper
1534
+ .querySelectorAll<SVGCircleElement>('circle.dot')
1535
+ .forEach(dot => {
1536
+ if (!dot.hasAttribute('tabindex')) {
1537
+ dot.setAttribute('tabindex', '0');
1538
+ }
1539
+ });
1540
+ };
1541
+
1542
+ const isDot = (el: EventTarget | null): el is SVGCircleElement =>
1543
+ el instanceof Element &&
1544
+ el.nodeName.toLowerCase() === 'circle' &&
1545
+ el.classList.contains('dot');
1546
+
1547
+ const onFocusIn = (e: FocusEvent) => {
1548
+ if (!isDot(e.target)) return;
1549
+ const dot = e.target as SVGCircleElement;
1550
+
1551
+ const { left, top, width, height } = dot.getBoundingClientRect();
1552
+ const cx = left + width / 2;
1553
+ const cy = top + height / 2;
1554
+
1555
+ dot.dispatchEvent(
1556
+ new MouseEvent('mouseover', {
1557
+ bubbles: true,
1558
+ clientX: cx,
1559
+ clientY: cy,
1560
+ screenX: cx,
1561
+ screenY: cy,
1562
+ })
1563
+ );
1564
+ dot.dispatchEvent(
1565
+ new MouseEvent('mousemove', {
1566
+ bubbles: true,
1567
+ clientX: cx,
1568
+ clientY: cy,
1569
+ screenX: cx,
1570
+ screenY: cy,
1571
+ })
1572
+ );
1573
+ };
1574
+
1575
+ const onFocusOut = (e: FocusEvent) => {
1576
+ if (!isDot(e.target)) return;
1577
+ const dot = e.target;
1578
+
1579
+ // Keep tooltip context while Tab focus moves between chart points.
1580
+ if (isDot(e.relatedTarget)) {
1581
+ return;
1582
+ }
1583
+
1584
+ dot.dispatchEvent(new MouseEvent('mouseout', { bubbles: true }));
1585
+ };
1586
+
1587
+ const onKeyDown = (e: KeyboardEvent) => {
1588
+ if (!isDot(e.target)) return;
1589
+ if (e.key !== 'Enter' && e.key !== ' ') return;
1590
+ e.preventDefault();
1591
+ const dot = e.target;
1592
+ const { left, top, width, height } = dot.getBoundingClientRect();
1593
+ const cx = left + width / 2;
1594
+ const cy = top + height / 2;
1595
+
1596
+ dot.dispatchEvent(
1597
+ new MouseEvent('click', {
1598
+ bubbles: true,
1599
+ clientX: cx,
1600
+ clientY: cy,
1601
+ })
1602
+ );
1603
+ };
1604
+
1605
+ const rafId = requestAnimationFrame(() => {
1606
+ stampDotTabIndex();
1607
+ });
1608
+
1609
+ const dotObserver = new MutationObserver(() => {
1610
+ stampDotTabIndex();
1611
+ });
1612
+
1613
+ dotObserver.observe(wrapper, {
1614
+ childList: true,
1615
+ subtree: true,
1616
+ attributes: true,
1617
+ attributeFilter: ['class', 'style', 'tabindex'],
1618
+ });
1619
+
1620
+ wrapper.addEventListener('focusin', onFocusIn);
1621
+ wrapper.addEventListener('focusout', onFocusOut);
1622
+ wrapper.addEventListener('keydown', onKeyDown);
1623
+
1624
+ return () => {
1625
+ cancelAnimationFrame(rafId);
1626
+ dotObserver.disconnect();
1627
+ wrapper.removeEventListener('focusin', onFocusIn);
1628
+ wrapper.removeEventListener('focusout', onFocusOut);
1629
+ wrapper.removeEventListener('keydown', onKeyDown);
1630
+ };
1631
+ }, [type, dataSet]);
1632
+
1575
1633
  const groupsLength = Object.keys(colorScale).length;
1576
1634
 
1577
1635
  const showTable = chartToolbar?.showAsTable !== false;