-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathTimeline.tsx
More file actions
270 lines (237 loc) · 7.05 KB
/
Timeline.tsx
File metadata and controls
270 lines (237 loc) · 7.05 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
import {
Component,
ComponentPropsWithoutRef,
Fragment,
ReactNode,
createContext,
useCallback,
useContext,
useEffect,
useRef,
useState,
} from "react";
import { inverseLerp, lerp } from "~/utils/lerp";
interface MousePosition {
x: number;
y: number;
}
const MousePositionContext = createContext<MousePosition | undefined>(undefined);
export function MousePositionProvider({
children,
recalculateTrigger,
}: {
children: ReactNode;
recalculateTrigger?: unknown;
}) {
const ref = useRef<HTMLDivElement>(null);
const [position, setPosition] = useState<MousePosition | undefined>(undefined);
const lastMouseCoordsRef = useRef<{ clientX: number; clientY: number } | null>(null);
const handleMouseMove = useCallback(
(e: React.MouseEvent) => {
lastMouseCoordsRef.current = { clientX: e.clientX, clientY: e.clientY };
if (!ref.current) {
setPosition(undefined);
return;
}
const { top, left, width, height } = ref.current.getBoundingClientRect();
const x = (e.clientX - left) / width;
const y = (e.clientY - top) / height;
if (x < 0 || x > 1 || y < 0 || y > 1) {
setPosition(undefined);
return;
}
setPosition({ x, y });
},
[ref.current]
);
// Recalculate position when trigger changes (e.g., panel opens/closes)
// Use requestAnimationFrame to wait for the DOM layout to complete
useEffect(() => {
if (!lastMouseCoordsRef.current) {
return;
}
const rafId = requestAnimationFrame(() => {
if (!ref.current || !lastMouseCoordsRef.current) {
return;
}
const { top, left, width, height } = ref.current.getBoundingClientRect();
const x = (lastMouseCoordsRef.current.clientX - left) / width;
const y = (lastMouseCoordsRef.current.clientY - top) / height;
if (x < 0 || x > 1 || y < 0 || y > 1) {
setPosition(undefined);
return;
}
setPosition({ x, y });
});
return () => cancelAnimationFrame(rafId);
}, [recalculateTrigger]);
return (
<div
ref={ref}
onMouseEnter={handleMouseMove}
onMouseLeave={() => setPosition(undefined)}
onMouseMove={handleMouseMove}
style={{ width: "100%", height: "100%" }}
>
<MousePositionContext.Provider value={position}>{children}</MousePositionContext.Provider>
</div>
);
}
export const useMousePosition = () => {
return useContext(MousePositionContext);
};
type TimelineContextState = {
startMs: number;
durationMs: number;
};
const TimelineContext = createContext<TimelineContextState>({} as TimelineContextState);
function useTimeline() {
return useContext(TimelineContext);
}
type TimelineMousePositionContextState = { x: number; y: number } | undefined;
const TimelineMousePositionContext = createContext<TimelineMousePositionContextState>(undefined);
function useTimelineMousePosition() {
return useContext(TimelineMousePositionContext);
}
export type RootProps = {
/** If the timeline doesn't start at zero. Doesn't impact layout but gives you the times back */
startMs?: number;
durationMs: number;
/** A number between 0 and 1, determines the width between min and max */
scale: number;
minWidth: number;
maxWidth: number;
children?: ReactNode;
className?: string;
/** When this value changes, recalculate the mouse position (useful when panels resize) */
recalculateTrigger?: unknown;
};
/** The main element that determines the dimensions for all sub-elements */
export function Root({
startMs = 0,
durationMs,
scale,
minWidth,
maxWidth,
children,
className,
recalculateTrigger,
}: RootProps) {
const pixelWidth = calculatePixelWidth(minWidth, maxWidth, scale);
return (
<TimelineContext.Provider value={{ startMs, durationMs }}>
<div
className={className}
style={{
position: "relative",
width: `${pixelWidth}px`,
}}
>
<MousePositionProvider recalculateTrigger={recalculateTrigger}>
{children}
</MousePositionProvider>
</div>
</TimelineContext.Provider>
);
}
export type RowProps = ComponentPropsWithoutRef<"div">;
/** This simply acts as a container, with position relative.
* This allows you to nest "Rows" and put heights on them */
export function Row({ className, children, ...props }: RowProps) {
return (
<div {...props} className={className} style={{ ...props.style, position: "relative" }}>
{children}
</div>
);
}
export type PointProps = {
ms: number;
className?: string;
children?: (ms: number) => ReactNode;
};
/** A point in time, it has no duration */
export function Point({ ms, className, children }: PointProps) {
const { startMs, durationMs } = useTimeline();
const position = inverseLerp(startMs, startMs + durationMs, ms);
return (
<div
className={className}
style={{
position: "absolute",
left: `${position * 100}%`,
}}
>
{children && children(ms)}
</div>
);
}
export type SpanProps = {
startMs: number;
durationMs: number;
className?: string;
children?: ReactNode;
};
/** As span of time with a start and duration */
export function Span({ startMs, durationMs, className, children }: SpanProps) {
const { startMs: rootStartMs, durationMs: rootDurationMs } = useTimeline();
const position = inverseLerp(rootStartMs, rootStartMs + rootDurationMs, startMs);
const width =
inverseLerp(rootStartMs, rootStartMs + rootDurationMs, startMs + durationMs) - position;
return (
<div
className={className}
style={{
position: "absolute",
left: `${position * 100}%`,
width: `${width * 100}%`,
}}
>
{children}
</div>
);
}
export type EquallyDistributeProps = {
count: number;
children: (ms: number, index: number) => ReactNode;
};
/** Render a child equally distributed across the duration */
export function EquallyDistribute({ count, children }: EquallyDistributeProps) {
const { startMs, durationMs } = useTimeline();
return (
<>
{Array.from({ length: count }).map((_, index) => {
const ms = startMs + (durationMs / (count - 1)) * index;
return <Fragment key={index}>{children(ms, index)}</Fragment>;
})}
</>
);
}
export type FollowCursorProps = {
children: (ms: number) => ReactNode;
};
/** Renders a child that follows the cursor */
export function FollowCursor({ children }: FollowCursorProps) {
const { startMs, durationMs } = useTimeline();
const relativeMousePosition = useMousePosition();
const ms = relativeMousePosition?.x
? lerp(startMs, startMs + durationMs, relativeMousePosition.x)
: undefined;
if (ms === undefined) return null;
return (
<div
style={{
position: "absolute",
top: 0,
left: relativeMousePosition ? `${relativeMousePosition?.x * 100}%` : 0,
height: "100%",
pointerEvents: "none",
}}
>
{children(ms)}
</div>
);
}
/** Gives the total width of the root */
function calculatePixelWidth(minWidth: number, maxWidth: number, scale: number) {
return lerp(minWidth, maxWidth, scale);
}