From 2b20dcf8e62b1ffe5a83debd75b0822ab61ba691 Mon Sep 17 00:00:00 2001 From: Septicity Date: Sat, 11 Apr 2026 05:56:38 -0500 Subject: [PATCH] Make zoom input editable by typing (moved View Original Size functionality to new button) --- .../image-viewer/ImageViewer.css.ts | 11 ++ .../components/image-viewer/ImageViewer.tsx | 109 ++++++++++++++++-- 2 files changed, 108 insertions(+), 12 deletions(-) diff --git a/src/app/components/image-viewer/ImageViewer.css.ts b/src/app/components/image-viewer/ImageViewer.css.ts index cf303c7e5..939198405 100644 --- a/src/app/components/image-viewer/ImageViewer.css.ts +++ b/src/app/components/image-viewer/ImageViewer.css.ts @@ -28,6 +28,17 @@ export const ImageViewerContent = style([ }, ]); +export const ImageViewerInput = style([ + DefaultReset, + { + all: 'unset', + fieldSizing: 'content', + textAlign: 'center', + font: 'inherit', + color: 'inherit', + }, +]); + export const ImageViewerImg = style([ DefaultReset, { diff --git a/src/app/components/image-viewer/ImageViewer.tsx b/src/app/components/image-viewer/ImageViewer.tsx index 41c05b690..3c9edd1b8 100644 --- a/src/app/components/image-viewer/ImageViewer.tsx +++ b/src/app/components/image-viewer/ImageViewer.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState } from 'react'; +import { useEffect, useState, useRef } from 'react'; import FileSaver from 'file-saver'; import classNames from 'classnames'; import { Box, Chip, Header, Icon, IconButton, Icons, Text, as } from 'folds'; @@ -25,13 +25,30 @@ export const ImageViewer = as<'div', ImageViewerProps>( setZoom, } = useImageGestures(true, 0.2); - const [fitRatio, setFitRatio] = useState(1); const [isImageReady, setIsImageReady] = useState(false); + const [isEditingZoom, setIsEditingZoom] = useState(false); + const [fitRatio, setFitRatio] = useState(1); + const [zoomInput, setZoomInput] = useState('100'); useEffect(() => { setFitRatio(1); setIsImageReady(false); + setIsEditingZoom(false); + setZoomInput('100'); }, [src]); + useEffect(() => { + if (!isEditingZoom) { + setZoomInput(Math.round(transforms.zoom * 100).toString()); + } + }, [isEditingZoom, transforms.zoom]); + + const zoomInputRef = useRef(null); + useEffect(() => { + if (isEditingZoom) { + zoomInputRef.current?.focus(); + } + }, [isEditingZoom]); + const handleDownload = async () => { const fileContent = await downloadMedia(src); FileSaver.saveAs(fileContent, alt); @@ -57,13 +74,28 @@ export const ImageViewer = as<'div', ImageViewerProps>( { + setZoom(1); + }} + aria-label="View Original Size" + > + + + ( resetTransforms(); setZoom(fitRatio); }} - aria-label="Refresh View" + aria-label="Reset Zoom" > @@ -85,8 +117,61 @@ export const ImageViewer = as<'div', ImageViewerProps>( > - - {Math.round(transforms.zoom * 100)}% + { + setZoomInput(Math.round(transforms.zoom * 100).toString()); + setIsEditingZoom(true); + }} + > + + {isEditingZoom ? ( + + { + setZoomInput(e.target.value); + }} + onBlur={() => { + const next = parseInt(zoomInput, 10); + if (!Number.isNaN(next)) { + setZoom(next / 100); + } + setIsEditingZoom(false); + }} + onKeyDown={(e) => { + if (e.key === 'Enter') { + const next = parseInt(zoomInput, 10); + if (!Number.isNaN(next)) { + setZoom(next / 100); + } + setIsEditingZoom(false); + } + }} + /> + % + + ) : ( + `${Math.round(transforms.zoom * 100)}%` + )} + 1 ? 'Success' : 'SurfaceVariant'}