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

Unique identifier conflict in monorepo with multiple Lottie animations #117

Open
Gregory-Gerard opened this issue Sep 27, 2024 · 0 comments

Comments

@Gregory-Gerard
Copy link

Gregory-Gerard commented Sep 27, 2024

First off, thanks for this awesome library.

Issue

I stumbled upon an interesting edge case that I think could be valuable to document. It's not a bug in lottie-react or lottie-web, but rather a gotcha when using Lottie in a specific setup.

Setup

  • Project structure: Monorepo
  • Multiple applications/packages using lottie-react
  • Two Lottie animations on the same page, coming from different packages

Problem

The animations had clipPath elements with identical identifiers (__lottie_element_X). This caused the latter animation to be "cut off" as its clipPath was overwritten by the first one.

Investigation

Digging into the lottie-web source, I found that unique identifiers are generated using a simple increment:
https://github.com/airbnb/lottie-web/blob/0d658b34c40d4e81eafdccbf698815346454a899/player/js/utils/common.js#L117-L123

Solution

Luckily, lottie-web provides a method to customize the prefix (and lottie-react exports LottiePlayer from lottie-web), which solves this issue. Here's a quick example:

import Lottie, { LottiePlayer } from 'lottie-react';
import animationData from '@assets/lottie/animation.json';

LottiePlayer.setIDPrefix('crm');

export default function MyComponent() {
  return <Lottie animationData={animationData} />;
}

I think this piece of code would be better placed in an index.ts, given that IDPrefix is a global variable, but this is an example. This would avoid duplicating the code and having multiple identifiers that could conflict in the same package.

Could the documentation be improved in this direction? If so, I could make a PR.

Thank you in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant