Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)
Python
Refactor screenshot generation controls into a reusable component
Flightdeck Agent8051685daa0d
diff --git a/frontend/src/components/unified-input/ScreenshotToCodeControls.tsx b/frontend/src/components/unified-input/ScreenshotToCodeControls.tsxnew file mode 100644index 00000000..3f0c10b1--- /dev/null+++ b/frontend/src/components/unified-input/ScreenshotToCodeControls.tsx@@ -0,0 +1,200 @@+import { useState } from "react";+import { LuChevronDown, LuMessageSquarePlus } from "react-icons/lu";+import { Checkbox } from "../ui/checkbox";+import { Button } from "../ui/button";+import OutputSettingsSection from "../settings/OutputSettingsSection";+import { DesignSystemSelectorProps } from "../settings/DesignSystemSelector";+import { Stack } from "../../lib/stacks";++interface Props {+ textPrompt: string;+ onTextPromptChange: (value: string) => void;+ textInputRef: React.RefObject<HTMLTextAreaElement>;+ onTextInputKeyDown: (event: React.KeyboardEvent<HTMLTextAreaElement>) => void;+ stack: Stack;+ setStack: (stack: Stack) => void;+ designSystem: DesignSystemSelectorProps;+ showAssetExtraction: boolean;+ isAssetExtractionEnabled: boolean;+ onAssetExtractionChange: (enabled: boolean) => void;+ onGenerate: () => void;+ actionLabel?: string;+ loadingActionLabel?: string;+ isActionLoading?: boolean;+ actionTestId?: string;+}++function AssetToggle({+ enabled,+ onChange,+}: {+ enabled: boolean;+ onChange: (enabled: boolean) => void;+}) {+ return (+ <label+ htmlFor="asset-extraction-original"+ className="flex cursor-pointer items-center gap-2.5 text-[13px] font-medium text-gray-600 dark:text-zinc-300"+ >+ <Checkbox+ id="asset-extraction-original"+ checked={enabled}+ onCheckedChange={(checked) => onChange(checked === true)}+ />+ <span>Extract image assets from original</span>+ </label>+ );+}++export default function ScreenshotToCodeControls({+ textPrompt,+ onTextPromptChange,+ textInputRef,+ onTextInputKeyDown,+ stack,+ setStack,+ designSystem,+ showAssetExtraction,+ isAssetExtractionEnabled,+ onAssetExtractionChange,+ onGenerate,+ actionLabel = "Generate Code",+ loadingActionLabel = "Generating…",+ isActionLoading = false,+ actionTestId = "upload-generate",+}: Props) {+ const [instructionsOpen, setInstructionsOpen] = useState(false);+ const hasInstructions = textPrompt.trim().length > 0;++ const toggleInstructions = () => {+ const shouldOpen = !instructionsOpen;+ setInstructionsOpen(shouldOpen);+ if (shouldOpen) {+ window.setTimeout(() => textInputRef.current?.focus(), 50);+ }+ };++ return (+ <div className="w-full max-w-2xl">+ <div className="overflow-hidden rounded-xl border border-gray-200 bg-white shadow-sm dark:border-zinc-700 dark:bg-zinc-900">+ <div className="p-4 sm:p-5">+ <div className="mb-4">+ <h3 className="text-sm font-semibold text-gray-900 dark:text-zinc-100">+ Output settings+ </h3>+ <p className="mt-0.5 text-xs text-gray-500 dark:text-zinc-400">+ Choose the framework for your generated code.+ </p>+ </div>+ <div className="rounded-lg border border-gray-100 bg-gray-50/80 p-3.5 dark:border-zinc-800 dark:bg-zinc-800/50">+ <OutputSettingsSection+ stack={stack}+ setStack={setStack}+ designSystem={designSystem}+ label="Stack"+ />+ </div>+ </div>++ <div className="border-t border-gray-100 dark:border-zinc-800">+ <button+ type="button"+ onClick={toggleInstructions}+ className="group flex w-full items-center gap-3 px-4 py-3 text-left transition-colors hover:bg-gray-50/80 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-gray-300 dark:hover:bg-zinc-800/50 dark:focus-visible:ring-zinc-600 sm:px-5"+ aria-expanded={instructionsOpen}+ aria-controls="additional-instructions"+ >+ <span className="flex h-7 w-7 shrink-0 items-center justify-center rounded-md bg-gray-100 text-gray-400 transition-colors group-hover:text-gray-600 dark:bg-zinc-800 dark:text-zinc-500 dark:group-hover:text-zinc-300">+ <LuMessageSquarePlus className="h-3.5 w-3.5" />+ </span>+ <span className="min-w-0 flex-1">+ <span className="flex items-center gap-2">+ <span className="text-sm font-medium text-gray-600 dark:text-zinc-300">+ {hasInstructions ? "Additional instructions" : "Add instructions"}+ </span>+ {!hasInstructions && (+ <span className="text-[11px] text-gray-400 dark:text-zinc-500">+ Optional+ </span>+ )}+ </span>+ {hasInstructions && !instructionsOpen && (+ <span className="mt-0.5 block truncate text-xs text-gray-400 dark:text-zinc-500">+ {textPrompt}+ </span>+ )}+ </span>+ <LuChevronDown+ className={`h-4 w-4 shrink-0 text-gray-400 transition-transform duration-200 dark:text-zinc-500 ${+ instructionsOpen ? "rotate-180" : ""+ }`}+ />+ </button>++ {instructionsOpen && (+ <div id="additional-instructions" className="px-4 pb-4 sm:px-5">+ <textarea+ ref={textInputRef}+ value={textPrompt}+ onChange={(event) => onTextPromptChange(event.target.value)}+ onKeyDown={onTextInputKeyDown}+ placeholder="Describe anything you want changed or emphasized…"+ className="w-full resize-none rounded-lg border border-gray-200 bg-white px-3 py-2.5 text-sm text-gray-800 outline-none transition placeholder:text-gray-400 focus:border-gray-400 focus:ring-2 focus:ring-gray-100 dark:border-zinc-700 dark:bg-zinc-950 dark:text-zinc-100 dark:focus:border-zinc-500 dark:focus:ring-zinc-800"+ rows={2}+ aria-label="Instructions for generated code"+ />+ </div>+ )}+ </div>++ <div className="flex flex-col gap-3 border-t border-gray-200 bg-gray-50/70 px-4 py-3.5 dark:border-zinc-700 dark:bg-zinc-800/50 sm:flex-row sm:items-center sm:justify-between sm:px-5">+ <div className="w-full sm:w-56">+ <Button+ onClick={onGenerate}+ disabled={isActionLoading}+ className="w-full"+ size="lg"+ data-testid={actionTestId}+ >+ {isActionLoading && (+ <svg+ className="mr-2 h-4 w-4 animate-spin"+ xmlns="http://www.w3.org/2000/svg"+ fill="none"+ viewBox="0 0 24 24"+ aria-hidden="true"+ >+ <circle+ className="opacity-25"+ cx="12"+ cy="12"+ r="10"+ stroke="currentColor"+ strokeWidth="4"+ />+ <path+ className="opacity-75"+ fill="currentColor"+ d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"+ />+ </svg>+ )}+ {isActionLoading ? loadingActionLabel : actionLabel}+ {!isActionLoading && (+ <span className="ml-2 text-xs font-normal opacity-60">↵</span>+ )}+ </Button>+ </div>+ {showAssetExtraction && (+ <div className="self-end sm:self-auto">+ <AssetToggle+ enabled={isAssetExtractionEnabled}+ onChange={onAssetExtractionChange}+ />+ </div>+ )}+ </div>+ </div>+ </div>+ );+}diff --git a/frontend/src/components/unified-input/tabs/UploadTab.tsx b/frontend/src/components/unified-input/tabs/UploadTab.tsxindex 0bf12d21..5e31aa2e 100644--- a/frontend/src/components/unified-input/tabs/UploadTab.tsx+++ b/frontend/src/components/unified-input/tabs/UploadTab.tsx@@ -2,13 +2,11 @@ import { useState, useEffect, useMemo, useRef, useCallback } from "react";import { useDropzone } from "react-dropzone";import { toast } from "react-hot-toast";import { Cross2Icon, ImageIcon } from "@radix-ui/react-icons";-import { Button } from "../../ui/button";-import { Checkbox } from "../../ui/checkbox";import { ScreenRecorderState } from "../../../types";import ScreenRecorder from "../../recording/ScreenRecorder";-import OutputSettingsSection from "../../settings/OutputSettingsSection";import { DesignSystemSelectorProps } from "../../settings/DesignSystemSelector";import { Stack } from "../../../lib/stacks";+import ScreenshotToCodeControls from "../ScreenshotToCodeControls";function fileToDataURL(file: File): Promise<string> {return new Promise((resolve, reject) => {@@ -64,7 +62,6 @@ function UploadTab({ doCreate, stack, setStack, designSystem }: Props) {"image" | "video">("image");const [textPrompt, setTextPrompt] = useState("");- const [showTextPrompt, setShowTextPrompt] = useState(false);const [isAssetExtractionEnabled, setIsAssetExtractionEnabled] = useState(true);const [selectedIndex, setSelectedIndex] = useState(0);const textInputRef = useRef<HTMLTextAreaElement>(null);@@ -113,7 +110,6 @@ function UploadTab({ doCreate, stack, setStack, designSystem }: Props) {setUploadedDataUrls([]);setFiles([]);setTextPrompt("");- setShowTextPrompt(false);setUploadedInputMode("image");setSelectedIndex(0);};@@ -457,72 +453,19 @@ function UploadTab({ doCreate, stack, setStack, designSystem }: Props) {)}</div>- {!showTextPrompt ? (- <button- onClick={() => {- setShowTextPrompt(true);- setTimeout(() => textInputRef.current?.focus(), 50);- }}- className="text-sm text-gray-500 dark:text-zinc-400 hover:text-gray-700 dark:hover:text-zinc-200 underline"- >- Add instructions (optional)- </button>- ) : (- <div className="w-full max-w-lg">- <textarea- ref={textInputRef}- value={textPrompt}- onChange={(e) => setTextPrompt(e.target.value)}- onKeyDown={handleKeyDown}- placeholder="Describe any specific requirements..."- className="w-full p-3 text-sm border border-gray-200 dark:border-zinc-700 bg-white dark:bg-zinc-900 text-gray-900 dark:text-zinc-100 rounded-lg resize-none focus:outline-none focus:ring-2 focus:ring-gray-300 dark:focus:ring-zinc-600 focus:border-transparent placeholder:text-gray-400 dark:placeholder:text-zinc-500"- rows={2}- />- </div>- )}-- <div className="w-full max-w-md space-y-4">- <OutputSettingsSection- stack={stack}- setStack={setStack}- designSystem={designSystem}- />- {uploadedInputMode === "image" && (- <label- htmlFor="asset-extraction"- className="flex cursor-pointer items-start gap-3 rounded-lg border border-gray-200 bg-gray-50 p-3 dark:border-zinc-700 dark:bg-zinc-800/60"- >- <Checkbox- id="asset-extraction"- checked={isAssetExtractionEnabled}- onCheckedChange={(checked) =>- setIsAssetExtractionEnabled(checked === true)- }- className="mt-0.5"- />- <span>- <span className="block text-sm font-medium text-gray-700 dark:text-zinc-200">- Extract image assets- </span>- <span className="mt-0.5 block text-xs text-gray-500 dark:text-zinc-400">- Reuse visual assets from this screenshot in the generated code.- </span>- </span>- </label>- )}- </div>-- <div className="flex flex-col items-center gap-1 w-full max-w-md">- <Button- onClick={handleGenerate}- className="w-full"- size="lg"- data-testid="upload-generate"- >- Generate Code- </Button>- <p className="text-xs text-gray-400 dark:text-zinc-500">Press Enter to generate</p>- </div>+ <ScreenshotToCodeControls+ textPrompt={textPrompt}+ onTextPromptChange={setTextPrompt}+ textInputRef={textInputRef}+ onTextInputKeyDown={handleKeyDown}+ stack={stack}+ setStack={setStack}+ designSystem={designSystem}+ showAssetExtraction={uploadedInputMode === "image"}+ isAssetExtractionEnabled={isAssetExtractionEnabled}+ onAssetExtractionChange={setIsAssetExtractionEnabled}+ onGenerate={handleGenerate}+ /></div>)}diff --git a/frontend/src/components/unified-input/tabs/UrlTab.tsx b/frontend/src/components/unified-input/tabs/UrlTab.tsxindex 97cc453b..d6a57ecb 100644--- a/frontend/src/components/unified-input/tabs/UrlTab.tsx+++ b/frontend/src/components/unified-input/tabs/UrlTab.tsx@@ -1,12 +1,11 @@-import { useState } from "react";+import { useRef, useState } from "react";+import { LuGlobe2 } from "react-icons/lu";import { HTTP_BACKEND_URL } from "../../../config";-import { Button } from "../../ui/button";-import { Checkbox } from "../../ui/checkbox";import { Input } from "../../ui/input";import { toast } from "react-hot-toast";-import OutputSettingsSection from "../../settings/OutputSettingsSection";import { DesignSystemSelectorProps } from "../../settings/DesignSystemSelector";import { Stack } from "../../../lib/stacks";+import ScreenshotToCodeControls from "../ScreenshotToCodeControls";interface Props {screenshotOneApiKey: string | null;@@ -34,7 +33,9 @@ function UrlTab({}: Props) {const [isLoading, setIsLoading] = useState(false);const [referenceUrl, setReferenceUrl] = useState("");+ const [textPrompt, setTextPrompt] = useState("");const [isAssetExtractionEnabled, setIsAssetExtractionEnabled] = useState(true);+ const textInputRef = useRef<HTMLTextAreaElement>(null);async function takeScreenshot() {const trimmedReferenceUrl = referenceUrl.trim();@@ -85,7 +86,12 @@ function UrlTab({}const res = await response.json();- doCreate([res.url], "image", "", isAssetExtractionEnabled);+ doCreate(+ [res.url],+ "image",+ textPrompt,+ isAssetExtractionEnabled,+ );} catch (error) {console.error(error);toast.error("Failed to capture screenshot. Check console for details.");@@ -94,121 +100,86 @@ function UrlTab({}}- return (- <div className="flex flex-col items-center gap-6">- <div className="w-full max-w-lg">- <div className="flex flex-col items-center gap-6 p-8 border border-gray-200 dark:border-zinc-700 rounded-xl bg-gray-50/50 dark:bg-zinc-900/50">- <div className="w-16 h-16 rounded-full bg-gray-100 dark:bg-zinc-800 flex items-center justify-center">- <svg- xmlns="http://www.w3.org/2000/svg"- width="28"- height="28"- viewBox="0 0 24 24"- fill="none"- stroke="currentColor"- strokeWidth="2"- strokeLinecap="round"- strokeLinejoin="round"- className="text-gray-400 dark:text-zinc-500"- >- <path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71" />- <path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71" />- </svg>- </div>-- <div className="text-center">- <h3 className="text-gray-700 dark:text-zinc-200 font-medium">Screenshot from URL</h3>- </div>+ const handleTextKeyDown = (+ event: React.KeyboardEvent<HTMLTextAreaElement>,+ ) => {+ if (event.key === "Enter" && !event.shiftKey && !isLoading) {+ event.preventDefault();+ takeScreenshot();+ }+ };- <div className="w-full space-y-3">- <Input- placeholder="https://"- onChange={(e) => setReferenceUrl(e.target.value)}- value={referenceUrl}- onKeyDown={(e) => {- if (e.key === "Enter" && !isLoading) {- takeScreenshot();- }- }}- className="w-full"- data-testid="url-input"- />- {isFigmaUrl(referenceUrl) && (- <p className="text-xs text-amber-600 dark:text-amber-400">- Direct Figma import is not supported. Take a screenshot of your- design or export the artboards as images, then use the Upload- tab.- </p>- )}- <OutputSettingsSection- stack={stack}- setStack={setStack}- designSystem={designSystem}- />- <label- htmlFor="url-asset-extraction"- className="flex cursor-pointer items-start gap-3 rounded-lg border border-gray-200 bg-gray-50 p-3 dark:border-zinc-700 dark:bg-zinc-800/60"- >- <Checkbox- id="url-asset-extraction"- checked={isAssetExtractionEnabled}- onCheckedChange={(checked) =>- setIsAssetExtractionEnabled(checked === true)- }- className="mt-0.5"- />- <span>- <span className="block text-sm font-medium text-gray-700 dark:text-zinc-200">- Extract image assets- </span>- <span className="mt-0.5 block text-xs text-gray-500 dark:text-zinc-400">- Reuse visual assets from this screenshot in the generated code.- </span>- </span>- </label>-- <Button- onClick={takeScreenshot}- disabled={isLoading}- className="w-full"- size="lg"- data-testid="url-capture"- >- {isLoading ? (- <span className="flex items-center gap-2">- <svg- className="animate-spin h-4 w-4"- xmlns="http://www.w3.org/2000/svg"- fill="none"- viewBox="0 0 24 24"- >- <circle- className="opacity-25"- cx="12"- cy="12"- r="10"- stroke="currentColor"- strokeWidth="4"- />- <path- className="opacity-75"- fill="currentColor"- d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"- />- </svg>- Capturing...- </span>- ) : (- "Capture & Generate"- )}- </Button>+ return (+ <div className="flex flex-col items-center gap-4">+ <div className="w-full max-w-2xl overflow-hidden rounded-xl border border-gray-200 bg-white shadow-sm dark:border-zinc-700 dark:bg-zinc-900">+ <div className="flex items-start gap-3 border-b border-gray-100 px-4 py-4 dark:border-zinc-800 sm:px-5">+ <span className="flex h-9 w-9 shrink-0 items-center justify-center rounded-lg bg-gray-100 text-gray-500 dark:bg-zinc-800 dark:text-zinc-400">+ <LuGlobe2 className="h-4 w-4" />+ </span>+ <div>+ <h3 className="text-sm font-semibold text-gray-900 dark:text-zinc-100">+ Screenshot from URL+ </h3>+ <p className="mt-0.5 text-xs leading-5 text-gray-500 dark:text-zinc-400">+ Enter a public webpage and we’ll capture it before generating code.+ </p></div>+ </div>- <p className="text-xs text-gray-400 dark:text-zinc-500 text-center">- Requires ScreenshotOne API key.- </p>+ <div className="space-y-2 px-4 py-4 sm:px-5">+ <label+ htmlFor="reference-url"+ className="block text-xs font-medium text-gray-600 dark:text-zinc-300"+ >+ Website URL+ </label>+ <Input+ id="reference-url"+ type="url"+ inputMode="url"+ autoComplete="url"+ placeholder="https://example.com"+ onChange={(event) => setReferenceUrl(event.target.value)}+ value={referenceUrl}+ onKeyDown={(event) => {+ if (event.key === "Enter" && !isLoading) {+ event.preventDefault();+ takeScreenshot();+ }+ }}+ className="h-11 w-full"+ data-testid="url-input"+ />+ {isFigmaUrl(referenceUrl) ? (+ <p className="text-xs leading-5 text-amber-600 dark:text-amber-400">+ Direct Figma import isn’t supported. Export your artboards as+ images and use the Upload tab instead.+ </p>+ ) : (+ <p className="text-[11px] text-gray-400 dark:text-zinc-500">+ Requires a ScreenshotOne API key in Settings.+ </p>+ )}</div></div>++ <ScreenshotToCodeControls+ textPrompt={textPrompt}+ onTextPromptChange={setTextPrompt}+ textInputRef={textInputRef}+ onTextInputKeyDown={handleTextKeyDown}+ stack={stack}+ setStack={setStack}+ designSystem={designSystem}+ showAssetExtraction+ isAssetExtractionEnabled={isAssetExtractionEnabled}+ onAssetExtractionChange={setIsAssetExtractionEnabled}+ onGenerate={takeScreenshot}+ actionLabel="Capture & Generate"+ loadingActionLabel="Capturing…"+ isActionLoading={isLoading}+ actionTestId="url-capture"+ /></div>);}