@automattic/charts 4.6.0 → 4.8.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 (91) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/TOKENS.md +15 -10
  3. package/dist/index.cjs +427 -211
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.css +40 -26
  6. package/dist/index.d.cts +69 -29
  7. package/dist/index.d.ts +69 -29
  8. package/dist/index.js +427 -213
  9. package/dist/index.js.map +1 -1
  10. package/package.json +1 -1
  11. package/src/charts/area-chart/area-chart.tsx +2 -2
  12. package/src/charts/area-chart/test/area-chart.test.tsx +1 -1
  13. package/src/charts/area-chart/test/tooltip-resolution-branches.test.tsx +1 -1
  14. package/src/charts/bar-chart/bar-chart.module.scss +7 -0
  15. package/src/charts/bar-chart/bar-chart.tsx +20 -14
  16. package/src/charts/bar-chart/private/comparison-bars.tsx +1 -0
  17. package/src/charts/bar-chart/test/bar-chart.test.tsx +25 -0
  18. package/src/charts/conversion-funnel-chart/conversion-funnel-chart.module.scss +0 -10
  19. package/src/charts/conversion-funnel-chart/conversion-funnel-chart.tsx +5 -3
  20. package/src/charts/conversion-funnel-chart/test/conversion-funnel-chart.test.tsx +20 -1
  21. package/src/charts/conversion-funnel-chart/types.ts +3 -0
  22. package/src/charts/heatmap-chart/heatmap-chart.module.scss +16 -7
  23. package/src/charts/heatmap-chart/heatmap-chart.tsx +80 -53
  24. package/src/charts/heatmap-chart/private/heatmap-context.ts +2 -2
  25. package/src/charts/heatmap-chart/private/heatmap-legend.tsx +2 -2
  26. package/src/charts/heatmap-chart/private/heatmap-scale.ts +74 -0
  27. package/src/charts/heatmap-chart/private/index.ts +2 -1
  28. package/src/charts/heatmap-chart/private/use-heatmap-colors.ts +24 -6
  29. package/src/charts/heatmap-chart/test/heatmap-chart.test.tsx +221 -25
  30. package/src/charts/heatmap-chart/test/heatmap-scale.test.ts +78 -0
  31. package/src/charts/heatmap-chart/test/use-heatmap-colors.test.ts +18 -6
  32. package/src/charts/heatmap-chart/types.ts +6 -9
  33. package/src/charts/line-chart/crosshair-style-types.fixture.ts +23 -0
  34. package/src/charts/line-chart/line-chart.module.scss +0 -5
  35. package/src/charts/line-chart/line-chart.tsx +10 -42
  36. package/src/charts/line-chart/test/line-chart.test.tsx +19 -47
  37. package/src/charts/pie-chart/pie-chart.module.scss +9 -0
  38. package/src/charts/pie-chart/pie-chart.tsx +26 -80
  39. package/src/charts/pie-chart/test/pie-chart.test.tsx +21 -8
  40. package/src/charts/pie-semi-circle-chart/pie-semi-circle-chart.tsx +4 -4
  41. package/src/charts/pie-semi-circle-chart/test/pie-semi-circle-chart.test.tsx +1 -1
  42. package/src/charts/private/chart-composition/chart-html.tsx +1 -1
  43. package/src/charts/private/chart-composition/chart-svg.tsx +1 -1
  44. package/src/charts/private/label-text-color.ts +116 -0
  45. package/src/charts/private/radial-wipe-animation/radial-wipe-animation.tsx +2 -0
  46. package/src/charts/private/svg-empty-state/svg-empty-state.tsx +1 -1
  47. package/src/charts/private/test/label-text-color.test.ts +189 -0
  48. package/src/charts/private/with-responsive/test/with-responsive.test.tsx +1 -0
  49. package/src/charts/private/with-responsive/with-responsive.tsx +2 -5
  50. package/src/components/tooltip/base-tooltip.tsx +31 -30
  51. package/src/components/tooltip/index.ts +3 -1
  52. package/src/components/tooltip/private/bounded-tooltip.tsx +32 -13
  53. package/src/components/tooltip/private/label-value-content.tsx +8 -0
  54. package/src/components/tooltip/private/tooltip-theme.tsx +37 -0
  55. package/src/components/tooltip/private/xy-chart-tooltip-overlay.tsx +336 -0
  56. package/src/components/tooltip/test/bounded-tooltip.test.tsx +82 -1
  57. package/src/components/tooltip/test/tool-tip.test.tsx +10 -3
  58. package/src/components/tooltip/test/tooltip-box.test.tsx +53 -0
  59. package/src/components/tooltip/test/tooltip-theme.test.tsx +61 -0
  60. package/src/components/tooltip/test/xy-chart-tooltip-overlay.test.tsx +445 -0
  61. package/src/components/tooltip/test/xy-chart-tooltip.test.tsx +286 -356
  62. package/src/components/tooltip/tooltip-box.module.scss +21 -0
  63. package/src/components/tooltip/tooltip-box.tsx +43 -0
  64. package/src/components/tooltip/types.ts +7 -1
  65. package/src/components/tooltip/xy-chart-tooltip.tsx +411 -301
  66. package/src/components/trend-indicator/trend-indicator.tsx +1 -0
  67. package/src/hooks/index.ts +1 -0
  68. package/src/hooks/test/use-chart-margin.test.tsx +8 -4
  69. package/src/hooks/test/use-xychart-theme.test.tsx +3 -3
  70. package/src/hooks/use-chart-margin.tsx +21 -18
  71. package/src/hooks/use-isomorphic-layout-effect.ts +5 -0
  72. package/src/hooks/use-xychart-theme.ts +4 -12
  73. package/src/index.ts +8 -3
  74. package/src/providers/chart-context/global-charts-provider.tsx +5 -14
  75. package/src/providers/chart-context/hooks/use-default-hidden-series.ts +3 -5
  76. package/src/providers/chart-context/private/catalog-pointers.ts +1 -0
  77. package/src/providers/chart-context/private/perceptual-color.ts +45 -4
  78. package/src/providers/chart-context/private/resolve-opaque-hex.ts +16 -0
  79. package/src/providers/chart-context/test/perceptual-color.test.ts +31 -0
  80. package/src/providers/chart-context/test/use-default-hidden-series.test.tsx +1 -1
  81. package/src/styles/chart-scope-class.ts +5 -0
  82. package/src/styles/chart-scope.scss +10 -6
  83. package/src/styles/test/chart-scope.test.ts +3 -3
  84. package/src/test-utils/zoned-parsing-suite.ts +0 -10
  85. package/src/utils/color-utils.ts +2 -1
  86. package/src/utils/test/date-formatting.test.ts +0 -10
  87. package/src/visx/types.ts +1 -1
  88. package/src/charts/line-chart/test/crosshair-style-types.test.ts +0 -31
  89. package/src/components/tooltip/accessible-tooltip.tsx +0 -433
  90. package/src/components/tooltip/base-tooltip.module.scss +0 -20
  91. package/src/components/tooltip/test/accessible-tooltip.test.tsx +0 -331
@@ -0,0 +1,445 @@
1
+ import { render, screen, waitFor } from '@testing-library/react';
2
+ import { BarSeries, LineSeries, TooltipContext, XYChart } from '@visx/xychart';
3
+ import { useContext, useEffect } from 'react';
4
+ import { XyChartTooltipOverlay } from '../private/xy-chart-tooltip-overlay';
5
+ import type { XyChartTooltipProps } from '../../../visx/types';
6
+ import type { EventHandlerParams, XYChartTheme } from '@visx/xychart';
7
+
8
+ type Datum = { x: number; y: number };
9
+
10
+ const SERIES_A: Datum[] = [
11
+ { x: 0, y: 0 },
12
+ { x: 5, y: 50 },
13
+ { x: 10, y: 100 },
14
+ ];
15
+ const SERIES_B: Datum[] = [
16
+ { x: 0, y: 100 },
17
+ { x: 5, y: 25 },
18
+ { x: 10, y: 0 },
19
+ ];
20
+
21
+ const xAccessor = ( d: Datum ) => d.x;
22
+ const yAccessor = ( d: Datum ) => d.y;
23
+
24
+ type BandDatum = { x: string; y: number };
25
+ const BAND_DATA: BandDatum[] = [
26
+ { x: 'a', y: 50 },
27
+ { x: 'b', y: 80 },
28
+ ];
29
+ const bandXAccessor = ( d: BandDatum ) => d.x;
30
+ const bandYAccessor = ( d: BandDatum ) => d.y;
31
+ const renderBarTooltip = () => <span>bar</span>;
32
+ const BAND_PARAMS = [
33
+ { key: 'bars', index: 0, datum: BAND_DATA[ 0 ], svgPoint: { x: 20, y: 90 } },
34
+ ] as unknown as EventHandlerParams< Datum >[];
35
+
36
+ // The pointer sits on A's datum, 25px above B's, so A is the nearest datum.
37
+ const BOTH_SERIES: EventHandlerParams< Datum >[] = [
38
+ {
39
+ key: 'A',
40
+ index: 1,
41
+ datum: SERIES_A[ 1 ],
42
+ svgPoint: { x: 100, y: 50 },
43
+ distanceX: 0,
44
+ distanceY: 0,
45
+ },
46
+ {
47
+ key: 'B',
48
+ index: 1,
49
+ datum: SERIES_B[ 1 ],
50
+ svgPoint: { x: 100, y: 50 },
51
+ distanceX: 0,
52
+ distanceY: 25,
53
+ },
54
+ ];
55
+
56
+ // Opens the tooltip the way a pointer event would, once the series have registered.
57
+ const OpenTooltip = ( { params }: { params: EventHandlerParams< Datum >[] } ) => {
58
+ const tooltipContext = useContext( TooltipContext );
59
+ useEffect( () => {
60
+ params.forEach( p => tooltipContext?.showTooltip( p ) );
61
+ // The context object changes identity on every tooltip update; re-running would loop.
62
+ // eslint-disable-next-line react-hooks/exhaustive-deps
63
+ }, [] );
64
+ return null;
65
+ };
66
+
67
+ const renderTooltip: XyChartTooltipProps< Datum >[ 'renderTooltip' ] = ( { tooltipData } ) => (
68
+ <span>{ `nearest:${ tooltipData?.nearestDatum?.key ?? '' }` }</span>
69
+ );
70
+
71
+ const NO_MARGIN = { top: 0, right: 0, bottom: 0, left: 0 };
72
+
73
+ // 200x100 chart; with no margin x maps 0..10 to 0..200px and y maps 0..100 to 100..0px.
74
+ const renderChart = (
75
+ tooltipProps: Partial< XyChartTooltipProps< Datum > > = {},
76
+ params: EventHandlerParams< Datum >[] = [
77
+ { key: 'A', index: 1, datum: SERIES_A[ 1 ], svgPoint: { x: 123, y: 45 } },
78
+ ],
79
+ margin = NO_MARGIN,
80
+ theme?: XYChartTheme
81
+ ) =>
82
+ render(
83
+ <div data-testid="wrapper" style={ { position: 'relative' } }>
84
+ <XYChart
85
+ width={ 200 }
86
+ height={ 100 }
87
+ margin={ margin }
88
+ theme={ theme }
89
+ xScale={ { type: 'linear', domain: [ 0, 10 ] } }
90
+ yScale={ { type: 'linear', domain: [ 0, 100 ] } }
91
+ >
92
+ <LineSeries dataKey="A" data={ SERIES_A } xAccessor={ xAccessor } yAccessor={ yAccessor } />
93
+ <LineSeries dataKey="B" data={ SERIES_B } xAccessor={ xAccessor } yAccessor={ yAccessor } />
94
+ <XyChartTooltipOverlay
95
+ renderTooltip={ renderTooltip }
96
+ data-testid="tooltip-box"
97
+ { ...tooltipProps }
98
+ />
99
+ <OpenTooltip params={ params } />
100
+ </XYChart>
101
+ </div>
102
+ );
103
+
104
+ describe( 'XyChartTooltipOverlay', () => {
105
+ test.each( [
106
+ [ 'bounded', {} ],
107
+ [ 'unbounded', { detectBounds: false } ],
108
+ [ 'below-axis', { tooltipPlacement: 'below-axis' as const } ],
109
+ ] )( 'draws the %s box on the package surface, with no visx box styles', async ( _, props ) => {
110
+ renderChart( props );
111
+ const box = await screen.findByTestId( 'tooltip-box' );
112
+ expect( box ).toHaveClass( 'surface' );
113
+ expect( box ).not.toHaveAttribute( 'style', expect.stringContaining( 'box-shadow' ) );
114
+ expect( box ).not.toHaveAttribute( 'style', expect.stringContaining( 'background-color' ) );
115
+ expect( box ).not.toHaveAttribute( 'style', expect.stringContaining( 'padding' ) );
116
+ expect( box ).not.toHaveAttribute( 'style', expect.stringContaining( 'border-radius' ) );
117
+ } );
118
+
119
+ test( 'strips the surface from an unbounded box when unstyled', async () => {
120
+ renderChart( { detectBounds: false, unstyled: true } );
121
+ const box = await screen.findByTestId( 'tooltip-box' );
122
+ expect( box ).not.toHaveClass( 'surface' );
123
+ } );
124
+
125
+ test( 'strips the surface from a bounded box when unstyled', async () => {
126
+ renderChart( { unstyled: true } );
127
+ const box = await screen.findByTestId( 'tooltip-box' );
128
+ expect( box ).not.toHaveClass( 'surface' );
129
+ } );
130
+
131
+ test( 'keeps a caller class beside the surface on an unbounded box', async () => {
132
+ renderChart( { detectBounds: false, className: 'extra' } );
133
+ const box = await screen.findByTestId( 'tooltip-box' );
134
+ expect( box ).toHaveClass( 'surface', 'extra' );
135
+ } );
136
+
137
+ test( 'renders the tooltip box inside the chart wrapper, not in a body-level portal', async () => {
138
+ const { container } = renderChart();
139
+
140
+ const box = await screen.findByTestId( 'tooltip-box' );
141
+
142
+ expect( box ).toHaveTextContent( 'nearest:A' );
143
+ expect( screen.getByTestId( 'wrapper' ) ).toContainElement( box );
144
+ // A body-level portal would sit outside the render root.
145
+ expect( container ).toContainElement( box );
146
+ } );
147
+
148
+ test( 'places the box at the pointer position when snapping is off', async () => {
149
+ renderChart();
150
+
151
+ // TooltipWithBounds moves the box through a transform from (0, 0), after its 10px offsets.
152
+ await expect( screen.findByTestId( 'tooltip-box' ) ).resolves.toHaveStyle( {
153
+ transform: 'translate(133px, 55px)',
154
+ } );
155
+ } );
156
+
157
+ test( 'places the unbounded box at the anchor plus the default offsets', async () => {
158
+ renderChart( { detectBounds: false } );
159
+ const box = await screen.findByTestId( 'tooltip-box' );
160
+ expect( box ).toHaveStyle( { position: 'absolute', left: '133px', top: '55px' } );
161
+ expect( box ).toHaveClass( 'visx-tooltip' );
162
+ } );
163
+
164
+ test( 'places the unbounded box with custom offsets', async () => {
165
+ renderChart( { detectBounds: false, offsetLeft: 4, offsetTop: 2 } );
166
+ await expect( screen.findByTestId( 'tooltip-box' ) ).resolves.toHaveStyle( {
167
+ left: '127px',
168
+ top: '47px',
169
+ } );
170
+ } );
171
+
172
+ test( 'keeps the unstyled unbounded box positioned but drops caller style and zIndex', async () => {
173
+ renderChart( { detectBounds: false, unstyled: true, style: { background: 'purple' } } );
174
+ const box = await screen.findByTestId( 'tooltip-box' );
175
+ expect( box ).toHaveStyle( { position: 'absolute', left: '133px', top: '55px' } );
176
+ expect( box ).not.toHaveStyle( { background: 'purple' } );
177
+ expect( box ).not.toHaveStyle( { zIndex: '3' } );
178
+ } );
179
+
180
+ test( 'pins beside placement to the explicit SVG top anchor', async () => {
181
+ renderChart( { tooltipPlacement: 'beside', tooltipAnchorTop: 20, snapTooltipToDatumY: true } );
182
+ await expect( screen.findByTestId( 'tooltip-box' ) ).resolves.toHaveStyle( {
183
+ transform: 'translate(133px, 20px)',
184
+ } );
185
+ } );
186
+
187
+ test( 'snaps the box to the nearest datum', async () => {
188
+ renderChart( { snapTooltipToDatumX: true, snapTooltipToDatumY: true } );
189
+
190
+ // Datum (5, 50) sits at (100px, 50px); the default 10px offsets apply after snapping.
191
+ await waitFor( () =>
192
+ expect( screen.getByTestId( 'tooltip-box' ) ).toHaveStyle( {
193
+ transform: 'translate(110px, 60px)',
194
+ } )
195
+ );
196
+ } );
197
+
198
+ test( 'anchors below the x-axis label band without moving datum glyphs or crosshairs', async () => {
199
+ const originalRect = Element.prototype.getBoundingClientRect;
200
+ const rectSpy = jest
201
+ .spyOn( Element.prototype, 'getBoundingClientRect' )
202
+ .mockImplementation( function ( this: Element ) {
203
+ const rect = originalRect.call( this );
204
+ return this.classList.contains( 'visx-tooltip' )
205
+ ? { ...rect, width: 80, height: 30, right: rect.left + 80, bottom: rect.top + 30 }
206
+ : rect;
207
+ } );
208
+
209
+ try {
210
+ renderChart(
211
+ {
212
+ tooltipPlacement: 'below-axis',
213
+ snapTooltipToDatumY: true,
214
+ showDatumGlyph: true,
215
+ showVerticalCrosshair: true,
216
+ showHorizontalCrosshair: true,
217
+ },
218
+ undefined,
219
+ { top: 10, right: 20, bottom: 30, left: 40 }
220
+ );
221
+
222
+ const box = await screen.findByTestId( 'tooltip-box' );
223
+ expect( box ).toHaveStyle( { transform: 'translate(70px, 106px)' } );
224
+ expect( screen.getByTestId( 'wrapper' ) ).toContainElement( box );
225
+ expect( screen.getByTestId( 'tooltip-axis-pointer' ) ).toHaveStyle( {
226
+ left: '34px',
227
+ top: '-6px',
228
+ } );
229
+ expect( screen.getByTestId( 'xy-chart-tooltip-glyph-group-A' ) ).toHaveAttribute(
230
+ 'transform',
231
+ 'translate(110, 40)'
232
+ );
233
+ expect( screen.getByTestId( 'xy-chart-tooltip-crosshair-vertical' ) ).toHaveAttribute(
234
+ 'y2',
235
+ '70'
236
+ );
237
+ expect( screen.getByTestId( 'xy-chart-tooltip-crosshair-horizontal' ) ).toHaveAttribute(
238
+ 'y1',
239
+ '40'
240
+ );
241
+ } finally {
242
+ rectSpy.mockRestore();
243
+ }
244
+ } );
245
+
246
+ test( 'draws one glyph per series in the SVG at the datum position', async () => {
247
+ renderChart( { showSeriesGlyphs: true }, BOTH_SERIES );
248
+
249
+ const glyphA = await screen.findByTestId( 'xy-chart-tooltip-glyph-A' );
250
+ const glyphB = await screen.findByTestId( 'xy-chart-tooltip-glyph-B' );
251
+
252
+ // The group is placed at the datum; the glyph itself draws at the origin.
253
+ expect( screen.getByTestId( 'xy-chart-tooltip-glyph-group-A' ) ).toHaveAttribute(
254
+ 'transform',
255
+ 'translate(100, 50)'
256
+ );
257
+ expect( screen.getByTestId( 'xy-chart-tooltip-glyph-group-B' ) ).toHaveAttribute(
258
+ 'transform',
259
+ 'translate(100, 75)'
260
+ );
261
+ expect( glyphA ).toHaveAttribute( 'cx', '0' );
262
+ expect( glyphA ).toHaveAttribute( 'cy', '0' );
263
+ expect( glyphB ).toHaveAttribute( 'cx', '0' );
264
+ expect( glyphB ).toHaveAttribute( 'cy', '0' );
265
+ // Drawn into the chart SVG, not portaled out as HTML.
266
+ expect( glyphA ).toBeInstanceOf( SVGElement );
267
+ } );
268
+
269
+ test( 'hands the glyph renderer the origin, like visx, and marks the nearest datum', async () => {
270
+ const renderGlyph = jest.fn( () => null );
271
+ renderChart( { showSeriesGlyphs: true, renderGlyph }, BOTH_SERIES );
272
+
273
+ await waitFor( () => expect( renderGlyph ).toHaveBeenCalledTimes( 2 ) );
274
+
275
+ const calls = renderGlyph.mock.calls as unknown as Array<
276
+ [ { key: string; x: number; y: number; isNearestDatum: boolean; datum: Datum } ]
277
+ >;
278
+ const byKey = Object.fromEntries( calls.map( ( [ p ] ) => [ p.key, p ] ) );
279
+ // A renderer written against visx's Tooltip ignores x and y, so both stay 0.
280
+ expect( byKey.A ).toMatchObject( { x: 0, y: 0, datum: SERIES_A[ 1 ] } );
281
+ expect( byKey.B ).toMatchObject( { x: 0, y: 0, datum: SERIES_B[ 1 ] } );
282
+ expect( byKey.A.isNearestDatum ).toBe( true );
283
+ expect( byKey.B.isNearestDatum ).toBe( false );
284
+ } );
285
+
286
+ test( 'centres a band-scale datum in its band', async () => {
287
+ render(
288
+ <div style={ { position: 'relative' } }>
289
+ <XYChart
290
+ width={ 200 }
291
+ height={ 100 }
292
+ margin={ NO_MARGIN }
293
+ xScale={ { type: 'band', domain: [ 'a', 'b' ], paddingInner: 0, paddingOuter: 0 } }
294
+ yScale={ { type: 'linear', domain: [ 0, 100 ] } }
295
+ >
296
+ <BarSeries
297
+ dataKey="bars"
298
+ data={ BAND_DATA }
299
+ xAccessor={ bandXAccessor }
300
+ yAccessor={ bandYAccessor }
301
+ />
302
+ <XyChartTooltipOverlay
303
+ renderTooltip={ renderBarTooltip }
304
+ data-testid="tooltip-box"
305
+ snapTooltipToDatumX
306
+ snapTooltipToDatumY
307
+ showDatumGlyph
308
+ />
309
+ <OpenTooltip params={ BAND_PARAMS } />
310
+ </XYChart>
311
+ </div>
312
+ );
313
+
314
+ // Band 'a' spans 0..100px; the datum sits at its centre, 50px, not its start.
315
+ await waitFor( () =>
316
+ expect( screen.getByTestId( 'xy-chart-tooltip-glyph-group-bars' ) ).toHaveAttribute(
317
+ 'transform',
318
+ 'translate(50, 50)'
319
+ )
320
+ );
321
+ expect( screen.getByTestId( 'tooltip-box' ) ).toHaveStyle( {
322
+ transform: 'translate(60px, 60px)',
323
+ } );
324
+ } );
325
+
326
+ test( 'draws the crosshairs across the plot area, inside the margins, at the snapped position', async () => {
327
+ renderChart(
328
+ {
329
+ snapTooltipToDatumX: true,
330
+ snapTooltipToDatumY: true,
331
+ showVerticalCrosshair: true,
332
+ showHorizontalCrosshair: true,
333
+ },
334
+ undefined,
335
+ { top: 10, right: 20, bottom: 30, left: 40 }
336
+ );
337
+
338
+ const vertical = await screen.findByTestId( 'xy-chart-tooltip-crosshair-vertical' );
339
+ const horizontal = await screen.findByTestId( 'xy-chart-tooltip-crosshair-horizontal' );
340
+
341
+ // Plot area is 140x60 at (40, 10); datum (5, 50) sits at its centre, (110, 40).
342
+ expect( vertical ).toHaveAttribute( 'x1', '110' );
343
+ expect( vertical ).toHaveAttribute( 'y1', '10' );
344
+ expect( vertical ).toHaveAttribute( 'y2', '70' );
345
+ expect( horizontal ).toHaveAttribute( 'y1', '40' );
346
+ expect( horizontal ).toHaveAttribute( 'x1', '40' );
347
+ expect( horizontal ).toHaveAttribute( 'x2', '180' );
348
+ } );
349
+
350
+ test( 'applies crosshair paint without allowing geometry overrides', async () => {
351
+ const consumerStyle = {
352
+ stroke: 'purple',
353
+ strokeWidth: 8,
354
+ className: 'custom-crosshair',
355
+ x1: 999,
356
+ transform: 'translate(100 0)',
357
+ style: { strokeOpacity: 0.25, transform: 'translateX(100px)' },
358
+ };
359
+ renderChart( {
360
+ snapTooltipToDatumX: true,
361
+ snapTooltipToDatumY: true,
362
+ showVerticalCrosshair: true,
363
+ showHorizontalCrosshair: true,
364
+ verticalCrosshairStyle: consumerStyle,
365
+ horizontalCrosshairStyle: consumerStyle,
366
+ } );
367
+
368
+ for ( const direction of [ 'vertical', 'horizontal' ] ) {
369
+ const guide = await screen.findByTestId( `xy-chart-tooltip-crosshair-${ direction }` );
370
+ expect( guide ).toHaveAttribute( 'stroke', 'purple' );
371
+ expect( guide ).toHaveAttribute( 'stroke-width', '8' );
372
+ expect( guide ).toHaveClass( 'custom-crosshair' );
373
+ expect( guide ).toHaveStyle( { 'stroke-opacity': '0.25', transform: 'none' } );
374
+ expect( guide ).not.toHaveAttribute( 'transform' );
375
+ expect( guide ).toHaveAttribute( 'x1', direction === 'vertical' ? '100' : '0' );
376
+ expect( guide ).toHaveAttribute( 'x2', direction === 'vertical' ? '100' : '200' );
377
+ expect( guide ).toHaveAttribute( 'y1', direction === 'vertical' ? '0' : '50' );
378
+ expect( guide ).toHaveAttribute( 'y2', direction === 'vertical' ? '100' : '50' );
379
+ }
380
+ } );
381
+
382
+ test( 'stacks the box above the chart overlays, with zIndex as the override', async () => {
383
+ const { unmount } = renderChart();
384
+ await expect( screen.findByTestId( 'tooltip-box' ) ).resolves.toHaveStyle( { zIndex: '3' } );
385
+ unmount();
386
+
387
+ renderChart( { zIndex: 9 } );
388
+ await expect( screen.findByTestId( 'tooltip-box' ) ).resolves.toHaveStyle( { zIndex: '9' } );
389
+ } );
390
+
391
+ test.each( [
392
+ [ 'auto', { tooltipPlacement: 'auto' } ],
393
+ [ 'below-axis', { tooltipPlacement: 'below-axis' } ],
394
+ [ 'unbounded', { detectBounds: false } ],
395
+ ] as const )(
396
+ 'keeps the %s surface and stacking under a partial style override',
397
+ async ( _name, placementProps ) => {
398
+ renderChart( { ...placementProps, style: { color: 'red' } } );
399
+ const box = await screen.findByTestId( 'tooltip-box' );
400
+ expect( box ).toHaveClass( 'surface' );
401
+ expect( box ).toHaveStyle( { zIndex: '3', color: 'rgb(255, 0, 0)' } );
402
+ }
403
+ );
404
+
405
+ test( 'lets a style override paint the box', async () => {
406
+ renderChart( { style: { background: 'rgb(0, 0, 0)', color: 'rgb(255, 255, 255)' } } );
407
+ await expect( screen.findByTestId( 'tooltip-box' ) ).resolves.toHaveStyle( {
408
+ background: 'rgb(0, 0, 0)',
409
+ color: 'rgb(255, 255, 255)',
410
+ } );
411
+ } );
412
+
413
+ test( 'accepts the portal-era options without passing them to the box', async () => {
414
+ renderChart( { scroll: true, debounce: 50, resizeObserverPolyfill: undefined } );
415
+
416
+ const box = await screen.findByTestId( 'tooltip-box' );
417
+
418
+ expect( box ).not.toHaveAttribute( 'scroll' );
419
+ expect( box ).not.toHaveAttribute( 'debounce' );
420
+ } );
421
+
422
+ test( 'renders no box when there is no tooltip renderer', async () => {
423
+ renderChart( { renderTooltip: undefined } );
424
+
425
+ await expect( screen.findByTestId( 'xy-chart-tooltip-anchor' ) ).resolves.toBeInTheDocument();
426
+ expect( screen.queryByTestId( 'tooltip-box' ) ).not.toBeInTheDocument();
427
+ } );
428
+
429
+ test( 'renders nothing when the tooltip content is empty', async () => {
430
+ renderChart( { renderTooltip: () => null, showSeriesGlyphs: true }, BOTH_SERIES );
431
+
432
+ // The anchor is the only thing rendered while there is nothing to show.
433
+ await expect( screen.findByTestId( 'xy-chart-tooltip-anchor' ) ).resolves.toBeInTheDocument();
434
+ expect( screen.queryByTestId( 'tooltip-box' ) ).not.toBeInTheDocument();
435
+ expect( screen.queryByTestId( 'xy-chart-tooltip-glyph-A' ) ).not.toBeInTheDocument();
436
+ } );
437
+
438
+ test( 'renders nothing while the tooltip is closed', () => {
439
+ renderChart( { showSeriesGlyphs: true, showVerticalCrosshair: true }, [] );
440
+
441
+ expect( screen.queryByTestId( 'tooltip-box' ) ).not.toBeInTheDocument();
442
+ expect( screen.queryByTestId( 'xy-chart-tooltip-glyph-A' ) ).not.toBeInTheDocument();
443
+ expect( screen.queryByTestId( 'xy-chart-tooltip-crosshair-vertical' ) ).not.toBeInTheDocument();
444
+ } );
445
+ } );