@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.
package/dist/charts.js CHANGED
@@ -2924,8 +2924,11 @@ var CarbonChart = React__namespace.forwardRef(function (props, ref) {
2924
2924
  var _a = React__namespace.useState(false), isTableOpen = _a[0], setIsTableOpen = _a[1];
2925
2925
  var _b = React__namespace.useState(false), isFullscreen = _b[0], setIsFullscreen = _b[1];
2926
2926
  var _c = React__namespace.useState(''), legendAnnouncement = _c[0], setLegendAnnouncement = _c[1];
2927
+ var legendGroupAnnounceRef = React__namespace.useRef(null);
2927
2928
  var lastTableTriggerRef = React__namespace.useRef(null);
2928
2929
  var legendAnnouncedRef = React__namespace.useRef(false);
2930
+ var selectionSignatureRef = React__namespace.useRef(undefined);
2931
+ var _d = reactMagmaDom.useDeviceDetect(), isMacOS = _d.isMacOS, isChrome = _d.isChrome;
2929
2932
  var mergedRef = React__namespace.useCallback(function (node) {
2930
2933
  internalRef.current = node;
2931
2934
  if (typeof ref === 'function') {
@@ -2998,20 +3001,49 @@ var CarbonChart = React__namespace.forwardRef(function (props, ref) {
2998
3001
  if (!container)
2999
3002
  return;
3000
3003
  var applyListSemantics = function (legend) {
3001
- legend.removeAttribute('aria-label');
3002
- legend.setAttribute('role', 'list');
3003
- legend
3004
- .querySelectorAll('.legend-item')
3005
- .forEach(function (item) { return item.setAttribute('role', 'listitem'); });
3004
+ var itemsHost = legend.matches('[data-name="legend-items"]')
3005
+ ? legend
3006
+ : legend.querySelector('[data-name="legend-items"]') || legend;
3007
+ itemsHost.removeAttribute('aria-label');
3008
+ if (itemsHost.getAttribute('role') !== 'list') {
3009
+ itemsHost.setAttribute('role', 'list');
3010
+ }
3011
+ legend.querySelectorAll('.legend-item').forEach(function (item) {
3012
+ if (item.getAttribute('role') !== 'listitem') {
3013
+ item.setAttribute('role', 'listitem');
3014
+ }
3015
+ });
3016
+ container.querySelectorAll('[role="list"]').forEach(function (el) {
3017
+ if (el !== itemsHost && el.contains(itemsHost)) {
3018
+ el.removeAttribute('role');
3019
+ }
3020
+ });
3021
+ container.querySelectorAll('[aria-label="Data groups"]').forEach(function (el) {
3022
+ if (el !== itemsHost && el.contains(itemsHost)) {
3023
+ el.removeAttribute('aria-label');
3024
+ }
3025
+ });
3026
+ };
3027
+ var unwrapFieldset = function (fieldset) {
3028
+ var _a;
3029
+ (_a = fieldset.querySelector(':scope > legend')) === null || _a === void 0 ? void 0 : _a.remove();
3030
+ fieldset.replaceWith.apply(fieldset, Array.from(fieldset.childNodes));
3006
3031
  };
3007
3032
  var wrapLegend = function () {
3008
3033
  var legend = container.querySelector('.cds--cc--legend');
3009
3034
  if (!legend)
3010
3035
  return;
3011
3036
  applyListSemantics(legend);
3012
- var parent = legend.parentElement;
3013
- if ((parent === null || parent === void 0 ? void 0 : parent.tagName) === 'FIELDSET') {
3014
- var existingCaption = parent.querySelector('legend');
3037
+ var closestFieldset = legend.closest('fieldset.cds--cc--legend-fieldset');
3038
+ container
3039
+ .querySelectorAll('fieldset.cds--cc--legend-fieldset')
3040
+ .forEach(function (fieldset) {
3041
+ if (fieldset !== closestFieldset) {
3042
+ unwrapFieldset(fieldset);
3043
+ }
3044
+ });
3045
+ if (closestFieldset) {
3046
+ var existingCaption = closestFieldset.querySelector(':scope > legend');
3015
3047
  if (existingCaption && existingCaption.textContent !== legendLabel) {
3016
3048
  existingCaption.textContent = legendLabel;
3017
3049
  }
@@ -3026,9 +3058,58 @@ var CarbonChart = React__namespace.forwardRef(function (props, ref) {
3026
3058
  };
3027
3059
  wrapLegend();
3028
3060
  var observer = new MutationObserver(wrapLegend);
3029
- observer.observe(container, { childList: true, subtree: true });
3061
+ observer.observe(container, {
3062
+ childList: true,
3063
+ subtree: true,
3064
+ attributes: true,
3065
+ attributeFilter: ['role', 'aria-label'],
3066
+ });
3030
3067
  return function () { return observer.disconnect(); };
3031
3068
  }, [legendLabel]);
3069
+ React__namespace.useEffect(function () {
3070
+ var container = internalRef.current;
3071
+ if (!container || !isMacOS || !isChrome)
3072
+ return;
3073
+ var announceTimer;
3074
+ var alternateSuffix = false;
3075
+ var closestLegend = function (node) {
3076
+ return node instanceof Element ? node.closest('.cds--cc--legend') : null;
3077
+ };
3078
+ var handleFocusIn = function (event) {
3079
+ var enteredLegend = closestLegend(event.target);
3080
+ var cameFromLegend = closestLegend(event.relatedTarget);
3081
+ if (enteredLegend && !cameFromLegend) {
3082
+ if (legendGroupAnnounceRef.current) {
3083
+ legendGroupAnnounceRef.current.textContent = '';
3084
+ }
3085
+ clearTimeout(announceTimer);
3086
+ announceTimer = setTimeout(function () {
3087
+ if (legendGroupAnnounceRef.current) {
3088
+ alternateSuffix = !alternateSuffix;
3089
+ legendGroupAnnounceRef.current.textContent =
3090
+ legendLabel + (alternateSuffix ? ' ' : '');
3091
+ }
3092
+ }, 250);
3093
+ }
3094
+ };
3095
+ var handleFocusOut = function (event) {
3096
+ var leftLegend = closestLegend(event.target) && !closestLegend(event.relatedTarget);
3097
+ // Reset once focus leaves the legend so re-entering announces again.
3098
+ if (leftLegend) {
3099
+ clearTimeout(announceTimer);
3100
+ if (legendGroupAnnounceRef.current) {
3101
+ legendGroupAnnounceRef.current.textContent = '';
3102
+ }
3103
+ }
3104
+ };
3105
+ container.addEventListener('focusin', handleFocusIn);
3106
+ container.addEventListener('focusout', handleFocusOut);
3107
+ return function () {
3108
+ clearTimeout(announceTimer);
3109
+ container.removeEventListener('focusin', handleFocusIn);
3110
+ container.removeEventListener('focusout', handleFocusOut);
3111
+ };
3112
+ }, [isMacOS, isChrome, legendLabel]);
3032
3113
  var handleModalDownloadCsv = React__namespace.useCallback(function () {
3033
3114
  downloadCsv(dataSet, chartTitle);
3034
3115
  }, [dataSet, chartTitle]);
@@ -3186,6 +3267,17 @@ var CarbonChart = React__namespace.forwardRef(function (props, ref) {
3186
3267
  activeLabels.push(((_a = label.textContent) === null || _a === void 0 ? void 0 : _a.trim()) || '');
3187
3268
  }
3188
3269
  });
3270
+ // Carbon re-applies aria-checked (with the same values) on every
3271
+ // render, which still triggers this observer. Only announce when the
3272
+ // selection actually changed, otherwise unrelated re-renders (e.g.
3273
+ // the legend group announcement) cause phantom announcements.
3274
+ var selectionSignature = activeLabels.join('|') + "::" + allItems.length;
3275
+ if (selectionSignatureRef.current === selectionSignature)
3276
+ return;
3277
+ var isFirstRun = selectionSignatureRef.current === undefined;
3278
+ selectionSignatureRef.current = selectionSignature;
3279
+ if (isFirstRun)
3280
+ return;
3189
3281
  if (activeLabels.length === 1) {
3190
3282
  if (!legendAnnouncedRef.current) {
3191
3283
  legendAnnouncedRef.current = true;
@@ -3202,6 +3294,7 @@ var CarbonChart = React__namespace.forwardRef(function (props, ref) {
3202
3294
  }
3203
3295
  });
3204
3296
  observer.observe(wrapper, {
3297
+ childList: true,
3205
3298
  subtree: true,
3206
3299
  attributes: true,
3207
3300
  attributeFilter: ['aria-checked'],
@@ -3215,6 +3308,8 @@ var CarbonChart = React__namespace.forwardRef(function (props, ref) {
3215
3308
  return (React__namespace.createElement(FullscreenRoot, { ref: mergedRef, isInverse: isInverse, theme: theme },
3216
3309
  React__namespace.createElement(reactMagmaDom.VisuallyHidden, null,
3217
3310
  React__namespace.createElement(reactMagmaDom.Announce, null, legendAnnouncement)),
3311
+ React__namespace.createElement(reactMagmaDom.VisuallyHidden, null,
3312
+ React__namespace.createElement("div", { "aria-live": "polite", ref: legendGroupAnnounceRef })),
3218
3313
  React__namespace.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),
3219
3314
  React__namespace.createElement(ChartContentWrapper, null,
3220
3315
  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 })),