-
Notifications
You must be signed in to change notification settings - Fork 669
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Innei <i@innei.in>
- Loading branch information
Showing
7 changed files
with
126 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
apps/renderer/src/components/ui/lottie-container/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import type { DotLottie } from "@lottiefiles/dotlottie-react" | ||
import { DotLottieReact } from "@lottiefiles/dotlottie-react" | ||
import { atom, useAtomValue } from "jotai" | ||
import type { FC, ReactNode, RefCallback } from "react" | ||
import { useEffect, useState } from "react" | ||
|
||
import { jotaiStore } from "~/lib/jotai" | ||
|
||
import { RootPortal } from "../portal" | ||
|
||
const portalElementsAtom = atom([] as ReactNode[]) | ||
|
||
export function LottieRenderContainer() { | ||
const elements = useAtomValue(portalElementsAtom, { store: jotaiStore }) | ||
|
||
return ( | ||
<RootPortal> | ||
<div className="pointer-events-none fixed z-[999]" data-testid="lottie-render-container"> | ||
{elements.map((element) => element)} | ||
</div> | ||
</RootPortal> | ||
) | ||
} | ||
|
||
type LottieOptions = { | ||
once?: boolean | ||
|
||
x: number | ||
y: number | ||
|
||
height?: number | ||
width?: number | ||
className?: string | ||
|
||
onComplete?: () => void | ||
|
||
speed?: number | ||
} | ||
export const mountLottie = (url: string, options: LottieOptions) => { | ||
const { once = true, height, width, x, y, className, speed } = options | ||
|
||
const Lottie: FC = () => { | ||
const [dotLottie, setDotLottie] = useState<DotLottie | null>(null) | ||
|
||
useEffect(() => { | ||
function onComplete() { | ||
if (once) { | ||
unmount() | ||
} | ||
|
||
options.onComplete?.() | ||
} | ||
|
||
if (dotLottie) { | ||
dotLottie.addEventListener("complete", onComplete) | ||
} | ||
|
||
return () => { | ||
if (dotLottie) { | ||
dotLottie.removeEventListener("complete", onComplete) | ||
} | ||
} | ||
}, [dotLottie]) | ||
|
||
const dotLottieRefCallback: RefCallback<DotLottie> = (dotLottie) => { | ||
setDotLottie(dotLottie) | ||
} | ||
|
||
return ( | ||
<DotLottieReact | ||
speed={speed} | ||
dotLottieRefCallback={dotLottieRefCallback} | ||
src={url} | ||
autoplay | ||
loop={false} | ||
height={height} | ||
width={width} | ||
style={{ | ||
height, | ||
width, | ||
position: "fixed", | ||
|
||
left: 0, | ||
top: 0, | ||
transform: `translate(${x}px, ${y}px)`, | ||
}} | ||
className={className} | ||
/> | ||
) | ||
} | ||
|
||
const element = <Lottie /> | ||
const unmount = () => { | ||
jotaiStore.set(portalElementsAtom, (prev) => prev.filter((e) => e !== element)) | ||
} | ||
|
||
jotaiStore.set(portalElementsAtom, (prev) => [...prev, element]) | ||
return unmount | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters