Skip to content

Commit 245c772

Browse files
committed
Change replay method to reset option of play
1 parent e18298b commit 245c772

File tree

5 files changed

+24
-30
lines changed

5 files changed

+24
-30
lines changed

src/hooks/core.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ export const createAnimation = (
4545
}
4646
};
4747

48+
export type PlayOptions = { reset?: boolean };
49+
4850
export const createHandle = () => {
4951
const handle = {
50-
_play: (animation: Animation) => {
51-
animation.play();
52-
},
53-
_replay: (animation: Animation) => {
54-
animation.currentTime = 0;
52+
_play: (animation: Animation, opts: PlayOptions = {}) => {
53+
if (opts.reset) {
54+
animation.currentTime = 0;
55+
}
5556
animation.play();
5657
},
5758
_reverse: (animation: Animation) => {

src/hooks/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export type {
33
TypedEasing,
44
AnimationOptions,
55
AnimatableCSSProperties,
6+
PlayOptions,
67
} from "./core";
78
export { useAnimation } from "./useAnimation";
89
export type { AnimationHandle, WithRef } from "./useAnimation";

src/hooks/useAnimation.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import {
44
AnimationOptions,
55
createAnimation,
66
createHandle,
7+
PlayOptions,
78
TypedKeyframe,
89
} from "./core";
910

1011
export type AnimationHandle = {
11-
play: () => AnimationHandle;
12-
replay: () => AnimationHandle;
12+
play: (opts?: PlayOptions) => AnimationHandle;
1313
reverse: () => AnimationHandle;
1414
cancel: () => AnimationHandle;
1515
finish: () => AnimationHandle;
@@ -70,12 +70,8 @@ export const useAnimation = (
7070
const handle = createHandle();
7171

7272
const externalHandle: WithRef<AnimationHandle> = {
73-
play: () => {
74-
handle._play(initAnimation(getKeyframes(), getOptions()));
75-
return externalHandle;
76-
},
77-
replay: () => {
78-
handle._replay(initAnimation(getKeyframes(), getOptions()));
73+
play: (opts) => {
74+
handle._play(initAnimation(getKeyframes(), getOptions()), opts);
7975
return externalHandle;
8076
},
8177
reverse: () => {

src/hooks/useAnimationFunction.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { useEffect, useLayoutEffect, useRef, useState } from "react";
22
import { isSameObject } from "../utils";
3-
import { AnimationOptions, createAnimation, createHandle } from "./core";
3+
import {
4+
AnimationOptions,
5+
createAnimation,
6+
createHandle,
7+
PlayOptions,
8+
} from "./core";
49

510
export type AnimationFunctionHandle = {
6-
play: () => AnimationFunctionHandle;
7-
replay: () => AnimationFunctionHandle;
11+
play: (opts?: PlayOptions) => AnimationFunctionHandle;
812
reverse: () => AnimationFunctionHandle;
913
cancel: () => AnimationFunctionHandle;
1014
finish: () => AnimationFunctionHandle;
@@ -76,12 +80,8 @@ export const useAnimationFunction = (
7680
const handle = createHandle();
7781

7882
const externalHandle: AnimationFunctionHandle = {
79-
play: () => {
80-
handle._play(initAnimation(getOptions()));
81-
return externalHandle;
82-
},
83-
replay: () => {
84-
handle._replay(initAnimation(getOptions()));
83+
play: (opts) => {
84+
handle._play(initAnimation(getOptions()), opts);
8585
return externalHandle;
8686
},
8787
reverse: () => {

src/hooks/useAnimations.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import {
44
AnimationOptions,
55
createAnimation,
66
createHandle,
7+
PlayOptions,
78
TypedKeyframe,
89
} from "./core";
910
import type { WithRef } from "./useAnimation";
1011

1112
export type AnimationsHandle<ID extends string> = {
12-
play: (name: ID) => AnimationsHandle<ID>;
13-
replay: (name: ID) => AnimationsHandle<ID>;
13+
play: (name: ID, opts?: PlayOptions) => AnimationsHandle<ID>;
1414
reverse: (name: ID) => AnimationsHandle<ID>;
1515
cancel: (name: ID) => AnimationsHandle<ID>;
1616
finish: (name: ID) => AnimationsHandle<ID>;
@@ -72,12 +72,8 @@ export const useAnimations = <ID extends string>(
7272
const handle = createHandle();
7373

7474
const externalHandle: WithRef<AnimationsHandle<ID>> = {
75-
play: (name) => {
76-
handle._play(initAnimation(name));
77-
return externalHandle;
78-
},
79-
replay: (name) => {
80-
handle._replay(initAnimation(name));
75+
play: (name, opts) => {
76+
handle._play(initAnimation(name), opts);
8177
return externalHandle;
8278
},
8379
reverse: (name) => {

0 commit comments

Comments
 (0)