@react-magma/charts 13.2.0-next.0 → 13.2.0
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 +202 -7
- package/dist/charts.js.map +1 -1
- package/dist/charts.modern.module.js +203 -8
- package/dist/charts.modern.module.js.map +1 -1
- package/dist/charts.umd.js +392 -32
- package/dist/charts.umd.js.map +1 -1
- package/dist/components/ChartTable/chartToolbarI18n.d.ts +1 -0
- package/package.json +3 -3
- package/src/components/CarbonChart/CarbonChart.test.js +214 -4
- package/src/components/CarbonChart/CarbonChart.tsx +264 -0
- package/src/components/ChartTable/chartToolbarI18n.ts +4 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-magma/charts",
|
|
3
|
-
"version": "13.2.0
|
|
3
|
+
"version": "13.2.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"identity-obj-proxy": "^3.0.0",
|
|
48
48
|
"react": "^17.0.2",
|
|
49
49
|
"react-dom": "^17.0.2",
|
|
50
|
-
"react-magma-dom": "^4.
|
|
50
|
+
"react-magma-dom": "^4.15.0",
|
|
51
51
|
"react-magma-icons": "^3.2.5",
|
|
52
52
|
"rollup": "^4.52.4",
|
|
53
53
|
"rollup-plugin-postcss": "^4.0.2"
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"@emotion/styled": "^11.13.0",
|
|
58
58
|
"react": "^17.0.2",
|
|
59
59
|
"react-dom": "^17.0.2",
|
|
60
|
-
"react-magma-dom": "^4.
|
|
60
|
+
"react-magma-dom": "^4.15.0",
|
|
61
61
|
"react-magma-icons": "^3.2.5"
|
|
62
62
|
},
|
|
63
63
|
"engines": {
|
|
@@ -2,7 +2,13 @@ import React from 'react';
|
|
|
2
2
|
|
|
3
3
|
import { act, render, screen, fireEvent } from '@testing-library/react';
|
|
4
4
|
import userEvent from '@testing-library/user-event';
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
ThemeContext,
|
|
7
|
+
magma,
|
|
8
|
+
DropdownMenuItem,
|
|
9
|
+
I18nContext,
|
|
10
|
+
defaultI18n,
|
|
11
|
+
} from 'react-magma-dom';
|
|
6
12
|
|
|
7
13
|
import { CarbonChart, CarbonChartType } from '.';
|
|
8
14
|
|
|
@@ -12,10 +18,14 @@ global.ResizeObserver = jest.fn().mockImplementation(() => ({
|
|
|
12
18
|
disconnect: jest.fn(),
|
|
13
19
|
}));
|
|
14
20
|
|
|
15
|
-
// Capture MutationObserver callbacks so we can trigger them manually
|
|
16
|
-
|
|
21
|
+
// Capture MutationObserver callbacks so we can trigger them manually.
|
|
22
|
+
// Multiple observers may be created per component; we collect all of them and
|
|
23
|
+
// call each one so that no observer is silently skipped.
|
|
24
|
+
let mutationObserverCallbacks = [];
|
|
25
|
+
const mutationObserverCallback = mutations =>
|
|
26
|
+
mutationObserverCallbacks.forEach(cb => cb(mutations));
|
|
17
27
|
global.MutationObserver = jest.fn().mockImplementation(callback => {
|
|
18
|
-
|
|
28
|
+
mutationObserverCallbacks.push(callback);
|
|
19
29
|
return {
|
|
20
30
|
observe: jest.fn(),
|
|
21
31
|
disconnect: jest.fn(),
|
|
@@ -624,6 +634,7 @@ describe('CarbonChart', () => {
|
|
|
624
634
|
let otherButton;
|
|
625
635
|
|
|
626
636
|
beforeEach(() => {
|
|
637
|
+
mutationObserverCallbacks = [];
|
|
627
638
|
jest.useFakeTimers();
|
|
628
639
|
jest.spyOn(window, 'requestAnimationFrame').mockImplementation(cb => {
|
|
629
640
|
cb(0);
|
|
@@ -1080,4 +1091,203 @@ describe('CarbonChart', () => {
|
|
|
1080
1091
|
});
|
|
1081
1092
|
});
|
|
1082
1093
|
});
|
|
1094
|
+
|
|
1095
|
+
describe('Legend accessibility wrap', () => {
|
|
1096
|
+
const testId = 'legend-a11y';
|
|
1097
|
+
|
|
1098
|
+
beforeEach(() => {
|
|
1099
|
+
mutationObserverCallbacks = [];
|
|
1100
|
+
});
|
|
1101
|
+
|
|
1102
|
+
it('wraps the Carbon legend in a fieldset with a legend caption derived from the chart title', () => {
|
|
1103
|
+
const { getByTestId } = render(
|
|
1104
|
+
<CarbonChart
|
|
1105
|
+
testId={testId}
|
|
1106
|
+
dataSet={dataSet}
|
|
1107
|
+
options={chartOptions}
|
|
1108
|
+
type={CarbonChartType.bar}
|
|
1109
|
+
/>
|
|
1110
|
+
);
|
|
1111
|
+
const wrapper = getByTestId(testId);
|
|
1112
|
+
|
|
1113
|
+
const fieldset = wrapper.querySelector('.cds--cc--legend-fieldset');
|
|
1114
|
+
expect(fieldset).not.toBeNull();
|
|
1115
|
+
expect(fieldset.tagName).toBe('FIELDSET');
|
|
1116
|
+
expect(fieldset.querySelector('.cds--cc--legend')).not.toBeNull();
|
|
1117
|
+
|
|
1118
|
+
const caption = fieldset.querySelector('legend');
|
|
1119
|
+
expect(caption).not.toBeNull();
|
|
1120
|
+
expect(caption.textContent).toBe(
|
|
1121
|
+
`${chartOptions.title}. Checking these checkboxes will update the chart.`
|
|
1122
|
+
);
|
|
1123
|
+
});
|
|
1124
|
+
|
|
1125
|
+
it('uses ariaLabel for the legend caption when provided', () => {
|
|
1126
|
+
const ariaLabel = 'Quarterly sales chart';
|
|
1127
|
+
const { getByTestId } = render(
|
|
1128
|
+
<CarbonChart
|
|
1129
|
+
testId={testId}
|
|
1130
|
+
ariaLabel={ariaLabel}
|
|
1131
|
+
dataSet={dataSet}
|
|
1132
|
+
options={chartOptions}
|
|
1133
|
+
type={CarbonChartType.bar}
|
|
1134
|
+
/>
|
|
1135
|
+
);
|
|
1136
|
+
const wrapper = getByTestId(testId);
|
|
1137
|
+
|
|
1138
|
+
const caption = wrapper.querySelector(
|
|
1139
|
+
'.cds--cc--legend-fieldset > legend'
|
|
1140
|
+
);
|
|
1141
|
+
expect(caption).not.toBeNull();
|
|
1142
|
+
expect(caption.textContent).toBe(
|
|
1143
|
+
`${ariaLabel}. Checking these checkboxes will update the chart.`
|
|
1144
|
+
);
|
|
1145
|
+
});
|
|
1146
|
+
|
|
1147
|
+
it('does not duplicate the fieldset when the mutation observer fires again with no DOM changes', () => {
|
|
1148
|
+
const { getByTestId } = render(
|
|
1149
|
+
<CarbonChart
|
|
1150
|
+
testId={testId}
|
|
1151
|
+
dataSet={dataSet}
|
|
1152
|
+
options={chartOptions}
|
|
1153
|
+
type={CarbonChartType.bar}
|
|
1154
|
+
/>
|
|
1155
|
+
);
|
|
1156
|
+
const wrapper = getByTestId(testId);
|
|
1157
|
+
|
|
1158
|
+
const before = wrapper.querySelectorAll('.cds--cc--legend-fieldset');
|
|
1159
|
+
expect(before).toHaveLength(1);
|
|
1160
|
+
|
|
1161
|
+
act(() => {
|
|
1162
|
+
mutationObserverCallback?.();
|
|
1163
|
+
mutationObserverCallback?.();
|
|
1164
|
+
});
|
|
1165
|
+
|
|
1166
|
+
const after = wrapper.querySelectorAll('.cds--cc--legend-fieldset');
|
|
1167
|
+
expect(after).toHaveLength(1);
|
|
1168
|
+
expect(after[0]).toBe(before[0]);
|
|
1169
|
+
});
|
|
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
|
+
|
|
1202
|
+
it('replaces Carbon\'s generic "Data groups" aria-label with semantic list roles on the legend and its items', () => {
|
|
1203
|
+
const { getByTestId } = render(
|
|
1204
|
+
<CarbonChart
|
|
1205
|
+
testId={testId}
|
|
1206
|
+
dataSet={dataSet}
|
|
1207
|
+
options={chartOptions}
|
|
1208
|
+
type={CarbonChartType.bar}
|
|
1209
|
+
/>
|
|
1210
|
+
);
|
|
1211
|
+
const wrapper = getByTestId(testId);
|
|
1212
|
+
|
|
1213
|
+
const legend = wrapper.querySelector('.cds--cc--legend');
|
|
1214
|
+
expect(legend).not.toBeNull();
|
|
1215
|
+
|
|
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
|
+
expect(legend.getAttribute('role')).toBe(
|
|
1223
|
+
itemsHost === legend ? 'list' : null
|
|
1224
|
+
);
|
|
1225
|
+
expect(legend.getAttribute('aria-label')).toBeNull();
|
|
1226
|
+
|
|
1227
|
+
const items = itemsHost.querySelectorAll('.legend-item');
|
|
1228
|
+
expect(items.length).toBeGreaterThan(0);
|
|
1229
|
+
items.forEach(item => {
|
|
1230
|
+
expect(item.getAttribute('role')).toBe('listitem');
|
|
1231
|
+
});
|
|
1232
|
+
});
|
|
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
|
+
|
|
1262
|
+
it('uses translated legend instructions from I18nContext', () => {
|
|
1263
|
+
const translatedInstructions =
|
|
1264
|
+
'Marquer ces cases mettra à jour le graphique.';
|
|
1265
|
+
const i18nValue = {
|
|
1266
|
+
...defaultI18n,
|
|
1267
|
+
charts: {
|
|
1268
|
+
...defaultI18n.charts,
|
|
1269
|
+
toolbar: { legendInstructions: translatedInstructions },
|
|
1270
|
+
},
|
|
1271
|
+
};
|
|
1272
|
+
|
|
1273
|
+
const { getByTestId } = render(
|
|
1274
|
+
<I18nContext.Provider value={i18nValue}>
|
|
1275
|
+
<CarbonChart
|
|
1276
|
+
testId={testId}
|
|
1277
|
+
dataSet={dataSet}
|
|
1278
|
+
options={chartOptions}
|
|
1279
|
+
type={CarbonChartType.bar}
|
|
1280
|
+
/>
|
|
1281
|
+
</I18nContext.Provider>
|
|
1282
|
+
);
|
|
1283
|
+
const wrapper = getByTestId(testId);
|
|
1284
|
+
|
|
1285
|
+
const caption = wrapper.querySelector(
|
|
1286
|
+
'.cds--cc--legend-fieldset > legend'
|
|
1287
|
+
);
|
|
1288
|
+
expect(caption.textContent).toBe(
|
|
1289
|
+
`${chartOptions.title}. ${translatedInstructions}`
|
|
1290
|
+
);
|
|
1291
|
+
});
|
|
1292
|
+
});
|
|
1083
1293
|
});
|
|
@@ -25,11 +25,14 @@ import {
|
|
|
25
25
|
import styled from '@emotion/styled';
|
|
26
26
|
import { transparentize } from 'polished';
|
|
27
27
|
import {
|
|
28
|
+
Announce,
|
|
28
29
|
DropdownDivider,
|
|
29
30
|
DropdownMenuItem,
|
|
30
31
|
ThemeInterface,
|
|
31
32
|
ThemeContext,
|
|
33
|
+
useDeviceDetect,
|
|
32
34
|
useIsInverse,
|
|
35
|
+
VisuallyHidden,
|
|
33
36
|
} from 'react-magma-dom';
|
|
34
37
|
import {
|
|
35
38
|
FullscreenExitIcon,
|
|
@@ -39,6 +42,7 @@ import {
|
|
|
39
42
|
} from 'react-magma-icons';
|
|
40
43
|
|
|
41
44
|
import { useCarbonModalFocusManagement } from '../../hooks/useCarbonModalFocusManagement';
|
|
45
|
+
// @ts-ignore
|
|
42
46
|
import './carbon-charts.css';
|
|
43
47
|
import {
|
|
44
48
|
ChartFullscreenButton,
|
|
@@ -167,6 +171,23 @@ const CarbonChartWrapper = styled.div<{
|
|
|
167
171
|
groupsLength: number;
|
|
168
172
|
theme: ThemeInterface;
|
|
169
173
|
}>`
|
|
174
|
+
.cds--cc--legend-fieldset {
|
|
175
|
+
border: 0;
|
|
176
|
+
margin: 0;
|
|
177
|
+
min-inline-size: 0;
|
|
178
|
+
padding: 0;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
.cds--cc--legend-fieldset > legend {
|
|
182
|
+
clip: rect(1px, 1px, 1px, 1px);
|
|
183
|
+
height: 1px;
|
|
184
|
+
overflow: hidden;
|
|
185
|
+
position: absolute;
|
|
186
|
+
top: auto;
|
|
187
|
+
white-space: nowrap;
|
|
188
|
+
width: 1px;
|
|
189
|
+
}
|
|
190
|
+
|
|
170
191
|
&:fullscreen,
|
|
171
192
|
&:-webkit-full-screen {
|
|
172
193
|
background: ${props =>
|
|
@@ -1105,7 +1126,12 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
|
|
|
1105
1126
|
|
|
1106
1127
|
const [isTableOpen, setIsTableOpen] = React.useState(false);
|
|
1107
1128
|
const [isFullscreen, setIsFullscreen] = React.useState(false);
|
|
1129
|
+
const [legendAnnouncement, setLegendAnnouncement] = React.useState('');
|
|
1130
|
+
const legendGroupAnnounceRef = React.useRef<HTMLDivElement | null>(null);
|
|
1108
1131
|
const lastTableTriggerRef = React.useRef<HTMLButtonElement | null>(null);
|
|
1132
|
+
const legendAnnouncedRef = React.useRef(false);
|
|
1133
|
+
const selectionSignatureRef = React.useRef<string | undefined>(undefined);
|
|
1134
|
+
const { isMacOS, isChrome } = useDeviceDetect();
|
|
1109
1135
|
|
|
1110
1136
|
const mergedRef = React.useCallback(
|
|
1111
1137
|
(node: HTMLDivElement | null) => {
|
|
@@ -1185,6 +1211,154 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
|
|
|
1185
1211
|
const chartTitle: string =
|
|
1186
1212
|
(options as ExtendedChartOptions).title || toolbarI18n.defaultTitle;
|
|
1187
1213
|
|
|
1214
|
+
const legendLabel = `${ariaLabel || chartTitle}. ${toolbarI18n.legendInstructions}`;
|
|
1215
|
+
|
|
1216
|
+
React.useEffect(() => {
|
|
1217
|
+
const container = internalRef.current;
|
|
1218
|
+
|
|
1219
|
+
if (!container) return;
|
|
1220
|
+
|
|
1221
|
+
const applyListSemantics = (legend: Element) => {
|
|
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));
|
|
1253
|
+
};
|
|
1254
|
+
|
|
1255
|
+
const wrapLegend = () => {
|
|
1256
|
+
const legend = container.querySelector('.cds--cc--legend');
|
|
1257
|
+
|
|
1258
|
+
if (!legend) return;
|
|
1259
|
+
|
|
1260
|
+
applyListSemantics(legend);
|
|
1261
|
+
|
|
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
|
+
});
|
|
1273
|
+
|
|
1274
|
+
if (closestFieldset) {
|
|
1275
|
+
const existingCaption =
|
|
1276
|
+
closestFieldset.querySelector(':scope > legend');
|
|
1277
|
+
|
|
1278
|
+
if (existingCaption && existingCaption.textContent !== legendLabel) {
|
|
1279
|
+
existingCaption.textContent = legendLabel;
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
return;
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
const fieldset = document.createElement('fieldset');
|
|
1286
|
+
const caption = document.createElement('legend');
|
|
1287
|
+
|
|
1288
|
+
fieldset.className = 'cds--cc--legend-fieldset';
|
|
1289
|
+
caption.textContent = legendLabel;
|
|
1290
|
+
legend.before(fieldset);
|
|
1291
|
+
fieldset.append(caption, legend);
|
|
1292
|
+
};
|
|
1293
|
+
|
|
1294
|
+
wrapLegend();
|
|
1295
|
+
const observer = new MutationObserver(wrapLegend);
|
|
1296
|
+
|
|
1297
|
+
observer.observe(container, {
|
|
1298
|
+
childList: true,
|
|
1299
|
+
subtree: true,
|
|
1300
|
+
attributes: true,
|
|
1301
|
+
attributeFilter: ['role', 'aria-label'],
|
|
1302
|
+
});
|
|
1303
|
+
|
|
1304
|
+
return () => observer.disconnect();
|
|
1305
|
+
}, [legendLabel]);
|
|
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
|
+
|
|
1188
1362
|
const handleModalDownloadCsv = React.useCallback(() => {
|
|
1189
1363
|
downloadCsv(dataSet as Array<Record<string, unknown>>, chartTitle);
|
|
1190
1364
|
}, [dataSet, chartTitle]);
|
|
@@ -1329,6 +1503,87 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
|
|
|
1329
1503
|
};
|
|
1330
1504
|
}, [type]);
|
|
1331
1505
|
|
|
1506
|
+
React.useEffect(() => {
|
|
1507
|
+
const timer = setTimeout(() => {
|
|
1508
|
+
if (internalRef.current) {
|
|
1509
|
+
internalRef.current
|
|
1510
|
+
.querySelectorAll<SVGGElement>('g[aria-label]')
|
|
1511
|
+
.forEach(g => {
|
|
1512
|
+
const role = g.getAttribute('role');
|
|
1513
|
+
|
|
1514
|
+
if (!role) {
|
|
1515
|
+
g.setAttribute('role', 'img');
|
|
1516
|
+
} else if (role === 'graphics-object group') {
|
|
1517
|
+
g.setAttribute('role', 'graphics-object');
|
|
1518
|
+
}
|
|
1519
|
+
});
|
|
1520
|
+
}
|
|
1521
|
+
}, 0);
|
|
1522
|
+
|
|
1523
|
+
return () => clearTimeout(timer);
|
|
1524
|
+
}, [type, dataSet]);
|
|
1525
|
+
|
|
1526
|
+
React.useEffect(() => {
|
|
1527
|
+
const wrapper = internalRef.current;
|
|
1528
|
+
|
|
1529
|
+
if (!wrapper) return;
|
|
1530
|
+
|
|
1531
|
+
const observer = new MutationObserver(() => {
|
|
1532
|
+
const allItems = wrapper.querySelectorAll<HTMLElement>(
|
|
1533
|
+
'.legend-item:not(.additional)'
|
|
1534
|
+
);
|
|
1535
|
+
|
|
1536
|
+
if (allItems.length <= 1) return;
|
|
1537
|
+
|
|
1538
|
+
const activeLabels: string[] = [];
|
|
1539
|
+
|
|
1540
|
+
allItems.forEach(el => {
|
|
1541
|
+
const checkbox = el.querySelector<HTMLElement>('.checkbox');
|
|
1542
|
+
const label = el.querySelector('p');
|
|
1543
|
+
|
|
1544
|
+
if (checkbox?.getAttribute('aria-checked') === 'true' && label) {
|
|
1545
|
+
activeLabels.push(label.textContent?.trim() || '');
|
|
1546
|
+
}
|
|
1547
|
+
});
|
|
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
|
+
|
|
1563
|
+
if (activeLabels.length === 1) {
|
|
1564
|
+
if (!legendAnnouncedRef.current) {
|
|
1565
|
+
legendAnnouncedRef.current = true;
|
|
1566
|
+
setLegendAnnouncement(`Only ${activeLabels[0]} is selected`);
|
|
1567
|
+
}
|
|
1568
|
+
} else if (activeLabels.length === allItems.length) {
|
|
1569
|
+
legendAnnouncedRef.current = false;
|
|
1570
|
+
setLegendAnnouncement('All items selected');
|
|
1571
|
+
} else {
|
|
1572
|
+
legendAnnouncedRef.current = false;
|
|
1573
|
+
setLegendAnnouncement('');
|
|
1574
|
+
}
|
|
1575
|
+
});
|
|
1576
|
+
|
|
1577
|
+
observer.observe(wrapper, {
|
|
1578
|
+
childList: true,
|
|
1579
|
+
subtree: true,
|
|
1580
|
+
attributes: true,
|
|
1581
|
+
attributeFilter: ['aria-checked'],
|
|
1582
|
+
});
|
|
1583
|
+
|
|
1584
|
+
return () => observer.disconnect();
|
|
1585
|
+
}, [type, dataSet]);
|
|
1586
|
+
|
|
1332
1587
|
const groupsLength = Object.keys(colorScale).length;
|
|
1333
1588
|
|
|
1334
1589
|
const showTable = chartToolbar?.showAsTable !== false;
|
|
@@ -1337,12 +1592,21 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
|
|
|
1337
1592
|
|
|
1338
1593
|
return (
|
|
1339
1594
|
<FullscreenRoot ref={mergedRef} isInverse={isInverse} theme={theme}>
|
|
1595
|
+
<VisuallyHidden>
|
|
1596
|
+
<Announce>{legendAnnouncement}</Announce>
|
|
1597
|
+
</VisuallyHidden>
|
|
1598
|
+
<VisuallyHidden>
|
|
1599
|
+
<div aria-live="polite" ref={legendGroupAnnounceRef} />
|
|
1600
|
+
</VisuallyHidden>
|
|
1340
1601
|
<CarbonChartWrapper
|
|
1341
1602
|
data-testid={testId}
|
|
1342
1603
|
isInverse={isInverse}
|
|
1343
1604
|
theme={theme}
|
|
1344
1605
|
className={`carbon-chart-wrapper${chartToolbar ? ' has-magma-toolbar' : ''}`}
|
|
1345
1606
|
groupsLength={groupsLength < 6 ? groupsLength : 14}
|
|
1607
|
+
role="region"
|
|
1608
|
+
aria-label={ariaLabel || chartTitle}
|
|
1609
|
+
aria-roledescription="chart"
|
|
1346
1610
|
{...rest}
|
|
1347
1611
|
>
|
|
1348
1612
|
<ChartContentWrapper>
|
|
@@ -8,6 +8,7 @@ export interface ChartToolbarI18n {
|
|
|
8
8
|
downloadAsJpg: string;
|
|
9
9
|
downloadAsPng: string;
|
|
10
10
|
exitFullScreen: string;
|
|
11
|
+
legendInstructions: string;
|
|
11
12
|
makeFullScreen: string;
|
|
12
13
|
moreOptionsAriaLabel: string;
|
|
13
14
|
showAsTableTooltip: string;
|
|
@@ -20,6 +21,7 @@ const defaults: ChartToolbarI18n = {
|
|
|
20
21
|
downloadAsJpg: 'Download as JPG',
|
|
21
22
|
downloadAsPng: 'Download as PNG',
|
|
22
23
|
exitFullScreen: 'Exit full screen',
|
|
24
|
+
legendInstructions: 'Checking these checkboxes will update the chart.',
|
|
23
25
|
makeFullScreen: 'Make full screen',
|
|
24
26
|
moreOptionsAriaLabel: 'More options',
|
|
25
27
|
showAsTableTooltip: 'Show as table',
|
|
@@ -44,6 +46,8 @@ export function useChartToolbarI18n(): ChartToolbarI18n {
|
|
|
44
46
|
downloadAsJpg: toolbar.downloadAsJpg ?? defaults.downloadAsJpg,
|
|
45
47
|
downloadAsPng: toolbar.downloadAsPng ?? defaults.downloadAsPng,
|
|
46
48
|
exitFullScreen: toolbar.exitFullScreen ?? defaults.exitFullScreen,
|
|
49
|
+
legendInstructions:
|
|
50
|
+
toolbar.legendInstructions ?? defaults.legendInstructions,
|
|
47
51
|
makeFullScreen: toolbar.makeFullScreen ?? defaults.makeFullScreen,
|
|
48
52
|
moreOptionsAriaLabel:
|
|
49
53
|
toolbar.moreOptionsAriaLabel ?? defaults.moreOptionsAriaLabel,
|