Skip to content

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

Python

Enable lightbox zoom and scrolling

Abi Raja1d69885fb334
diff --git a/frontend/src/components/sidebar/Sidebar.tsx b/frontend/src/components/sidebar/Sidebar.tsx
index f88a44d4..c7df993d 100644
--- a/frontend/src/components/sidebar/Sidebar.tsx
+++ b/frontend/src/components/sidebar/Sidebar.tsx
@@ -3,7 +3,14 @@ import { useProjectStore } from "../../store/project-store";
import { AppState } from "../../types";
import { Button } from "../ui/button";
import { useEffect, useRef, useState, useCallback } from "react";
-import { LuMousePointerClick, LuRefreshCw, LuArrowUp, LuX } from "react-icons/lu";
+import {
+ LuMousePointerClick,
+ LuRefreshCw,
+ LuArrowUp,
+ LuMinus,
+ LuPlus,
+ LuX,
+} from "react-icons/lu";
import { toast } from "react-hot-toast";
import Variants from "../variants/Variants";
@@ -22,6 +29,8 @@ interface SidebarProps {
}
const MAX_UPDATE_IMAGES = 5;
+const MIN_LIGHTBOX_ZOOM = 0.5;
+const MAX_LIGHTBOX_ZOOM = 6;
function summarizeLatestChange(commit: Commit | null): string | null {
if (!commit) return null;
@@ -52,10 +61,17 @@ function Sidebar({
}: SidebarProps) {
const textareaRef = useRef<HTMLTextAreaElement>(null);
const middlePaneRef = useRef<HTMLDivElement>(null);
+ const lightboxViewportRef = useRef<HTMLDivElement>(null);
const [isErrorExpanded, setIsErrorExpanded] = useState(false);
const [isDragging, setIsDragging] = useState(false);
const [nowMs, setNowMs] = useState(() => Date.now());
const [lightboxImage, setLightboxImage] = useState<string | null>(null);
+ const [lightboxZoom, setLightboxZoom] = useState(1);
+ const [lightboxNaturalSize, setLightboxNaturalSize] = useState<{
+ width: number;
+ height: number;
+ } | null>(null);
+ const [lightboxFitScale, setLightboxFitScale] = useState(1);
const { appState, updateInstruction, setUpdateInstruction, updateImages, setUpdateImages, inSelectAndEditMode, toggleInSelectAndEditMode } = useAppStore();
@@ -213,6 +229,72 @@ function Sidebar({
return () => window.clearInterval(intervalId);
}, [appState]);
+ const closeLightbox = useCallback(() => {
+ setLightboxImage(null);
+ setLightboxZoom(1);
+ setLightboxNaturalSize(null);
+ setLightboxFitScale(1);
+ }, []);
+
+ const openLightbox = useCallback((image: string) => {
+ setLightboxImage(image);
+ setLightboxZoom(1);
+ setLightboxNaturalSize(null);
+ setLightboxFitScale(1);
+ }, []);
+
+ const recomputeLightboxFitScale = useCallback(() => {
+ if (!lightboxViewportRef.current || !lightboxNaturalSize) return;
+
+ const viewportWidth = lightboxViewportRef.current.clientWidth;
+ const viewportHeight = lightboxViewportRef.current.clientHeight;
+ if (viewportWidth <= 0 || viewportHeight <= 0) return;
+
+ const fitScale = Math.min(
+ viewportWidth / lightboxNaturalSize.width,
+ viewportHeight / lightboxNaturalSize.height,
+ 1
+ );
+ setLightboxFitScale(fitScale);
+ }, [lightboxNaturalSize]);
+
+ useEffect(() => {
+ if (!lightboxImage) return;
+ recomputeLightboxFitScale();
+
+ const handleResize = () => recomputeLightboxFitScale();
+ window.addEventListener("resize", handleResize);
+ return () => window.removeEventListener("resize", handleResize);
+ }, [lightboxImage, recomputeLightboxFitScale]);
+
+ useEffect(() => {
+ recomputeLightboxFitScale();
+ }, [recomputeLightboxFitScale]);
+
+ const zoomInLightbox = () => {
+ setLightboxZoom((current) =>
+ Math.min(MAX_LIGHTBOX_ZOOM, Math.round((current + 0.25) * 100) / 100)
+ );
+ };
+
+ const zoomOutLightbox = () => {
+ setLightboxZoom((current) =>
+ Math.max(MIN_LIGHTBOX_ZOOM, Math.round((current - 0.25) * 100) / 100)
+ );
+ };
+
+ const resetLightboxZoom = () => {
+ setLightboxZoom(1);
+ };
+
+ const effectiveLightboxScale = lightboxFitScale * lightboxZoom;
+ const lightboxDisplayWidth = lightboxNaturalSize
+ ? Math.max(1, Math.round(lightboxNaturalSize.width * effectiveLightboxScale))
+ : undefined;
+ const lightboxDisplayHeight = lightboxNaturalSize
+ ? Math.max(1, Math.round(lightboxNaturalSize.height * effectiveLightboxScale))
+ : undefined;
+
return (
<div className="flex flex-col h-full">
@@ -237,7 +319,7 @@ function Sidebar({
{latestChangeImages.map((image, index) => (
<button
key={`${image.slice(0, 40)}-${index}`}
- onClick={() => setLightboxImage(image)}
+ onClick={() => openLightbox(image)}
className="shrink-0 cursor-zoom-in rounded-lg border border-gray-200 bg-white p-1 dark:border-zinc-700 dark:bg-zinc-900 hover:border-violet-300 dark:hover:border-violet-500 transition-colors"
>
<img
@@ -458,27 +540,77 @@ function Sidebar({
)}
{/* Image lightbox */}
- <Dialog open={!!lightboxImage} onOpenChange={() => setLightboxImage(null)}>
+ <Dialog open={!!lightboxImage} onOpenChange={(open) => !open && closeLightbox()}>
<DialogPortal>
<DialogOverlay className="bg-black/80 backdrop-blur-sm" />
<div
- className="fixed inset-0 z-50 flex items-center justify-center p-8"
- onClick={() => setLightboxImage(null)}
+ className="fixed inset-0 z-50 p-4 sm:p-6"
+ onClick={closeLightbox}
>
- <button
- onClick={() => setLightboxImage(null)}
- className="absolute top-4 right-4 rounded-full bg-black/50 p-2 text-white hover:bg-black/70 transition-colors"
- >
- <LuX className="w-5 h-5" />
- </button>
- {lightboxImage && (
- <img
- src={lightboxImage}
- alt="Reference image"
- className="max-h-full max-w-full rounded-lg object-contain"
- onClick={(e) => e.stopPropagation()}
- />
- )}
+ <div className="relative flex h-full w-full flex-col" onClick={(e) => e.stopPropagation()}>
+ <div className="absolute right-0 top-0 z-10 flex items-center gap-2 rounded-lg bg-black/60 px-2 py-1.5 backdrop-blur-sm">
+ <button
+ onClick={zoomOutLightbox}
+ className="rounded-md p-1.5 text-white hover:bg-white/10 disabled:opacity-40"
+ disabled={lightboxZoom <= MIN_LIGHTBOX_ZOOM}
+ title="Zoom out"
+ >
+ <LuMinus className="h-4 w-4" />
+ </button>
+ <button
+ onClick={resetLightboxZoom}
+ className="rounded-md px-2 py-1 text-xs font-medium text-white hover:bg-white/10"
+ title="Reset zoom"
+ >
+ {Math.round(lightboxZoom * 100)}%
+ </button>
+ <button
+ onClick={zoomInLightbox}
+ className="rounded-md p-1.5 text-white hover:bg-white/10 disabled:opacity-40"
+ disabled={lightboxZoom >= MAX_LIGHTBOX_ZOOM}
+ title="Zoom in"
+ >
+ <LuPlus className="h-4 w-4" />
+ </button>
+ <button
+ onClick={closeLightbox}
+ className="rounded-md p-1.5 text-white hover:bg-white/10"
+ title="Close"
+ >
+ <LuX className="w-5 h-5" />
+ </button>
+ </div>
+ <div
+ ref={lightboxViewportRef}
+ className="mt-12 flex-1 overflow-auto rounded-lg"
+ >
+ <div className="flex min-h-full min-w-full items-center justify-center p-4">
+ {lightboxImage && (
+ <img
+ src={lightboxImage}
+ alt="Reference image"
+ className="rounded-lg object-contain"
+ style={
+ lightboxDisplayWidth && lightboxDisplayHeight
+ ? {
+ width: `${lightboxDisplayWidth}px`,
+ height: `${lightboxDisplayHeight}px`,
+ maxWidth: "none",
+ maxHeight: "none",
+ }
+ : undefined
+ }
+ onLoad={(event) => {
+ setLightboxNaturalSize({
+ width: event.currentTarget.naturalWidth,
+ height: event.currentTarget.naturalHeight,
+ });
+ }}
+ />
+ )}
+ </div>
+ </div>
+ </div>
</div>
</DialogPortal>
</Dialog>