@react-magma/charts 15.0.0-rc.4 → 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
@@ -1505,8 +1505,11 @@ var CarbonChart = React__namespace.forwardRef(function (props, ref) {
1505
1505
  var _a = React__namespace.useState(false), isTableOpen = _a[0], setIsTableOpen = _a[1];
1506
1506
  var _b = React__namespace.useState(false), isFullscreen = _b[0], setIsFullscreen = _b[1];
1507
1507
  var _c = React__namespace.useState(''), legendAnnouncement = _c[0], setLegendAnnouncement = _c[1];
1508
+ var legendGroupAnnounceRef = React__namespace.useRef(null);
1508
1509
  var lastTableTriggerRef = React__namespace.useRef(null);
1509
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;
1510
1513
  var mergedRef = React__namespace.useCallback(function (node) {
1511
1514
  internalRef.current = node;
1512
1515
  if (typeof ref === 'function') {
@@ -1573,20 +1576,49 @@ var CarbonChart = React__namespace.forwardRef(function (props, ref) {
1573
1576
  if (!container)
1574
1577
  return;
1575
1578
  var applyListSemantics = function (legend) {
1576
- legend.removeAttribute('aria-label');
1577
- legend.setAttribute('role', 'list');
1578
- legend
1579
- .querySelectorAll('.legend-item')
1580
- .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));
1581
1606
  };
1582
1607
  var wrapLegend = function () {
1583
1608
  var legend = container.querySelector('.cds--cc--legend');
1584
1609
  if (!legend)
1585
1610
  return;
1586
1611
  applyListSemantics(legend);
1587
- var parent = legend.parentElement;
1588
- if ((parent === null || parent === void 0 ? void 0 : parent.tagName) === 'FIELDSET') {
1589
- 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');
1590
1622
  if (existingCaption && existingCaption.textContent !== legendLabel) {
1591
1623
  existingCaption.textContent = legendLabel;
1592
1624
  }
@@ -1601,9 +1633,58 @@ var CarbonChart = React__namespace.forwardRef(function (props, ref) {
1601
1633
  };
1602
1634
  wrapLegend();
1603
1635
  var observer = new MutationObserver(wrapLegend);
1604
- 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
+ });
1605
1642
  return function () { return observer.disconnect(); };
1606
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]);
1607
1688
  var handleModalDownloadCsv = React__namespace.useCallback(function () {
1608
1689
  downloadCsv(dataSet, chartTitle);
1609
1690
  }, [dataSet, chartTitle]);
@@ -1688,6 +1769,17 @@ var CarbonChart = React__namespace.forwardRef(function (props, ref) {
1688
1769
  activeLabels.push(((_a = label.textContent) === null || _a === void 0 ? void 0 : _a.trim()) || '');
1689
1770
  }
1690
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;
1691
1783
  if (activeLabels.length === 1) {
1692
1784
  if (!legendAnnouncedRef.current) {
1693
1785
  legendAnnouncedRef.current = true;
@@ -1704,6 +1796,7 @@ var CarbonChart = React__namespace.forwardRef(function (props, ref) {
1704
1796
  }
1705
1797
  });
1706
1798
  observer.observe(wrapper, {
1799
+ childList: true,
1707
1800
  subtree: true,
1708
1801
  attributes: true,
1709
1802
  attributeFilter: ['aria-checked'],
@@ -1715,6 +1808,8 @@ var CarbonChart = React__namespace.forwardRef(function (props, ref) {
1715
1808
  return (React__namespace.createElement(FullscreenRoot, { ref: mergedRef, isInverse: isInverse, theme: theme },
1716
1809
  React__namespace.createElement(reactMagmaDom.VisuallyHidden, null,
1717
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 })),
1718
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),
1719
1814
  React__namespace.createElement(ChartContentWrapper, null,
1720
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 })),