Skip to content

Commit

Permalink
Remove persist method in favor of keyframe function
Browse files Browse the repository at this point in the history
  • Loading branch information
inokawa committed Nov 19, 2022
1 parent 54bee0a commit d235e04
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 34 deletions.
6 changes: 0 additions & 6 deletions src/react/hooks/useAnimation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
_end,
_finish,
_pause,
_persist,
_play,
_reverse,
_setRate,
Expand All @@ -30,7 +29,6 @@ export type AnimationHandle = {
cancel: () => AnimationHandle;
finish: () => AnimationHandle;
pause: () => AnimationHandle;
persist: () => AnimationHandle;
setTime: (time: number) => AnimationHandle;
setPlaybackRate: (
rate: number | ((prevRate: number) => number)
Expand Down Expand Up @@ -118,10 +116,6 @@ export const useAnimation = (
_pause(getAnimation());
return externalHandle;
},
persist: () => {
_persist(getAnimation(), getTarget()!, getKeyframes());
return externalHandle;
},
setTime: (time: number) => {
_setTime(getAnimation(), time);
return externalHandle;
Expand Down
5 changes: 0 additions & 5 deletions src/react/hooks/useTransitionAnimation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
useAnimationController,
} from "./useAnimationController";
import { getKeys } from "../../core/utils";
import { usePrevious } from "./usePrevious";
import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect";

export const useTransitionAnimation = <T extends TransitionState>(
Expand Down Expand Up @@ -41,15 +40,11 @@ export const useTransitionAnimation = <T extends TransitionState>(
};
});

const prevState = usePrevious(currentState);

useIsomorphicLayoutEffect(() => {
if (currentState === "update") return;
const handle = definitions[currentState as T];
if (!handle) return;
animation.get(prevState as T).persist();
animation
.cancelAll()
.get(currentState as T)
.play()
.end()
Expand Down
36 changes: 15 additions & 21 deletions stories/hooks/useAnimationController.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,34 +226,28 @@ export const Chained: StoryObj = {
export const Sequence: StoryObj = {
render: () => {
const timing: AnimationOptions = { duration: 600, easing: "ease-out" };
const red = useAnimation({ fill: "red" }, timing);
const blue = useAnimation({ fill: "blue" }, timing);
const green = useAnimation({ fill: "green" }, timing);
const red = useAnimation(
(prev) => [{ fill: prev.fill }, { fill: "red" }],
timing
);
const blue = useAnimation(
(prev) => [{ fill: prev.fill }, { fill: "blue" }],
timing
);
const green = useAnimation(
(prev) => [{ fill: prev.fill }, { fill: "green" }],
timing
);
const animate = useAnimationController({ red, blue, green });

const onClickRed = useCallback(async () => {
const handle = animate.get("red");
try {
await handle.play().end();
} finally {
handle.persist();
}
await animate.get("red").play().end();
}, []);
const onClickBlue = useCallback(async () => {
const handle = animate.get("blue");
try {
await handle.play().end();
} finally {
handle.persist();
}
await animate.get("blue").play().end();
}, []);
const onClickGreen = useCallback(async () => {
const handle = animate.get("green");
try {
await handle.play().end();
} finally {
handle.persist();
}
await animate.get("green").play().end();
}, []);
const onClickAll = useCallback(async () => {
try {
Expand Down
20 changes: 18 additions & 2 deletions stories/hooks/useTransitionAnimation.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,29 @@ const ExpandRect = ({ i, length }: { i: number; length: number }) => {
duration: 1000,
};

const startStyle = (s: CSSStyleDeclaration, defaultScale: number) => {
const transform = s.transform;
const scale =
transform.slice("matrix".length + 1, transform.indexOf(",") - 1) ||
defaultScale;
return {
backgroundColor: s.backgroundColor,
transform: `scale(${scale})`,
opacity: s.opacity,
};
};

const transition = useTransitionAnimation({
enter: useAnimation(
[{ backgroundColor: "skyblue", transform: "scale(1)", opacity: 1 }],
(prev) => [
startStyle(prev, 0),
{ backgroundColor: "skyblue", transform: "scale(1)", opacity: 1 },
],
{ ...timing, delay: i * 100 }
),
exit: useAnimation(
[
(prev) => [
startStyle(prev, 1),
{
backgroundColor: "limegreen",
transform: "scale(0)",
Expand Down

0 comments on commit d235e04

Please sign in to comment.