@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-magma/charts",
3
- "version": "14.0.0-next.4",
3
+ "version": "14.0.0-next.5",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1168,6 +1168,37 @@ describe('CarbonChart', () => {
1168
1168
  expect(after[0]).toBe(before[0]);
1169
1169
  });
1170
1170
 
1171
+ it('unwraps the stale fieldset when Carbon re-renders the legend deeper inside it', () => {
1172
+ const { getByTestId } = render(
1173
+ <CarbonChart
1174
+ testId={testId}
1175
+ dataSet={dataSet}
1176
+ options={chartOptions}
1177
+ type={CarbonChartType.bar}
1178
+ />
1179
+ );
1180
+ const wrapper = getByTestId(testId);
1181
+
1182
+ const staleFieldset = wrapper.querySelector('.cds--cc--legend-fieldset');
1183
+ const legend = staleFieldset.querySelector('.cds--cc--legend');
1184
+
1185
+ act(() => {
1186
+ const layoutChild = document.createElement('div');
1187
+ layoutChild.className = 'layout-child legend';
1188
+ staleFieldset.append(layoutChild);
1189
+ layoutChild.append(legend);
1190
+ mutationObserverCallback?.();
1191
+ });
1192
+
1193
+ const fieldsets = wrapper.querySelectorAll('.cds--cc--legend-fieldset');
1194
+ expect(fieldsets).toHaveLength(1);
1195
+ expect(
1196
+ fieldsets[0].querySelector('.cds--cc--legend-fieldset')
1197
+ ).toBeNull();
1198
+ expect(fieldsets[0].contains(legend)).toBe(true);
1199
+ expect(fieldsets[0].querySelectorAll(':scope > legend')).toHaveLength(1);
1200
+ });
1201
+
1171
1202
  it('replaces Carbon\'s generic "Data groups" aria-label with semantic list roles on the legend and its items', () => {
1172
1203
  const { getByTestId } = render(
1173
1204
  <CarbonChart
@@ -1181,16 +1212,53 @@ describe('CarbonChart', () => {
1181
1212
 
1182
1213
  const legend = wrapper.querySelector('.cds--cc--legend');
1183
1214
  expect(legend).not.toBeNull();
1184
- expect(legend.getAttribute('aria-label')).toBeNull();
1185
- expect(legend.getAttribute('role')).toBe('list');
1186
1215
 
1187
- const items = legend.querySelectorAll('.legend-item');
1216
+ const itemsHost = legend.matches('[data-name="legend-items"]')
1217
+ ? legend
1218
+ : legend.querySelector('[data-name="legend-items"]') || legend;
1219
+ expect(itemsHost.getAttribute('aria-label')).toBeNull();
1220
+ expect(itemsHost.getAttribute('role')).toBe('list');
1221
+
1222
+ if (itemsHost !== legend) {
1223
+ expect(legend.getAttribute('role')).toBeNull();
1224
+ expect(legend.getAttribute('aria-label')).toBeNull();
1225
+ }
1226
+
1227
+ const items = itemsHost.querySelectorAll('.legend-item');
1188
1228
  expect(items.length).toBeGreaterThan(0);
1189
1229
  items.forEach(item => {
1190
1230
  expect(item.getAttribute('role')).toBe('listitem');
1191
1231
  });
1192
1232
  });
1193
1233
 
1234
+ it('restores list semantics when Carbon re-applies role="group" and aria-label on the items container', () => {
1235
+ const { getByTestId } = render(
1236
+ <CarbonChart
1237
+ testId={testId}
1238
+ dataSet={dataSet}
1239
+ options={chartOptions}
1240
+ type={CarbonChartType.bar}
1241
+ />
1242
+ );
1243
+ const wrapper = getByTestId(testId);
1244
+
1245
+ const itemsHost = wrapper.querySelector('[data-name="legend-items"]');
1246
+ expect(itemsHost).not.toBeNull();
1247
+ expect(itemsHost.getAttribute('role')).toBe('list');
1248
+
1249
+ act(() => {
1250
+ itemsHost.setAttribute('role', 'group');
1251
+ itemsHost.setAttribute('aria-label', 'Data groups');
1252
+ mutationObserverCallback();
1253
+ });
1254
+ act(() => {
1255
+ mutationObserverCallback();
1256
+ });
1257
+
1258
+ expect(itemsHost.getAttribute('role')).toBe('list');
1259
+ expect(itemsHost.getAttribute('aria-label')).toBeNull();
1260
+ });
1261
+
1194
1262
  it('uses translated legend instructions from I18nContext', () => {
1195
1263
  const translatedInstructions =
1196
1264
  'Marquer ces cases mettra à jour le graphique.';
@@ -30,6 +30,7 @@ import {
30
30
  DropdownMenuItem,
31
31
  ThemeInterface,
32
32
  ThemeContext,
33
+ useDeviceDetect,
33
34
  useIsInverse,
34
35
  VisuallyHidden,
35
36
  } from 'react-magma-dom';
@@ -1126,8 +1127,11 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1126
1127
  const [isTableOpen, setIsTableOpen] = React.useState(false);
1127
1128
  const [isFullscreen, setIsFullscreen] = React.useState(false);
1128
1129
  const [legendAnnouncement, setLegendAnnouncement] = React.useState('');
1130
+ const legendGroupAnnounceRef = React.useRef<HTMLDivElement | null>(null);
1129
1131
  const lastTableTriggerRef = React.useRef<HTMLButtonElement | null>(null);
1130
1132
  const legendAnnouncedRef = React.useRef(false);
1133
+ const selectionSignatureRef = React.useRef<string | undefined>(undefined);
1134
+ const { isMacOS, isChrome } = useDeviceDetect();
1131
1135
 
1132
1136
  const mergedRef = React.useCallback(
1133
1137
  (node: HTMLDivElement | null) => {
@@ -1215,11 +1219,37 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1215
1219
  if (!container) return;
1216
1220
 
1217
1221
  const applyListSemantics = (legend: Element) => {
1218
- legend.removeAttribute('aria-label');
1219
- legend.setAttribute('role', 'list');
1220
- legend
1221
- .querySelectorAll('.legend-item')
1222
- .forEach(item => item.setAttribute('role', 'listitem'));
1222
+ const itemsHost = legend.matches('[data-name="legend-items"]')
1223
+ ? legend
1224
+ : legend.querySelector('[data-name="legend-items"]') || legend;
1225
+
1226
+ itemsHost.removeAttribute('aria-label');
1227
+
1228
+ if (itemsHost.getAttribute('role') !== 'list') {
1229
+ itemsHost.setAttribute('role', 'list');
1230
+ }
1231
+
1232
+ legend.querySelectorAll('.legend-item').forEach(item => {
1233
+ if (item.getAttribute('role') !== 'listitem') {
1234
+ item.setAttribute('role', 'listitem');
1235
+ }
1236
+ });
1237
+
1238
+ container.querySelectorAll('[role="list"]').forEach(el => {
1239
+ if (el !== itemsHost && el.contains(itemsHost)) {
1240
+ el.removeAttribute('role');
1241
+ }
1242
+ });
1243
+ container.querySelectorAll('[aria-label="Data groups"]').forEach(el => {
1244
+ if (el !== itemsHost && el.contains(itemsHost)) {
1245
+ el.removeAttribute('aria-label');
1246
+ }
1247
+ });
1248
+ };
1249
+
1250
+ const unwrapFieldset = (fieldset: Element) => {
1251
+ fieldset.querySelector(':scope > legend')?.remove();
1252
+ fieldset.replaceWith(...Array.from(fieldset.childNodes));
1223
1253
  };
1224
1254
 
1225
1255
  const wrapLegend = () => {
@@ -1229,10 +1259,21 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1229
1259
 
1230
1260
  applyListSemantics(legend);
1231
1261
 
1232
- const parent = legend.parentElement;
1262
+ const closestFieldset = legend.closest(
1263
+ 'fieldset.cds--cc--legend-fieldset'
1264
+ );
1265
+
1266
+ container
1267
+ .querySelectorAll('fieldset.cds--cc--legend-fieldset')
1268
+ .forEach(fieldset => {
1269
+ if (fieldset !== closestFieldset) {
1270
+ unwrapFieldset(fieldset);
1271
+ }
1272
+ });
1233
1273
 
1234
- if (parent?.tagName === 'FIELDSET') {
1235
- const existingCaption = parent.querySelector('legend');
1274
+ if (closestFieldset) {
1275
+ const existingCaption =
1276
+ closestFieldset.querySelector(':scope > legend');
1236
1277
 
1237
1278
  if (existingCaption && existingCaption.textContent !== legendLabel) {
1238
1279
  existingCaption.textContent = legendLabel;
@@ -1253,11 +1294,71 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1253
1294
  wrapLegend();
1254
1295
  const observer = new MutationObserver(wrapLegend);
1255
1296
 
1256
- observer.observe(container, { childList: true, subtree: true });
1297
+ observer.observe(container, {
1298
+ childList: true,
1299
+ subtree: true,
1300
+ attributes: true,
1301
+ attributeFilter: ['role', 'aria-label'],
1302
+ });
1257
1303
 
1258
1304
  return () => observer.disconnect();
1259
1305
  }, [legendLabel]);
1260
1306
 
1307
+ React.useEffect(() => {
1308
+ const container = internalRef.current;
1309
+
1310
+ if (!container || !isMacOS || !isChrome) return;
1311
+
1312
+ let announceTimer: ReturnType<typeof setTimeout>;
1313
+ let alternateSuffix = false;
1314
+
1315
+ const closestLegend = (node: EventTarget | null) =>
1316
+ node instanceof Element ? node.closest('.cds--cc--legend') : null;
1317
+
1318
+ const handleFocusIn = (event: FocusEvent) => {
1319
+ const enteredLegend = closestLegend(event.target);
1320
+ const cameFromLegend = closestLegend(event.relatedTarget);
1321
+
1322
+ if (enteredLegend && !cameFromLegend) {
1323
+ if (legendGroupAnnounceRef.current) {
1324
+ legendGroupAnnounceRef.current.textContent = '';
1325
+ }
1326
+
1327
+ clearTimeout(announceTimer);
1328
+ announceTimer = setTimeout(() => {
1329
+ if (legendGroupAnnounceRef.current) {
1330
+ alternateSuffix = !alternateSuffix;
1331
+ legendGroupAnnounceRef.current.textContent =
1332
+ legendLabel + (alternateSuffix ? ' ' : '');
1333
+ }
1334
+ }, 250);
1335
+ }
1336
+ };
1337
+
1338
+ const handleFocusOut = (event: FocusEvent) => {
1339
+ const leftLegend =
1340
+ closestLegend(event.target) && !closestLegend(event.relatedTarget);
1341
+
1342
+ // Reset once focus leaves the legend so re-entering announces again.
1343
+ if (leftLegend) {
1344
+ clearTimeout(announceTimer);
1345
+
1346
+ if (legendGroupAnnounceRef.current) {
1347
+ legendGroupAnnounceRef.current.textContent = '';
1348
+ }
1349
+ }
1350
+ };
1351
+
1352
+ container.addEventListener('focusin', handleFocusIn);
1353
+ container.addEventListener('focusout', handleFocusOut);
1354
+
1355
+ return () => {
1356
+ clearTimeout(announceTimer);
1357
+ container.removeEventListener('focusin', handleFocusIn);
1358
+ container.removeEventListener('focusout', handleFocusOut);
1359
+ };
1360
+ }, [isMacOS, isChrome, legendLabel]);
1361
+
1261
1362
  const handleModalDownloadCsv = React.useCallback(() => {
1262
1363
  downloadCsv(dataSet as Array<Record<string, unknown>>, chartTitle);
1263
1364
  }, [dataSet, chartTitle]);
@@ -1445,6 +1546,20 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1445
1546
  }
1446
1547
  });
1447
1548
 
1549
+ // Carbon re-applies aria-checked (with the same values) on every
1550
+ // render, which still triggers this observer. Only announce when the
1551
+ // selection actually changed, otherwise unrelated re-renders (e.g.
1552
+ // the legend group announcement) cause phantom announcements.
1553
+ const selectionSignature = `${activeLabels.join('|')}::${allItems.length}`;
1554
+
1555
+ if (selectionSignatureRef.current === selectionSignature) return;
1556
+
1557
+ const isFirstRun = selectionSignatureRef.current === undefined;
1558
+
1559
+ selectionSignatureRef.current = selectionSignature;
1560
+
1561
+ if (isFirstRun) return;
1562
+
1448
1563
  if (activeLabels.length === 1) {
1449
1564
  if (!legendAnnouncedRef.current) {
1450
1565
  legendAnnouncedRef.current = true;
@@ -1460,6 +1575,7 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1460
1575
  });
1461
1576
 
1462
1577
  observer.observe(wrapper, {
1578
+ childList: true,
1463
1579
  subtree: true,
1464
1580
  attributes: true,
1465
1581
  attributeFilter: ['aria-checked'],
@@ -1479,6 +1595,9 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
1479
1595
  <VisuallyHidden>
1480
1596
  <Announce>{legendAnnouncement}</Announce>
1481
1597
  </VisuallyHidden>
1598
+ <VisuallyHidden>
1599
+ <div aria-live="polite" ref={legendGroupAnnounceRef} />
1600
+ </VisuallyHidden>
1482
1601
  <CarbonChartWrapper
1483
1602
  data-testid={testId}
1484
1603
  isInverse={isInverse}