File size: 852 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import clsx from 'clsx';
import lottie from 'lottie-web/build/player/lottie_light';
import { useEffect, useRef } from 'react';

const AnimatedIcon = ( { icon, className } ) => {
	const iconEl = useRef();

	useEffect( () => {
		const reducedMotion = window.matchMedia( '(prefers-reduced-motion: reduce)' ).matches;

		const animation = lottie.loadAnimation( {
			container: iconEl.current,
			renderer: 'svg',
			loop: false,
			autoplay: ! reducedMotion,
			path: icon,
			rendererSettings: {
				viewBoxOnly: true,
			},
		} );

		if ( reducedMotion ) {
			animation.addEventListener( 'config_ready', () => {
				animation.goToAndPlay( animation.totalFrames, true );
			} );
		}

		return () => animation.destroy();
	}, [ icon ] );

	return <div ref={ iconEl } className={ clsx( 'animated-icon', className ) }></div>;
};

export default AnimatedIcon;