Skip to content

Instantly share code, notes, and snippets.

@zbeyens
Created June 16, 2025 12:32
Show Gist options
  • Save zbeyens/2daf18cfb87d14bd9c62c3229af42354 to your computer and use it in GitHub Desktop.
Save zbeyens/2daf18cfb87d14bd9c62c3229af42354 to your computer and use it in GitHub Desktop.
WithSkeleton
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