Skip to content

UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.

feat: add video text component (#618)

daniel1975c9927e0a
diff --git a/__registry__/index.tsx b/__registry__/index.tsx
index 4498e33a..923c5270 100644
--- a/__registry__/index.tsx
+++ b/__registry__/index.tsx
@@ -1530,6 +1530,30 @@ export const Index: Record<string, any> = {
}),
meta: undefined,
},
+ "video-text": {
+ name: "video-text",
+ description:
+ "A component that displays text with a video playing in the background.",
+ type: "registry:ui",
+ registryDependencies: undefined,
+ files: [
+ {
+ path: "registry/magicui/video-text.tsx",
+ type: "registry:ui",
+ target: "components/magicui/video-text.tsx",
+ },
+ ],
+ component: React.lazy(async () => {
+ const mod = await import("@/registry/magicui/video-text.tsx");
+ const exportName =
+ Object.keys(mod).find(
+ (key) =>
+ typeof mod[key] === "function" || typeof mod[key] === "object",
+ ) || item.name;
+ return { default: mod.default || mod[exportName] };
+ }),
+ meta: undefined,
+ },
"magic-card-demo": {
name: "magic-card-demo",
description:
@@ -4325,6 +4349,29 @@ export const Index: Record<string, any> = {
}),
meta: undefined,
},
+ "video-text-demo": {
+ name: "video-text-demo",
+ description: "Example showing text with a video background.",
+ type: "registry:example",
+ registryDependencies: ["https://magicui.design/r/video-text"],
+ files: [
+ {
+ path: "registry/example/video-text-demo.tsx",
+ type: "registry:example",
+ target: "components/video-text-demo.tsx",
+ },
+ ],
+ component: React.lazy(async () => {
+ const mod = await import("@/registry/example/video-text-demo.tsx");
+ const exportName =
+ Object.keys(mod).find(
+ (key) =>
+ typeof mod[key] === "function" || typeof mod[key] === "object",
+ ) || item.name;
+ return { default: mod.default || mod[exportName] };
+ }),
+ meta: undefined,
+ },
utils: {
name: "utils",
description: "",
diff --git a/config/docs.ts b/config/docs.ts
index fae74194..c1834582 100644
--- a/config/docs.ts
+++ b/config/docs.ts
@@ -364,6 +364,12 @@ export const docsConfig: DocsConfig = {
items: [],
label: "",
},
+ {
+ title: "Video Text",
+ href: `/docs/components/video-text`,
+ items: [],
+ label: "New",
+ },
{
title: "Number Ticker",
href: `/docs/components/number-ticker`,
diff --git a/content/docs/components/video-text.mdx b/content/docs/components/video-text.mdx
new file mode 100644
index 00000000..585ccfca
--- /dev/null
+++ b/content/docs/components/video-text.mdx
@@ -0,0 +1,69 @@
+---
+title: Video Text
+date: 2025-04-16
+description: A text component with a video background.
+author: magicui
+published: true
+---
+
+<ComponentPreview name="video-text-demo" />
+
+## Installation
+
+<Tabs defaultValue="cli">
+
+<TabsList>
+ <TabsTrigger value="cli">CLI</TabsTrigger>
+ <TabsTrigger value="manual">Manual</TabsTrigger>
+</TabsList>
+<TabsContent value="cli">
+
+```bash
+npx shadcn@latest add "https://magicui.design/r/video-text"
+```
+
+</TabsContent>
+
+<TabsContent value="manual">
+
+<Steps>
+
+<Step>Copy and paste the following code into your project.</Step>
+
+<ComponentSource name="video-text" />
+
+</Steps>
+
+</TabsContent>
+
+</Tabs>
+
+## Usage
+
+```tsx
+import { VideoText } from "@/registry/magicui/video-text";
+```
+
+```tsx
+<div className="relative h-[500px] w-full overflow-hidden">
+ <VideoText src="https://cdn.magicui.design/ocean-small.webm">OCEAN</VideoText>
+</div>
+```
+
+## Props
+
+| Prop | Type | Default | Description |
+| ------------------ | -------------------------------- | -------------- | -------------------------------------------------------- |
+| `src` | `string` | Required | The video source URL |
+| `as` | `ElementType` | `"div"` | The element type to render for the text |
+| `children` | `ReactNode` | Required | The content to display (will have the video "inside" it) |
+| `className` | `string` | `""` | Additional className for the container |
+| `autoPlay` | `boolean` | `true` | Whether to autoplay the video |
+| `muted` | `boolean` | `true` | Whether to mute the video |
+| `loop` | `boolean` | `true` | Whether to loop the video |
+| `preload` | `"auto" \| "metadata" \| "none"` | `"auto"` | Whether to preload the video |
+| `fontSize` | `string \| number` | `"120"` | Font size for the text mask |
+| `fontWeight` | `string \| number` | `"bold"` | Font weight for the text mask |
+| `textAnchor` | `string` | `"middle"` | Text anchor for the text mask |
+| `dominantBaseline` | `string` | `"middle"` | Dominant baseline for the text mask |
+| `fontFamily` | `string` | `"sans-serif"` | Font family for the text mask |
diff --git a/public/r/video-text-demo.json b/public/r/video-text-demo.json
new file mode 100644
index 00000000..2929f399
--- /dev/null
+++ b/public/r/video-text-demo.json
@@ -0,0 +1,18 @@
+{
+ "$schema": "https://ui.shadcn.com/schema/registry-item.json",
+ "name": "video-text-demo",
+ "type": "registry:example",
+ "title": "Video Text Demo",
+ "description": "Example showing text with a video background.",
+ "registryDependencies": [
+ "https://magicui.design/r/video-text"
+ ],
+ "files": [
+ {
+ "path": "registry/example/video-text-demo.tsx",
+ "content": "import { VideoText } from \"@/registry/magicui/video-text\";\n\nexport default function VideoTextDemo() {\n return (\n <div className=\"relative h-[200px] w-full overflow-hidden\">\n <VideoText src=\"https://cdn.magicui.design/ocean-small.webm\">\n OCEAN\n </VideoText>\n </div>\n );\n}\n",
+ "type": "registry:example",
+ "target": "components/video-text-demo.tsx"
+ }
+ ]
+}
\ No newline at end of file
diff --git a/public/r/video-text.json b/public/r/video-text.json
new file mode 100644
index 00000000..b0445d4f
--- /dev/null
+++ b/public/r/video-text.json
@@ -0,0 +1,15 @@
+{
+ "$schema": "https://ui.shadcn.com/schema/registry-item.json",
+ "name": "video-text",
+ "type": "registry:ui",
+ "title": "Video Text",
+ "description": "A component that displays text with a video playing in the background.",
+ "files": [
+ {
+ "path": "registry/magicui/video-text.tsx",
+ "content": "\"use client\";\n\nimport { cn } from \"@/lib/utils\";\nimport React, { ElementType, ReactNode, useEffect, useState } from \"react\";\n\nexport interface VideoTextProps {\n /**\n * The video source URL\n */\n src: string;\n /**\n * Additional className for the container\n */\n className?: string;\n /**\n * Whether to autoplay the video\n */\n autoPlay?: boolean;\n /**\n * Whether to mute the video\n */\n muted?: boolean;\n /**\n * Whether to loop the video\n */\n loop?: boolean;\n /**\n * Whether to preload the video\n */\n preload?: \"auto\" | \"metadata\" | \"none\";\n /**\n * The content to display (will have the video \"inside\" it)\n */\n children: ReactNode;\n /**\n * Font size for the text mask (in viewport width units)\n * @default 10\n */\n fontSize?: string | number;\n /**\n * Font weight for the text mask\n * @default \"bold\"\n */\n fontWeight?: string | number;\n /**\n * Text anchor for the text mask\n * @default \"middle\"\n */\n textAnchor?: string;\n /**\n * Dominant baseline for the text mask\n * @default \"middle\"\n */\n dominantBaseline?: string;\n /**\n * Font family for the text mask\n * @default \"sans-serif\"\n */\n fontFamily?: string;\n /**\n * The element type to render for the text\n * @default \"div\"\n */\n as?: ElementType;\n}\n\nexport function VideoText({\n src,\n children,\n className = \"\",\n autoPlay = true,\n muted = true,\n loop = true,\n preload = \"auto\",\n fontSize = 20,\n fontWeight = \"bold\",\n textAnchor = \"middle\",\n dominantBaseline = \"middle\",\n fontFamily = \"sans-serif\",\n as: Component = \"div\",\n}: VideoTextProps) {\n const [svgMask, setSvgMask] = useState(\"\");\n const content = React.Children.toArray(children).join(\"\");\n\n useEffect(() => {\n const updateSvgMask = () => {\n const responsiveFontSize =\n typeof fontSize === \"number\" ? `${fontSize}vw` : fontSize;\n 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>`;\n setSvgMask(newSvgMask);\n };\n\n updateSvgMask();\n window.addEventListener(\"resize\", updateSvgMask);\n return () => window.removeEventListener(\"resize\", updateSvgMask);\n }, [content, fontSize, fontWeight, textAnchor, dominantBaseline, fontFamily]);\n\n const dataUrlMask = `url(\"data:image/svg+xml,${encodeURIComponent(svgMask)}\")`;\n\n return (\n <Component className={cn(`relative size-full`, className)}>\n {/* Create a container that masks the video to only show within text */}\n <div\n className=\"absolute inset-0 flex items-center justify-center\"\n style={{\n maskImage: dataUrlMask,\n WebkitMaskImage: dataUrlMask,\n maskSize: \"contain\",\n WebkitMaskSize: \"contain\",\n maskRepeat: \"no-repeat\",\n WebkitMaskRepeat: \"no-repeat\",\n maskPosition: \"center\",\n WebkitMaskPosition: \"center\",\n }}\n >\n <video\n className=\"w-full h-full object-cover\"\n autoPlay={autoPlay}\n muted={muted}\n loop={loop}\n preload={preload}\n >\n <source src={src} />\n Your browser does not support the video tag.\n </video>\n </div>\n\n {/* Add a backup text element for SEO/accessibility */}\n <span className=\"sr-only\">{content}</span>\n </Component>\n );\n}\n",
+ "type": "registry:ui",
+ "target": "components/magicui/video-text.tsx"
+ }
+ ]
+}
\ No newline at end of file
diff --git a/public/registry.json b/public/registry.json
index cc736375..d1b0d6ba 100644
--- a/public/registry.json
+++ b/public/registry.json
@@ -1228,6 +1228,19 @@
}
]
},
+ {
+ "name": "video-text",
+ "type": "registry:ui",
+ "title": "Video Text",
+ "description": "A component that displays text with a video playing in the background.",
+ "files": [
+ {
+ "path": "registry/magicui/video-text.tsx",
+ "type": "registry:ui",
+ "target": "components/magicui/video-text.tsx"
+ }
+ ]
+ },
{
"name": "magic-card-demo",
"type": "registry:example",
@@ -3164,6 +3177,22 @@
}
]
},
+ {
+ "name": "video-text-demo",
+ "type": "registry:example",
+ "title": "Video Text Demo",
+ "description": "Example showing text with a video background.",
+ "registryDependencies": [
+ "https://magicui.design/r/video-text"
+ ],
+ "files": [
+ {
+ "path": "registry/example/video-text-demo.tsx",
+ "type": "registry:example",
+ "target": "components/video-text-demo.tsx"
+ }
+ ]
+ },
{
"name": "utils",
"type": "registry:lib",
diff --git a/registry.json b/registry.json
index cc736375..d1b0d6ba 100644
--- a/registry.json
+++ b/registry.json
@@ -1228,6 +1228,19 @@
}
]
},
+ {
+ "name": "video-text",
+ "type": "registry:ui",
+ "title": "Video Text",
+ "description": "A component that displays text with a video playing in the background.",
+ "files": [
+ {
+ "path": "registry/magicui/video-text.tsx",
+ "type": "registry:ui",
+ "target": "components/magicui/video-text.tsx"
+ }
+ ]
+ },
{
"name": "magic-card-demo",
"type": "registry:example",
@@ -3164,6 +3177,22 @@
}
]
},
+ {
+ "name": "video-text-demo",
+ "type": "registry:example",
+ "title": "Video Text Demo",
+ "description": "Example showing text with a video background.",
+ "registryDependencies": [
+ "https://magicui.design/r/video-text"
+ ],
+ "files": [
+ {
+ "path": "registry/example/video-text-demo.tsx",
+ "type": "registry:example",
+ "target": "components/video-text-demo.tsx"
+ }
+ ]
+ },
{
"name": "utils",
"type": "registry:lib",
diff --git a/registry/example/video-text-demo.tsx b/registry/example/video-text-demo.tsx
new file mode 100644
index 00000000..adc09b50
--- /dev/null
+++ b/registry/example/video-text-demo.tsx
@@ -0,0 +1,11 @@
+import { VideoText } from "@/registry/magicui/video-text";
+
+export default function VideoTextDemo() {
+ return (
+ <div className="relative h-[200px] w-full overflow-hidden">
+ <VideoText src="https://cdn.magicui.design/ocean-small.webm">
+ OCEAN
+ </VideoText>
+ </div>
+ );
+}
diff --git a/registry/magicui/video-text.tsx b/registry/magicui/video-text.tsx
new file mode 100644
index 00000000..a54e38e1
--- /dev/null
+++ b/registry/magicui/video-text.tsx
@@ -0,0 +1,132 @@
+"use client";
+
+import { cn } from "@/lib/utils";
+import React, { ElementType, ReactNode, useEffect, useState } from "react";
+
+export interface VideoTextProps {
+ /**
+ * The video source URL
+ */
+ src: string;
+ /**
+ * Additional className for the container
+ */
+ className?: string;
+ /**
+ * Whether to autoplay the video
+ */
+ autoPlay?: boolean;
+ /**
+ * Whether to mute the video
+ */
+ muted?: boolean;
+ /**
+ * Whether to loop the video
+ */
+ loop?: boolean;
+ /**
+ * Whether to preload the video
+ */
+ preload?: "auto" | "metadata" | "none";
+ /**
+ * The content to display (will have the video "inside" it)
+ */
+ children: ReactNode;
+ /**
+ * Font size for the text mask (in viewport width units)
+ * @default 10
+ */
+ fontSize?: string | number;
+ /**
+ * Font weight for the text mask
+ * @default "bold"
+ */
+ fontWeight?: string | number;
+ /**
+ * Text anchor for the text mask
+ * @default "middle"
+ */
+ textAnchor?: string;
+ /**
+ * Dominant baseline for the text mask
+ * @default "middle"
+ */
+ dominantBaseline?: string;
+ /**
+ * Font family for the text mask
+ * @default "sans-serif"
+ */
+ fontFamily?: string;
+ /**
+ * The element type to render for the text
+ * @default "div"
+ */
+ as?: ElementType;
+}
+
+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: Component = "div",
+}: VideoTextProps) {
+ const [svgMask, setSvgMask] = useState("");
+ const content = React.Children.toArray(children).join("");
+
+ useEffect(() => {
+ const updateSvgMask = () => {
+ 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}' font-weight='${fontWeight}' text-anchor='${textAnchor}' dominant-baseline='${dominantBaseline}' font-family='${fontFamily}'>${content}</text></svg>`;
+ setSvgMask(newSvgMask);
+ };
+
+ updateSvgMask();
+ window.addEventListener("resize", updateSvgMask);
+ return () => window.removeEventListener("resize", updateSvgMask);
+ }, [content, fontSize, fontWeight, textAnchor, dominantBaseline, fontFamily]);
+
+ const dataUrlMask = `url("data:image/svg+xml,${encodeURIComponent(svgMask)}")`;
+
+ return (
+ <Component className={cn(`relative size-full`, className)}>
+ {/* Create a container that masks the video to only show within text */}
+ <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",
+ }}
+ >
+ <video
+ className="w-full h-full object-cover"
+ autoPlay={autoPlay}
+ muted={muted}
+ loop={loop}
+ preload={preload}
+ >
+ <source src={src} />
+ Your browser does not support the video tag.
+ </video>
+ </div>
+
+ {/* Add a backup text element for SEO/accessibility */}
+ <span className="sr-only">{content}</span>
+ </Component>
+ );
+}
diff --git a/registry/registry-examples.ts b/registry/registry-examples.ts
index 8037416a..ee7ed90f 100644
--- a/registry/registry-examples.ts
+++ b/registry/registry-examples.ts
@@ -1696,4 +1696,18 @@ export const examples: Registry["items"] = [
},
],
},
+ {
+ name: "video-text-demo",
+ type: "registry:example",
+ title: "Video Text Demo",
+ description: "Example showing text with a video background.",
+ registryDependencies: ["https://magicui.design/r/video-text"],
+ files: [
+ {
+ path: "registry/example/video-text-demo.tsx",
+ type: "registry:example",
+ target: "components/video-text-demo.tsx",
+ },
+ ],
+ },
];
diff --git a/registry/registry-ui.ts b/registry/registry-ui.ts
index beded7f2..680792ce 100644
--- a/registry/registry-ui.ts
+++ b/registry/registry-ui.ts
@@ -1160,4 +1160,18 @@ export const ui: Registry["items"] = [
},
],
},
+ {
+ name: "video-text",
+ type: "registry:ui",
+ title: "Video Text",
+ description:
+ "A component that displays text with a video playing in the background.",
+ files: [
+ {
+ path: "registry/magicui/video-text.tsx",
+ type: "registry:ui",
+ target: "components/magicui/video-text.tsx",
+ },
+ ],
+ },
];