Files
hats-app/components/ui/lazy-image.tsx
T
Ichsan Kurniawan fb2d746fc0 Add UI components and Tooltip provider
Introduce a set of reusable UI primitives and integrate tooltip provider in the app layout. Adds: Combobox (with virtualized and non-virtualized variants), Command palette components (cmdk wrapper and related subcomponents), DatePicker and DateTimePicker (popover-based date/time selection with time pickers and Done footer), LazyImage (progressive image with blur/scale transition), and Tooltip (Radix wrapper and provider). Also imports TooltipProvider in app/layout.tsx and wraps the app content so tooltips are available globally.
2026-04-16 22:37:45 +07:00

33 lines
1.1 KiB
TypeScript

"use client"
import { cn } from "@/lib/utils";
import { useState, useRef, useEffect } from "react";
export function LazyImage({ src, alt, className }: { src: string; alt: string; className?: string }) {
const [isLoaded, setIsLoaded] = useState(false);
const imgRef = useRef<HTMLImageElement>(null);
// Check if the image is already loaded from the cache
useEffect(() => {
if (imgRef.current?.complete) {
setIsLoaded(true);
}
}, [src]); // Re-run this check if the src prop changes
return (
<div className={cn("relative overflow-hidden bg-secondary/50", className)}>
<img
ref={imgRef}
src={src}
alt={alt}
loading="lazy"
referrerPolicy="no-referrer"
onLoad={() => setIsLoaded(true)}
className={cn(
"w-full h-full object-cover transition-all duration-700 ease-in-out",
isLoaded ? "blur-0 scale-100 opacity-100" : "blur-xl scale-110 opacity-0"
)}
/>
</div>
);
}