Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
inokawa committed Sep 1, 2023
1 parent bf6f637 commit 7d436e8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 29 deletions.
9 changes: 4 additions & 5 deletions src/react/hooks/useAnimation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef } from "react";
import { useEffect } from "react";
import {
assign,
getStyle,
Expand All @@ -24,6 +24,7 @@ import {
} from "../../core/waapi";
import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect";
import { useStatic } from "./useStatic";
import { useLatestRef } from "./useLatestRef";

export type PlayOptionsWithArgs<Args = void> = PlayOptions & { args: Args };

Expand Down Expand Up @@ -163,8 +164,8 @@ export const useAnimation = <Args = void>(
keyframe: TypedKeyframe | TypedKeyframe[] | GetKeyframeFunction<Args>,
options?: AnimationOptions
): AnimationHandle<Args> => {
const keyframeRef = useRef(keyframe);
const optionsRef = useRef(options);
const keyframeRef = useLatestRef(keyframe);
const optionsRef = useLatestRef(options);

const [handle, s] = useStatic((): [AnimationHandle<Args>, AnimationState] => {
let target: Element | null = null;
Expand Down Expand Up @@ -233,8 +234,6 @@ export const useAnimation = <Args = void>(
});

useIsomorphicLayoutEffect(() => {
keyframeRef.current = keyframe;
optionsRef.current = options;
if (
options?.autoPlay &&
// Keyframe function may have arguments, so don't handle it for now.
Expand Down
13 changes: 4 additions & 9 deletions src/react/hooks/useAnimationFunction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef } from "react";
import { useEffect } from "react";
import { isSameObject } from "../../core/utils";
import {
TypedKeyframeEffectOptions,
Expand All @@ -14,8 +14,8 @@ import {
WaitingAnimationEventName,
} from "../../core/waapi";
import type { BaseAnimationHandle } from "./useAnimation";
import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect";
import { useStatic } from "./useStatic";
import { useLatestRef } from "./useLatestRef";

/**
* Handle of {@link useAnimationFunction}.
Expand Down Expand Up @@ -70,8 +70,8 @@ export const useAnimationFunction = <Args = void>(
onUpdate: AnimationFunction<Args>,
options?: AnimationFunctionOptions
): AnimationFunctionHandle<Args> => {
const onUpdateRef = useRef(onUpdate);
const optionsRef = useRef(options);
const onUpdateRef = useLatestRef(onUpdate);
const optionsRef = useLatestRef(options);

const [handle, cleanup] = useStatic(
(): [AnimationFunctionHandle<Args>, () => void] => {
Expand Down Expand Up @@ -134,11 +134,6 @@ export const useAnimationFunction = <Args = void>(
}
);

useIsomorphicLayoutEffect(() => {
onUpdateRef.current = onUpdate;
optionsRef.current = options;
});

useEffect(() => cleanup, []);

return handle;
Expand Down
12 changes: 12 additions & 0 deletions src/react/hooks/useLatestRef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useRef } from "react";
import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect";

export const useLatestRef = <T>(value: T) => {
const ref = useRef<T>(value);

useIsomorphicLayoutEffect(() => {
ref.current = value;
}, [value]);

return ref;
};
9 changes: 0 additions & 9 deletions src/react/hooks/usePrevious.ts

This file was deleted.

9 changes: 3 additions & 6 deletions src/react/hooks/useTransitionAnimation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext, useEffect, useRef } from "react";
import { useContext, useEffect } from "react";
import {
EXITED,
EXITING,
Expand All @@ -16,6 +16,7 @@ import type {
TypedKeyframe,
} from "../../core";
import { useStatic } from "./useStatic";
import { useLatestRef } from "./useLatestRef";

export interface TransitionAnimationHandle {
(ref: Element | null): void;
Expand Down Expand Up @@ -47,7 +48,7 @@ export const useTransitionAnimation = (keyframes: {
return acc;
}, {} as { [key in TransitionState]: AnimationHandle | undefined });

const animationsRef = useRef(animations);
const animationsRef = useLatestRef(animations);

const [animation, cleanup] = useStatic(
(): [TransitionAnimationHandle, () => void] => {
Expand Down Expand Up @@ -75,10 +76,6 @@ export const useTransitionAnimation = (keyframes: {
}
);

useIsomorphicLayoutEffect(() => {
animationsRef.current = animations;
});

useEffect(() => cleanup, []);

const currentState = useContext(TransitionStateContext);
Expand Down

0 comments on commit 7d436e8

Please sign in to comment.