@react-magma/charts 14.1.1-rc.0 → 14.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.
Files changed (34) hide show
  1. package/dist/charts.js +220 -187
  2. package/dist/charts.js.map +1 -1
  3. package/dist/charts.modern.module.js +221 -188
  4. package/dist/charts.modern.module.js.map +1 -1
  5. package/dist/charts.umd.js +648 -376
  6. package/dist/charts.umd.js.map +1 -1
  7. package/dist/components/CarbonChart/CarbonChart.d.ts +25 -0
  8. package/package.json +3 -3
  9. package/src/components/CarbonChart/CarbonChart.test.js +423 -0
  10. package/src/components/CarbonChart/CarbonChart.tsx +381 -246
  11. package/src/components/CarbonChart/CarbonChartArea.stories.tsx +1 -1
  12. package/src/components/CarbonChart/CarbonChartAreaStacked.stories.tsx +1 -1
  13. package/src/components/CarbonChart/CarbonChartBarFloating.stories.tsx +1 -1
  14. package/src/components/CarbonChart/CarbonChartBarGrouped.stories.tsx +1 -1
  15. package/src/components/CarbonChart/CarbonChartBarStacked.stories.tsx +1 -1
  16. package/src/components/CarbonChart/CarbonChartBoxplot.stories.tsx +1 -1
  17. package/src/components/CarbonChart/CarbonChartBubble.stories.tsx +1 -1
  18. package/src/components/CarbonChart/CarbonChartBullet.stories.tsx +1 -1
  19. package/src/components/CarbonChart/CarbonChartCombo.stories.tsx +1 -1
  20. package/src/components/CarbonChart/CarbonChartDonut.stories.tsx +3 -1
  21. package/src/components/CarbonChart/CarbonChartGauge.stories.tsx +1 -1
  22. package/src/components/CarbonChart/CarbonChartHistogram.stories.tsx +1 -1
  23. package/src/components/CarbonChart/CarbonChartLine.stories.tsx +1 -1
  24. package/src/components/CarbonChart/CarbonChartLollipop.stories.tsx +1 -1
  25. package/src/components/CarbonChart/CarbonChartMeter.stories.tsx +1 -1
  26. package/src/components/CarbonChart/CarbonChartPie.stories.tsx +1 -1
  27. package/src/components/CarbonChart/CarbonChartRadar.stories.tsx +1 -1
  28. package/src/components/CarbonChart/CarbonChartScatter.stories.tsx +1 -1
  29. package/src/components/CarbonChart/CarbonChartSparkline.stories.tsx +1 -1
  30. package/src/components/CarbonChart/CarbonChartStep.stories.tsx +1 -1
  31. package/src/components/CarbonChart/carbon-charts.css +89 -87
  32. package/src/components/ChartTable/ChartMoreOptionsButton.tsx +1 -0
  33. package/src/components/ChartTable/ChartTable.stories.tsx +140 -1
  34. package/src/components/ChartTable/ChartTable.test.js +452 -0
@@ -61,6 +61,21 @@ export interface ChartToolbarConfig {
61
61
  * @default 2
62
62
  */
63
63
  titleLevel?: 1 | 2 | 3 | 4 | 5 | 6;
64
+ /**
65
+ * Custom content rendered before the chart title.
66
+ *
67
+ * The content is placed inline in the title row and follows the same
68
+ * accessibility rules as `titleSuffix`.
69
+ */
70
+ titlePrefix?: React.ReactNode;
71
+ /**
72
+ * Custom content rendered after the chart title and before toolbar actions.
73
+ *
74
+ * The content is placed inline in the title row and renders outside the
75
+ * heading element to preserve `options.title` as the heading's accessible name.
76
+ * Useful for patterns like Tooltip + IconButton.
77
+ */
78
+ titleSuffix?: React.ReactNode;
64
79
  }
65
80
  export interface CarbonChartProps extends React.HTMLAttributes<HTMLDivElement> {
66
81
  dataSet: Array<object>;
@@ -88,5 +103,15 @@ export interface CarbonChartProps extends React.HTMLAttributes<HTMLDivElement> {
88
103
  * toolbar is automatically disabled.
89
104
  */
90
105
  chartToolbar?: ChartToolbarConfig;
106
+ /**
107
+ * Custom content rendered below the toolbar row and above the chart itself,
108
+ * for example a filter. Requires `chartToolbar`; ignored without it.
109
+ *
110
+ * Providing this lays the toolbar out in normal flow instead of overlaying
111
+ * the single row Carbon reserves for its title, so the rendered height
112
+ * becomes toolbar + content + chart rather than the chart box alone. Set
113
+ * `options.height` so the chart still has a definite height of its own.
114
+ */
115
+ additionalContent?: React.ReactNode;
91
116
  }
92
117
  export declare const CarbonChart: React.ForwardRefExoticComponent<CarbonChartProps & React.RefAttributes<HTMLDivElement>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-magma/charts",
3
- "version": "14.1.1-rc.0",
3
+ "version": "14.2.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.1.1-rc.0",
53
+ "react-magma-dom": "^5.2.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.1.1-rc.0",
63
+ "react-magma-dom": "^5.0.0",
64
64
  "react-magma-icons": "^3.2.5"
65
65
  },
66
66
  "engines": {
@@ -1,12 +1,15 @@
1
1
  import React from 'react';
2
2
 
3
+ import { SimpleBarChart } from '@carbon/charts-react';
3
4
  import { act, render, screen, fireEvent } from '@testing-library/react';
5
+ import { axe } from 'jest-axe';
4
6
  import {
5
7
  ThemeContext,
6
8
  magma,
7
9
  DropdownMenuItem,
8
10
  I18nContext,
9
11
  defaultI18n,
12
+ useIsInverse,
10
13
  } from 'react-magma-dom';
11
14
 
12
15
  import { CarbonChart, CarbonChartType } from '.';
@@ -187,6 +190,27 @@ describe('CarbonChart', () => {
187
190
  );
188
191
  });
189
192
 
193
+ it("pins Carbon's export `.filled` background to the inverse surface color", () => {
194
+ const testId = 'filled-bg-inverse';
195
+ const { getByTestId } = render(
196
+ <ThemeContext.Provider value={magma}>
197
+ <CarbonChart
198
+ testId={testId}
199
+ dataSet={dataSet}
200
+ options={chartOptions}
201
+ type={CarbonChartType.bar}
202
+ isInverse
203
+ />
204
+ </ThemeContext.Provider>
205
+ );
206
+
207
+ expect(getByTestId(testId)).toHaveStyleRule(
208
+ 'background-color',
209
+ magma.colors.primary600,
210
+ { target: '.cds--chart-holder.filled' }
211
+ );
212
+ });
213
+
190
214
  it('should apply theme.colors.neutral100 for inverse text color', () => {
191
215
  const testId = 'text-inverse-test';
192
216
  const { getByTestId } = render(
@@ -928,6 +952,268 @@ describe('CarbonChart', () => {
928
952
  });
929
953
  });
930
954
 
955
+ describe('image export', () => {
956
+ const toolbarProps = {
957
+ dataSet,
958
+ options: chartOptions,
959
+ type: CarbonChartType.bar,
960
+ chartToolbar: {},
961
+ };
962
+
963
+ let exportToPNG;
964
+ let exportToJPG;
965
+ let createChartSpy;
966
+
967
+ beforeEach(() => {
968
+ exportToPNG = jest.fn();
969
+ exportToJPG = jest.fn();
970
+ createChartSpy = jest
971
+ .spyOn(SimpleBarChart.prototype, 'createChart')
972
+ .mockImplementation(() => ({
973
+ services: { domUtils: { exportToPNG, exportToJPG } },
974
+ }));
975
+ });
976
+
977
+ afterEach(() => {
978
+ createChartSpy.mockRestore();
979
+ });
980
+
981
+ it("runs Carbon's own PNG export from the More options menu", () => {
982
+ render(<CarbonChart {...toolbarProps} />);
983
+
984
+ fireEvent.click(screen.getByRole('button', { name: 'More options' }));
985
+ fireEvent.click(screen.getByText('Download as PNG'));
986
+
987
+ expect(exportToPNG).toHaveBeenCalledTimes(1);
988
+ expect(exportToJPG).not.toHaveBeenCalled();
989
+ });
990
+
991
+ it("runs Carbon's own JPG export from the More options menu", () => {
992
+ render(<CarbonChart {...toolbarProps} />);
993
+
994
+ fireEvent.click(screen.getByRole('button', { name: 'More options' }));
995
+ fireEvent.click(screen.getByText('Download as JPG'));
996
+
997
+ expect(exportToJPG).toHaveBeenCalledTimes(1);
998
+ expect(exportToPNG).not.toHaveBeenCalled();
999
+ });
1000
+
1001
+ it("keeps Carbon's own title alive and uses it as the export file name", () => {
1002
+ render(<CarbonChart {...toolbarProps} />);
1003
+
1004
+ const options = createChartSpy.mock.calls[0][2];
1005
+ expect(options.title).toBe(chartOptions.title);
1006
+ expect(options.fileDownload.fileName).toBe(chartOptions.title);
1007
+ });
1008
+
1009
+ it("reveals Carbon's hidden title for the capture and restores it afterwards", () => {
1010
+ jest.useFakeTimers();
1011
+ try {
1012
+ const { container } = render(<CarbonChart {...toolbarProps} />);
1013
+ const wrapper = container.querySelector('.carbon-chart-wrapper');
1014
+
1015
+ expect(wrapper).toHaveClass('has-magma-toolbar');
1016
+
1017
+ fireEvent.click(screen.getByRole('button', { name: 'More options' }));
1018
+ fireEvent.click(screen.getByText('Download as PNG'));
1019
+
1020
+ expect(wrapper).not.toHaveClass('has-magma-toolbar');
1021
+
1022
+ jest.runOnlyPendingTimers();
1023
+ expect(wrapper).toHaveClass('has-magma-toolbar');
1024
+ } finally {
1025
+ jest.useRealTimers();
1026
+ }
1027
+ });
1028
+
1029
+ it('reveals the title for the flow layout (additionalContent) export too', () => {
1030
+ jest.useFakeTimers();
1031
+ try {
1032
+ const { container } = render(
1033
+ <CarbonChart
1034
+ {...toolbarProps}
1035
+ additionalContent={<div>Filter</div>}
1036
+ />
1037
+ );
1038
+ const wrapper = container.querySelector('.carbon-chart-wrapper');
1039
+
1040
+ expect(createChartSpy.mock.calls[0][2].title).toBe(chartOptions.title);
1041
+ expect(wrapper).toHaveClass('has-magma-toolbar');
1042
+
1043
+ fireEvent.click(screen.getByRole('button', { name: 'More options' }));
1044
+ fireEvent.click(screen.getByText('Download as JPG'));
1045
+
1046
+ expect(exportToJPG).toHaveBeenCalledTimes(1);
1047
+ expect(wrapper).not.toHaveClass('has-magma-toolbar');
1048
+
1049
+ jest.runOnlyPendingTimers();
1050
+ expect(wrapper).toHaveClass('has-magma-toolbar');
1051
+ } finally {
1052
+ jest.useRealTimers();
1053
+ }
1054
+ });
1055
+ });
1056
+
1057
+ describe('custom content slots', () => {
1058
+ const toolbarProps = {
1059
+ dataSet,
1060
+ options: chartOptions,
1061
+ type: CarbonChartType.bar,
1062
+ chartToolbar: {},
1063
+ };
1064
+
1065
+ it('should render titlePrefix and titleSuffix around the title', () => {
1066
+ render(
1067
+ <CarbonChart
1068
+ {...toolbarProps}
1069
+ chartToolbar={{
1070
+ titlePrefix: <span data-testid="before">Badge</span>,
1071
+ titleSuffix: <button type="button">About this chart</button>,
1072
+ }}
1073
+ />
1074
+ );
1075
+
1076
+ const before = screen.getByTestId('before');
1077
+ const heading = screen.getByRole('heading', {
1078
+ level: 2,
1079
+ name: chartOptions.title,
1080
+ });
1081
+ const after = screen.getByRole('button', { name: 'About this chart' });
1082
+
1083
+ expect(
1084
+ before.compareDocumentPosition(heading) &
1085
+ Node.DOCUMENT_POSITION_FOLLOWING
1086
+ ).toBeTruthy();
1087
+ expect(
1088
+ heading.compareDocumentPosition(after) &
1089
+ Node.DOCUMENT_POSITION_FOLLOWING
1090
+ ).toBeTruthy();
1091
+ });
1092
+
1093
+ it('should place title slots before the toolbar actions in focus order', () => {
1094
+ render(
1095
+ <CarbonChart
1096
+ {...toolbarProps}
1097
+ chartToolbar={{
1098
+ titleSuffix: <button type="button">About this chart</button>,
1099
+ }}
1100
+ />
1101
+ );
1102
+
1103
+ const after = screen.getByRole('button', { name: 'About this chart' });
1104
+ const tableButton = screen.getByRole('button', {
1105
+ name: chartOptions.title,
1106
+ });
1107
+
1108
+ expect(
1109
+ after.compareDocumentPosition(tableButton) &
1110
+ Node.DOCUMENT_POSITION_FOLLOWING
1111
+ ).toBeTruthy();
1112
+ });
1113
+
1114
+ it('should keep the title as the accessible name when titleSuffix is used', () => {
1115
+ render(
1116
+ <CarbonChart
1117
+ {...toolbarProps}
1118
+ chartToolbar={{
1119
+ titleSuffix: <button type="button">About this chart</button>,
1120
+ }}
1121
+ />
1122
+ );
1123
+
1124
+ // The heading, the chart region and the table modal must all stay
1125
+ // anchored to options.title, which is also the exported file name.
1126
+ expect(
1127
+ screen.getByRole('heading', { level: 2, name: chartOptions.title })
1128
+ ).toBeInTheDocument();
1129
+ expect(
1130
+ screen.getByRole('region', { name: chartOptions.title })
1131
+ ).toBeInTheDocument();
1132
+
1133
+ fireEvent.click(screen.getByRole('button', { name: chartOptions.title }));
1134
+
1135
+ expect(
1136
+ screen.getByRole('heading', {
1137
+ level: 2,
1138
+ name: `Tabular representation ${chartOptions.title}`,
1139
+ })
1140
+ ).toBeInTheDocument();
1141
+ });
1142
+
1143
+ it('should render no slot markup by default', () => {
1144
+ render(<CarbonChart {...toolbarProps} />);
1145
+
1146
+ expect(screen.queryByTestId('before')).not.toBeInTheDocument();
1147
+ expect(
1148
+ screen.getByRole('heading', { level: 2, name: chartOptions.title })
1149
+ ).toBeInTheDocument();
1150
+ });
1151
+
1152
+ it('should expose the resolved inverse value to slot content', () => {
1153
+ // Slot content is the adopter's, so it reads isInverse from context
1154
+ // rather than from our prop.
1155
+ function InverseProbe() {
1156
+ return <span>{useIsInverse() ? 'inverse' : 'default'}</span>;
1157
+ }
1158
+
1159
+ render(
1160
+ <CarbonChart
1161
+ {...toolbarProps}
1162
+ isInverse
1163
+ chartToolbar={{
1164
+ titlePrefix: <InverseProbe />,
1165
+ titleSuffix: <InverseProbe />,
1166
+ }}
1167
+ />
1168
+ );
1169
+
1170
+ expect(screen.getAllByText('inverse')).toHaveLength(2);
1171
+ });
1172
+
1173
+ it('should not introduce accessibility violations with both slots', async () => {
1174
+ const { container } = render(
1175
+ <CarbonChart
1176
+ {...toolbarProps}
1177
+ chartToolbar={{
1178
+ titlePrefix: <span>Badge</span>,
1179
+ titleSuffix: <button type="button">About this chart</button>,
1180
+ }}
1181
+ />
1182
+ );
1183
+
1184
+ // aria-prohibited-attr and aria-tooltip-name come from Carbon's own SVG
1185
+ // output and are present on a chart with no slots at all, so they are
1186
+ // excluded here rather than being attributed to the slots.
1187
+ const results = await axe(container, {
1188
+ rules: {
1189
+ 'aria-prohibited-attr': { enabled: false },
1190
+ 'aria-tooltip-name': { enabled: false },
1191
+ },
1192
+ });
1193
+
1194
+ expect(results).toHaveNoViolations();
1195
+ });
1196
+
1197
+ it('keeps a single semantic heading and no a11y violations in the flow layout', async () => {
1198
+ const { container } = render(
1199
+ <CarbonChart {...toolbarProps} additionalContent={<div>Filter</div>} />
1200
+ );
1201
+
1202
+ expect(
1203
+ screen.getAllByRole('heading', { name: chartOptions.title })
1204
+ ).toHaveLength(1);
1205
+
1206
+ const results = await axe(container, {
1207
+ rules: {
1208
+ 'aria-prohibited-attr': { enabled: false },
1209
+ 'aria-tooltip-name': { enabled: false },
1210
+ },
1211
+ });
1212
+
1213
+ expect(results).toHaveNoViolations();
1214
+ });
1215
+ });
1216
+
931
1217
  describe('Legend accessibility wrap', () => {
932
1218
  const testId = 'legend-a11y';
933
1219
 
@@ -1122,4 +1408,141 @@ describe('CarbonChart', () => {
1122
1408
  );
1123
1409
  });
1124
1410
  });
1411
+
1412
+ describe('dot keyboard accessibility', () => {
1413
+ let rafCallbacks;
1414
+
1415
+ beforeEach(() => {
1416
+ rafCallbacks = [];
1417
+ jest.spyOn(window, 'requestAnimationFrame').mockImplementation(cb => {
1418
+ rafCallbacks.push(cb);
1419
+ return rafCallbacks.length - 1;
1420
+ });
1421
+ });
1422
+
1423
+ afterEach(() => {
1424
+ window.requestAnimationFrame.mockRestore();
1425
+ rafCallbacks = [];
1426
+ });
1427
+
1428
+ function renderChart() {
1429
+ const { getByTestId } = render(
1430
+ <CarbonChart
1431
+ testId="dot-tab-test"
1432
+ dataSet={dataSet}
1433
+ options={chartOptions}
1434
+ type={CarbonChartType.scatter}
1435
+ />
1436
+ );
1437
+ return getByTestId('dot-tab-test');
1438
+ }
1439
+
1440
+ function addDot(wrapper) {
1441
+ const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
1442
+ const dot = document.createElementNS(
1443
+ 'http://www.w3.org/2000/svg',
1444
+ 'circle'
1445
+ );
1446
+ dot.classList.add('dot');
1447
+ svg.appendChild(dot);
1448
+ wrapper.appendChild(svg);
1449
+ return dot;
1450
+ }
1451
+
1452
+ describe('tabbing', () => {
1453
+ it('should stamp tabindex="0" on circle.dot elements so they are reachable by Tab', () => {
1454
+ const wrapper = renderChart();
1455
+ const dot = addDot(wrapper);
1456
+
1457
+ // The last captured RAF is the dots-stamping callback (no ariaLabel provided)
1458
+ act(() => rafCallbacks[rafCallbacks.length - 1](0));
1459
+
1460
+ expect(dot).toHaveAttribute('tabindex', '0');
1461
+ });
1462
+
1463
+ it('should not overwrite an existing tabindex on circle.dot', () => {
1464
+ const wrapper = renderChart();
1465
+ const dot = addDot(wrapper);
1466
+ dot.setAttribute('tabindex', '-1');
1467
+
1468
+ act(() => rafCallbacks[rafCallbacks.length - 1](0));
1469
+
1470
+ expect(dot).toHaveAttribute('tabindex', '-1');
1471
+ });
1472
+ });
1473
+
1474
+ describe('focus on dot content', () => {
1475
+ it('should set opacity to 1 when a dot receives focus', () => {
1476
+ const wrapper = renderChart();
1477
+ const dot = addDot(wrapper);
1478
+
1479
+ fireEvent.focusIn(dot);
1480
+
1481
+ expect(dot.style.opacity).toBe('1');
1482
+ });
1483
+
1484
+ it('should dispatch mouseover on dot focusin to reveal tooltip data', () => {
1485
+ const wrapper = renderChart();
1486
+ const dot = addDot(wrapper);
1487
+
1488
+ const mouseoverSpy = jest.fn();
1489
+ dot.addEventListener('mouseover', mouseoverSpy);
1490
+
1491
+ fireEvent.focusIn(dot);
1492
+
1493
+ expect(mouseoverSpy).toHaveBeenCalledTimes(1);
1494
+ });
1495
+
1496
+ it('should dispatch mousemove on dot focusin', () => {
1497
+ const wrapper = renderChart();
1498
+ const dot = addDot(wrapper);
1499
+
1500
+ const mousemoveSpy = jest.fn();
1501
+ dot.addEventListener('mousemove', mousemoveSpy);
1502
+
1503
+ fireEvent.focusIn(dot);
1504
+
1505
+ expect(mousemoveSpy).toHaveBeenCalledTimes(1);
1506
+ });
1507
+
1508
+ it('should not change opacity for non-dot circle elements on focusin', () => {
1509
+ const wrapper = renderChart();
1510
+ const nonDot = document.createElementNS(
1511
+ 'http://www.w3.org/2000/svg',
1512
+ 'circle'
1513
+ );
1514
+ wrapper.appendChild(nonDot);
1515
+
1516
+ fireEvent.focusIn(nonDot);
1517
+
1518
+ expect(nonDot.style.opacity).toBe('');
1519
+ });
1520
+ });
1521
+
1522
+ describe('data visibility', () => {
1523
+ it('should reset dot opacity when dot loses focus', () => {
1524
+ const wrapper = renderChart();
1525
+ const dot = addDot(wrapper);
1526
+
1527
+ fireEvent.focusIn(dot);
1528
+ expect(dot.style.opacity).toBe('1');
1529
+
1530
+ fireEvent.focusOut(dot);
1531
+ expect(dot.style.opacity).toBe('');
1532
+ });
1533
+
1534
+ it('should dispatch mouseout on dot focusout to hide tooltip', () => {
1535
+ const wrapper = renderChart();
1536
+ const dot = addDot(wrapper);
1537
+
1538
+ const mouseoutSpy = jest.fn();
1539
+ dot.addEventListener('mouseout', mouseoutSpy);
1540
+
1541
+ fireEvent.focusIn(dot);
1542
+ fireEvent.focusOut(dot);
1543
+
1544
+ expect(mouseoutSpy).toHaveBeenCalledTimes(1);
1545
+ });
1546
+ });
1547
+ });
1125
1548
  });