Skip to content

Commit 0c56721

Browse files
committed
animation.id, animation.cancel
Use KeyframeAnimationOptions instead of KeyframeEffectOptions to read id from options and attach it to the returned animation Remove an animation from animations when the cancel method is called
1 parent 6887572 commit 0c56721

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

src/mocks/web-animations-api/__tests__/index.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,22 @@ describe('Animations API', () => {
3535
expect(element.getAnimations().length).toBe(0);
3636
expect(document.getAnimations().length).toBe(0);
3737
});
38+
39+
it('should read id from options and attach it to the returned animation', async () => {
40+
const element = document.createElement('div');
41+
const testId = 'testId';
42+
43+
const animation = element.animate({ opacity: 0 }, { id: testId });
44+
expect(animation.id).toBe(testId);
45+
});
46+
47+
it('should remove an animation from element if the animation was cancelled', () => {
48+
const element = document.createElement('div');
49+
50+
const animation = element.animate({ opacity: 0 }, 1000);
51+
expect(element.getAnimations().length).toBe(1);
52+
53+
animation.cancel();
54+
expect(element.getAnimations().length).toBe(0);
55+
});
3856
});

src/mocks/web-animations-api/index.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,42 @@ import { mockAnimation } from './Animation';
22

33
const elementAnimations = new Map<Element, Animation[]>();
44

5+
function removeFromAnimations(element: Element, animation: Animation) {
6+
const animations = elementAnimations.get(element);
7+
8+
if (animations) {
9+
const index = animations.indexOf(animation);
10+
11+
if (index !== -1) {
12+
animations.splice(index, 1);
13+
}
14+
}
15+
}
16+
517
function animate(
618
this: Element,
719
keyframes: Keyframe[],
8-
options?: number | KeyframeEffectOptions
20+
options?: number | KeyframeAnimationOptions
921
) {
1022
const keyframeEffect = new KeyframeEffect(this, keyframes, options);
1123

1224
const animation = new Animation(keyframeEffect);
25+
if (typeof options == 'object' && options.id) {
26+
animation.id = options.id;
27+
}
1328

1429
const animations = elementAnimations.get(this) ?? [];
1530

1631
animations.push(animation);
1732

1833
elementAnimations.set(this, animations);
1934

20-
animation.addEventListener('finish', () => {
21-
const animations = elementAnimations.get(this);
22-
23-
if (animations) {
24-
const index = animations.indexOf(animation);
25-
26-
if (index !== -1) {
27-
animations.splice(index, 1);
28-
}
29-
}
30-
});
35+
animation.addEventListener('finish', () =>
36+
removeFromAnimations(this, animation)
37+
);
38+
animation.addEventListener('cancel', () =>
39+
removeFromAnimations(this, animation)
40+
);
3141

3242
animation.play();
3343

0 commit comments

Comments
 (0)