-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimatedPrice.tsx
More file actions
70 lines (60 loc) · 2.15 KB
/
Copy pathAnimatedPrice.tsx
File metadata and controls
70 lines (60 loc) · 2.15 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
import { useLayoutEffect, useRef } from "react";
import gsap from "gsap";
import { formatPrice } from "./api";
const prefersReducedMotion = () =>
window.matchMedia("(prefers-reduced-motion: reduce)").matches;
interface Props {
value: number;
className?: string;
}
// Animated numeric display — digits roll up from below on each value change.
// Static chars ($, commas, decimals) don't animate. Reduced-motion hard-swaps.
// Outer span is the themed root (e.g. .price). Visual layer is aria-hidden;
// an sr-only twin carries the live announcement for assistive tech.
export function AnimatedPrice({ value, className }: Props) {
const visualRef = useRef<HTMLSpanElement | null>(null);
const prevRef = useRef<number | null>(null);
const tweenRef = useRef<gsap.core.Tween | null>(null);
const formatted = formatPrice(value);
useLayoutEffect(() => {
const el = visualRef.current;
if (!el) return;
const isFirst = prevRef.current === null;
const changed = !isFirst && prevRef.current !== value;
prevRef.current = value;
tweenRef.current?.kill();
if (isFirst || !changed || prefersReducedMotion()) {
el.textContent = formatted;
return;
}
el.textContent = "";
const digitSpans: HTMLElement[] = [];
for (const ch of formatted) {
const span = document.createElement("span");
span.className = "animated-price__char";
span.textContent = ch;
el.appendChild(span);
if (/\d/.test(ch)) digitSpans.push(span);
}
tweenRef.current = gsap.from(digitSpans, {
yPercent: 110,
opacity: 0,
duration: 0.55,
ease: "expo.out",
stagger: 0.035,
onComplete: () => {
// Flatten DOM so the visual layer is simple text again —
// next change re-splits from scratch.
el.textContent = formatted;
},
});
}, [value, formatted]);
return (
<span className={`animated-price${className ? ` ${className}` : ""}`}>
<span ref={visualRef} className="animated-price__visual" aria-hidden="true" />
<span className="sr-only" aria-live="polite" aria-atomic="true">
{`Bitcoin price ${formatted}`}
</span>
</span>
);
}