forked from solana-foundation/solana-com
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtoken-transitions.client.tsx
More file actions
49 lines (44 loc) · 1.4 KB
/
Copy pathtoken-transitions.client.tsx
File metadata and controls
49 lines (44 loc) · 1.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
"use client";
import { CustomPreProps, InnerPre, getPreRef } from "codehike/code";
import {
TokenTransitionsSnapshot,
calculateTransitions,
getStartingSnapshot,
} from "codehike/utils/token-transitions";
import React from "react";
const MAX_TRANSITION_DURATION = 900; // milliseconds
export class PreWithRef extends React.Component<CustomPreProps> {
ref: React.RefObject<HTMLPreElement>;
constructor(props: CustomPreProps) {
super(props);
this.ref = getPreRef(this.props);
}
override render() {
return <InnerPre merge={this.props} style={{ position: "relative" }} />;
}
override getSnapshotBeforeUpdate() {
return getStartingSnapshot(this.ref.current!);
}
override componentDidUpdate(
_prevProps: never,
_prevState: never,
snapshot: TokenTransitionsSnapshot,
) {
const transitions = calculateTransitions(this.ref.current!, snapshot);
transitions.forEach(({ element, keyframes, options }) => {
const { translateX, translateY, ...kf } = keyframes as any;
if (translateX && translateY) {
kf.translate = [
`${translateX[0]}px ${translateY[0]}px`,
`${translateX[1]}px ${translateY[1]}px`,
];
}
element.animate(kf, {
duration: options.duration * MAX_TRANSITION_DURATION,
delay: options.delay * MAX_TRANSITION_DURATION,
easing: options.easing,
fill: "backwards",
});
});
}
}