Glare Hover
Card with a glare on hover that sweeps across its surface.
pnpm dlx shadcn@latest add @leo/glare-hoverProps
| Name | Type | Default | Description |
|---|---|---|---|
| background | string | undefined | #000 | |
| borderColor | string | undefined | #333 | |
| borderRadius | string | undefined | 10px | |
| children | ReactNode | - | |
| className | string | undefined | - | |
| glareAngle | number | undefined | -45 | |
| glareColor | string | undefined | #ffffff | |
| glareOpacity | number | undefined | 0.5 | |
| glareSize | number | undefined | 250 | |
| height | string | undefined | 500px | |
| playOnce | boolean | undefined | false | |
| style | CSSProperties | undefined | {} | |
| transitionDuration | number | undefined | 650 | |
| width | string | undefined | 500px |
Source
"use client";
import type React from "react";
import { useRef } from "react";
import { cn } from "@/lib/utils";
export interface GlareHoverProps {
width?: string;
height?: string;
background?: string;
borderRadius?: string;
borderColor?: string;
children?: React.ReactNode;
glareColor?: string;
glareOpacity?: number;
glareAngle?: number;
glareSize?: number;
transitionDuration?: number;
playOnce?: boolean;
className?: string;
style?: React.CSSProperties;
}
const GlareHover: React.FC<GlareHoverProps> = ({
width = "500px",
height = "500px",
background = "#000",
borderRadius = "10px",
borderColor = "#333",
children,
glareColor = "#ffffff",
glareOpacity = 0.5,
glareAngle = -45,
glareSize = 250,
transitionDuration = 650,
playOnce = false,
className,
style = {},
}) => {
const hex = glareColor.replace("#", "");
let rgba = glareColor;
if (/^[\dA-Fa-f]{6}$/.test(hex)) {
const r = parseInt(hex.slice(0, 2), 16);
const g = parseInt(hex.slice(2, 4), 16);
const b = parseInt(hex.slice(4, 6), 16);
rgba = `rgba(${r}, ${g}, ${b}, ${glareOpacity})`;
} else if (/^[\dA-Fa-f]{3}$/.test(hex)) {
const r = parseInt(hex[0] + hex[0], 16);
const g = parseInt(hex[1] + hex[1], 16);
const b = parseInt(hex[2] + hex[2], 16);
rgba = `rgba(${r}, ${g}, ${b}, ${glareOpacity})`;
}
const overlayRef = useRef<HTMLDivElement | null>(null);
const animateIn = () => {
const el = overlayRef.current;
if (!el) return;
el.style.transition = "none";
el.style.backgroundPosition = "-100% -100%, 0 0";
el.style.transition = `${transitionDuration}ms ease`;
el.style.backgroundPosition = "100% 100%, 0 0";
};
const animateOut = () => {
const el = overlayRef.current;
if (!el) return;
if (playOnce) {
el.style.transition = "none";
el.style.backgroundPosition = "-100% -100%, 0 0";
} else {
el.style.transition = `${transitionDuration}ms ease`;
el.style.backgroundPosition = "-100% -100%, 0 0";
}
};
const overlayStyle: React.CSSProperties = {
position: "absolute",
inset: 0,
background: `linear-gradient(${glareAngle}deg,
hsla(0,0%,0%,0) 60%,
${rgba} 70%,
hsla(0,0%,0%,0) 100%)`,
backgroundSize: `${glareSize}% ${glareSize}%, 100% 100%`,
backgroundRepeat: "no-repeat",
backgroundPosition: "-100% -100%, 0 0",
pointerEvents: "none",
};
return (
// biome-ignore lint/a11y/noStaticElementInteractions: hover only triggers a decorative glare
<div
className={cn(
"relative grid place-items-center overflow-hidden border cursor-pointer",
className,
)}
style={{
width,
height,
background,
borderRadius,
borderColor,
...style,
}}
onMouseEnter={animateIn}
onMouseLeave={animateOut}
>
<div ref={overlayRef} style={overlayStyle} />
{children}
</div>
);
};
export { GlareHover };
export default GlareHover;