feat: dynamic loading of lottie

This commit is contained in:
DIYgod
2023-06-16 17:24:48 +01:00
parent ae4511e8c8
commit 495e377a00
+22 -9
View File
@@ -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<LottieRefCurrentProps>(null)
const containerRef = useRef<HTMLDivElement>(null)
const [isInView] = useInViewport(containerRef)
switch (type) {
case "svg":
return (
@@ -43,18 +50,24 @@ export const Logo = ({
)
case "lottie":
return (
<Lottie
animationData={LogoLottieJSON}
loop={loop ?? false}
autoplay={autoplay ?? true}
lottieRef={ref}
onMouseEnter={() => ref.current?.goToAndPlay(0)}
<div
ref={containerRef}
style={{
width: width || 100,
height: height || 100,
}}
className="xlog-lottie-logo"
/>
>
{isInView && (
<DynamicLottie
animationData={LogoLottieJSON}
loop={loop ?? false}
autoplay={autoplay ?? true}
lottieRef={ref}
onMouseEnter={() => ref.current?.goToAndPlay(0)}
className="xlog-lottie-logo text-black w-full h-full"
/>
)}
</div>
)
}
}