@react-magma/charts 14.0.0-next.4 → 14.0.0-next.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,6 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import { css } from '@emotion/react';
3
- import { Card, Datagrid, Spinner, useForceUpdate, StyledTooltip, ThemeContext, TooltipPosition, TooltipArrow, Checkbox, I18nContext, styled as styled$1, Paragraph, TabsContainer, TabPanel, useDescendants, TypographyVisualStyle, Tabs, Tab, Tooltip, IconButton, ButtonVariant, Announce, TabPanelsContainer, Table, TableHead, TableRow, TableHeaderCell, TableBody, TableCell, Dropdown, DropdownButton, DropdownContent, ModalSize, Modal, Button, Heading, useIsInverse, VisuallyHidden, DropdownMenuItem, DropdownDivider } from 'react-magma-dom';
3
+ import { Card, Datagrid, Spinner, useForceUpdate, StyledTooltip, ThemeContext, TooltipPosition, TooltipArrow, Checkbox, I18nContext, styled as styled$1, Paragraph, TabsContainer, TabPanel, useDescendants, TypographyVisualStyle, Tabs, Tab, Tooltip, IconButton, ButtonVariant, Announce, TabPanelsContainer, Table, TableHead, TableRow, TableHeaderCell, TableBody, TableCell, Dropdown, DropdownButton, DropdownContent, ModalSize, Modal, Button, Heading, useIsInverse, useDeviceDetect, VisuallyHidden, DropdownMenuItem, DropdownDivider } from 'react-magma-dom';
4
4
  import { KeyboardIcon, TableIcon, FullscreenExitIcon, FullscreenIcon, MoreVertIcon } from 'react-magma-icons';
5
5
  import styled from '@emotion/styled';
6
6
  import { LineSegment, Point, VictoryChart, VictoryVoronoiContainer, VictoryTooltip, VictoryAxis, VictoryLine, VictoryScatter } from 'victory';
@@ -2903,8 +2903,11 @@ var CarbonChart = React.forwardRef(function (props, ref) {
2903
2903
  var _a = React.useState(false), isTableOpen = _a[0], setIsTableOpen = _a[1];
2904
2904
  var _b = React.useState(false), isFullscreen = _b[0], setIsFullscreen = _b[1];
2905
2905
  var _c = React.useState(''), legendAnnouncement = _c[0], setLegendAnnouncement = _c[1];
2906
+ var legendGroupAnnounceRef = React.useRef(null);
2906
2907
  var lastTableTriggerRef = React.useRef(null);
2907
2908
  var legendAnnouncedRef = React.useRef(false);
2909
+ var selectionSignatureRef = React.useRef(undefined);
2910
+ var _d = useDeviceDetect(), isMacOS = _d.isMacOS, isChrome = _d.isChrome;
2908
2911
  var mergedRef = React.useCallback(function (node) {
2909
2912
  internalRef.current = node;
2910
2913
  if (typeof ref === 'function') {
@@ -2977,20 +2980,49 @@ var CarbonChart = React.forwardRef(function (props, ref) {
2977
2980
  if (!container)
2978
2981
  return;
2979
2982
  var applyListSemantics = function (legend) {
2980
- legend.removeAttribute('aria-label');
2981
- legend.setAttribute('role', 'list');
2982
- legend
2983
- .querySelectorAll('.legend-item')
2984
- .forEach(function (item) { return item.setAttribute('role', 'listitem'); });
2983
+ var itemsHost = legend.matches('[data-name="legend-items"]')
2984
+ ? legend
2985
+ : legend.querySelector('[data-name="legend-items"]') || legend;
2986
+ itemsHost.removeAttribute('aria-label');
2987
+ if (itemsHost.getAttribute('role') !== 'list') {
2988
+ itemsHost.setAttribute('role', 'list');
2989
+ }
2990
+ legend.querySelectorAll('.legend-item').forEach(function (item) {
2991
+ if (item.getAttribute('role') !== 'listitem') {
2992
+ item.setAttribute('role', 'listitem');
2993
+ }
2994
+ });
2995
+ container.querySelectorAll('[role="list"]').forEach(function (el) {
2996
+ if (el !== itemsHost && el.contains(itemsHost)) {
2997
+ el.removeAttribute('role');
2998
+ }
2999
+ });
3000
+ container.querySelectorAll('[aria-label="Data groups"]').forEach(function (el) {
3001
+ if (el !== itemsHost && el.contains(itemsHost)) {
3002
+ el.removeAttribute('aria-label');
3003
+ }
3004
+ });
3005
+ };
3006
+ var unwrapFieldset = function (fieldset) {
3007
+ var _a;
3008
+ (_a = fieldset.querySelector(':scope > legend')) === null || _a === void 0 ? void 0 : _a.remove();
3009
+ fieldset.replaceWith.apply(fieldset, Array.from(fieldset.childNodes));
2985
3010
  };
2986
3011
  var wrapLegend = function () {
2987
3012
  var legend = container.querySelector('.cds--cc--legend');
2988
3013
  if (!legend)
2989
3014
  return;
2990
3015
  applyListSemantics(legend);
2991
- var parent = legend.parentElement;
2992
- if ((parent === null || parent === void 0 ? void 0 : parent.tagName) === 'FIELDSET') {
2993
- var existingCaption = parent.querySelector('legend');
3016
+ var closestFieldset = legend.closest('fieldset.cds--cc--legend-fieldset');
3017
+ container
3018
+ .querySelectorAll('fieldset.cds--cc--legend-fieldset')
3019
+ .forEach(function (fieldset) {
3020
+ if (fieldset !== closestFieldset) {
3021
+ unwrapFieldset(fieldset);
3022
+ }
3023
+ });
3024
+ if (closestFieldset) {
3025
+ var existingCaption = closestFieldset.querySelector(':scope > legend');
2994
3026
  if (existingCaption && existingCaption.textContent !== legendLabel) {
2995
3027
  existingCaption.textContent = legendLabel;
2996
3028
  }
@@ -3005,9 +3037,58 @@ var CarbonChart = React.forwardRef(function (props, ref) {
3005
3037
  };
3006
3038
  wrapLegend();
3007
3039
  var observer = new MutationObserver(wrapLegend);
3008
- observer.observe(container, { childList: true, subtree: true });
3040
+ observer.observe(container, {
3041
+ childList: true,
3042
+ subtree: true,
3043
+ attributes: true,
3044
+ attributeFilter: ['role', 'aria-label'],
3045
+ });
3009
3046
  return function () { return observer.disconnect(); };
3010
3047
  }, [legendLabel]);
3048
+ React.useEffect(function () {
3049
+ var container = internalRef.current;
3050
+ if (!container || !isMacOS || !isChrome)
3051
+ return;
3052
+ var announceTimer;
3053
+ var alternateSuffix = false;
3054
+ var closestLegend = function (node) {
3055
+ return node instanceof Element ? node.closest('.cds--cc--legend') : null;
3056
+ };
3057
+ var handleFocusIn = function (event) {
3058
+ var enteredLegend = closestLegend(event.target);
3059
+ var cameFromLegend = closestLegend(event.relatedTarget);
3060
+ if (enteredLegend && !cameFromLegend) {
3061
+ if (legendGroupAnnounceRef.current) {
3062
+ legendGroupAnnounceRef.current.textContent = '';
3063
+ }
3064
+ clearTimeout(announceTimer);
3065
+ announceTimer = setTimeout(function () {
3066
+ if (legendGroupAnnounceRef.current) {
3067
+ alternateSuffix = !alternateSuffix;
3068
+ legendGroupAnnounceRef.current.textContent =
3069
+ legendLabel + (alternateSuffix ? ' ' : '');
3070
+ }
3071
+ }, 250);
3072
+ }
3073
+ };
3074
+ var handleFocusOut = function (event) {
3075
+ var leftLegend = closestLegend(event.target) && !closestLegend(event.relatedTarget);
3076
+ // Reset once focus leaves the legend so re-entering announces again.
3077
+ if (leftLegend) {
3078
+ clearTimeout(announceTimer);
3079
+ if (legendGroupAnnounceRef.current) {
3080
+ legendGroupAnnounceRef.current.textContent = '';
3081
+ }
3082
+ }
3083
+ };
3084
+ container.addEventListener('focusin', handleFocusIn);
3085
+ container.addEventListener('focusout', handleFocusOut);
3086
+ return function () {
3087
+ clearTimeout(announceTimer);
3088
+ container.removeEventListener('focusin', handleFocusIn);
3089
+ container.removeEventListener('focusout', handleFocusOut);
3090
+ };
3091
+ }, [isMacOS, isChrome, legendLabel]);
3011
3092
  var handleModalDownloadCsv = React.useCallback(function () {
3012
3093
  downloadCsv(dataSet, chartTitle);
3013
3094
  }, [dataSet, chartTitle]);
@@ -3165,6 +3246,17 @@ var CarbonChart = React.forwardRef(function (props, ref) {
3165
3246
  activeLabels.push(((_a = label.textContent) === null || _a === void 0 ? void 0 : _a.trim()) || '');
3166
3247
  }
3167
3248
  });
3249
+ // Carbon re-applies aria-checked (with the same values) on every
3250
+ // render, which still triggers this observer. Only announce when the
3251
+ // selection actually changed, otherwise unrelated re-renders (e.g.
3252
+ // the legend group announcement) cause phantom announcements.
3253
+ var selectionSignature = activeLabels.join('|') + "::" + allItems.length;
3254
+ if (selectionSignatureRef.current === selectionSignature)
3255
+ return;
3256
+ var isFirstRun = selectionSignatureRef.current === undefined;
3257
+ selectionSignatureRef.current = selectionSignature;
3258
+ if (isFirstRun)
3259
+ return;
3168
3260
  if (activeLabels.length === 1) {
3169
3261
  if (!legendAnnouncedRef.current) {
3170
3262
  legendAnnouncedRef.current = true;
@@ -3181,6 +3273,7 @@ var CarbonChart = React.forwardRef(function (props, ref) {
3181
3273
  }
3182
3274
  });
3183
3275
  observer.observe(wrapper, {
3276
+ childList: true,
3184
3277
  subtree: true,
3185
3278
  attributes: true,
3186
3279
  attributeFilter: ['aria-checked'],
@@ -3194,6 +3287,8 @@ var CarbonChart = React.forwardRef(function (props, ref) {
3194
3287
  return (React.createElement(FullscreenRoot, { ref: mergedRef, isInverse: isInverse, theme: theme },
3195
3288
  React.createElement(VisuallyHidden, null,
3196
3289
  React.createElement(Announce, null, legendAnnouncement)),
3290
+ React.createElement(VisuallyHidden, null,
3291
+ React.createElement("div", { "aria-live": "polite", ref: legendGroupAnnounceRef })),
3197
3292
  React.createElement(CarbonChartWrapper, __assign({ "data-testid": testId, isInverse: isInverse, theme: theme, className: "carbon-chart-wrapper" + (chartToolbar ? ' has-magma-toolbar' : ''), groupsLength: groupsLength < 6 ? groupsLength : 14, role: "region", "aria-label": ariaLabel || chartTitle, "aria-roledescription": "chart" }, rest),
3198
3293
  React.createElement(ChartContentWrapper, null,
3199
3294
  chartToolbar && (React.createElement(CarbonChartToolbar, { config: chartToolbar, dataSet: dataSet, isInverse: isInverse, isTableOpen: isTableOpen, isFullscreen: isFullscreen, onOpenTable: openTableModal, onToggleFullscreen: toggleFullscreen, theme: theme, title: chartTitle, wrapperRef: internalRef })),