-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathScrubberProvider.tsx
More file actions
315 lines (278 loc) · 10.4 KB
/
ScrubberProvider.tsx
File metadata and controls
315 lines (278 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { useCartesianChartContext } from '../ChartProvider';
import {
type ChartScaleFunction,
isCategoricalScale,
ScrubberContext,
type ScrubberContextValue,
} from '../utils';
export type ScrubberProviderProps = Partial<
Pick<ScrubberContextValue, 'enableScrubbing' | 'onScrubberPositionChange'>
> & {
children: React.ReactNode;
/**
* A reference to the root SVG element, where interaction event handlers will be attached.
*/
svgRef: React.RefObject<SVGSVGElement> | null;
};
/**
* A component which encapsulates the ScrubberContext.
* It depends on a ChartContext in order to provide accurate mouse tracking.
*/
export const ScrubberProvider: React.FC<ScrubberProviderProps> = ({
children,
svgRef,
enableScrubbing,
onScrubberPositionChange,
}) => {
const chartContext = useCartesianChartContext();
if (!chartContext) {
throw new Error('ScrubberProvider must be used within a ChartContext');
}
const { layout, getXScale, getYScale, getXAxis, getYAxis, series } = chartContext;
const [scrubberPosition, setScrubberPosition] = useState<number | undefined>(undefined);
const [isChartFocused, setIsChartFocused] = useState(false);
const getDataIndexFromPosition = useCallback(
(mousePosition: number): number => {
const categoryAxisIsX = layout !== 'horizontal';
const categoryScale = (categoryAxisIsX ? getXScale() : getYScale()) as ChartScaleFunction;
const categoryAxis = categoryAxisIsX ? getXAxis() : getYAxis();
if (!categoryScale || !categoryAxis) return 0;
if (isCategoricalScale(categoryScale)) {
const categories = categoryScale.domain?.() ?? categoryAxis.data ?? [];
const bandwidth = categoryScale.bandwidth?.() ?? 0;
let closestIndex = 0;
let closestDistance = Infinity;
for (let i = 0; i < categories.length; i++) {
const pos = categoryScale(i);
if (pos !== undefined) {
const distance = Math.abs(mousePosition - (pos + bandwidth / 2));
if (distance < closestDistance) {
closestDistance = distance;
closestIndex = i;
}
}
}
return closestIndex;
} else {
// For numeric scales with axis data, find the nearest data point
const axisData = categoryAxis.data;
if (axisData && Array.isArray(axisData) && typeof axisData[0] === 'number') {
// We have numeric axis data - find the closest data point
const numericData = axisData as number[];
let closestIndex = 0;
let closestDistance = Infinity;
for (let i = 0; i < numericData.length; i++) {
const dataValue = numericData[i];
const pos = categoryScale(dataValue);
if (pos !== undefined) {
const distance = Math.abs(mousePosition - pos);
if (distance < closestDistance) {
closestDistance = distance;
closestIndex = i;
}
}
}
return closestIndex;
} else {
const dataValue = (categoryScale as any).invert(mousePosition);
const dataIndexVal = Math.round(dataValue);
const domain = categoryAxis.domain;
return Math.max(domain.min ?? 0, Math.min(dataIndexVal, domain.max ?? 0));
}
}
},
[layout, getXScale, getYScale, getXAxis, getYAxis],
);
const handlePointerMove = useCallback(
(clientX: number, clientY: number, target: SVGSVGElement) => {
if (!enableScrubbing || !series || series.length === 0) return;
const rect = target.getBoundingClientRect();
const position = layout === 'horizontal' ? clientY - rect.top : clientX - rect.left;
const dataIndex = getDataIndexFromPosition(position);
if (dataIndex !== scrubberPosition) {
setScrubberPosition(dataIndex);
onScrubberPositionChange?.(dataIndex);
}
},
[
enableScrubbing,
series,
layout,
getDataIndexFromPosition,
scrubberPosition,
onScrubberPositionChange,
],
);
const handleMouseMove = useCallback(
(event: MouseEvent) => {
const target = event.currentTarget as SVGSVGElement;
handlePointerMove(event.clientX, event.clientY, target);
},
[handlePointerMove],
);
const handleTouchMove = useCallback(
(event: TouchEvent) => {
if (!event.touches.length) return;
// Prevent scrolling while scrubbing
event.preventDefault();
const touch = event.touches[0];
const target = event.currentTarget as SVGSVGElement;
handlePointerMove(touch.clientX, touch.clientY, target);
},
[handlePointerMove],
);
const handleTouchStart = useCallback(
(event: TouchEvent) => {
if (!enableScrubbing || !event.touches.length) return;
// Handle initial touch
const touch = event.touches[0];
const target = event.currentTarget as SVGSVGElement;
handlePointerMove(touch.clientX, touch.clientY, target);
},
[enableScrubbing, handlePointerMove],
);
const handlePointerLeave = useCallback(() => {
if (!enableScrubbing) return;
setScrubberPosition(undefined);
onScrubberPositionChange?.(undefined);
}, [enableScrubbing, onScrubberPositionChange]);
const handleMouseLeave = handlePointerLeave;
const handleTouchEnd = handlePointerLeave;
const handleKeyDown = useCallback(
(event: KeyboardEvent) => {
if (!enableScrubbing) return;
const categoryAxisIsX = layout !== 'horizontal';
const categoryScale = (categoryAxisIsX ? getXScale() : getYScale()) as ChartScaleFunction;
const categoryAxis = categoryAxisIsX ? getXAxis() : getYAxis();
if (!categoryScale || !categoryAxis) return;
const isBand = isCategoricalScale(categoryScale);
// Determine the actual data indices we can navigate to
let minIndex: number;
let maxIndex: number;
let dataPoints: number | undefined;
if (isBand) {
// For categorical scales, use the categories
const categories = categoryScale.domain?.() ?? categoryAxis.data ?? [];
minIndex = 0;
maxIndex = Math.max(0, categories.length - 1);
dataPoints = categories.length;
} else {
// For numeric scales, check if we have specific data points
const axisData = categoryAxis.data;
if (axisData && Array.isArray(axisData)) {
// We have specific data points - use their indices
minIndex = 0;
maxIndex = Math.max(0, axisData.length - 1);
dataPoints = axisData.length;
} else {
// Fall back to domain-based navigation for continuous scales without specific data
const domain = categoryAxis.domain;
minIndex = domain.min ?? 0;
maxIndex = domain.max ?? 0;
dataPoints = maxIndex - minIndex + 1;
}
}
const currentIndex = scrubberPosition ?? minIndex;
const dataRange = maxIndex - minIndex;
// Multi-step jump when shift is held (10% of data range, minimum 1, maximum 10)
const multiSkip = event.shiftKey;
const stepSize = multiSkip ? Math.min(10, Math.max(1, Math.floor(dataRange * 0.1))) : 1;
let newIndex: number | undefined;
switch (event.key) {
case categoryAxisIsX ? 'ArrowLeft' : 'ArrowUp':
event.preventDefault();
newIndex = Math.max(minIndex, currentIndex - stepSize);
break;
case categoryAxisIsX ? 'ArrowRight' : 'ArrowDown':
event.preventDefault();
newIndex = Math.min(maxIndex, currentIndex + stepSize);
break;
case 'Home':
event.preventDefault();
newIndex = minIndex;
break;
case 'End':
event.preventDefault();
newIndex = maxIndex;
break;
case 'Escape':
event.preventDefault();
newIndex = undefined; // Clear highlighting
break;
default:
return; // Don't handle other keys
}
if (newIndex !== scrubberPosition) {
setScrubberPosition(newIndex);
onScrubberPositionChange?.(newIndex);
}
},
[
enableScrubbing,
layout,
getXScale,
getYScale,
getXAxis,
getYAxis,
scrubberPosition,
onScrubberPositionChange,
],
);
const handleFocus = useCallback(() => {
setIsChartFocused(true);
}, []);
const handleBlur = useCallback(() => {
setIsChartFocused(false);
if (!enableScrubbing || scrubberPosition === undefined) return;
setScrubberPosition(undefined);
onScrubberPositionChange?.(undefined);
}, [enableScrubbing, onScrubberPositionChange, scrubberPosition]);
// Attach event listeners to SVG element
useEffect(() => {
if (!svgRef?.current || !enableScrubbing) return;
const svg = svgRef.current;
// Add event listeners
svg.addEventListener('mousemove', handleMouseMove);
svg.addEventListener('mouseleave', handleMouseLeave);
svg.addEventListener('touchstart', handleTouchStart, { passive: false });
svg.addEventListener('touchmove', handleTouchMove, { passive: false });
svg.addEventListener('touchend', handleTouchEnd);
svg.addEventListener('touchcancel', handleTouchEnd);
svg.addEventListener('keydown', handleKeyDown);
svg.addEventListener('focus', handleFocus);
svg.addEventListener('blur', handleBlur);
return () => {
svg.removeEventListener('mousemove', handleMouseMove);
svg.removeEventListener('mouseleave', handleMouseLeave);
svg.removeEventListener('touchstart', handleTouchStart);
svg.removeEventListener('touchmove', handleTouchMove);
svg.removeEventListener('touchend', handleTouchEnd);
svg.removeEventListener('touchcancel', handleTouchEnd);
svg.removeEventListener('keydown', handleKeyDown);
svg.removeEventListener('focus', handleFocus);
svg.removeEventListener('blur', handleBlur);
};
}, [
svgRef,
enableScrubbing,
handleMouseMove,
handleMouseLeave,
handleTouchStart,
handleTouchMove,
handleTouchEnd,
handleKeyDown,
handleFocus,
handleBlur,
]);
const contextValue: ScrubberContextValue = useMemo(
() => ({
enableScrubbing: !!enableScrubbing,
scrubberPosition,
onScrubberPositionChange: setScrubberPosition,
isChartFocused,
}),
[enableScrubbing, scrubberPosition, isChartFocused],
);
return <ScrubberContext.Provider value={contextValue}>{children}</ScrubberContext.Provider>;
};