···
import { useState, useCallback } from 'react';
import { useLayoutEffect } from './utils/react';
5
-
interface AnimationState {
6
-
animation: Animation;
10
-
const animations = new WeakMap<HTMLElement, AnimationState>();
5
+
const animations = new WeakMap<HTMLElement, Animation>();
export interface TransitionOptions {
8
+
style?: Style | null;
duration?: number | string;
easing?: string | [number, number, number, number];
const animate = (element: HTMLElement, options: TransitionOptions) => {
19
-
const prevState = animations.get(element);
20
-
const prevTo = prevState ? prevState.to : {};
14
+
const style = options.style || {};
const computed = getComputedStyle(element);
const from: Keyframe = {};
25
-
let changed = !prevState;
26
-
for (const propName in options.style) {
27
-
let value: string = options.style[propName];
19
+
for (const propName in style) {
20
+
let value: string = style[propName];
if (typeof value === 'number') (value as string) += 'px';
if (/^--/.test(propName)) {
from[key] = element.style.getPropertyValue(propName);
34
-
element.style.setProperty(key, (to[key] = options.style[propName]));
27
+
element.style.setProperty(key, (to[key] = value));
36
-
if (propName === 'float') {
38
-
} else if (propName === 'offset') {
40
-
} else if (propName === 'transform') {
29
+
if (propName === 'transform') {
('' + value || '').replace(/\w+\((?:0\w*\s*)+\)\s*/g, '') || 'none';
···
from[key] = computed[key];
element.style[key] = to[key] = value;
52
-
changed = changed || prevState!.to[key] !== to[key];
55
-
if (!changed && Object.keys(to).length === Object.keys(prevTo).length) return;
const effect: KeyframeEffectOptions = {
typeof options.duration === 'number'
? options.duration * 1000
46
+
: options.duration || 1000,
easing: Array.isArray(options.easing)
? `cubic-bezier(${options.easing.join(', ')})`
49
+
: options.easing || 'ease',
67
-
if (prevState) prevState.animation.cancel();
52
+
const prevAnimation = animations.get(element);
53
+
if (prevAnimation) prevAnimation.cancel();
const animation = element.animate([from, to], effect);
animation.playbackRate = 1.000001;
···
return new Promise<unknown>((resolve, reject) => {
91
-
animations.set(element, { animation, to });
77
+
animations.set(element, animation);
animation.addEventListener('cancel', reject);
animation.addEventListener('finish', resolve);
···
export function useTransition<T extends HTMLElement>(
99
-
options: TransitionOptions
85
+
options?: TransitionOptions
): [boolean, (options: TransitionOptions) => Promise<void>] {
101
-
const [animating, setAnimating] = useState(false);
87
+
if (!options) options = {};
89
+
const style = options.style || {};
90
+
const [state, setState] = useState<[boolean, Style]>([false, style]);
91
+
if (JSON.stringify(style) !== JSON.stringify(state[1])) {
92
+
setState([true, style]);
const animateTo = useCallback(
(options: TransitionOptions) => {
97
+
const updateAnimating = (animating: boolean) => {
99
+
state[0] !== animating ? [animating, state[1]] : state
const animation = animate(ref.current!, options);
107
-
setAnimating(true);
105
+
updateAnimating(true);
110
-
setAnimating(false);
108
+
updateAnimating(false);
112
+
updateAnimating(false);
return Promise.resolve();
···
121
-
animateTo(options);
122
-
}, [animateTo, options.style]);
120
+
animateTo(options!);
121
+
}, [animateTo, state[1]]);
124
-
return [animating, animateTo];
123
+
return [state[0], animateTo];