Skip to content

Commit 9a4378b

Browse files
committed
Add fallback for commitStyles
1 parent 7d92633 commit 9a4378b

File tree

5 files changed

+48
-10
lines changed

5 files changed

+48
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Currently it only detects enter/update/exit of children by key, that works simil
118118
1. [browsers that have Element.animate()](https://caniuse.com/mdn-api_element_animate)
119119
1. browsers that have no Web Animations APIs
120120

121-
In 1, you can use all functions of this library without polyfill. Some of the newer features like [composite mode](https://caniuse.com/web-animation), [commitStyles](https://caniuse.com/mdn-api_animation_commitstyles) and [CSS Motion Path](https://caniuse.com/css-motion-paths) may be ignored in some browsers though.
121+
In 1, you can use all functions of this library without polyfill. Some of the newer features like [composite mode](https://caniuse.com/web-animation) and [CSS Motion Path](https://caniuse.com/css-motion-paths) may be ignored in some browsers though.
122122

123123
In 2, you can use this library but `useAnimationFuction` would not work.
124124

src/hooks/core.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,21 @@ export const createHandle = () => {
6868
if (!animation) return;
6969
animation.pause();
7070
},
71-
_commit: (animation: Animation | undefined) => {
71+
_commit: (
72+
animation: Animation | undefined,
73+
el: HTMLElement,
74+
keys: string[]
75+
) => {
7276
if (!animation) return;
73-
animation.commitStyles?.();
77+
if (animation.commitStyles) {
78+
animation.commitStyles();
79+
return;
80+
}
81+
// Fallback for commitStyles
82+
const computedStyle = getComputedStyle(el);
83+
keys.forEach((k) => {
84+
(el.style as any)[k] = (computedStyle as any)[k];
85+
});
7486
},
7587
_setTime: (animation: Animation | undefined, time: number) => {
7688
if (!animation) return;

src/hooks/useAnimation.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { createRef, useEffect, useLayoutEffect, useRef, useState } from "react";
2-
import { isArray, isSameObject, isSameObjectArray } from "../utils";
2+
import {
3+
getKeys,
4+
isSameObject,
5+
isSameObjectArray,
6+
toArray,
7+
uniqBy,
8+
} from "../utils";
39
import {
410
AnimationOptions,
511
createAnimation,
@@ -35,7 +41,7 @@ export const useAnimation = (
3541
const ref = createRef<HTMLElement>();
3642

3743
const getTarget = () => ref.current;
38-
const getKeyframes = () => keyframeRef.current;
44+
const getKeyframes = () => toArray(keyframeRef.current);
3945
const getOptions = () => optionsRef.current;
4046

4147
let cache:
@@ -47,11 +53,10 @@ export const useAnimation = (
4753
]
4854
| undefined;
4955
const initAnimation = (
50-
kf: TypedKeyframe | TypedKeyframe[],
56+
keyframes: TypedKeyframe[],
5157
options: AnimationOptions | undefined
5258
): Animation => {
5359
const el = getTarget()!;
54-
const keyframes = isArray(kf) ? kf : [kf];
5560
if (cache) {
5661
const [prevAnimation, prevEl, prevKeyframes, prevOptions] = cache;
5762
if (
@@ -96,7 +101,11 @@ export const useAnimation = (
96101
return externalHandle;
97102
},
98103
commit: () => {
99-
handle._commit(getAnimation());
104+
handle._commit(
105+
getAnimation(),
106+
getTarget()!,
107+
uniqBy(getKeys(getKeyframes()))
108+
);
100109
return externalHandle;
101110
},
102111
setTime: (time) => {

src/hooks/useAnimations.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { createRef, useEffect, useLayoutEffect, useRef, useState } from "react";
2-
import { isArray, isSameObject, isSameObjectArray } from "../utils";
2+
import {
3+
getKeys,
4+
isArray,
5+
isSameObject,
6+
isSameObjectArray,
7+
uniqBy,
8+
} from "../utils";
39
import {
410
AnimationOptions,
511
createAnimation,
@@ -97,7 +103,11 @@ export const useAnimations = <ID extends string>(
97103
return externalHandle;
98104
},
99105
commit: (name) => {
100-
handle._commit(getAnimation(name));
106+
handle._commit(
107+
getAnimation(name),
108+
getTarget()!,
109+
uniqBy(getKeys(getKeyframesAndOptions(name)[0]))
110+
);
101111
return externalHandle;
102112
},
103113
setTime: (name, time) => {

src/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,10 @@ export const isSameObjectArray = (
1818
if (target.length !== prev.length) return false;
1919
return target.every((t, i) => isSameObject(t, prev[i]));
2020
};
21+
22+
export const toArray = <T>(items: T | T[]): T[] =>
23+
isArray(items) ? items : [items];
24+
25+
export const uniqBy = <T extends string | number>(items: T[]): T[] => {
26+
return Array.from(new Set(items));
27+
};

0 commit comments

Comments
 (0)