A svg-mask-effect component from Aceternity UI.
From Aceternity UI
1"use client";
2import { useState, useEffect, useRef } from "react";
3import { motion } from "motion/react";
4import { cn } from "@/lib/utils";
5
6export const MaskContainer = ({
7 children,
8 revealText,
9 size = 10,
10 revealSize = 600,
11 className,
12}: {
13 children?: string | React.ReactNode;
14 revealText?: string | React.ReactNode;
15 size?: number;
16 revealSize?: number;
17 className?: string;
18}) => {
19 const [isHovered, setIsHovered] = useState(false);
20 const [mousePosition, setMousePosition] = useState<any>({ x: null, y: null });
21 const containerRef = useRef<any>(null);
22 const updateMousePosition = (e: any) => {
23 const rect = containerRef.current.getBoundingClientRect();
24 setMousePosition({ x: e.clientX - rect.left, y: e.clientY - rect.top });
25 };
26
27 useEffect(() => {
28 containerRef.current.addEventListener("mousemove", updateMousePosition);
29 return () => {
30 if (containerRef.current) {
31 containerRef.current.removeEventListener(
32 "mousemove",
33 updateMousePosition,
34 );
35 }
36 };
37 }, []);
38 let maskSize = isHovered ? revealSize : size;
39
40 return (
41 <motion.div
42 ref={containerRef}
43 className={cn("relative h-screen", className)}
44 animate={{
45 backgroundColor: isHovered ? "var(--slate-900)" : "var(--white)",
46 }}
47 transition={{
48 backgroundColor: { duration: 0.3 },
49 }}
50 >
51 <motion.div
52 className="absolute flex h-full w-full items-center justify-center bg-black text-6xl [mask-image:url(/mask.svg)] [mask-repeat:no-repeat] [mask-size:40px] dark:bg-white"
53 animate={{
54 maskPosition: `${mousePosition.x - maskSize / 2}px ${
55 mousePosition.y - maskSize / 2
56 }px`,
57 maskSize: `${maskSize}px`,
58 }}
59 transition={{
60 maskSize: { duration: 0.3, ease: "easeInOut" },
61 maskPosition: { duration: 0.15, ease: "linear" },
62 }}
63 >
64 <div className="absolute inset-0 z-0 h-full w-full bg-black opacity-50 dark:bg-white" />
65 <div
66 onMouseEnter={() => {
67 setIsHovered(true);
68 }}
69 onMouseLeave={() => {
70 setIsHovered(false);
71 }}
72 className="relative z-20 mx-auto max-w-4xl text-center text-4xl font-bold"
73 >
74 {children}
75 </div>
76 </motion.div>
77
78 <div className="flex h-full w-full items-center justify-center">
79 {revealText}
80 </div>
81 </motion.div>
82 );
83};
84