Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/wc-state-machine-setters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lottiefiles/dotlottie-wc': patch
---

fix: apply `stateMachineId` and `stateMachineConfig` to the player when they are set as properties, not only as attributes
45 changes: 45 additions & 0 deletions packages/wc/__tests__/dotlottie-wc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -735,4 +735,49 @@ describe.each([
expect(stateMachineSetConfig).toHaveBeenCalledTimes(1);
expect(stateMachineSetConfig).toHaveBeenCalledWith(config);
});

test('calls stateMachineLoad and stateMachineStart when the stateMachineId property is set', async () => {
const { element } = render(elementName, { src: smSrc });

const dotLottie = element.dotLottie as DotLottie | DotLottieWorker;

await vi.waitFor(() => {
expect(dotLottie.isLoaded).toBe(true);
});

const stateMachineLoad = vi.spyOn(dotLottie, 'stateMachineLoad').mockReturnValue(true);
const stateMachineStart = vi.spyOn(dotLottie, 'stateMachineStart');
const stateMachineStop = vi.spyOn(dotLottie, 'stateMachineStop');

element.stateMachineId = 'testSM';

expect(stateMachineLoad).toHaveBeenCalledTimes(1);
expect(stateMachineLoad).toHaveBeenCalledWith('testSM');
expect(stateMachineStart).toHaveBeenCalledTimes(1);
expect(element.stateMachineId).toBe('testSM');

element.stateMachineId = undefined;

expect(stateMachineStop).toHaveBeenCalledTimes(1);
});

test('calls stateMachineSetConfig when the stateMachineConfig property is set', async () => {
const { element } = render(elementName, { src: smSrc });

const dotLottie = element.dotLottie as DotLottie | DotLottieWorker;

await vi.waitFor(() => {
expect(dotLottie.isLoaded).toBe(true);
});

const stateMachineSetConfig = vi.spyOn(dotLottie, 'stateMachineSetConfig');

const config = { openUrlPolicy: { whitelist: ['*'] } };

element.stateMachineConfig = config;

expect(stateMachineSetConfig).toHaveBeenCalledTimes(1);
expect(stateMachineSetConfig).toHaveBeenCalledWith(config);
expect(element.stateMachineConfig).toEqual(config);
});
});
55 changes: 33 additions & 22 deletions packages/wc/src/base-dotlottie-wc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export abstract class BaseDotLottieWC<T extends DotLottie | DotLottieWorker> ext

private _themeId: Config['themeId'];

private _stateMachineId: Config['stateMachineId'];

private _stateMachineConfig: Config['stateMachineConfig'];

private _isPlayerReady(): boolean {
return this.dotLottie?.isReady ?? false;
}
Expand Down Expand Up @@ -193,10 +197,35 @@ export abstract class BaseDotLottieWC<T extends DotLottie | DotLottieWorker> ext
public workerId?: string;

@property({ type: String })
public stateMachineId: Config['stateMachineId'];
public get stateMachineId(): Config['stateMachineId'] {
return this._stateMachineId;
}

public set stateMachineId(value: Config['stateMachineId']) {
this._stateMachineId = value;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, tested on both elements and it works well.

One thing: the old attribute branch skipped when old === value. Now setting the same id again restarts the state machine. A guard here brings that back:

Suggested change
this._stateMachineId = value;
if (value === this._stateMachineId) return;
this._stateMachineId = value;


if (!this.dotLottie?.isLoaded) {
return;
}

if (value) {
if (this.dotLottie.stateMachineLoad(value)) {
this.dotLottie.stateMachineStart();
}
} else {
this.dotLottie.stateMachineStop();
}
}

@property({ type: Object })
public stateMachineConfig: Config['stateMachineConfig'];
public get stateMachineConfig(): Config['stateMachineConfig'] {
return this._stateMachineConfig;
}

public set stateMachineConfig(value: Config['stateMachineConfig']) {
this._stateMachineConfig = value;
this.dotLottie?.stateMachineSetConfig(value ?? null);
}

/**
* Supplies bytes for assets the player cannot load itself. Set it as a property before `src` or `data`.
Expand Down Expand Up @@ -233,8 +262,8 @@ export abstract class BaseDotLottieWC<T extends DotLottie | DotLottieWorker> ext
useFrameInterpolation: this._useFrameInterpolation,
themeId: this._themeId,
backgroundColor: this._backgroundColor,
stateMachineConfig: this.stateMachineConfig,
stateMachineId: this.stateMachineId,
stateMachineConfig: this._stateMachineConfig,
stateMachineId: this._stateMachineId,
assetResolver: this.assetResolver,
};
}
Expand Down Expand Up @@ -296,24 +325,6 @@ export abstract class BaseDotLottieWC<T extends DotLottie | DotLottieWorker> ext
animationId: this._animationId,
});
}

if (name === 'statemachineid') {
if (this.dotLottie.isLoaded) {
if (value) {
const smLoaded = this.dotLottie.stateMachineLoad(value);

if (smLoaded) {
this.dotLottie.stateMachineStart();
}
} else {
this.dotLottie.stateMachineStop();
}
}
}

if (name === 'statemachineconfig') {
this.dotLottie.stateMachineSetConfig(value ? JSON.parse(value) : null);
}
}

protected abstract _createDotLottieInstance(config: Config & { workerId?: string }): T;
Expand Down
Loading