A glare-card component from Aceternity UI.
From Aceternity UI
1"use client";
2import { cn } from "@/lib/utils";
3import { useRef } from "react";
4
5export const GlareCard = ({
6 children,
7 className,
8}: {
9 children: React.ReactNode;
10 className?: string;
11}) => {
12 const isPointerInside = useRef(false);
13 const refElement = useRef<HTMLDivElement>(null);
14 const state = useRef({
15 glare: {
16 x: 50,
17 y: 50,
18 },
19 background: {
20 x: 50,
21 y: 50,
22 },
23 rotate: {
24 x: 0,
25 y: 0,
26 },
27 });
28 const containerStyle = {
29 "--m-x": "50%",
30 "--m-y": "50%",
31 "--r-x": "0deg",
32 "--r-y": "0deg",
33 "--bg-x": "50%",
34 "--bg-y": "50%",
35 "--duration": "300ms",
36 "--foil-size": "100%",
37 "--opacity": "0",
38 "--radius": "48px",
39 "--easing": "ease",
40 "--transition": "var(--duration) var(--easing)",
41 } as any;
42
43 const backgroundStyle = {
44 "--step": "5%",
45 "--foil-svg": `url("data:image/svg+xml,%3Csvg width='26' height='26' viewBox='0 0 26 26' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M2.99994 3.419C2.99994 3.419 21.6142 7.43646 22.7921 12.153C23.97 16.8695 3.41838 23.0306 3.41838 23.0306' stroke='white' stroke-width='5' stroke-miterlimit='3.86874' stroke-linecap='round' style='mix-blend-mode:darken'/%3E%3C/svg%3E")`,
46 "--pattern": "var(--foil-svg) center/100% no-repeat",
47 "--rainbow":
48 "repeating-linear-gradient( 0deg,rgb(255,119,115) calc(var(--step) * 1),rgba(255,237,95,1) calc(var(--step) * 2),rgba(168,255,95,1) calc(var(--step) * 3),rgba(131,255,247,1) calc(var(--step) * 4),rgba(120,148,255,1) calc(var(--step) * 5),rgb(216,117,255) calc(var(--step) * 6),rgb(255,119,115) calc(var(--step) * 7) ) 0% var(--bg-y)/200% 700% no-repeat",
49 "--diagonal":
50 "repeating-linear-gradient( 128deg,#0e152e 0%,hsl(180,10%,60%) 3.8%,hsl(180,10%,60%) 4.5%,hsl(180,10%,60%) 5.2%,#0e152e 10%,#0e152e 12% ) var(--bg-x) var(--bg-y)/300% no-repeat",
51 "--shade":
52 "radial-gradient( farthest-corner circle at var(--m-x) var(--m-y),rgba(255,255,255,0.1) 12%,rgba(255,255,255,0.15) 20%,rgba(255,255,255,0.25) 120% ) var(--bg-x) var(--bg-y)/300% no-repeat",
53 backgroundBlendMode: "hue, hue, hue, overlay",
54 };
55
56 const updateStyles = () => {
57 if (refElement.current) {
58 console.log(state.current);
59 const { background, rotate, glare } = state.current;
60 refElement.current?.style.setProperty("--m-x", `${glare.x}%`);
61 refElement.current?.style.setProperty("--m-y", `${glare.y}%`);
62 refElement.current?.style.setProperty("--r-x", `${rotate.x}deg`);
63 refElement.current?.style.setProperty("--r-y", `${rotate.y}deg`);
64 refElement.current?.style.setProperty("--bg-x", `${background.x}%`);
65 refElement.current?.style.setProperty("--bg-y", `${background.y}%`);
66 }
67 };
68 return (
69 <div
70 style={containerStyle}
71 className="relative isolate [aspect-ratio:17/21] w-[320px] transition-transform delay-[var(--delay)] duration-[var(--duration)] ease-[var(--easing)] will-change-transform [contain:layout_style] [perspective:600px]"
72 ref={refElement}
73 onPointerMove={(event) => {
74 const rotateFactor = 0.4;
75 const rect = event.currentTarget.getBoundingClientRect();
76 const position = {
77 x: event.clientX - rect.left,
78 y: event.clientY - rect.top,
79 };
80 const percentage = {
81 x: (100 / rect.width) * position.x,
82 y: (100 / rect.height) * position.y,
83 };
84 const delta = {
85 x: percentage.x - 50,
86 y: percentage.y - 50,
87 };
88
89 const { background, rotate, glare } = state.current;
90 background.x = 50 + percentage.x / 4 - 12.5;
91 background.y = 50 + percentage.y / 3 - 16.67;
92 rotate.x = -(delta.x / 3.5);
93 rotate.y = delta.y / 2;
94 rotate.x *= rotateFactor;
95 rotate.y *= rotateFactor;
96 glare.x = percentage.x;
97 glare.y = percentage.y;
98
99 updateStyles();
100 }}
101 onPointerEnter={() => {
102 isPointerInside.current = true;
103 if (refElement.current) {
104 setTimeout(() => {
105 if (isPointerInside.current) {
106 refElement.current?.style.setProperty("--duration", "0s");
107 }
108 }, 300);
109 }
110 }}
111 onPointerLeave={() => {
112 isPointerInside.current = false;
113 if (refElement.current) {
114 refElement.current.style.removeProperty("--duration");
115 refElement.current?.style.setProperty("--r-x", `0deg`);
116 refElement.current?.style.setProperty("--r-y", `0deg`);
117 }
118 }}
119 >
120 <div className="grid h-full origin-center [transform:rotateY(var(--r-x))_rotateX(var(--r-y))] overflow-hidden rounded-[var(--radius)] border border-slate-800 transition-transform delay-[var(--delay)] duration-[var(--duration)] ease-[var(--easing)] will-change-transform hover:filter-none hover:[--duration:200ms] hover:[--easing:linear] hover:[--opacity:0.6]">
121 <div className="grid h-full w-full mix-blend-soft-light [clip-path:inset(0_0_0_0_round_var(--radius))] [grid-area:1/1]">
122 <div className={cn("h-full w-full bg-slate-950", className)}>
123 {children}
124 </div>
125 </div>
126 <div className="transition-background will-change-background grid h-full w-full opacity-[var(--opacity)] mix-blend-soft-light transition-opacity delay-[var(--delay)] duration-[var(--duration)] ease-[var(--easing)] [background:radial-gradient(farthest-corner_circle_at_var(--m-x)_var(--m-y),_rgba(255,255,255,0.8)_10%,_rgba(255,255,255,0.65)_20%,_rgba(255,255,255,0)_90%)] [clip-path:inset(0_0_1px_0_round_var(--radius))] [grid-area:1/1]" />
127 <div
128 className="will-change-background after:grid-area-[inherit] after:bg-repeat-[inherit] after:bg-attachment-[inherit] after:bg-origin-[inherit] after:bg-clip-[inherit] relative grid h-full w-full opacity-[var(--opacity)] [background-blend-mode:hue_hue_hue_overlay] mix-blend-color-dodge transition-opacity [background:var(--pattern),_var(--rainbow),_var(--diagonal),_var(--shade)] [clip-path:inset(0_0_1px_0_round_var(--radius))] [grid-area:1/1] after:bg-[inherit] after:[background-size:var(--foil-size),_200%_400%,_800%,_200%] after:[background-position:center,_0%_var(--bg-y),_calc(var(--bg-x)*_-1)_calc(var(--bg-y)*_-1),_var(--bg-x)_var(--bg-y)] after:[background-blend-mode:soft-light,_hue,_hard-light] after:mix-blend-exclusion after:content-['']"
129 style={{ ...backgroundStyle }}
130 />
131 </div>
132 </div>
133 );
134};
135