lightswind
Version:
A collection of beautifully crafted React Components, Blocks & Templates for Modern Developers. Create stunning web applications effortlessly by using our 160+ professional and animated react components.
41 lines (40 loc) • 2.72 kB
JavaScript
// @ts-nocheck
"use client";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { cn } from "../../lib/utils";
import React, { useEffect, useState } from "react";
import { motion } from "framer-motion";
export function VideoText({ src, children, className = "", autoPlay = true, muted = true, loop = true, preload = "auto", fontSize = 20, fontWeight = "bold", textAnchor = "middle", dominantBaseline = "middle", fontFamily = "sans-serif", as = "div", ...motionProps }) {
const [svgMask, setSvgMask] = useState("");
const content = React.Children.toArray(children).join("");
useEffect(() => {
const responsiveFontSize = typeof fontSize === "number" ? `${fontSize}vw` : fontSize;
const newSvgMask = `<svg xmlns='http://www.w3.org/2000/svg' width='100%' height='100%'>
<text x='50%' y='50%'
font-size='${responsiveFontSize}'
fontWeight='${fontWeight}'
text-anchor='${textAnchor}'
dominant-baseline='${dominantBaseline}'
font-family='${fontFamily}'
fill='black'>${content}</text>
</svg>`;
setSvgMask(newSvgMask);
}, [content, fontSize, fontWeight, textAnchor, dominantBaseline, fontFamily]);
const validTags = ["div", "span", "section", "article", "p", "h1", "h2", "h3", "h4", "h5", "h6"];
const MotionComponent = motion[validTags.includes(as) ? as : "div"];
if (!svgMask) {
return React.createElement(MotionComponent, { className: cn("relative size-full", className), ...motionProps }, _jsx("span", { className: "sr-only", children: content }));
}
const dataUrlMask = `url("data:image/svg+xml,${encodeURIComponent(svgMask)}")`;
return React.createElement(MotionComponent, { className: cn("relative overflow-hidden", className), ...motionProps }, _jsxs(_Fragment, { children: [_jsx("div", { className: "absolute inset-0 flex items-center justify-center", style: {
maskImage: dataUrlMask,
WebkitMaskImage: dataUrlMask,
maskSize: "contain",
WebkitMaskSize: "contain",
maskRepeat: "no-repeat",
WebkitMaskRepeat: "no-repeat",
maskPosition: "center",
WebkitMaskPosition: "center",
opacity: 1,
}, children: _jsxs("video", { className: "w-full h-full object-cover", autoPlay: autoPlay, muted: muted, loop: loop, preload: preload, playsInline: true, children: [_jsx("source", { src: src, type: "video/mp4" }), "Your browser does not support the video tag."] }) }), _jsx("span", { className: "sr-only", children: content })] }));
}