🎥 Make videos programmatically with React
Deduplicate PromptInput code via isLanding
ASchwad0f71c6f7d636
diff --git a/packages/template-prompt-to-motion-graphics/src/app/page.tsx b/packages/template-prompt-to-motion-graphics/src/app/page.tsxindex c570128976..9e6bff140d 100644--- a/packages/template-prompt-to-motion-graphics/src/app/page.tsx+++ b/packages/template-prompt-to-motion-graphics/src/app/page.tsx@@ -3,147 +3,27 @@import { useState } from "react";import type { NextPage } from "next";import { useRouter } from "next/navigation";-import Link from "next/link";-import {- ArrowUp,- SquareArrowOutUpRight,- Loader2,- Type,- MessageCircle,- Hash,- BarChart3,- Disc,- type LucideIcon,-} from "lucide-react";-import {- Select,- SelectContent,- SelectItem,- SelectTrigger,- SelectValue,-} from "@/components/ui/select";-import { examplePrompts } from "@/data/examplePrompts";-import { MODELS, type ModelId } from "@/components/PromptInput";+import { PromptInput, type ModelId } from "@/components/PromptInput";import { PageLayout } from "@/components/PageLayout";-const iconMap: Record<string, LucideIcon> = {- Type,- MessageCircle,- Hash,- BarChart3,- Disc,-};-const Home: NextPage = () => {const router = useRouter();- const [prompt, setPrompt] = useState("");- const [model, setModel] = useState<ModelId>("gpt-5.1:low");const [isNavigating, setIsNavigating] = useState(false);- const handleSubmit = (e: React.FormEvent) => {- e.preventDefault();- if (!prompt.trim()) return;-+ const handleNavigate = (prompt: string, model: ModelId) => {setIsNavigating(true);const params = new URLSearchParams({ prompt, model });router.push(`/generate?${params.toString()}`);};- const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {- if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) {- e.preventDefault();- handleSubmit(e);- }- };-return (<PageLayout>- <div className="flex flex-col items-center justify-center flex-1 px-4">- <h1 className="text-5xl font-bold text-white mb-10 text-center">- What do you want to create?- </h1>-- <form onSubmit={handleSubmit} className="w-full max-w-3xl">- <div className="bg-background-elevated rounded-xl border border-border p-4">- <textarea- value={prompt}- onChange={(e) => setPrompt(e.target.value)}- onKeyDown={handleKeyDown}- placeholder="Describe your animation..."- className="w-full bg-transparent text-foreground placeholder:text-muted-foreground-dim focus:outline-none resize-none overflow-y-auto text-base min-h-[60px] max-h-[200px]"- style={{ fieldSizing: "content" }}- disabled={isNavigating}- />-- <div className="flex justify-between items-center mt-3 pt-3 border-t border-border">- <Select- value={model}- onValueChange={(value) => setModel(value as ModelId)}- disabled={isNavigating}- >- <SelectTrigger className="w-auto bg-transparent border-none text-muted-foreground hover:text-foreground transition-colors">- <SelectValue />- </SelectTrigger>- <SelectContent className="bg-background-elevated border-border">- {MODELS.map((m) => (- <SelectItem- key={m.id}- value={m.id}- className="text-foreground focus:bg-secondary focus:text-foreground"- >- {m.name}- </SelectItem>- ))}- </SelectContent>- </Select>-- <button- type="submit"- disabled={isNavigating || !prompt.trim()}- className="p-2 rounded-lg bg-foreground text-background hover:bg-gray-200 disabled:opacity-30 disabled:cursor-not-allowed transition-all"- >- {isNavigating ? (- <Loader2 className="w-5 h-5 animate-spin" />- ) : (- <ArrowUp className="w-5 h-5" />- )}- </button>- </div>- </div>-- <div className="flex flex-wrap items-center justify-center mt-6 gap-2">- <span className="text-muted-foreground-dim text-xs mr-1">Prompt examples</span>- {examplePrompts.map((example) => {- const Icon = iconMap[example.icon];- return (- <button- key={example.id}- type="button"- onClick={() => setPrompt(example.prompt)}- style={{- borderColor: `${example.color}40`,- color: example.color,- }}- className="rounded-full bg-background-elevated border hover:brightness-125 transition-all flex items-center gap-1 px-1.5 py-0.5 text-[11px]"- >- {Icon && <Icon className="w-3 h-3" />}- {example.headline}- </button>- );- })}- </div>-- <div className="flex justify-center mt-4">- <Link- href="/code-examples"- className="text-muted-foreground-dim hover:text-muted-foreground text-xs transition-colors flex items-center gap-1"- >- View Code examples- <SquareArrowOutUpRight className="w-3 h-3" />- </Link>- </div>- </form>- </div>+ <PromptInput+ variant="landing"+ onNavigate={handleNavigate}+ isNavigating={isNavigating}+ showCodeExamplesLink+ /></PageLayout>);};diff --git a/packages/template-prompt-to-motion-graphics/src/components/PromptInput.tsx b/packages/template-prompt-to-motion-graphics/src/components/PromptInput.tsxindex c522230399..4b3794578e 100644--- a/packages/template-prompt-to-motion-graphics/src/components/PromptInput.tsx+++ b/packages/template-prompt-to-motion-graphics/src/components/PromptInput.tsx@@ -1,8 +1,10 @@"use client";import { useState, forwardRef, useImperativeHandle } from "react";+import Link from "next/link";import {ArrowUp,+ SquareArrowOutUpRight,Type,MessageCircle,Hash,@@ -46,13 +48,19 @@ export interface PromptInputRef {}interface PromptInputProps {- onCodeGenerated: (code: string) => void;+ onCodeGenerated?: (code: string) => void;onStreamingChange?: (isStreaming: boolean) => void;onStreamPhaseChange?: (phase: StreamPhase) => void;onError?: (error: string) => void;variant?: "landing" | "editor";prompt?: string;onPromptChange?: (prompt: string) => void;+ /** Called when landing variant submits - receives prompt and model for navigation */+ onNavigate?: (prompt: string, model: ModelId) => void;+ /** Whether navigation is in progress (for landing variant) */+ isNavigating?: boolean;+ /** Whether to show the "View Code examples" link (landing variant) */+ showCodeExamplesLink?: boolean;}export const PromptInput = forwardRef<PromptInputRef, PromptInputProps>(@@ -65,6 +73,9 @@ export const PromptInput = forwardRef<PromptInputRef, PromptInputProps>(variant = "editor",prompt: controlledPrompt,onPromptChange,+ onNavigate,+ isNavigating = false,+ showCodeExamplesLink = false,},ref,) {@@ -138,7 +149,7 @@ export const PromptInput = forwardRef<PromptInputRef, PromptInputProps>(codeToShow = codeToShow.replace(/^```(?:tsx?|jsx?)?\n?/, "");codeToShow = codeToShow.replace(/\n?```\s*$/, "");- onCodeGenerated(codeToShow.trim());+ onCodeGenerated?.(codeToShow.trim());}} catch {// Ignore parse errors for malformed JSON@@ -166,6 +177,14 @@ export const PromptInput = forwardRef<PromptInputRef, PromptInputProps>(const handleSubmit = async (e: React.FormEvent) => {e.preventDefault();+ if (!prompt.trim()) return;++ // Landing variant uses navigation instead of API call+ if (isLanding && onNavigate) {+ onNavigate(prompt, model);+ return;+ }+await runGeneration();};@@ -178,6 +197,7 @@ export const PromptInput = forwardRef<PromptInputRef, PromptInputProps>(};const isLanding = variant === "landing";+ const isDisabled = isLanding ? isNavigating : isLoading;return (<div@@ -209,14 +229,14 @@ export const PromptInput = forwardRef<PromptInputRef, PromptInputProps>(: "text-sm min-h-[40px] max-h-[150px]"}`}style={{ fieldSizing: "content" }}- disabled={isLoading}+ disabled={isDisabled}/><div className="flex justify-between items-center mt-3 pt-3 border-t border-border"><Selectvalue={model}onValueChange={(value) => setModel(value as ModelId)}- disabled={isLoading}+ disabled={isDisabled}><SelectTrigger className="w-auto bg-transparent border-none text-muted-foreground hover:text-foreground transition-colors"><SelectValue />@@ -237,8 +257,8 @@ export const PromptInput = forwardRef<PromptInputRef, PromptInputProps>(<Buttontype="submit"size="icon-sm"- disabled={!prompt.trim()}- loading={isLoading}+ disabled={!prompt.trim() || isDisabled}+ loading={isDisabled}className="bg-foreground text-background hover:bg-gray-200"><ArrowUp className="w-5 h-5" />@@ -273,6 +293,18 @@ export const PromptInput = forwardRef<PromptInputRef, PromptInputProps>();})}</div>++ {showCodeExamplesLink && (+ <div className="flex justify-center mt-4">+ <Link+ href="/code-examples"+ className="text-muted-foreground-dim hover:text-muted-foreground text-xs transition-colors flex items-center gap-1"+ >+ View Code examples+ <SquareArrowOutUpRight className="w-3 h-3" />+ </Link>+ </div>+ )}</form></div>);