@react-magma/charts 15.0.0-rc.3 → 15.0.0-rc.5

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/dist/charts.js CHANGED
@@ -1467,6 +1467,7 @@ function downloadImage(wrapperRef, title, format) {
1467
1467
  img.src = url;
1468
1468
  }
1469
1469
  function CarbonChartToolbar(_a) {
1470
+ var _b;
1470
1471
  var config = _a.config, dataSet = _a.dataSet, isInverse = _a.isInverse, isTableOpen = _a.isTableOpen, isFullscreen = _a.isFullscreen, onOpenTable = _a.onOpenTable, onToggleFullscreen = _a.onToggleFullscreen, theme = _a.theme, title = _a.title, wrapperRef = _a.wrapperRef;
1471
1472
  var t = useChartToolbarI18n();
1472
1473
  var showTable = config.showAsTable !== false;
@@ -1489,7 +1490,7 @@ function CarbonChartToolbar(_a) {
1489
1490
  React__namespace.createElement(reactMagmaDom.DropdownDivider, null),
1490
1491
  config.moreOptions))));
1491
1492
  return (React__namespace.createElement(ToolbarWrapper, { isFullscreen: isFullscreen, isInverse: isInverse, theme: theme },
1492
- React__namespace.createElement(ChartTitle, { isInverse: isInverse, theme: theme }, resolvedTitle),
1493
+ React__namespace.createElement(ChartTitle, { as: "h".concat((_b = config.titleLevel) !== null && _b !== void 0 ? _b : 2), isInverse: isInverse, theme: theme }, resolvedTitle),
1493
1494
  React__namespace.createElement(ToolbarActions, { theme: theme },
1494
1495
  showTable && (React__namespace.createElement(ChartTableButton, { ariaLabel: resolvedTitle, icon: React__namespace.createElement(reactMagmaIcons.TableIcon, { size: 20 }), isInverse: isInverse, isTableOpen: isTableOpen, onClick: onOpenTable })),
1495
1496
  showFullscreen && (React__namespace.createElement(ChartFullscreenButton, { ariaLabel: "".concat(isFullscreen ? 'Exit' : 'View', " ").concat(resolvedTitle, " full screen"), icon: React__namespace.createElement(reactMagmaIcons.FullscreenIcon, { size: 20 }), exitIcon: React__namespace.createElement(reactMagmaIcons.FullscreenExitIcon, { size: 20 }), isInverse: isInverse, isFullscreen: isFullscreen, onClick: onToggleFullscreen })),
@@ -1504,8 +1505,11 @@ var CarbonChart = React__namespace.forwardRef(function (props, ref) {
1504
1505
  var _a = React__namespace.useState(false), isTableOpen = _a[0], setIsTableOpen = _a[1];
1505
1506
  var _b = React__namespace.useState(false), isFullscreen = _b[0], setIsFullscreen = _b[1];
1506
1507
  var _c = React__namespace.useState(''), legendAnnouncement = _c[0], setLegendAnnouncement = _c[1];
1508
+ var legendGroupAnnounceRef = React__namespace.useRef(null);
1507
1509
  var lastTableTriggerRef = React__namespace.useRef(null);
1508
1510
  var legendAnnouncedRef = React__namespace.useRef(false);
1511
+ var selectionSignatureRef = React__namespace.useRef(undefined);
1512
+ var _d = reactMagmaDom.useDeviceDetect(), isMacOS = _d.isMacOS, isChrome = _d.isChrome;
1509
1513
  var mergedRef = React__namespace.useCallback(function (node) {
1510
1514
  internalRef.current = node;
1511
1515
  if (typeof ref === 'function') {
@@ -1572,20 +1576,49 @@ var CarbonChart = React__namespace.forwardRef(function (props, ref) {
1572
1576
  if (!container)
1573
1577
  return;
1574
1578
  var applyListSemantics = function (legend) {
1575
- legend.removeAttribute('aria-label');
1576
- legend.setAttribute('role', 'list');
1577
- legend
1578
- .querySelectorAll('.legend-item')
1579
- .forEach(function (item) { return item.setAttribute('role', 'listitem'); });
1579
+ var itemsHost = legend.matches('[data-name="legend-items"]')
1580
+ ? legend
1581
+ : legend.querySelector('[data-name="legend-items"]') || legend;
1582
+ itemsHost.removeAttribute('aria-label');
1583
+ if (itemsHost.getAttribute('role') !== 'list') {
1584
+ itemsHost.setAttribute('role', 'list');
1585
+ }
1586
+ legend.querySelectorAll('.legend-item').forEach(function (item) {
1587
+ if (item.getAttribute('role') !== 'listitem') {
1588
+ item.setAttribute('role', 'listitem');
1589
+ }
1590
+ });
1591
+ container.querySelectorAll('[role="list"]').forEach(function (el) {
1592
+ if (el !== itemsHost && el.contains(itemsHost)) {
1593
+ el.removeAttribute('role');
1594
+ }
1595
+ });
1596
+ container.querySelectorAll('[aria-label="Data groups"]').forEach(function (el) {
1597
+ if (el !== itemsHost && el.contains(itemsHost)) {
1598
+ el.removeAttribute('aria-label');
1599
+ }
1600
+ });
1601
+ };
1602
+ var unwrapFieldset = function (fieldset) {
1603
+ var _a;
1604
+ (_a = fieldset.querySelector(':scope > legend')) === null || _a === void 0 ? void 0 : _a.remove();
1605
+ fieldset.replaceWith.apply(fieldset, Array.from(fieldset.childNodes));
1580
1606
  };
1581
1607
  var wrapLegend = function () {
1582
1608
  var legend = container.querySelector('.cds--cc--legend');
1583
1609
  if (!legend)
1584
1610
  return;
1585
1611
  applyListSemantics(legend);
1586
- var parent = legend.parentElement;
1587
- if ((parent === null || parent === void 0 ? void 0 : parent.tagName) === 'FIELDSET') {
1588
- var existingCaption = parent.querySelector('legend');
1612
+ var closestFieldset = legend.closest('fieldset.cds--cc--legend-fieldset');
1613
+ container
1614
+ .querySelectorAll('fieldset.cds--cc--legend-fieldset')
1615
+ .forEach(function (fieldset) {
1616
+ if (fieldset !== closestFieldset) {
1617
+ unwrapFieldset(fieldset);
1618
+ }
1619
+ });
1620
+ if (closestFieldset) {
1621
+ var existingCaption = closestFieldset.querySelector(':scope > legend');
1589
1622
  if (existingCaption && existingCaption.textContent !== legendLabel) {
1590
1623
  existingCaption.textContent = legendLabel;
1591
1624
  }
@@ -1600,9 +1633,58 @@ var CarbonChart = React__namespace.forwardRef(function (props, ref) {
1600
1633
  };
1601
1634
  wrapLegend();
1602
1635
  var observer = new MutationObserver(wrapLegend);
1603
- observer.observe(container, { childList: true, subtree: true });
1636
+ observer.observe(container, {
1637
+ childList: true,
1638
+ subtree: true,
1639
+ attributes: true,
1640
+ attributeFilter: ['role', 'aria-label'],
1641
+ });
1604
1642
  return function () { return observer.disconnect(); };
1605
1643
  }, [legendLabel]);
1644
+ React__namespace.useEffect(function () {
1645
+ var container = internalRef.current;
1646
+ if (!container || !isMacOS || !isChrome)
1647
+ return;
1648
+ var announceTimer;
1649
+ var alternateSuffix = false;
1650
+ var closestLegend = function (node) {
1651
+ return node instanceof Element ? node.closest('.cds--cc--legend') : null;
1652
+ };
1653
+ var handleFocusIn = function (event) {
1654
+ var enteredLegend = closestLegend(event.target);
1655
+ var cameFromLegend = closestLegend(event.relatedTarget);
1656
+ if (enteredLegend && !cameFromLegend) {
1657
+ if (legendGroupAnnounceRef.current) {
1658
+ legendGroupAnnounceRef.current.textContent = '';
1659
+ }
1660
+ clearTimeout(announceTimer);
1661
+ announceTimer = setTimeout(function () {
1662
+ if (legendGroupAnnounceRef.current) {
1663
+ alternateSuffix = !alternateSuffix;
1664
+ legendGroupAnnounceRef.current.textContent =
1665
+ legendLabel + (alternateSuffix ? ' ' : '');
1666
+ }
1667
+ }, 250);
1668
+ }
1669
+ };
1670
+ var handleFocusOut = function (event) {
1671
+ var leftLegend = closestLegend(event.target) && !closestLegend(event.relatedTarget);
1672
+ // Reset once focus leaves the legend so re-entering announces again.
1673
+ if (leftLegend) {
1674
+ clearTimeout(announceTimer);
1675
+ if (legendGroupAnnounceRef.current) {
1676
+ legendGroupAnnounceRef.current.textContent = '';
1677
+ }
1678
+ }
1679
+ };
1680
+ container.addEventListener('focusin', handleFocusIn);
1681
+ container.addEventListener('focusout', handleFocusOut);
1682
+ return function () {
1683
+ clearTimeout(announceTimer);
1684
+ container.removeEventListener('focusin', handleFocusIn);
1685
+ container.removeEventListener('focusout', handleFocusOut);
1686
+ };
1687
+ }, [isMacOS, isChrome, legendLabel]);
1606
1688
  var handleModalDownloadCsv = React__namespace.useCallback(function () {
1607
1689
  downloadCsv(dataSet, chartTitle);
1608
1690
  }, [dataSet, chartTitle]);
@@ -1687,6 +1769,17 @@ var CarbonChart = React__namespace.forwardRef(function (props, ref) {
1687
1769
  activeLabels.push(((_a = label.textContent) === null || _a === void 0 ? void 0 : _a.trim()) || '');
1688
1770
  }
1689
1771
  });
1772
+ // Carbon re-applies aria-checked (with the same values) on every
1773
+ // render, which still triggers this observer. Only announce when the
1774
+ // selection actually changed, otherwise unrelated re-renders (e.g.
1775
+ // the legend group announcement) cause phantom announcements.
1776
+ var selectionSignature = "".concat(activeLabels.join('|'), "::").concat(allItems.length);
1777
+ if (selectionSignatureRef.current === selectionSignature)
1778
+ return;
1779
+ var isFirstRun = selectionSignatureRef.current === undefined;
1780
+ selectionSignatureRef.current = selectionSignature;
1781
+ if (isFirstRun)
1782
+ return;
1690
1783
  if (activeLabels.length === 1) {
1691
1784
  if (!legendAnnouncedRef.current) {
1692
1785
  legendAnnouncedRef.current = true;
@@ -1703,6 +1796,7 @@ var CarbonChart = React__namespace.forwardRef(function (props, ref) {
1703
1796
  }
1704
1797
  });
1705
1798
  observer.observe(wrapper, {
1799
+ childList: true,
1706
1800
  subtree: true,
1707
1801
  attributes: true,
1708
1802
  attributeFilter: ['aria-checked'],
@@ -1714,6 +1808,8 @@ var CarbonChart = React__namespace.forwardRef(function (props, ref) {
1714
1808
  return (React__namespace.createElement(FullscreenRoot, { ref: mergedRef, isInverse: isInverse, theme: theme },
1715
1809
  React__namespace.createElement(reactMagmaDom.VisuallyHidden, null,
1716
1810
  React__namespace.createElement(reactMagmaDom.Announce, null, legendAnnouncement)),
1811
+ React__namespace.createElement(reactMagmaDom.VisuallyHidden, null,
1812
+ React__namespace.createElement("div", { "aria-live": "polite", ref: legendGroupAnnounceRef })),
1717
1813
  React__namespace.createElement(CarbonChartWrapper, __assign({ "data-testid": testId, isInverse: isInverse, theme: theme, className: "carbon-chart-wrapper".concat(chartToolbar ? ' has-magma-toolbar' : ''), groupsLength: groupsLength < 6 ? groupsLength : 14, role: "region", "aria-label": ariaLabel || chartTitle, "aria-roledescription": "chart" }, rest),
1718
1814
  React__namespace.createElement(ChartContentWrapper, null,
1719
1815
  chartToolbar && (React__namespace.createElement(CarbonChartToolbar, { config: chartToolbar, dataSet: dataSet, isInverse: isInverse, isTableOpen: isTableOpen, isFullscreen: isFullscreen, onOpenTable: openTableModal, onToggleFullscreen: toggleFullscreen, theme: theme, title: chartTitle, wrapperRef: internalRef })),