@react-magma/charts 14.0.0 → 14.1.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 -14
- package/dist/charts.js.map +1 -1
- package/dist/charts.modern.module.js +203 -15
- package/dist/charts.modern.module.js.map +1 -1
- package/dist/charts.umd.js +383 -36
- package/dist/charts.umd.js.map +1 -1
- package/dist/components/CarbonChart/CarbonChart.d.ts +5 -0
- package/dist/components/ChartTable/chartToolbarI18n.d.ts +1 -0
- package/package.json +3 -3
- package/src/components/CarbonChart/CarbonChart.test.js +231 -4
- package/src/components/CarbonChart/CarbonChart.tsx +271 -7
- package/src/components/CarbonChart/CarbonChartBar.stories.tsx +9 -2
- package/src/components/ChartTable/chartToolbarI18n.ts +4 -0
|
@@ -56,6 +56,11 @@ export interface ChartToolbarConfig {
|
|
|
56
56
|
* @default 2
|
|
57
57
|
*/
|
|
58
58
|
tableHeaderLevel?: 1 | 2 | 3 | 4 | 5 | 6;
|
|
59
|
+
/**
|
|
60
|
+
* Heading level for the chart title.
|
|
61
|
+
* @default 2
|
|
62
|
+
*/
|
|
63
|
+
titleLevel?: 1 | 2 | 3 | 4 | 5 | 6;
|
|
59
64
|
}
|
|
60
65
|
export interface CarbonChartProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
61
66
|
dataSet: Array<object>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-magma/charts",
|
|
3
|
-
"version": "14.
|
|
3
|
+
"version": "14.1.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"identity-obj-proxy": "3.0.0",
|
|
51
51
|
"react": "18.3.1",
|
|
52
52
|
"react-dom": "18.3.1",
|
|
53
|
-
"react-magma-dom": "^5.
|
|
53
|
+
"react-magma-dom": "^5.1.0",
|
|
54
54
|
"react-magma-icons": "^3.2.5",
|
|
55
55
|
"rollup": "^4.52.4",
|
|
56
56
|
"rollup-plugin-postcss": "^4.0.2"
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"@emotion/styled": "^11.14.0",
|
|
61
61
|
"react": "^18.3.1",
|
|
62
62
|
"react-dom": "^18.3.1",
|
|
63
|
-
"react-magma-dom": "^5.0.0",
|
|
63
|
+
"react-magma-dom": "^5.0.0 || ^5.1.0-0",
|
|
64
64
|
"react-magma-icons": "^3.2.5"
|
|
65
65
|
},
|
|
66
66
|
"engines": {
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
|
|
3
3
|
import { act, render, screen, fireEvent } from '@testing-library/react';
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
ThemeContext,
|
|
6
|
+
magma,
|
|
7
|
+
DropdownMenuItem,
|
|
8
|
+
I18nContext,
|
|
9
|
+
defaultI18n,
|
|
10
|
+
} from 'react-magma-dom';
|
|
5
11
|
|
|
6
12
|
import { CarbonChart, CarbonChartType } from '.';
|
|
7
13
|
|
|
@@ -11,10 +17,14 @@ global.ResizeObserver = jest.fn().mockImplementation(() => ({
|
|
|
11
17
|
disconnect: jest.fn(),
|
|
12
18
|
}));
|
|
13
19
|
|
|
14
|
-
// Capture MutationObserver callbacks so we can trigger them manually
|
|
15
|
-
|
|
20
|
+
// Capture MutationObserver callbacks so we can trigger them manually.
|
|
21
|
+
// Multiple observers may be created per component; we collect all of them and
|
|
22
|
+
// call each one so that no observer is silently skipped.
|
|
23
|
+
let mutationObserverCallbacks = [];
|
|
24
|
+
const mutationObserverCallback = mutations =>
|
|
25
|
+
mutationObserverCallbacks.forEach(cb => cb(mutations));
|
|
16
26
|
global.MutationObserver = jest.fn().mockImplementation(callback => {
|
|
17
|
-
|
|
27
|
+
mutationObserverCallbacks.push(callback);
|
|
18
28
|
return {
|
|
19
29
|
observe: jest.fn(),
|
|
20
30
|
disconnect: jest.fn(),
|
|
@@ -597,6 +607,7 @@ describe('CarbonChart', () => {
|
|
|
597
607
|
let otherButton;
|
|
598
608
|
|
|
599
609
|
beforeEach(() => {
|
|
610
|
+
mutationObserverCallbacks = [];
|
|
600
611
|
jest.useFakeTimers();
|
|
601
612
|
jest.spyOn(window, 'requestAnimationFrame').mockImplementation(cb => {
|
|
602
613
|
cb(0);
|
|
@@ -842,6 +853,27 @@ describe('CarbonChart', () => {
|
|
|
842
853
|
).not.toBeInTheDocument();
|
|
843
854
|
});
|
|
844
855
|
|
|
856
|
+
it('should render the chart title as h2 by default', () => {
|
|
857
|
+
render(<CarbonChart {...toolbarProps} />);
|
|
858
|
+
|
|
859
|
+
expect(
|
|
860
|
+
screen.getByRole('heading', { level: 2, name: chartOptions.title })
|
|
861
|
+
).toBeInTheDocument();
|
|
862
|
+
});
|
|
863
|
+
|
|
864
|
+
it('should render the chart title at the level set by titleLevel', () => {
|
|
865
|
+
render(
|
|
866
|
+
<CarbonChart {...toolbarProps} chartToolbar={{ titleLevel: 3 }} />
|
|
867
|
+
);
|
|
868
|
+
|
|
869
|
+
expect(
|
|
870
|
+
screen.getByRole('heading', { level: 3, name: chartOptions.title })
|
|
871
|
+
).toBeInTheDocument();
|
|
872
|
+
expect(
|
|
873
|
+
screen.queryByRole('heading', { level: 2, name: chartOptions.title })
|
|
874
|
+
).not.toBeInTheDocument();
|
|
875
|
+
});
|
|
876
|
+
|
|
845
877
|
it('should always render the more options dropdown with built-in download items', () => {
|
|
846
878
|
render(<CarbonChart {...toolbarProps} />);
|
|
847
879
|
|
|
@@ -895,4 +927,199 @@ describe('CarbonChart', () => {
|
|
|
895
927
|
).toBeInTheDocument();
|
|
896
928
|
});
|
|
897
929
|
});
|
|
930
|
+
|
|
931
|
+
describe('Legend accessibility wrap', () => {
|
|
932
|
+
const testId = 'legend-a11y';
|
|
933
|
+
|
|
934
|
+
it('wraps the Carbon legend in a fieldset with a legend caption derived from the chart title', () => {
|
|
935
|
+
const { getByTestId } = render(
|
|
936
|
+
<CarbonChart
|
|
937
|
+
testId={testId}
|
|
938
|
+
dataSet={dataSet}
|
|
939
|
+
options={chartOptions}
|
|
940
|
+
type={CarbonChartType.bar}
|
|
941
|
+
/>
|
|
942
|
+
);
|
|
943
|
+
const wrapper = getByTestId(testId);
|
|
944
|
+
|
|
945
|
+
const fieldset = wrapper.querySelector('.cds--cc--legend-fieldset');
|
|
946
|
+
expect(fieldset).not.toBeNull();
|
|
947
|
+
expect(fieldset.tagName).toBe('FIELDSET');
|
|
948
|
+
expect(fieldset.querySelector('.cds--cc--legend')).not.toBeNull();
|
|
949
|
+
|
|
950
|
+
const caption = fieldset.querySelector('legend');
|
|
951
|
+
expect(caption).not.toBeNull();
|
|
952
|
+
expect(caption.textContent).toBe(
|
|
953
|
+
`${chartOptions.title}. Checking these checkboxes will update the chart.`
|
|
954
|
+
);
|
|
955
|
+
});
|
|
956
|
+
|
|
957
|
+
it('uses ariaLabel for the legend caption when provided', () => {
|
|
958
|
+
const ariaLabel = 'Quarterly sales chart';
|
|
959
|
+
const { getByTestId } = render(
|
|
960
|
+
<CarbonChart
|
|
961
|
+
testId={testId}
|
|
962
|
+
ariaLabel={ariaLabel}
|
|
963
|
+
dataSet={dataSet}
|
|
964
|
+
options={chartOptions}
|
|
965
|
+
type={CarbonChartType.bar}
|
|
966
|
+
/>
|
|
967
|
+
);
|
|
968
|
+
const wrapper = getByTestId(testId);
|
|
969
|
+
|
|
970
|
+
const caption = wrapper.querySelector(
|
|
971
|
+
'.cds--cc--legend-fieldset > legend'
|
|
972
|
+
);
|
|
973
|
+
expect(caption).not.toBeNull();
|
|
974
|
+
expect(caption.textContent).toBe(
|
|
975
|
+
`${ariaLabel}. Checking these checkboxes will update the chart.`
|
|
976
|
+
);
|
|
977
|
+
});
|
|
978
|
+
|
|
979
|
+
it('does not duplicate the fieldset when the mutation observer fires again with no DOM changes', () => {
|
|
980
|
+
const { getByTestId } = render(
|
|
981
|
+
<CarbonChart
|
|
982
|
+
testId={testId}
|
|
983
|
+
dataSet={dataSet}
|
|
984
|
+
options={chartOptions}
|
|
985
|
+
type={CarbonChartType.bar}
|
|
986
|
+
/>
|
|
987
|
+
);
|
|
988
|
+
const wrapper = getByTestId(testId);
|
|
989
|
+
|
|
990
|
+
const before = wrapper.querySelectorAll('.cds--cc--legend-fieldset');
|
|
991
|
+
expect(before).toHaveLength(1);
|
|
992
|
+
|
|
993
|
+
act(() => {
|
|
994
|
+
mutationObserverCallback?.();
|
|
995
|
+
mutationObserverCallback?.();
|
|
996
|
+
});
|
|
997
|
+
|
|
998
|
+
const after = wrapper.querySelectorAll('.cds--cc--legend-fieldset');
|
|
999
|
+
expect(after).toHaveLength(1);
|
|
1000
|
+
expect(after[0]).toBe(before[0]);
|
|
1001
|
+
});
|
|
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
|
+
|
|
1034
|
+
it('replaces Carbon\'s generic "Data groups" aria-label with semantic list roles on the legend and its items', () => {
|
|
1035
|
+
const { getByTestId } = render(
|
|
1036
|
+
<CarbonChart
|
|
1037
|
+
testId={testId}
|
|
1038
|
+
dataSet={dataSet}
|
|
1039
|
+
options={chartOptions}
|
|
1040
|
+
type={CarbonChartType.bar}
|
|
1041
|
+
/>
|
|
1042
|
+
);
|
|
1043
|
+
const wrapper = getByTestId(testId);
|
|
1044
|
+
|
|
1045
|
+
const legend = wrapper.querySelector('.cds--cc--legend');
|
|
1046
|
+
expect(legend).not.toBeNull();
|
|
1047
|
+
|
|
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');
|
|
1060
|
+
expect(items.length).toBeGreaterThan(0);
|
|
1061
|
+
items.forEach(item => {
|
|
1062
|
+
expect(item.getAttribute('role')).toBe('listitem');
|
|
1063
|
+
});
|
|
1064
|
+
});
|
|
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
|
+
|
|
1094
|
+
it('uses translated legend instructions from I18nContext', () => {
|
|
1095
|
+
const translatedInstructions =
|
|
1096
|
+
'Marquer ces cases mettra à jour le graphique.';
|
|
1097
|
+
const i18nValue = {
|
|
1098
|
+
...defaultI18n,
|
|
1099
|
+
charts: {
|
|
1100
|
+
...defaultI18n.charts,
|
|
1101
|
+
toolbar: { legendInstructions: translatedInstructions },
|
|
1102
|
+
},
|
|
1103
|
+
};
|
|
1104
|
+
|
|
1105
|
+
const { getByTestId } = render(
|
|
1106
|
+
<I18nContext.Provider value={i18nValue}>
|
|
1107
|
+
<CarbonChart
|
|
1108
|
+
testId={testId}
|
|
1109
|
+
dataSet={dataSet}
|
|
1110
|
+
options={chartOptions}
|
|
1111
|
+
type={CarbonChartType.bar}
|
|
1112
|
+
/>
|
|
1113
|
+
</I18nContext.Provider>
|
|
1114
|
+
);
|
|
1115
|
+
const wrapper = getByTestId(testId);
|
|
1116
|
+
|
|
1117
|
+
const caption = wrapper.querySelector(
|
|
1118
|
+
'.cds--cc--legend-fieldset > legend'
|
|
1119
|
+
);
|
|
1120
|
+
expect(caption.textContent).toBe(
|
|
1121
|
+
`${chartOptions.title}. ${translatedInstructions}`
|
|
1122
|
+
);
|
|
1123
|
+
});
|
|
1124
|
+
});
|
|
898
1125
|
});
|
|
@@ -24,11 +24,14 @@ import {
|
|
|
24
24
|
import styled from '@emotion/styled';
|
|
25
25
|
import { transparentize } from 'polished';
|
|
26
26
|
import {
|
|
27
|
+
Announce,
|
|
27
28
|
DropdownDivider,
|
|
28
29
|
DropdownMenuItem,
|
|
29
30
|
ThemeInterface,
|
|
30
31
|
ThemeContext,
|
|
32
|
+
useDeviceDetect,
|
|
31
33
|
useIsInverse,
|
|
34
|
+
VisuallyHidden,
|
|
32
35
|
} from 'react-magma-dom';
|
|
33
36
|
import {
|
|
34
37
|
FullscreenExitIcon,
|
|
@@ -38,6 +41,7 @@ import {
|
|
|
38
41
|
} from 'react-magma-icons';
|
|
39
42
|
|
|
40
43
|
import { useCarbonModalFocusManagement } from '../../hooks/useCarbonModalFocusManagement';
|
|
44
|
+
// @ts-ignore
|
|
41
45
|
import './carbon-charts.css';
|
|
42
46
|
import {
|
|
43
47
|
ChartFullscreenButton,
|
|
@@ -102,6 +106,11 @@ export interface ChartToolbarConfig {
|
|
|
102
106
|
* @default 2
|
|
103
107
|
*/
|
|
104
108
|
tableHeaderLevel?: 1 | 2 | 3 | 4 | 5 | 6;
|
|
109
|
+
/**
|
|
110
|
+
* Heading level for the chart title.
|
|
111
|
+
* @default 2
|
|
112
|
+
*/
|
|
113
|
+
titleLevel?: 1 | 2 | 3 | 4 | 5 | 6;
|
|
105
114
|
}
|
|
106
115
|
|
|
107
116
|
export interface CarbonChartProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
@@ -161,6 +170,23 @@ const CarbonChartWrapper = styled.div<{
|
|
|
161
170
|
groupsLength: number;
|
|
162
171
|
theme: ThemeInterface;
|
|
163
172
|
}>`
|
|
173
|
+
.cds--cc--legend-fieldset {
|
|
174
|
+
border: 0;
|
|
175
|
+
margin: 0;
|
|
176
|
+
min-inline-size: 0;
|
|
177
|
+
padding: 0;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
.cds--cc--legend-fieldset > legend {
|
|
181
|
+
clip: rect(1px, 1px, 1px, 1px);
|
|
182
|
+
height: 1px;
|
|
183
|
+
overflow: hidden;
|
|
184
|
+
position: absolute;
|
|
185
|
+
top: auto;
|
|
186
|
+
white-space: nowrap;
|
|
187
|
+
width: 1px;
|
|
188
|
+
}
|
|
189
|
+
|
|
164
190
|
&:fullscreen,
|
|
165
191
|
&:-webkit-full-screen {
|
|
166
192
|
background: ${props =>
|
|
@@ -1004,7 +1030,11 @@ function CarbonChartToolbar({
|
|
|
1004
1030
|
isInverse={isInverse}
|
|
1005
1031
|
theme={theme}
|
|
1006
1032
|
>
|
|
1007
|
-
<ChartTitle
|
|
1033
|
+
<ChartTitle
|
|
1034
|
+
as={`h${config.titleLevel ?? 2}` as keyof JSX.IntrinsicElements}
|
|
1035
|
+
isInverse={isInverse}
|
|
1036
|
+
theme={theme}
|
|
1037
|
+
>
|
|
1008
1038
|
{resolvedTitle}
|
|
1009
1039
|
</ChartTitle>
|
|
1010
1040
|
<ToolbarActions theme={theme}>
|
|
@@ -1057,7 +1087,12 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
|
|
|
1057
1087
|
|
|
1058
1088
|
const [isTableOpen, setIsTableOpen] = React.useState(false);
|
|
1059
1089
|
const [isFullscreen, setIsFullscreen] = React.useState(false);
|
|
1090
|
+
const [legendAnnouncement, setLegendAnnouncement] = React.useState('');
|
|
1091
|
+
const legendGroupAnnounceRef = React.useRef<HTMLDivElement | null>(null);
|
|
1060
1092
|
const lastTableTriggerRef = React.useRef<HTMLButtonElement | null>(null);
|
|
1093
|
+
const legendAnnouncedRef = React.useRef(false);
|
|
1094
|
+
const selectionSignatureRef = React.useRef<string | undefined>(undefined);
|
|
1095
|
+
const { isMacOS, isChrome } = useDeviceDetect();
|
|
1061
1096
|
|
|
1062
1097
|
const mergedRef = React.useCallback(
|
|
1063
1098
|
(node: HTMLDivElement | null) => {
|
|
@@ -1134,6 +1169,154 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
|
|
|
1134
1169
|
const chartTitle: string =
|
|
1135
1170
|
(options as any).title || toolbarI18n.defaultTitle;
|
|
1136
1171
|
|
|
1172
|
+
const legendLabel = `${ariaLabel || chartTitle}. ${toolbarI18n.legendInstructions}`;
|
|
1173
|
+
|
|
1174
|
+
React.useEffect(() => {
|
|
1175
|
+
const container = internalRef.current;
|
|
1176
|
+
|
|
1177
|
+
if (!container) return;
|
|
1178
|
+
|
|
1179
|
+
const applyListSemantics = (legend: Element) => {
|
|
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));
|
|
1211
|
+
};
|
|
1212
|
+
|
|
1213
|
+
const wrapLegend = () => {
|
|
1214
|
+
const legend = container.querySelector('.cds--cc--legend');
|
|
1215
|
+
|
|
1216
|
+
if (!legend) return;
|
|
1217
|
+
|
|
1218
|
+
applyListSemantics(legend);
|
|
1219
|
+
|
|
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
|
+
});
|
|
1231
|
+
|
|
1232
|
+
if (closestFieldset) {
|
|
1233
|
+
const existingCaption =
|
|
1234
|
+
closestFieldset.querySelector(':scope > legend');
|
|
1235
|
+
|
|
1236
|
+
if (existingCaption && existingCaption.textContent !== legendLabel) {
|
|
1237
|
+
existingCaption.textContent = legendLabel;
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
return;
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
const fieldset = document.createElement('fieldset');
|
|
1244
|
+
const caption = document.createElement('legend');
|
|
1245
|
+
|
|
1246
|
+
fieldset.className = 'cds--cc--legend-fieldset';
|
|
1247
|
+
caption.textContent = legendLabel;
|
|
1248
|
+
legend.before(fieldset);
|
|
1249
|
+
fieldset.append(caption, legend);
|
|
1250
|
+
};
|
|
1251
|
+
|
|
1252
|
+
wrapLegend();
|
|
1253
|
+
const observer = new MutationObserver(wrapLegend);
|
|
1254
|
+
|
|
1255
|
+
observer.observe(container, {
|
|
1256
|
+
childList: true,
|
|
1257
|
+
subtree: true,
|
|
1258
|
+
attributes: true,
|
|
1259
|
+
attributeFilter: ['role', 'aria-label'],
|
|
1260
|
+
});
|
|
1261
|
+
|
|
1262
|
+
return () => observer.disconnect();
|
|
1263
|
+
}, [legendLabel]);
|
|
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
|
+
|
|
1137
1320
|
const handleModalDownloadCsv = React.useCallback(() => {
|
|
1138
1321
|
downloadCsv(dataSet as Array<Record<string, unknown>>, chartTitle);
|
|
1139
1322
|
}, [dataSet, chartTitle]);
|
|
@@ -1203,14 +1386,86 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
|
|
|
1203
1386
|
|
|
1204
1387
|
const ChartType = allCharts[type] as any;
|
|
1205
1388
|
|
|
1206
|
-
// Adding aria-label to main SVG container
|
|
1207
1389
|
React.useEffect(() => {
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1390
|
+
const timer = setTimeout(() => {
|
|
1391
|
+
if (internalRef.current) {
|
|
1392
|
+
internalRef.current
|
|
1393
|
+
.querySelectorAll<SVGGElement>('g[aria-label]')
|
|
1394
|
+
.forEach(g => {
|
|
1395
|
+
const role = g.getAttribute('role');
|
|
1396
|
+
|
|
1397
|
+
if (!role) {
|
|
1398
|
+
g.setAttribute('role', 'img');
|
|
1399
|
+
} else if (role === 'graphics-object group') {
|
|
1400
|
+
g.setAttribute('role', 'graphics-object');
|
|
1401
|
+
}
|
|
1402
|
+
});
|
|
1403
|
+
}
|
|
1404
|
+
}, 0);
|
|
1405
|
+
|
|
1406
|
+
return () => clearTimeout(timer);
|
|
1407
|
+
}, [type, dataSet]);
|
|
1408
|
+
|
|
1409
|
+
React.useEffect(() => {
|
|
1410
|
+
const wrapper = internalRef.current;
|
|
1411
|
+
|
|
1412
|
+
if (!wrapper) return;
|
|
1413
|
+
|
|
1414
|
+
const observer = new MutationObserver(() => {
|
|
1415
|
+
const allItems = wrapper.querySelectorAll<HTMLElement>(
|
|
1416
|
+
'.legend-item:not(.additional)'
|
|
1417
|
+
);
|
|
1418
|
+
|
|
1419
|
+
if (allItems.length <= 1) return;
|
|
1420
|
+
|
|
1421
|
+
const activeLabels: string[] = [];
|
|
1422
|
+
|
|
1423
|
+
allItems.forEach(el => {
|
|
1424
|
+
const checkbox = el.querySelector<HTMLElement>('.checkbox');
|
|
1425
|
+
const label = el.querySelector('p');
|
|
1426
|
+
|
|
1427
|
+
if (checkbox?.getAttribute('aria-checked') === 'true' && label) {
|
|
1428
|
+
activeLabels.push(label.textContent?.trim() || '');
|
|
1429
|
+
}
|
|
1211
1430
|
});
|
|
1212
|
-
|
|
1213
|
-
|
|
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
|
+
|
|
1446
|
+
if (activeLabels.length === 1) {
|
|
1447
|
+
if (!legendAnnouncedRef.current) {
|
|
1448
|
+
legendAnnouncedRef.current = true;
|
|
1449
|
+
setLegendAnnouncement(`Only ${activeLabels[0]} is selected`);
|
|
1450
|
+
}
|
|
1451
|
+
} else if (activeLabels.length === allItems.length) {
|
|
1452
|
+
legendAnnouncedRef.current = false;
|
|
1453
|
+
setLegendAnnouncement('All items selected');
|
|
1454
|
+
} else {
|
|
1455
|
+
legendAnnouncedRef.current = false;
|
|
1456
|
+
setLegendAnnouncement('');
|
|
1457
|
+
}
|
|
1458
|
+
});
|
|
1459
|
+
|
|
1460
|
+
observer.observe(wrapper, {
|
|
1461
|
+
childList: true,
|
|
1462
|
+
subtree: true,
|
|
1463
|
+
attributes: true,
|
|
1464
|
+
attributeFilter: ['aria-checked'],
|
|
1465
|
+
});
|
|
1466
|
+
|
|
1467
|
+
return () => observer.disconnect();
|
|
1468
|
+
}, [type, dataSet]);
|
|
1214
1469
|
|
|
1215
1470
|
const groupsLength = Object.keys(buildColors()).length;
|
|
1216
1471
|
|
|
@@ -1218,12 +1473,21 @@ export const CarbonChart = React.forwardRef<HTMLDivElement, CarbonChartProps>(
|
|
|
1218
1473
|
|
|
1219
1474
|
return (
|
|
1220
1475
|
<FullscreenRoot ref={mergedRef} isInverse={isInverse} theme={theme}>
|
|
1476
|
+
<VisuallyHidden>
|
|
1477
|
+
<Announce>{legendAnnouncement}</Announce>
|
|
1478
|
+
</VisuallyHidden>
|
|
1479
|
+
<VisuallyHidden>
|
|
1480
|
+
<div aria-live="polite" ref={legendGroupAnnounceRef} />
|
|
1481
|
+
</VisuallyHidden>
|
|
1221
1482
|
<CarbonChartWrapper
|
|
1222
1483
|
data-testid={testId}
|
|
1223
1484
|
isInverse={isInverse}
|
|
1224
1485
|
theme={theme}
|
|
1225
1486
|
className={`carbon-chart-wrapper${chartToolbar ? ' has-magma-toolbar' : ''}`}
|
|
1226
1487
|
groupsLength={groupsLength < 6 ? groupsLength : 14}
|
|
1488
|
+
role="region"
|
|
1489
|
+
aria-label={ariaLabel || chartTitle}
|
|
1490
|
+
aria-roledescription="chart"
|
|
1227
1491
|
{...rest}
|
|
1228
1492
|
>
|
|
1229
1493
|
<ChartContentWrapper>
|
|
@@ -18,12 +18,19 @@ export default {
|
|
|
18
18
|
control: { type: 'select' },
|
|
19
19
|
options: Object.values(CarbonChartType),
|
|
20
20
|
},
|
|
21
|
+
titleLevel: {
|
|
22
|
+
control: { type: 'select' },
|
|
23
|
+
options: [1, 2, 3, 4, 5, 6],
|
|
24
|
+
description: 'Heading level for the chart title (h1–h6).',
|
|
25
|
+
},
|
|
21
26
|
},
|
|
22
27
|
} as Meta;
|
|
23
28
|
|
|
24
|
-
const Template: StoryFn<
|
|
29
|
+
const Template: StoryFn<
|
|
30
|
+
CarbonChartProps & { titleLevel?: 1 | 2 | 3 | 4 | 5 | 6 }
|
|
31
|
+
> = ({ titleLevel, ...args }) => (
|
|
25
32
|
<Card isInverse={args.isInverse} style={{ padding: '12px' }}>
|
|
26
|
-
<CarbonChart {...args} />
|
|
33
|
+
<CarbonChart {...args} chartToolbar={{ titleLevel }} />
|
|
27
34
|
</Card>
|
|
28
35
|
);
|
|
29
36
|
|