Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/issue 291 2 #299

Merged
merged 3 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/breezy-fireants-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@dotlottie/react-player': patch
'@dotlottie/common': patch
'@dotlottie/player-component': patch
---

fix: loopComplete event does not fire after first cycle if direction is -1
18 changes: 7 additions & 11 deletions packages/common/src/dotlottie-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1596,16 +1596,6 @@
if (this.direction === -1) {
this._container?.dispatchEvent(new Event(PlayerEvents.Complete));
if (!this.loop) this.setCurrentState(PlayerState.Completed);

// Fix: First loopComplete is not fired by lottie-web when direction is -1
if (this.currentState === PlayerState.Playing && this._loop && this._lottie.playCount === 0) {
this._lottie.triggerEvent('loopComplete', {
currentLoop: this._lottie.playCount,
direction: this.direction,
totalLoops: typeof this.loop === 'number' ? this.loop : Infinity,
type: 'loopComplete',
});
}
}
}

Expand Down Expand Up @@ -1851,7 +1841,13 @@
}));

if (autoplay && !hover) {
this.play();
if (loop === false && direction === -1) {
// Trigger manual play since. Autoplay doesn't work in this scenario.
// See logic within play() function: `if (this._lottie.playDirection === -1 && this._lottie.currentFrame === 0) `
this.play();
} else {
this.setCurrentState(PlayerState.Playing);
}
}

this._updateTestData();
Expand All @@ -1869,7 +1865,7 @@
'Worker is only supported with svg renderer. Change or remove renderer prop to get rid of this warning.',
);
}
// @ts-ignore

Check warning on line 1868 in packages/common/src/dotlottie-player.ts

View workflow job for this annotation

GitHub Actions / validate

Do not use "@ts-ignore" because it alters compilation errors
LottieWebModule = await import(`lottie-web/build/player/lottie_worker`);

return LottieWebModule.default;
Expand All @@ -1889,7 +1885,7 @@
if (this._light) {
LottieWebModule = await import(`lottie-web/build/player/lottie_light_canvas`);
} else {
// @ts-ignore

Check warning on line 1888 in packages/common/src/dotlottie-player.ts

View workflow job for this annotation

GitHub Actions / validate

Do not use "@ts-ignore" because it alters compilation errors
LottieWebModule = await import(`lottie-web/build/player/lottie_canvas`);
}

Expand Down
44 changes: 43 additions & 1 deletion packages/react-player/cypress/component/player.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import { PlayerState } from '@dotlottie/common';
import React, { useRef } from 'react';
import React, { useRef, useState } from 'react';
import { DotLottieRefProps } from '../../dist';

import { Controls } from '../../src/controls';
Expand Down Expand Up @@ -90,6 +90,48 @@ describe('Player', () => {
cy.get('[name="currentState"]').should('have.value', PlayerState.Playing);
});

it('should fire initial `loopComplete` when direction is `-1`', () => {
function Wrapper(): JSX.Element {
const lottieRef = useRef<DotLottieRefProps>();
const [initialLoopComplete, setIntialLoopComplete] = useState(false);

return (
<>
<PlayerStateWrapper
onRef={(ref: DotLottieRefProps): void => {
lottieRef.current = ref;
const lottie = ref.getAnimationInstance();
ref.addEventListener('loopComplete', () => {
// Start of second loop
if (lottie.playCount === -2) {
setIntialLoopComplete(true);
}
});
}}
>
<input data-testid="initialLoopComplete" value={String(initialLoopComplete)} />
<DotLottiePlayer
testId="testPlayer"
src={`/bounce_wifi.lottie`}
style={{ height: '400px', display: 'inline-block' }}
loop
autoplay
speed={10}
direction={-1}
>
<Controls />
</DotLottiePlayer>
</PlayerStateWrapper>
</>
);
}

cy.mount(<Wrapper />);

cy.get('[name="currentState"]').should('have.value', PlayerState.Playing);
cy.get('[data-testid="initialLoopComplete"]').should('have.value', 'true');
});

it('should be able to load valid .lottie urls with additional query params', () => {
cy.mount(
<PlayerStateWrapper>
Expand Down
Loading