A hero-highlight component from Aceternity UI.
From Aceternity UI
1"use client";
2import { cn } from "@/lib/utils";
3import { useMotionValue, motion, useMotionTemplate } from "motion/react";
4import React from "react";
5
6export const HeroHighlight = ({
7 children,
8 className,
9 containerClassName,
10}: {
11 children: React.ReactNode;
12 className?: string;
13 containerClassName?: string;
14}) => {
15 let mouseX = useMotionValue(0);
16 let mouseY = useMotionValue(0);
17
18 // SVG patterns for different states and themes
19 const dotPatterns = {
20 light: {
21 default: `url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32' width='16' height='16' fill='none'%3E%3Ccircle fill='%23d4d4d4' id='pattern-circle' cx='10' cy='10' r='2.5'%3E%3C/circle%3E%3C/svg%3E")`,
22 hover: `url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32' width='16' height='16' fill='none'%3E%3Ccircle fill='%236366f1' id='pattern-circle' cx='10' cy='10' r='2.5'%3E%3C/circle%3E%3C/svg%3E")`,
23 },
24 dark: {
25 default: `url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32' width='16' height='16' fill='none'%3E%3Ccircle fill='%23404040' id='pattern-circle' cx='10' cy='10' r='2.5'%3E%3C/circle%3E%3C/svg%3E")`,
26 hover: `url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32' width='16' height='16' fill='none'%3E%3Ccircle fill='%238183f4' id='pattern-circle' cx='10' cy='10' r='2.5'%3E%3C/circle%3E%3C/svg%3E")`,
27 },
28 };
29
30 function handleMouseMove({
31 currentTarget,
32 clientX,
33 clientY,
34 }: React.MouseEvent<HTMLDivElement>) {
35 if (!currentTarget) return;
36 let { left, top } = currentTarget.getBoundingClientRect();
37
38 mouseX.set(clientX - left);
39 mouseY.set(clientY - top);
40 }
41 return (
42 <div
43 className={cn(
44 "group relative flex h-[40rem] w-full items-center justify-center bg-white dark:bg-black",
45 containerClassName,
46 )}
47 onMouseMove={handleMouseMove}
48 >
49 <div
50 className="pointer-events-none absolute inset-0 dark:hidden"
51 style={{
52 backgroundImage: dotPatterns.light.default,
53 }}
54 />
55 <div
56 className="pointer-events-none absolute inset-0 hidden dark:block"
57 style={{
58 backgroundImage: dotPatterns.dark.default,
59 }}
60 />
61 <motion.div
62 className="pointer-events-none absolute inset-0 opacity-0 transition duration-300 group-hover:opacity-100 dark:hidden"
63 style={{
64 backgroundImage: dotPatterns.light.hover,
65 WebkitMaskImage: useMotionTemplate`
66 radial-gradient(
67 200px circle at ${mouseX}px ${mouseY}px,
68 black 0%,
69 transparent 100%
70 )
71 `,
72 maskImage: useMotionTemplate`
73 radial-gradient(
74 200px circle at ${mouseX}px ${mouseY}px,
75 black 0%,
76 transparent 100%
77 )
78 `,
79 }}
80 />
81 <motion.div
82 className="pointer-events-none absolute inset-0 hidden opacity-0 transition duration-300 group-hover:opacity-100 dark:block"
83 style={{
84 backgroundImage: dotPatterns.dark.hover,
85 WebkitMaskImage: useMotionTemplate`
86 radial-gradient(
87 200px circle at ${mouseX}px ${mouseY}px,
88 black 0%,
89 transparent 100%
90 )
91 `,
92 maskImage: useMotionTemplate`
93 radial-gradient(
94 200px circle at ${mouseX}px ${mouseY}px,
95 black 0%,
96 transparent 100%
97 )
98 `,
99 }}
100 />
101
102 <div className={cn("relative z-20", className)}>{children}</div>
103 </div>
104 );
105};
106
107export const Highlight = ({
108 children,
109 className,
110}: {
111 children: React.ReactNode;
112 className?: string;
113}) => {
114 return (
115 <motion.span
116 initial={{
117 backgroundSize: "0% 100%",
118 }}
119 animate={{
120 backgroundSize: "100% 100%",
121 }}
122 transition={{
123 duration: 2,
124 ease: "linear",
125 delay: 0.5,
126 }}
127 style={{
128 backgroundRepeat: "no-repeat",
129 backgroundPosition: "left center",
130 display: "inline",
131 }}
132 className={cn(
133 `relative inline-block rounded-lg bg-gradient-to-r from-indigo-300 to-purple-300 px-1 pb-1 dark:from-indigo-500 dark:to-purple-500`,
134 className,
135 )}
136 >
137 {children}
138 </motion.span>
139 );
140};
141