@react-magma/charts 13.2.1-next.0 → 13.3.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.
@@ -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>>;
@@ -1,3 +1,4 @@
1
+ import React from 'react';
1
2
  import { CarbonChartProps, CarbonChartType } from '../CarbonChart';
2
3
  declare const _default: import("@storybook/csf").ComponentAnnotations<import("@storybook/react/types-6-0").ReactFramework, import("@storybook/react/types-6-0").Args>;
3
4
  export default _default;
@@ -114,3 +115,52 @@ export declare const TableOnly: {
114
115
  };
115
116
  };
116
117
  };
118
+ declare const slotContent: {
119
+ tooltip: React.JSX.Element;
120
+ badge: React.JSX.Element;
121
+ none: undefined;
122
+ };
123
+ interface TitleSlotsArgs extends Omit<CarbonChartProps, 'options'> {
124
+ content: keyof typeof slotContent;
125
+ slotPosition: 'titleSuffix' | 'titlePrefix';
126
+ title: string;
127
+ }
128
+ export declare const TitleSlots: {
129
+ render: ({ content, slotPosition, title, ...args }: TitleSlotsArgs) => React.JSX.Element;
130
+ argTypes: {
131
+ content: {
132
+ control: {
133
+ type: string;
134
+ };
135
+ options: string[];
136
+ };
137
+ slotPosition: {
138
+ control: {
139
+ type: string;
140
+ };
141
+ options: string[];
142
+ };
143
+ title: {
144
+ control: {
145
+ type: string;
146
+ };
147
+ };
148
+ };
149
+ args: {
150
+ content: string;
151
+ isInverse: boolean;
152
+ slotPosition: string;
153
+ title: string;
154
+ type: CarbonChartType;
155
+ dataSet: {
156
+ group: string;
157
+ value: number;
158
+ }[];
159
+ };
160
+ };
161
+ export declare const AdditionalContent: {
162
+ render: ({ isInverse }: Pick<CarbonChartProps, 'isInverse'>) => React.JSX.Element;
163
+ args: {
164
+ isInverse: boolean;
165
+ };
166
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-magma/charts",
3
- "version": "13.2.1-next.0",
3
+ "version": "13.3.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.15.1-next.0",
50
+ "react-magma-dom": "^4.16.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.15.1-next.0",
60
+ "react-magma-dom": "^4.16.0",
61
61
  "react-magma-icons": "^3.2.5"
62
62
  },
63
63
  "engines": {
@@ -1,13 +1,16 @@
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';
4
5
  import userEvent from '@testing-library/user-event';
6
+ import { axe } from 'jest-axe';
5
7
  import {
6
8
  ThemeContext,
7
9
  magma,
8
10
  DropdownMenuItem,
9
11
  I18nContext,
10
12
  defaultI18n,
13
+ useIsInverse,
11
14
  } from 'react-magma-dom';
12
15
 
13
16
  import { CarbonChart, CarbonChartType } from '.';
@@ -188,6 +191,27 @@ describe('CarbonChart', () => {
188
191
  );
189
192
  });
190
193
 
194
+ it("pins Carbon's export `.filled` background to the inverse surface color", () => {
195
+ const testId = 'filled-bg-inverse';
196
+ const { getByTestId } = render(
197
+ <ThemeContext.Provider value={magma}>
198
+ <CarbonChart
199
+ testId={testId}
200
+ dataSet={dataSet}
201
+ options={chartOptions}
202
+ type={CarbonChartType.bar}
203
+ isInverse
204
+ />
205
+ </ThemeContext.Provider>
206
+ );
207
+
208
+ expect(getByTestId(testId)).toHaveStyleRule(
209
+ 'background-color',
210
+ magma.colors.primary600,
211
+ { target: '.cds--chart-holder.filled' }
212
+ );
213
+ });
214
+
191
215
  it('should apply theme.colors.neutral100 for inverse text color', () => {
192
216
  const testId = 'text-inverse-test';
193
217
  const { getByTestId } = render(
@@ -955,6 +979,108 @@ describe('CarbonChart', () => {
955
979
  });
956
980
  });
957
981
 
982
+ describe('image export', () => {
983
+ const toolbarProps = {
984
+ dataSet,
985
+ options: chartOptions,
986
+ type: CarbonChartType.bar,
987
+ chartToolbar: {},
988
+ };
989
+
990
+ let exportToPNG;
991
+ let exportToJPG;
992
+ let createChartSpy;
993
+
994
+ beforeEach(() => {
995
+ exportToPNG = jest.fn();
996
+ exportToJPG = jest.fn();
997
+ createChartSpy = jest
998
+ .spyOn(SimpleBarChart.prototype, 'createChart')
999
+ .mockImplementation(() => ({
1000
+ services: { domUtils: { exportToPNG, exportToJPG } },
1001
+ }));
1002
+ });
1003
+
1004
+ afterEach(() => {
1005
+ createChartSpy.mockRestore();
1006
+ });
1007
+
1008
+ it("runs Carbon's own PNG export from the More options menu", () => {
1009
+ render(<CarbonChart {...toolbarProps} />);
1010
+
1011
+ fireEvent.click(screen.getByRole('button', { name: 'More options' }));
1012
+ fireEvent.click(screen.getByText('Download as PNG'));
1013
+
1014
+ expect(exportToPNG).toHaveBeenCalledTimes(1);
1015
+ expect(exportToJPG).not.toHaveBeenCalled();
1016
+ });
1017
+
1018
+ it("runs Carbon's own JPG export from the More options menu", () => {
1019
+ render(<CarbonChart {...toolbarProps} />);
1020
+
1021
+ fireEvent.click(screen.getByRole('button', { name: 'More options' }));
1022
+ fireEvent.click(screen.getByText('Download as JPG'));
1023
+
1024
+ expect(exportToJPG).toHaveBeenCalledTimes(1);
1025
+ expect(exportToPNG).not.toHaveBeenCalled();
1026
+ });
1027
+
1028
+ it("keeps Carbon's own title alive and uses it as the export file name", () => {
1029
+ render(<CarbonChart {...toolbarProps} />);
1030
+
1031
+ const options = createChartSpy.mock.calls[0][2];
1032
+ expect(options.title).toBe(chartOptions.title);
1033
+ expect(options.fileDownload.fileName).toBe(chartOptions.title);
1034
+ });
1035
+
1036
+ it("reveals Carbon's hidden title for the capture and restores it afterwards", () => {
1037
+ jest.useFakeTimers();
1038
+ try {
1039
+ const { container } = render(<CarbonChart {...toolbarProps} />);
1040
+ const wrapper = container.querySelector('.carbon-chart-wrapper');
1041
+
1042
+ expect(wrapper).toHaveClass('has-magma-toolbar');
1043
+
1044
+ fireEvent.click(screen.getByRole('button', { name: 'More options' }));
1045
+ fireEvent.click(screen.getByText('Download as PNG'));
1046
+
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
+ it('reveals the title for the flow layout (additionalContent) export too', () => {
1057
+ jest.useFakeTimers();
1058
+ try {
1059
+ const { container } = render(
1060
+ <CarbonChart
1061
+ {...toolbarProps}
1062
+ additionalContent={<div>Filter</div>}
1063
+ />
1064
+ );
1065
+ const wrapper = container.querySelector('.carbon-chart-wrapper');
1066
+
1067
+ expect(createChartSpy.mock.calls[0][2].title).toBe(chartOptions.title);
1068
+ expect(wrapper).toHaveClass('has-magma-toolbar');
1069
+
1070
+ fireEvent.click(screen.getByRole('button', { name: 'More options' }));
1071
+ fireEvent.click(screen.getByText('Download as JPG'));
1072
+
1073
+ expect(exportToJPG).toHaveBeenCalledTimes(1);
1074
+ expect(wrapper).not.toHaveClass('has-magma-toolbar');
1075
+
1076
+ jest.runOnlyPendingTimers();
1077
+ expect(wrapper).toHaveClass('has-magma-toolbar');
1078
+ } finally {
1079
+ jest.useRealTimers();
1080
+ }
1081
+ });
1082
+ });
1083
+
958
1084
  describe('dot keyboard accessibility', () => {
959
1085
  let rafCallbacks;
960
1086
 
@@ -1092,6 +1218,148 @@ describe('CarbonChart', () => {
1092
1218
  });
1093
1219
  });
1094
1220
 
1221
+ describe('custom content slots', () => {
1222
+ const toolbarProps = {
1223
+ dataSet,
1224
+ options: chartOptions,
1225
+ type: CarbonChartType.bar,
1226
+ chartToolbar: {},
1227
+ };
1228
+
1229
+ it('should render titlePrefix and titleSuffix around the title', () => {
1230
+ render(
1231
+ <CarbonChart
1232
+ {...toolbarProps}
1233
+ chartToolbar={{
1234
+ titlePrefix: <span data-testid="before">Badge</span>,
1235
+ titleSuffix: <button type="button">About this chart</button>,
1236
+ }}
1237
+ />
1238
+ );
1239
+
1240
+ const before = screen.getByTestId('before');
1241
+ const heading = screen.getByRole('heading', {
1242
+ level: 2,
1243
+ name: chartOptions.title,
1244
+ });
1245
+ const after = screen.getByRole('button', { name: 'About this chart' });
1246
+
1247
+ expect(
1248
+ before.compareDocumentPosition(heading) &
1249
+ Node.DOCUMENT_POSITION_FOLLOWING
1250
+ ).toBeTruthy();
1251
+ expect(
1252
+ heading.compareDocumentPosition(after) &
1253
+ Node.DOCUMENT_POSITION_FOLLOWING
1254
+ ).toBeTruthy();
1255
+ });
1256
+
1257
+ it('should place title slots before the toolbar actions in focus order', () => {
1258
+ render(
1259
+ <CarbonChart
1260
+ {...toolbarProps}
1261
+ chartToolbar={{
1262
+ titleSuffix: <button type="button">About this chart</button>,
1263
+ }}
1264
+ />
1265
+ );
1266
+
1267
+ const after = screen.getByRole('button', { name: 'About this chart' });
1268
+ const tableButton = screen.getByRole('button', {
1269
+ name: chartOptions.title,
1270
+ });
1271
+
1272
+ expect(
1273
+ after.compareDocumentPosition(tableButton) &
1274
+ Node.DOCUMENT_POSITION_FOLLOWING
1275
+ ).toBeTruthy();
1276
+ });
1277
+
1278
+ it('should keep the title as the accessible name when titleSuffix is used', () => {
1279
+ render(
1280
+ <CarbonChart
1281
+ {...toolbarProps}
1282
+ chartToolbar={{
1283
+ titleSuffix: <button type="button">About this chart</button>,
1284
+ }}
1285
+ />
1286
+ );
1287
+
1288
+ // The heading, the chart region and the table modal must all stay
1289
+ // anchored to options.title, which is also the exported file name.
1290
+ expect(
1291
+ screen.getByRole('heading', { level: 2, name: chartOptions.title })
1292
+ ).toBeInTheDocument();
1293
+ expect(
1294
+ screen.getByRole('region', { name: chartOptions.title })
1295
+ ).toBeInTheDocument();
1296
+
1297
+ fireEvent.click(screen.getByRole('button', { name: chartOptions.title }));
1298
+
1299
+ expect(
1300
+ screen.getByRole('heading', {
1301
+ level: 2,
1302
+ name: `Tabular representation ${chartOptions.title}`,
1303
+ })
1304
+ ).toBeInTheDocument();
1305
+ });
1306
+
1307
+ it('should render no slot markup by default', () => {
1308
+ render(<CarbonChart {...toolbarProps} />);
1309
+
1310
+ expect(screen.queryByTestId('before')).not.toBeInTheDocument();
1311
+ expect(
1312
+ screen.getByRole('heading', { level: 2, name: chartOptions.title })
1313
+ ).toBeInTheDocument();
1314
+ });
1315
+
1316
+ it('should expose the resolved inverse value to slot content', () => {
1317
+ // Slot content is the adopter's, so it reads isInverse from context
1318
+ // rather than from our prop.
1319
+ function InverseProbe() {
1320
+ return <span>{useIsInverse() ? 'inverse' : 'default'}</span>;
1321
+ }
1322
+
1323
+ render(
1324
+ <CarbonChart
1325
+ {...toolbarProps}
1326
+ isInverse
1327
+ chartToolbar={{
1328
+ titlePrefix: <InverseProbe />,
1329
+ titleSuffix: <InverseProbe />,
1330
+ }}
1331
+ />
1332
+ );
1333
+
1334
+ expect(screen.getAllByText('inverse')).toHaveLength(2);
1335
+ });
1336
+
1337
+ it('should not introduce accessibility violations with both slots', async () => {
1338
+ const { container } = render(
1339
+ <CarbonChart
1340
+ {...toolbarProps}
1341
+ chartToolbar={{
1342
+ titlePrefix: <span>Badge</span>,
1343
+ titleSuffix: <button type="button">About this chart</button>,
1344
+ }}
1345
+ />
1346
+ );
1347
+
1348
+ // aria-tooltip-name, aria-allowed-attr, and aria-roles come from
1349
+ // Carbon's own SVG output and are present on a chart with no slots at all,
1350
+ // so they are excluded here rather than being attributed to the slots.
1351
+ const results = await axe(container, {
1352
+ rules: {
1353
+ 'aria-tooltip-name': { enabled: false },
1354
+ 'aria-allowed-attr': { enabled: false },
1355
+ 'aria-roles': { enabled: false },
1356
+ },
1357
+ });
1358
+
1359
+ expect(results).toHaveNoViolations();
1360
+ });
1361
+ });
1362
+
1095
1363
  describe('Legend accessibility wrap', () => {
1096
1364
  const testId = 'legend-a11y';
1097
1365