Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

Refine preview scaling modes

Abi Raja5ee767764168
diff --git a/frontend/src/components/preview/PreviewComponent.tsx b/frontend/src/components/preview/PreviewComponent.tsx
index 307b30ce..b9bc2561 100644
--- a/frontend/src/components/preview/PreviewComponent.tsx
+++ b/frontend/src/components/preview/PreviewComponent.tsx
@@ -7,13 +7,20 @@ interface Props {
code: string;
device: "mobile" | "desktop";
doUpdate: (updateInstruction: string, selectedElement?: HTMLElement) => void;
+ onScaleChange?: (scale: number) => void;
+ viewMode?: "fit" | "actual";
}
const MOBILE_VIEWPORT_WIDTH = 375;
-const MOBILE_VIEWPORT_HEIGHT = 812;
-const DESKTOP_VIEWPORT_WIDTH = 1366;
-
-function PreviewComponent({ code, device, doUpdate }: Props) {
+export const DESKTOP_VIEWPORT_WIDTH = 1366;
+
+function PreviewComponent({
+ code,
+ device,
+ doUpdate,
+ onScaleChange,
+ viewMode,
+}: Props) {
const iframeRef = useRef<HTMLIFrameElement | null>(null);
const wrapperRef = useRef<HTMLDivElement | null>(null);
@@ -23,6 +30,7 @@ function PreviewComponent({ code, device, doUpdate }: Props) {
// Select and edit functionality
const [clickEvent, setClickEvent] = useState<MouseEvent | null>(null);
const [scale, setScale] = useState(1);
+ const activeMode = viewMode ?? "fit";
const handleIframeClick = useCallback((event: MouseEvent) => {
setClickEvent(event);
}, []);
@@ -38,10 +46,14 @@ function PreviewComponent({ code, device, doUpdate }: Props) {
const viewportHeight = wrapper.clientHeight;
if (device === "desktop") {
- const scaleValue = Math.min(1, viewportWidth / DESKTOP_VIEWPORT_WIDTH);
+ const scaleValue =
+ activeMode === "fit"
+ ? Math.min(1, viewportWidth / DESKTOP_VIEWPORT_WIDTH)
+ : 1;
const iframeHeight = scaleValue > 0 ? viewportHeight / scaleValue : viewportHeight;
setScale(scaleValue);
+ onScaleChange?.(scaleValue);
iframe.style.width = `${DESKTOP_VIEWPORT_WIDTH}px`;
iframe.style.height = `${iframeHeight}px`;
iframe.style.transform = `scale(${scaleValue})`;
@@ -49,16 +61,11 @@ function PreviewComponent({ code, device, doUpdate }: Props) {
return;
}
- const scaleValue = Math.min(
- 1,
- viewportWidth / MOBILE_VIEWPORT_WIDTH,
- viewportHeight / MOBILE_VIEWPORT_HEIGHT
- );
-
- setScale(scaleValue);
+ setScale(1);
+ onScaleChange?.(1);
iframe.style.width = `${MOBILE_VIEWPORT_WIDTH}px`;
- iframe.style.height = `${MOBILE_VIEWPORT_HEIGHT}px`;
- iframe.style.transform = `scale(${scaleValue})`;
+ iframe.style.height = `${viewportHeight}px`;
+ iframe.style.transform = "scale(1)";
iframe.style.transformOrigin = "top left";
};
@@ -73,7 +80,7 @@ function PreviewComponent({ code, device, doUpdate }: Props) {
window.removeEventListener("resize", updateScale);
resizeObserver.disconnect();
};
- }, [device]);
+ }, [activeMode, device, onScaleChange]);
useEffect(() => {
const iframe = iframeRef.current;
@@ -106,15 +113,17 @@ function PreviewComponent({ code, device, doUpdate }: Props) {
return (
<div
- className={`flex-1 min-h-0 relative overflow-hidden ${
+ className={`flex-1 min-h-0 relative ${
device === "mobile"
- ? "flex justify-center bg-gray-100 dark:bg-zinc-900"
- : "flex justify-center"
+ ? "flex justify-center overflow-hidden bg-gray-100 dark:bg-zinc-900"
+ : activeMode === "fit"
+ ? "flex justify-center overflow-hidden"
+ : "overflow-auto"
}`}
>
<div
ref={wrapperRef}
- className="w-full h-full"
+ className={`w-full h-full ${device === "mobile" ? "flex justify-center" : ""}`}
>
<iframe
id={`preview-${device}`}
diff --git a/frontend/src/components/preview/PreviewPane.tsx b/frontend/src/components/preview/PreviewPane.tsx
index a75e374a..7a3d91ef 100644
--- a/frontend/src/components/preview/PreviewPane.tsx
+++ b/frontend/src/components/preview/PreviewPane.tsx
@@ -41,6 +41,9 @@ function PreviewPane({ doUpdate, settings, onOpenVersions }: Props) {
const { appState } = useAppStore();
const { inputMode, referenceImages, head, commits, setHead } = useProjectStore();
const [activeReferenceIndex, setActiveReferenceIndex] = useState(0);
+ const [activeTab, setActiveTab] = useState("desktop");
+ const [desktopScale, setDesktopScale] = useState(1);
+ const [desktopViewMode, setDesktopViewMode] = useState<"fit" | "actual">("fit");
// Sorted commit list for version navigation
const sortedCommits = useMemo(() =>
@@ -65,9 +68,13 @@ function PreviewPane({ doUpdate, settings, onOpenVersions }: Props) {
return (
<div className="flex-1 flex flex-col min-h-0">
- <Tabs defaultValue="desktop" className="flex-1 flex flex-col min-h-0">
+ <Tabs
+ value={activeTab}
+ onValueChange={setActiveTab}
+ className="flex-1 flex flex-col min-h-0"
+ >
<div className="flex items-center justify-between px-4 py-2 shrink-0 border-b border-gray-200 dark:border-zinc-800 bg-white dark:bg-zinc-950">
- <div className="flex items-center">
+ <div className="flex items-center gap-2">
<TabsList>
<TabsTrigger value="desktop" title="Desktop" data-testid="tab-desktop">
<FaDesktop />
@@ -84,6 +91,57 @@ function PreviewPane({ doUpdate, settings, onOpenVersions }: Props) {
</TabsTrigger>
)}
</TabsList>
+ {(activeTab === "desktop" || activeTab === "mobile") && (
+ <div className="hidden sm:inline-flex items-center gap-1.5">
+ {activeTab === "desktop" && (
+ <div
+ className="inline-flex items-center gap-2 rounded-lg border border-gray-200 bg-gray-50 px-1.5 py-1 text-[11px] font-medium text-gray-600 dark:border-zinc-700 dark:bg-zinc-900 dark:text-zinc-300"
+ title={desktopViewMode === "fit"
+ ? "Resized to fit this pane."
+ : "Rendering at 100% scale. Use Fit to resize to this pane."}
+ >
+ <div className="inline-flex items-center rounded-md bg-white p-0.5 ring-1 ring-gray-200 dark:bg-zinc-950 dark:ring-zinc-700">
+ <button
+ type="button"
+ onClick={() => setDesktopViewMode("fit")}
+ className={`rounded px-2 py-0.5 text-[11px] font-semibold transition-colors ${
+ desktopViewMode === "fit"
+ ? "bg-gray-900 text-white dark:bg-zinc-200 dark:text-zinc-900"
+ : "text-gray-500 hover:text-gray-700 dark:text-zinc-400 dark:hover:text-zinc-200"
+ }`}
+ >
+ Fit to pane
+ </button>
+ <button
+ type="button"
+ onClick={() => setDesktopViewMode("actual")}
+ className={`rounded px-2 py-0.5 text-[11px] font-semibold transition-colors ${
+ desktopViewMode === "actual"
+ ? "bg-gray-900 text-white dark:bg-zinc-200 dark:text-zinc-900"
+ : "text-gray-500 hover:text-gray-700 dark:text-zinc-400 dark:hover:text-zinc-200"
+ }`}
+ >
+ Actual size
+ </button>
+ </div>
+ {desktopViewMode === "fit" && desktopScale < 0.999 && (
+ <span className="text-[11px] text-gray-500 dark:text-zinc-400">
+ Scaled: {Math.round(desktopScale * 100)}%
+ </span>
+ )}
+ </div>
+ )}
+ <Button
+ onClick={() => openInNewTab(previewCode)}
+ variant="ghost"
+ size="icon"
+ title="Open in New Tab"
+ className="h-8 w-8"
+ >
+ <LuExternalLink />
+ </Button>
+ </div>
+ )}
</div>
{/* Version navigation */}
@@ -130,15 +188,6 @@ function PreviewPane({ doUpdate, settings, onOpenVersions }: Props) {
<LuDownload />
</Button>
)}
- <Button
- onClick={() => openInNewTab(previewCode)}
- variant="ghost"
- size="icon"
- title="Open in New Tab"
- className="h-9 w-9"
- >
- <LuExternalLink />
- </Button>
<Button
onClick={() => {
const iframes = document.querySelectorAll("iframe");
@@ -164,6 +213,8 @@ function PreviewPane({ doUpdate, settings, onOpenVersions }: Props) {
code={previewCode}
device="desktop"
doUpdate={doUpdate}
+ onScaleChange={setDesktopScale}
+ viewMode={desktopViewMode}
/>
</TabsContent>
<TabsContent value="mobile" className="flex-1 min-h-0 mt-0 data-[state=active]:flex data-[state=active]:flex-col">
@@ -171,6 +222,7 @@ function PreviewPane({ doUpdate, settings, onOpenVersions }: Props) {
code={previewCode}
device="mobile"
doUpdate={doUpdate}
+ viewMode="actual"
/>
</TabsContent>
<TabsContent value="code" className="flex-1 min-h-0 mt-0 overflow-auto">
diff --git a/frontend/src/components/variants/Variants.tsx b/frontend/src/components/variants/Variants.tsx
index 9fed79d6..95c122c4 100644
--- a/frontend/src/components/variants/Variants.tsx
+++ b/frontend/src/components/variants/Variants.tsx
@@ -135,15 +135,15 @@ function Variants() {
isSelected={index === selectedVariantIndex}
/>
<div className="flex items-center px-1.5 py-0.5 bg-white dark:bg-zinc-900">
- <span className="flex items-center text-[10px] text-gray-500 dark:text-gray-400">
+ <span className="inline-flex min-w-0 items-center text-[10px] text-gray-500 dark:text-gray-400 whitespace-nowrap">
<span className={`w-1.5 h-1.5 rounded-full mr-1 ${statusColor}`} />
Option {index + 1}
- {variant.status === "generating" && (
- <div className="scale-50 ml-0.5">
- <Spinner />
- </div>
- )}
</span>
+ {variant.status === "generating" && (
+ <div className="ml-auto shrink-0 scale-50">
+ <Spinner />
+ </div>
+ )}
</div>
</div>
);