Skip to content

Commit fc6a4ae

Browse files
committed
Update useAnimation.stories.tsx
1 parent e7f1721 commit fc6a4ae

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

stories/hooks/useAnimation.stories.tsx

+46-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { StoryObj } from "@storybook/react";
22
import { useEffect, useState } from "react";
3-
import { TypedEasing, useAnimation } from "../../src";
3+
import { TypedEasing, TypedKeyframe, useAnimation } from "../../src";
44

55
export default { component: useAnimation };
66

@@ -269,23 +269,38 @@ const Bar = ({
269269
value,
270270
i,
271271
height,
272+
keep,
272273
}: {
273274
value: number;
274275
i: number;
275276
height: number;
277+
keep: boolean;
276278
}) => {
279+
const target: TypedKeyframe = {
280+
height: `${value}px`,
281+
transform: `translateY(-${value}px)`,
282+
opacity: String(1 - i * 0.025),
283+
};
277284
const animate = useAnimation(
278-
{
279-
height: `${value}px`,
280-
transform: `translateY(-${value}px)`,
281-
opacity: String(1 - i * 0.025),
282-
},
283-
{ duration: 150, easing: "ease-out", delay: i * 100 }
285+
keep
286+
? target
287+
: [
288+
{
289+
height: 0,
290+
transform: "translateY(0px)",
291+
opacity: target.opacity,
292+
},
293+
target,
294+
],
295+
{ duration: 150, easing: "ease-out", delay: i * 100, fill: "both" }
284296
);
285297

286298
useEffect(() => {
299+
if (keep) {
300+
animate.persist();
301+
}
287302
animate.play();
288-
}, [value]);
303+
}, [value, keep]);
289304

290305
return (
291306
<rect
@@ -304,21 +319,42 @@ export const Bars: StoryObj = {
304319
const init = () =>
305320
Array.from({ length: 30 }).map(() => 300 * Math.random() ** 2);
306321
const [rects, setRects] = useState(init);
322+
const [keep, setKeep] = useState(false);
307323

308324
const width = 800;
309325
const height = 400;
310326
const margin = 10;
311327
const maxBarHeight = height - margin * 2;
328+
329+
const refresh = () => setRects(init());
330+
312331
return (
313332
<>
314333
<div>
315-
<button onClick={() => setRects(init())}>refresh</button>
334+
<button onClick={refresh}>refresh</button>
335+
<label>
336+
<input
337+
type="checkbox"
338+
checked={keep}
339+
onChange={(e) => {
340+
setKeep(e.target.checked);
341+
refresh();
342+
}}
343+
/>
344+
keep
345+
</label>
316346
</div>
317347
<div>
318348
<svg width={width} height={height}>
319349
<g transform={`translate(${margin},${margin})`}>
320350
{rects.map((v, i) => (
321-
<Bar key={i} i={i} value={v} height={maxBarHeight} />
351+
<Bar
352+
key={i}
353+
i={i}
354+
value={v}
355+
height={maxBarHeight}
356+
keep={keep}
357+
/>
322358
))}
323359
</g>
324360
</svg>

0 commit comments

Comments
 (0)