Skip to content

Commit ef131b2

Browse files
committed
fix(higher-lower): skip transition on first render, improve animations with fade+slide and custom spring config
1 parent ca26eca commit ef131b2

1 file changed

Lines changed: 22 additions & 19 deletions

File tree

src/games/HLFlag.jsx

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import {
22
ArrowDownIcon as TrendingDownIcon,
33
ArrowUpIcon as TrendingUpIcon,
44
} from "@heroicons/react/24/outline";
5+
import { useRef } from "react";
56
import {
67
animated,
7-
config,
88
useSpring,
99
useTransition,
1010
} from "@react-spring/web";
@@ -18,31 +18,36 @@ export default function HLFlag({
1818
showPopulation,
1919
locked,
2020
}) {
21+
const isFirstRender = useRef(true);
22+
2123
const { number } = useSpring({
2224
reset: true,
2325
from: { number: 0 },
2426
number: population,
2527
config: { duration: 800 },
2628
});
2729

28-
const transitions = useTransition(svg, {
29-
from: { x: "100%" },
30-
enter: { x: "0" },
31-
leave: { x: "-100%" },
32-
config: config.slow,
30+
const immediate = isFirstRender.current;
31+
32+
const flagTransition = useTransition(svg, {
33+
from: immediate ? { opacity: 1, x: "0%" } : { opacity: 0, x: "60%" },
34+
enter: { opacity: 1, x: "0%" },
35+
leave: { opacity: 0, x: "-60%" },
36+
config: { tension: 200, friction: 26 },
37+
onRest: () => { isFirstRender.current = false; },
3338
});
3439

35-
const transitionp = useTransition(name, {
36-
from: { x: "200%" },
37-
enter: { x: "0" },
38-
leave: { x: "-200%" },
39-
config: config.slow,
40+
const nameTransition = useTransition(name, {
41+
from: immediate ? { opacity: 1, y: 0 } : { opacity: 0, y: 30 },
42+
enter: { opacity: 1, y: 0 },
43+
leave: { opacity: 0, y: -30 },
44+
config: { tension: 220, friction: 24 },
4045
});
4146

4247
return (
4348
<section className="w-full lg:w-1/2 h-full">
4449
<div className="relative h-full overflow-hidden">
45-
{transitions((style, item) => (
50+
{flagTransition((style, item) => (
4651
<animated.img
4752
src={item}
4853
alt={name}
@@ -51,18 +56,16 @@ export default function HLFlag({
5156
/>
5257
))}
5358
<div className="absolute top-0 bg-dark-mode-ligth/50 backdrop-blur-sm p-4 h-full z-20 w-full flex flex-col gap-2 items-center justify-center">
54-
{transitionp((style, item) => (
55-
<animated.div style={{ ...style, position: "absolute" }}>
56-
<div className="inline-block">
57-
<p className="text-6xl font-bold">{item}</p>
58-
</div>
59+
{nameTransition((style, item) => (
60+
<animated.div style={{ ...style, position: "absolute" }} className="flex flex-col items-center">
61+
<p className="text-4xl sm:text-6xl font-bold text-center">{item}</p>
5962
{showPopulation && (
60-
<animated.p className="font-bold text-4xl">
63+
<animated.p className="font-bold text-3xl sm:text-4xl mt-2">
6164
{number.to((n) => Math.round(n).toLocaleString())}
6265
</animated.p>
6366
)}
6467
{btn && !showPopulation && (
65-
<div className="flex flex-row gap-2 items-center justify-center my-2">
68+
<div className="flex flex-row gap-2 items-center justify-center my-3">
6669
<button
6770
onClick={() => onGuess("higher")}
6871
disabled={locked}

0 commit comments

Comments
 (0)