From 495e377a003cdf6275de53740f3219f2384e9647 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Fri, 16 Jun 2023 17:24:48 +0100 Subject: [PATCH] feat: dynamic loading of lottie --- src/components/common/Logo.tsx | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/components/common/Logo.tsx b/src/components/common/Logo.tsx index 8f164cc2..60043b8e 100644 --- a/src/components/common/Logo.tsx +++ b/src/components/common/Logo.tsx @@ -1,12 +1,16 @@ "use client" -import Lottie, { type LottieRefCurrentProps } from "lottie-react" +import { useInViewport } from "ahooks" +import { type LottieRefCurrentProps } from "lottie-react" +import dynamic from "next/dynamic" import React, { useRef } from "react" import { Image } from "~/components/ui/Image" import LogoLottieJSON from "../../../public/assets/logo.json" +const DynamicLottie = dynamic(() => import("lottie-react")) + export const Logo = ({ type, width, @@ -22,6 +26,9 @@ export const Logo = ({ }) => { const ref = useRef(null) + const containerRef = useRef(null) + const [isInView] = useInViewport(containerRef) + switch (type) { case "svg": return ( @@ -43,18 +50,24 @@ export const Logo = ({ ) case "lottie": return ( - ref.current?.goToAndPlay(0)} +
+ > + {isInView && ( + ref.current?.goToAndPlay(0)} + className="xlog-lottie-logo text-black w-full h-full" + /> + )} +
) } }