@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.
@@ -1,7 +1,7 @@
1
1
  import * as React from 'react';
2
2
  import { ComboChart, ScatterChart, MeterChart, HistogramChart, GaugeChart, BulletChart, BubbleChart, BoxplotChart, RadarChart, PieChart, LollipopChart, LineChart, DonutChart, StackedBarChart, GroupedBarChart, SimpleBarChart, StackedAreaChart, AreaChart } from '@carbon/charts-react';
3
3
  import styled from '@emotion/styled';
4
- import { Table, TableHead, TableRow, TableHeaderCell, TableBody, TableCell, I18nContext, Tooltip, IconButton, ButtonVariant, Dropdown, DropdownButton, DropdownContent, ModalSize, ThemeContext, Modal, Button, TypographyVisualStyle, Heading, useIsInverse, VisuallyHidden, Announce, DropdownMenuItem, DropdownDivider } from 'react-magma-dom';
4
+ import { Table, TableHead, TableRow, TableHeaderCell, TableBody, TableCell, I18nContext, Tooltip, IconButton, ButtonVariant, Dropdown, DropdownButton, DropdownContent, ModalSize, ThemeContext, Modal, Button, TypographyVisualStyle, Heading, useIsInverse, useDeviceDetect, VisuallyHidden, Announce, DropdownMenuItem, DropdownDivider } from 'react-magma-dom';
5
5
  import { TableIcon, FullscreenExitIcon, FullscreenIcon, MoreVertIcon } from 'react-magma-icons';
6
6
 
7
7
  /******************************************************************************
@@ -1446,6 +1446,7 @@ function downloadImage(wrapperRef, title, format) {
1446
1446
  img.src = url;
1447
1447
  }
1448
1448
  function CarbonChartToolbar(_a) {
1449
+ var _b;
1449
1450
  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;
1450
1451
  var t = useChartToolbarI18n();
1451
1452
  var showTable = config.showAsTable !== false;
@@ -1468,7 +1469,7 @@ function CarbonChartToolbar(_a) {
1468
1469
  React.createElement(DropdownDivider, null),
1469
1470
  config.moreOptions))));
1470
1471
  return (React.createElement(ToolbarWrapper, { isFullscreen: isFullscreen, isInverse: isInverse, theme: theme },
1471
- React.createElement(ChartTitle, { isInverse: isInverse, theme: theme }, resolvedTitle),
1472
+ React.createElement(ChartTitle, { as: "h".concat((_b = config.titleLevel) !== null && _b !== void 0 ? _b : 2), isInverse: isInverse, theme: theme }, resolvedTitle),
1472
1473
  React.createElement(ToolbarActions, { theme: theme },
1473
1474
  showTable && (React.createElement(ChartTableButton, { ariaLabel: resolvedTitle, icon: React.createElement(TableIcon, { size: 20 }), isInverse: isInverse, isTableOpen: isTableOpen, onClick: onOpenTable })),
1474
1475
  showFullscreen && (React.createElement(ChartFullscreenButton, { ariaLabel: "".concat(isFullscreen ? 'Exit' : 'View', " ").concat(resolvedTitle, " full screen"), icon: React.createElement(FullscreenIcon, { size: 20 }), exitIcon: React.createElement(FullscreenExitIcon, { size: 20 }), isInverse: isInverse, isFullscreen: isFullscreen, onClick: onToggleFullscreen })),
@@ -1483,8 +1484,11 @@ var CarbonChart = React.forwardRef(function (props, ref) {
1483
1484
  var _a = React.useState(false), isTableOpen = _a[0], setIsTableOpen = _a[1];
1484
1485
  var _b = React.useState(false), isFullscreen = _b[0], setIsFullscreen = _b[1];
1485
1486
  var _c = React.useState(''), legendAnnouncement = _c[0], setLegendAnnouncement = _c[1];
1487
+ var legendGroupAnnounceRef = React.useRef(null);
1486
1488
  var lastTableTriggerRef = React.useRef(null);
1487
1489
  var legendAnnouncedRef = React.useRef(false);
1490
+ var selectionSignatureRef = React.useRef(undefined);
1491
+ var _d = useDeviceDetect(), isMacOS = _d.isMacOS, isChrome = _d.isChrome;
1488
1492
  var mergedRef = React.useCallback(function (node) {
1489
1493
  internalRef.current = node;
1490
1494
  if (typeof ref === 'function') {
@@ -1551,20 +1555,49 @@ var CarbonChart = React.forwardRef(function (props, ref) {
1551
1555
  if (!container)
1552
1556
  return;
1553
1557
  var applyListSemantics = function (legend) {
1554
- legend.removeAttribute('aria-label');
1555
- legend.setAttribute('role', 'list');
1556
- legend
1557
- .querySelectorAll('.legend-item')
1558
- .forEach(function (item) { return item.setAttribute('role', 'listitem'); });
1558
+ var itemsHost = legend.matches('[data-name="legend-items"]')
1559
+ ? legend
1560
+ : legend.querySelector('[data-name="legend-items"]') || legend;
1561
+ itemsHost.removeAttribute('aria-label');
1562
+ if (itemsHost.getAttribute('role') !== 'list') {
1563
+ itemsHost.setAttribute('role', 'list');
1564
+ }
1565
+ legend.querySelectorAll('.legend-item').forEach(function (item) {
1566
+ if (item.getAttribute('role') !== 'listitem') {
1567
+ item.setAttribute('role', 'listitem');
1568
+ }
1569
+ });
1570
+ container.querySelectorAll('[role="list"]').forEach(function (el) {
1571
+ if (el !== itemsHost && el.contains(itemsHost)) {
1572
+ el.removeAttribute('role');
1573
+ }
1574
+ });
1575
+ container.querySelectorAll('[aria-label="Data groups"]').forEach(function (el) {
1576
+ if (el !== itemsHost && el.contains(itemsHost)) {
1577
+ el.removeAttribute('aria-label');
1578
+ }
1579
+ });
1580
+ };
1581
+ var unwrapFieldset = function (fieldset) {
1582
+ var _a;
1583
+ (_a = fieldset.querySelector(':scope > legend')) === null || _a === void 0 ? void 0 : _a.remove();
1584
+ fieldset.replaceWith.apply(fieldset, Array.from(fieldset.childNodes));
1559
1585
  };
1560
1586
  var wrapLegend = function () {
1561
1587
  var legend = container.querySelector('.cds--cc--legend');
1562
1588
  if (!legend)
1563
1589
  return;
1564
1590
  applyListSemantics(legend);
1565
- var parent = legend.parentElement;
1566
- if ((parent === null || parent === void 0 ? void 0 : parent.tagName) === 'FIELDSET') {
1567
- var existingCaption = parent.querySelector('legend');
1591
+ var closestFieldset = legend.closest('fieldset.cds--cc--legend-fieldset');
1592
+ container
1593
+ .querySelectorAll('fieldset.cds--cc--legend-fieldset')
1594
+ .forEach(function (fieldset) {
1595
+ if (fieldset !== closestFieldset) {
1596
+ unwrapFieldset(fieldset);
1597
+ }
1598
+ });
1599
+ if (closestFieldset) {
1600
+ var existingCaption = closestFieldset.querySelector(':scope > legend');
1568
1601
  if (existingCaption && existingCaption.textContent !== legendLabel) {
1569
1602
  existingCaption.textContent = legendLabel;
1570
1603
  }
@@ -1579,9 +1612,58 @@ var CarbonChart = React.forwardRef(function (props, ref) {
1579
1612
  };
1580
1613
  wrapLegend();
1581
1614
  var observer = new MutationObserver(wrapLegend);
1582
- observer.observe(container, { childList: true, subtree: true });
1615
+ observer.observe(container, {
1616
+ childList: true,
1617
+ subtree: true,
1618
+ attributes: true,
1619
+ attributeFilter: ['role', 'aria-label'],
1620
+ });
1583
1621
  return function () { return observer.disconnect(); };
1584
1622
  }, [legendLabel]);
1623
+ React.useEffect(function () {
1624
+ var container = internalRef.current;
1625
+ if (!container || !isMacOS || !isChrome)
1626
+ return;
1627
+ var announceTimer;
1628
+ var alternateSuffix = false;
1629
+ var closestLegend = function (node) {
1630
+ return node instanceof Element ? node.closest('.cds--cc--legend') : null;
1631
+ };
1632
+ var handleFocusIn = function (event) {
1633
+ var enteredLegend = closestLegend(event.target);
1634
+ var cameFromLegend = closestLegend(event.relatedTarget);
1635
+ if (enteredLegend && !cameFromLegend) {
1636
+ if (legendGroupAnnounceRef.current) {
1637
+ legendGroupAnnounceRef.current.textContent = '';
1638
+ }
1639
+ clearTimeout(announceTimer);
1640
+ announceTimer = setTimeout(function () {
1641
+ if (legendGroupAnnounceRef.current) {
1642
+ alternateSuffix = !alternateSuffix;
1643
+ legendGroupAnnounceRef.current.textContent =
1644
+ legendLabel + (alternateSuffix ? ' ' : '');
1645
+ }
1646
+ }, 250);
1647
+ }
1648
+ };
1649
+ var handleFocusOut = function (event) {
1650
+ var leftLegend = closestLegend(event.target) && !closestLegend(event.relatedTarget);
1651
+ // Reset once focus leaves the legend so re-entering announces again.
1652
+ if (leftLegend) {
1653
+ clearTimeout(announceTimer);
1654
+ if (legendGroupAnnounceRef.current) {
1655
+ legendGroupAnnounceRef.current.textContent = '';
1656
+ }
1657
+ }
1658
+ };
1659
+ container.addEventListener('focusin', handleFocusIn);
1660
+ container.addEventListener('focusout', handleFocusOut);
1661
+ return function () {
1662
+ clearTimeout(announceTimer);
1663
+ container.removeEventListener('focusin', handleFocusIn);
1664
+ container.removeEventListener('focusout', handleFocusOut);
1665
+ };
1666
+ }, [isMacOS, isChrome, legendLabel]);
1585
1667
  var handleModalDownloadCsv = React.useCallback(function () {
1586
1668
  downloadCsv(dataSet, chartTitle);
1587
1669
  }, [dataSet, chartTitle]);
@@ -1666,6 +1748,17 @@ var CarbonChart = React.forwardRef(function (props, ref) {
1666
1748
  activeLabels.push(((_a = label.textContent) === null || _a === void 0 ? void 0 : _a.trim()) || '');
1667
1749
  }
1668
1750
  });
1751
+ // Carbon re-applies aria-checked (with the same values) on every
1752
+ // render, which still triggers this observer. Only announce when the
1753
+ // selection actually changed, otherwise unrelated re-renders (e.g.
1754
+ // the legend group announcement) cause phantom announcements.
1755
+ var selectionSignature = "".concat(activeLabels.join('|'), "::").concat(allItems.length);
1756
+ if (selectionSignatureRef.current === selectionSignature)
1757
+ return;
1758
+ var isFirstRun = selectionSignatureRef.current === undefined;
1759
+ selectionSignatureRef.current = selectionSignature;
1760
+ if (isFirstRun)
1761
+ return;
1669
1762
  if (activeLabels.length === 1) {
1670
1763
  if (!legendAnnouncedRef.current) {
1671
1764
  legendAnnouncedRef.current = true;
@@ -1682,6 +1775,7 @@ var CarbonChart = React.forwardRef(function (props, ref) {
1682
1775
  }
1683
1776
  });
1684
1777
  observer.observe(wrapper, {
1778
+ childList: true,
1685
1779
  subtree: true,
1686
1780
  attributes: true,
1687
1781
  attributeFilter: ['aria-checked'],
@@ -1693,6 +1787,8 @@ var CarbonChart = React.forwardRef(function (props, ref) {
1693
1787
  return (React.createElement(FullscreenRoot, { ref: mergedRef, isInverse: isInverse, theme: theme },
1694
1788
  React.createElement(VisuallyHidden, null,
1695
1789
  React.createElement(Announce, null, legendAnnouncement)),
1790
+ React.createElement(VisuallyHidden, null,
1791
+ React.createElement("div", { "aria-live": "polite", ref: legendGroupAnnounceRef })),
1696
1792
  React.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),
1697
1793
  React.createElement(ChartContentWrapper, null,
1698
1794
  chartToolbar && (React.createElement(CarbonChartToolbar, { config: chartToolbar, dataSet: dataSet, isInverse: isInverse, isTableOpen: isTableOpen, isFullscreen: isFullscreen, onOpenTable: openTableModal, onToggleFullscreen: toggleFullscreen, theme: theme, title: chartTitle, wrapperRef: internalRef })),