1"use client"
2
3import React, { ElementType, ReactNode, useEffect, useState } from "react"
4
5import { cn } from "@/lib/utils"
6
7export interface VideoTextProps {
8 /**
9 * The video source URL
10 */
11 src: string
12 /**
13 * Additional className for the container
14 */
15 className?: string
16 /**
17 * Whether to autoplay the video
18 */
19 autoPlay?: boolean
20 /**
21 * Whether to mute the video
22 */
23 muted?: boolean
24 /**
25 * Whether to loop the video
26 */
27 loop?: boolean
28 /**
29 * Whether to preload the video
30 */
31 preload?: "auto" | "metadata" | "none"
32 /**
33 * The content to display (will have the video "inside" it)
34 */
35 children: ReactNode
36 /**
37 * Font size for the text mask (in viewport width units)
38 * @default 10
39 */
40 fontSize?: string | number
41 /**
42 * Font weight for the text mask
43 * @default "bold"
44 */
45 fontWeight?: string | number
46 /**
47 * Text anchor for the text mask
48 * @default "middle"
49 */
50 textAnchor?: string
51 /**
52 * Dominant baseline for the text mask
53 * @default "middle"
54 */
55 dominantBaseline?: string
56 /**
57 * Font family for the text mask
58 * @default "sans-serif"
59 */
60 fontFamily?: string
61 /**
62 * The element type to render for the text
63 * @default "div"
64 */
65 as?: ElementType
66}
67
68export function VideoText({
69 src,
70 children,
71 className = "",
72 autoPlay = true,
73 muted = true,
74 loop = true,
75 preload = "auto",
76 fontSize = 20,
77 fontWeight = "bold",
78 textAnchor = "middle",
79 dominantBaseline = "middle",
80 fontFamily = "sans-serif",
81 as: Component = "div",
82}: VideoTextProps) {
83 const [svgMask, setSvgMask] = useState("")
84 const content = React.Children.toArray(children).join("")
85
86 useEffect(() => {
87 const updateSvgMask = () => {
88 const responsiveFontSize =
89 typeof fontSize === "number" ? `${fontSize}vw` : fontSize
90 const newSvgMask = `<svg xmlns='http://www.w3.org/2000/svg' width='100%' height='100%'><text x='50%' y='50%' font-size='${responsiveFontSize}' font-weight='${fontWeight}' text-anchor='${textAnchor}' dominant-baseline='${dominantBaseline}' font-family='${fontFamily}'>${content}</text></svg>`
91 setSvgMask(newSvgMask)
92 }
93
94 updateSvgMask()
95 window.addEventListener("resize", updateSvgMask)
96 return () => window.removeEventListener("resize", updateSvgMask)
97 }, [content, fontSize, fontWeight, textAnchor, dominantBaseline, fontFamily])
98
99 const dataUrlMask = `url("data:image/svg+xml,${encodeURIComponent(svgMask)}")`
100
101 return (
102 <Component className={cn(`relative size-full`, className)}>
103 {/* Create a container that masks the video to only show within text */}
104 <div
105 className="absolute inset-0 flex items-center justify-center"
106 style={{
107 maskImage: dataUrlMask,
108 WebkitMaskImage: dataUrlMask,
109 maskSize: "contain",
110 WebkitMaskSize: "contain",
111 maskRepeat: "no-repeat",
112 WebkitMaskRepeat: "no-repeat",
113 maskPosition: "center",
114 WebkitMaskPosition: "center",
115 }}
116 >
117 <video
118 className="h-full w-full object-cover"
119 autoPlay={autoPlay}
120 muted={muted}
121 loop={loop}
122 preload={preload}
123 playsInline
124 >
125 <source src={src} />
126 Your browser does not support the video tag.
127 </video>
128 </div>
129
130 {/* Add a backup text element for SEO/accessibility */}
131 <span className="sr-only">{content}</span>
132 </Component>
133 )
134}
135