lottie-react-component
Version:
Render your lottie animations in React, with full typescript support
126 lines (125 loc) • 5.7 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Lottie = void 0;
const react_1 = __importStar(require("react"));
const lottie_web_1 = __importDefault(require("lottie-web"));
const lottie_api_1 = __importDefault(require("lottie-api"));
const getSize = (initial) => {
if (typeof initial === "number") {
return `${initial}px`;
}
return initial || "100%";
};
const Lottie = ({ eventListeners, isStopped = false, isPaused = false, speed = 1, ariaRole = "button", ariaLabel = "animation", title = "", tabIndex = 0, width, height, style, loop = true, autoplay = true, animationControl, direction, assetsPath, animationData, rendererSettings, renderer, }) => {
const ref = (0, react_1.useRef)(null);
const [animation, setAnimation] = (0, react_1.useState)(null);
const [animationApi, setAnimationApi] = (0, react_1.useState)(null);
const lottieStyles = Object.assign({ width: getSize(width), height: getSize(height), overflow: "hidden", margin: "0 auto", outline: "none" }, style);
(0, react_1.useEffect)(() => {
if (animation) {
eventListeners === null || eventListeners === void 0 ? void 0 : eventListeners.forEach((eventListener) => {
animation.addEventListener(eventListener.eventName, eventListener.callback);
});
}
return () => {
if (animation) {
eventListeners === null || eventListeners === void 0 ? void 0 : eventListeners.forEach((eventListener) => {
animation.removeEventListener(eventListener.eventName, eventListener.callback);
});
}
};
}, [animation, eventListeners]);
(0, react_1.useEffect)(() => {
if (animation && loop !== undefined) {
animation.loop = loop;
}
}, [animation, loop]);
(0, react_1.useEffect)(() => {
if (ref.current) {
const aggregatedOptions = {
container: ref.current,
renderer,
loop: loop !== false,
autoplay: autoplay !== false,
animationData,
rendererSettings,
assetsPath,
};
const newAnimation = lottie_web_1.default.loadAnimation(aggregatedOptions);
newAnimation.setSpeed(speed);
newAnimation.setDirection(direction || 1);
if (isStopped) {
newAnimation.stop();
}
else {
newAnimation.play();
}
const newApi = lottie_api_1.default.createAnimationApi(newAnimation);
setAnimation(newAnimation);
setAnimationApi(newApi);
return () => {
newAnimation.destroy();
};
}
// Yes we're lying to the dependency array - oh well
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [animationData]);
(0, react_1.useEffect)(() => {
if (isStopped) {
animation === null || animation === void 0 ? void 0 : animation.stop();
}
else {
animation === null || animation === void 0 ? void 0 : animation.play();
}
}, [animation, isStopped]);
(0, react_1.useEffect)(() => {
if (animationControl && animationApi) {
const properties = Object.keys(animationControl);
properties.forEach((property) => {
const propertyPath = animationApi.getKeyPath(property);
const value = animationControl[property];
animationApi.addValueCallback(propertyPath, () => value);
});
}
}, [animationApi, animationControl]);
(0, react_1.useEffect)(() => {
animation === null || animation === void 0 ? void 0 : animation.setDirection(direction || 1);
}, [animation, direction]);
(0, react_1.useEffect)(() => {
animation === null || animation === void 0 ? void 0 : animation.setSpeed(speed);
}, [animation, speed]);
(0, react_1.useEffect)(() => {
if (Boolean(isPaused) !== Boolean(animation === null || animation === void 0 ? void 0 : animation.isPaused)) {
animation === null || animation === void 0 ? void 0 : animation.pause();
}
}, [animation, isPaused]);
return (react_1.default.createElement("div", { ref: ref, style: lottieStyles, title: title, role: ariaRole, "aria-label": ariaLabel, tabIndex: tabIndex }));
};
exports.Lottie = Lottie;
exports.default = exports.Lottie;