@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-magma/charts",
3
- "version": "15.0.0-rc.4",
3
+ "version": "15.0.0-rc.5",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1000,6 +1000,37 @@ describe('CarbonChart', () => {
1000
1000
  expect(after[0]).toBe(before[0]);
1001
1001
  });
1002
1002
 
1003
+ it('unwraps the stale fieldset when Carbon re-renders the legend deeper inside it', () => {
1004
+ const { getByTestId } = render(
1005
+ <CarbonChart
1006
+ testId={testId}
1007
+ dataSet={dataSet}
1008
+ options={chartOptions}
1009
+ type={CarbonChartType.bar}
1010
+ />
1011
+ );
1012
+ const wrapper = getByTestId(testId);
1013
+
1014
+ const staleFieldset = wrapper.querySelector('.cds--cc--legend-fieldset');
1015
+ const legend = staleFieldset.querySelector('.cds--cc--legend');
1016
+
1017
+ act(() => {
1018
+ const layoutChild = document.createElement('div');
1019
+ layoutChild.className = 'layout-child legend';
1020
+ staleFieldset.append(layoutChild);
1021
+ layoutChild.append(legend);
1022
+ mutationObserverCallback?.();
1023
+ });
1024
+
1025
+ const fieldsets = wrapper.querySelectorAll('.cds--cc--legend-fieldset');
1026
+ expect(fieldsets).toHaveLength(1);
1027
+ expect(
1028
+ fieldsets[0].querySelector('.cds--cc--legend-fieldset')
1029
+ ).toBeNull();
1030
+ expect(fieldsets[0].contains(legend)).toBe(true);
1031
+ expect(fieldsets[0].querySelectorAll(':scope > legend')).toHaveLength(1);
1032
+ });
1033
+
1003
1034
  it('replaces Carbon\'s generic "Data groups" aria-label with semantic list roles on the legend and its items', () => {
1004
1035
  const { getByTestId } = render(
1005
1036
  <CarbonChart
@@ -1013,16 +1044,53 @@ describe('CarbonChart', () => {
1013
1044
 
1014
1045
  const legend = wrapper.querySelector('.cds--cc--legend');
1015
1046
  expect(legend).not.toBeNull();
1016
- expect(legend.getAttribute('aria-label')).toBeNull();
1017
- expect(legend.getAttribute('role')).toBe('list');
1018
1047
 
1019
- const items = legend.querySelectorAll('.legend-item');
1048
+ const itemsHost = legend.matches('[data-name="legend-items"]')
1049
+ ? legend
1050
+ : legend.querySelector('[data-name="legend-items"]') || legend;
1051
+ expect(itemsHost.getAttribute('aria-label')).toBeNull();
1052
+ expect(itemsHost.getAttribute('role')).toBe('list');
1053
+
1054
+ if (itemsHost !== legend) {
1055
+ expect(legend.getAttribute('role')).toBeNull();
1056
+ expect(legend.getAttribute('aria-label')).toBeNull();
1057
+ }
1058
+
1059
+ const items = itemsHost.querySelectorAll('.legend-item');
1020
1060
  expect(items.length).toBeGreaterThan(0);
1021
1061
  items.forEach(item => {
1022
1062
  expect(item.getAttribute('role')).toBe('listitem');
1023
1063
  });
1024
1064
  });
1025
1065
 
1066
+ it('restores list semantics when Carbon re-applies role="group" and aria-label on the items container', () => {
1067
+ const { getByTestId } = render(
1068
+ <CarbonChart
1069
+ testId={testId}
1070
+ dataSet={dataSet}
1071
+ options={chartOptions}
1072
+ type={CarbonChartType.bar}
1073
+ />
1074
+ );
1075
+ const wrapper = getByTestId(testId);
1076
+
1077
+ const itemsHost = wrapper.querySelector('[data-name="legend-items"]');
1078
+ expect(itemsHost).not.toBeNull();
1079
+ expect(itemsHost.getAttribute('role')).toBe('list');
1080
+
1081
+ act(() => {
1082
+ itemsHost.setAttribute('role', 'group');
1083
+ itemsHost.setAttribute('aria-label', 'Data groups');
1084
+ mutationObserverCallback();
1085
+ });
1086
+ act(() => {
1087
+ mutationObserverCallback();
1088
+ });
1089
+
1090
+ expect(itemsHost.getAttribute('role')).toBe('list');
1091
+ expect(itemsHost.getAttribute('aria-label')).toBeNull();
1092
+ });
1093
+
1026
1094
  it('uses translated legend instructions from I18nContext', () => {
1027
1095
  const translatedInstructions =
1028
1096
  'Marquer ces cases mettra à jour le graphique.';
@@ -29,6 +29,7 @@ import {
29
29
  DropdownMenuItem,
30
30
  ThemeInterface,
31
31
  ThemeContext,
32
+ useDeviceDetect,
32
33
  useIsInverse,
33
34
  VisuallyHidden,
34
35
  } from 'react-magma-dom';
@@ -1087,8 +1088,11 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1087
1088
  const [isTableOpen, setIsTableOpen] = React.useState(false);
1088
1089
  const [isFullscreen, setIsFullscreen] = React.useState(false);
1089
1090
  const [legendAnnouncement, setLegendAnnouncement] = React.useState('');
1091
+ const legendGroupAnnounceRef = React.useRef<HTMLDivElement | null>(null);
1090
1092
  const lastTableTriggerRef = React.useRef<HTMLButtonElement | null>(null);
1091
1093
  const legendAnnouncedRef = React.useRef(false);
1094
+ const selectionSignatureRef = React.useRef<string | undefined>(undefined);
1095
+ const { isMacOS, isChrome } = useDeviceDetect();
1092
1096
 
1093
1097
  const mergedRef = React.useCallback(
1094
1098
  (node: HTMLDivElement | null) => {
@@ -1173,11 +1177,37 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1173
1177
  if (!container) return;
1174
1178
 
1175
1179
  const applyListSemantics = (legend: Element) => {
1176
- legend.removeAttribute('aria-label');
1177
- legend.setAttribute('role', 'list');
1178
- legend
1179
- .querySelectorAll('.legend-item')
1180
- .forEach(item => item.setAttribute('role', 'listitem'));
1180
+ const itemsHost = legend.matches('[data-name="legend-items"]')
1181
+ ? legend
1182
+ : legend.querySelector('[data-name="legend-items"]') || legend;
1183
+
1184
+ itemsHost.removeAttribute('aria-label');
1185
+
1186
+ if (itemsHost.getAttribute('role') !== 'list') {
1187
+ itemsHost.setAttribute('role', 'list');
1188
+ }
1189
+
1190
+ legend.querySelectorAll('.legend-item').forEach(item => {
1191
+ if (item.getAttribute('role') !== 'listitem') {
1192
+ item.setAttribute('role', 'listitem');
1193
+ }
1194
+ });
1195
+
1196
+ container.querySelectorAll('[role="list"]').forEach(el => {
1197
+ if (el !== itemsHost && el.contains(itemsHost)) {
1198
+ el.removeAttribute('role');
1199
+ }
1200
+ });
1201
+ container.querySelectorAll('[aria-label="Data groups"]').forEach(el => {
1202
+ if (el !== itemsHost && el.contains(itemsHost)) {
1203
+ el.removeAttribute('aria-label');
1204
+ }
1205
+ });
1206
+ };
1207
+
1208
+ const unwrapFieldset = (fieldset: Element) => {
1209
+ fieldset.querySelector(':scope > legend')?.remove();
1210
+ fieldset.replaceWith(...Array.from(fieldset.childNodes));
1181
1211
  };
1182
1212
 
1183
1213
  const wrapLegend = () => {
@@ -1187,10 +1217,21 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1187
1217
 
1188
1218
  applyListSemantics(legend);
1189
1219
 
1190
- const parent = legend.parentElement;
1220
+ const closestFieldset = legend.closest(
1221
+ 'fieldset.cds--cc--legend-fieldset'
1222
+ );
1223
+
1224
+ container
1225
+ .querySelectorAll('fieldset.cds--cc--legend-fieldset')
1226
+ .forEach(fieldset => {
1227
+ if (fieldset !== closestFieldset) {
1228
+ unwrapFieldset(fieldset);
1229
+ }
1230
+ });
1191
1231
 
1192
- if (parent?.tagName === 'FIELDSET') {
1193
- const existingCaption = parent.querySelector('legend');
1232
+ if (closestFieldset) {
1233
+ const existingCaption =
1234
+ closestFieldset.querySelector(':scope > legend');
1194
1235
 
1195
1236
  if (existingCaption && existingCaption.textContent !== legendLabel) {
1196
1237
  existingCaption.textContent = legendLabel;
@@ -1211,11 +1252,71 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1211
1252
  wrapLegend();
1212
1253
  const observer = new MutationObserver(wrapLegend);
1213
1254
 
1214
- observer.observe(container, { childList: true, subtree: true });
1255
+ observer.observe(container, {
1256
+ childList: true,
1257
+ subtree: true,
1258
+ attributes: true,
1259
+ attributeFilter: ['role', 'aria-label'],
1260
+ });
1215
1261
 
1216
1262
  return () => observer.disconnect();
1217
1263
  }, [legendLabel]);
1218
1264
 
1265
+ React.useEffect(() => {
1266
+ const container = internalRef.current;
1267
+
1268
+ if (!container || !isMacOS || !isChrome) return;
1269
+
1270
+ let announceTimer: ReturnType<typeof setTimeout>;
1271
+ let alternateSuffix = false;
1272
+
1273
+ const closestLegend = (node: EventTarget | null) =>
1274
+ node instanceof Element ? node.closest('.cds--cc--legend') : null;
1275
+
1276
+ const handleFocusIn = (event: FocusEvent) => {
1277
+ const enteredLegend = closestLegend(event.target);
1278
+ const cameFromLegend = closestLegend(event.relatedTarget);
1279
+
1280
+ if (enteredLegend && !cameFromLegend) {
1281
+ if (legendGroupAnnounceRef.current) {
1282
+ legendGroupAnnounceRef.current.textContent = '';
1283
+ }
1284
+
1285
+ clearTimeout(announceTimer);
1286
+ announceTimer = setTimeout(() => {
1287
+ if (legendGroupAnnounceRef.current) {
1288
+ alternateSuffix = !alternateSuffix;
1289
+ legendGroupAnnounceRef.current.textContent =
1290
+ legendLabel + (alternateSuffix ? ' ' : '');
1291
+ }
1292
+ }, 250);
1293
+ }
1294
+ };
1295
+
1296
+ const handleFocusOut = (event: FocusEvent) => {
1297
+ const leftLegend =
1298
+ closestLegend(event.target) && !closestLegend(event.relatedTarget);
1299
+
1300
+ // Reset once focus leaves the legend so re-entering announces again.
1301
+ if (leftLegend) {
1302
+ clearTimeout(announceTimer);
1303
+
1304
+ if (legendGroupAnnounceRef.current) {
1305
+ legendGroupAnnounceRef.current.textContent = '';
1306
+ }
1307
+ }
1308
+ };
1309
+
1310
+ container.addEventListener('focusin', handleFocusIn);
1311
+ container.addEventListener('focusout', handleFocusOut);
1312
+
1313
+ return () => {
1314
+ clearTimeout(announceTimer);
1315
+ container.removeEventListener('focusin', handleFocusIn);
1316
+ container.removeEventListener('focusout', handleFocusOut);
1317
+ };
1318
+ }, [isMacOS, isChrome, legendLabel]);
1319
+
1219
1320
  const handleModalDownloadCsv = React.useCallback(() => {
1220
1321
  downloadCsv(dataSet as Array<Record<string, unknown>>, chartTitle);
1221
1322
  }, [dataSet, chartTitle]);
@@ -1328,6 +1429,20 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1328
1429
  }
1329
1430
  });
1330
1431
 
1432
+ // Carbon re-applies aria-checked (with the same values) on every
1433
+ // render, which still triggers this observer. Only announce when the
1434
+ // selection actually changed, otherwise unrelated re-renders (e.g.
1435
+ // the legend group announcement) cause phantom announcements.
1436
+ const selectionSignature = `${activeLabels.join('|')}::${allItems.length}`;
1437
+
1438
+ if (selectionSignatureRef.current === selectionSignature) return;
1439
+
1440
+ const isFirstRun = selectionSignatureRef.current === undefined;
1441
+
1442
+ selectionSignatureRef.current = selectionSignature;
1443
+
1444
+ if (isFirstRun) return;
1445
+
1331
1446
  if (activeLabels.length === 1) {
1332
1447
  if (!legendAnnouncedRef.current) {
1333
1448
  legendAnnouncedRef.current = true;
@@ -1343,6 +1458,7 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1343
1458
  });
1344
1459
 
1345
1460
  observer.observe(wrapper, {
1461
+ childList: true,
1346
1462
  subtree: true,
1347
1463
  attributes: true,
1348
1464
  attributeFilter: ['aria-checked'],
@@ -1360,6 +1476,9 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1360
1476
  <VisuallyHidden>
1361
1477
  <Announce>{legendAnnouncement}</Announce>
1362
1478
  </VisuallyHidden>
1479
+ <VisuallyHidden>
1480
+ <div aria-live="polite" ref={legendGroupAnnounceRef} />
1481
+ </VisuallyHidden>
1363
1482
  <CarbonChartWrapper
1364
1483
  data-testid={testId}
1365
1484
  isInverse={isInverse}