Gamote / lottie-react

A lightweight React library for rendering complex After Effects animations in real time using Lottie.
https://lottiereact.com
Other
796 stars 58 forks source link

How to change color icon using Lottie Animation #84

Open rafael-fecha opened 1 year ago

rafael-fecha commented 1 year ago

The same as other lottie packages like lottie react native supports colorFilters:

colorFilters={[
                    {
                        keypath: 'somekeypath',
                        color: 'someColor
                    },

I would like to know if it is possible to achieve color icon changing in this package.

Thanks !

rafael-fecha commented 1 year ago

@Gamote no updates here ?

jonscottclark commented 1 year ago

@rafael-fecha We send the Lottie container to styled-components, and set color of the child svg:

const StyledLottie = styled(Lottie)`
  svg,
  svg path {
    fill: currentColor; // we use `currentColor` to inherit from parent
    stroke: currentColor; // (or whatever colour value you want)
  }
`;
const LoadingAnimation = () => {
  return (
    <StyledLottie
      loop
      autoplay
      animationData={animationData}
    />
  );
};

Hope that helps.

dentemm commented 8 months ago

@jonscottclark that works, thanks! Can you also explain what the syntax would look like to change the color for a single path by name?

dentemm commented 8 months ago

@jonscottclark that works, thanks! Can you also explain what the syntax would look like to change the color for a single path by name?

After digging into the developer console I found a solution:

export const StyledLottie = styled(Lottie)`
  svg path[fill="rgb(205,160,111)"] { // or whatever color you have
    fill: hotpink;
  }
`;