Created
June 16, 2025 12:32
-
-
Save zbeyens/2daf18cfb87d14bd9c62c3229af42354 to your computer and use it in GitHub Desktop.
WithSkeleton
This file contains hidden or 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
import React from "react"; | |
import { useMounted } from "@/hooks/use-mounted"; | |
import { cn } from "@/lib/utils"; | |
export function Skeleton({ className, ...props }: React.ComponentProps<"div">) { | |
return ( | |
<div | |
className={cn("animate-pulse rounded-md bg-muted", className)} | |
{...props} | |
/> | |
); | |
} | |
export function WithSkeleton({ | |
children, | |
className, | |
isLoading, | |
...props | |
}: React.ComponentProps<"div"> & { | |
isLoading?: boolean; | |
}) { | |
const mounted = useMounted(); | |
return ( | |
<div className={cn("relative w-fit", className)} {...props}> | |
{children} | |
{(!mounted || isLoading) && ( | |
<> | |
<div className={cn("absolute inset-0 bg-background", className)} /> | |
<Skeleton className={cn("absolute inset-0", className)} /> | |
</> | |
)} | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment