Skip to content
TypeScript

fix(web): image viewer close button stays clear of the window controls (#13919)

Otavio Salvador27bf560fb261
diff --git a/apps/web/src/components/chat/ExpandedImageDialog.tsx b/apps/web/src/components/chat/ExpandedImageDialog.tsx
index 5ee656d894f..538c5b9994d 100644
--- a/apps/web/src/components/chat/ExpandedImageDialog.tsx
+++ b/apps/web/src/components/chat/ExpandedImageDialog.tsx
@@ -175,7 +175,7 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
variant="media"
showCloseButton={false}
bottomStickOnMobile={false}
- className="row-start-1 h-full max-h-[92vh] w-[92vw] max-w-[92vw] items-center justify-center overflow-visible [--media-width:92vw] [--media-height:min(86vh,calc(100vh-160px))] sm:[--media-width:calc(92vw-96px)]"
+ className="row-start-1 h-full max-h-[92vh] w-[92vw] max-w-[92vw] items-center justify-center overflow-visible [--media-width:92vw] [--media-height:min(calc((100vh-var(--native-titlebar-height,0px))*0.86),calc(100vh-var(--native-titlebar-height,0px)-160px))] sm:[--media-width:calc(92vw-96px)]"
onKeyDown={onKeyDown}
initialFocus={closeButtonRef}
finalFocus={() => returnFocusTarget}
diff --git a/apps/web/src/components/chat/ZoomableImage.tsx b/apps/web/src/components/chat/ZoomableImage.tsx
index 466544fb404..7012e6d2a96 100644
--- a/apps/web/src/components/chat/ZoomableImage.tsx
+++ b/apps/web/src/components/chat/ZoomableImage.tsx
@@ -1,7 +1,19 @@
import { useEffect, useImperativeHandle, useLayoutEffect, useRef, useState, type Ref } from "react";
+import {
+ onWindowControlsOverlayGeometryChange,
+ windowControlsOverlayHeight,
+} from "../../lib/windowControlsOverlay";
+
const MAX_ZOOM = 8;
+// The viewer lays out below the native caption strip so its controls never share a row with
+// the window buttons. ExpandedImageDialog and the media dialog viewport mirror this in CSS
+// through --native-titlebar-height.
+function readViewerSize() {
+ return { width: window.innerWidth, height: window.innerHeight - windowControlsOverlayHeight() };
+}
+
export interface ZoomableImageHandle {
pan: (key: string) => boolean;
}
@@ -19,10 +31,7 @@ export function ZoomableImage({
}) {
const viewportRef = useRef<HTMLDivElement>(null);
const [naturalSize, setNaturalSize] = useState({ width: 0, height: 0 });
- const [windowSize, setWindowSize] = useState(() => ({
- width: window.innerWidth,
- height: window.innerHeight,
- }));
+ const [viewerSize, setViewerSize] = useState(readViewerSize);
const [zoom, setZoom] = useState(1);
const zoomRef = useRef(1);
const anchorRef = useRef<{ x: number; y: number; clientX: number; clientY: number } | null>(null);
@@ -35,10 +44,10 @@ export function ZoomableImage({
} | null>(null);
const suppressClickRef = useRef(false);
const [dragging, setDragging] = useState(false);
- const maxHeight = Math.max(1, Math.min(windowSize.height * 0.86, windowSize.height - 160));
+ const maxHeight = Math.max(1, Math.min(viewerSize.height * 0.86, viewerSize.height - 160));
const fit = Math.min(
1,
- (windowSize.width * 0.92 - (windowSize.width >= 640 ? 96 : 0)) / (naturalSize.width || 1),
+ (viewerSize.width * 0.92 - (viewerSize.width >= 640 ? 96 : 0)) / (naturalSize.width || 1),
maxHeight / (naturalSize.height || 1),
);
const width = naturalSize.width * fit * zoom;
@@ -108,11 +117,16 @@ export function ZoomableImage({
useEffect(() => {
const resize = () => {
- setWindowSize({ width: window.innerWidth, height: window.innerHeight });
+ setViewerSize(readViewerSize());
changeZoom(1);
};
window.addEventListener("resize", resize);
- return () => window.removeEventListener("resize", resize);
+ // The caption strip can come and go around fullscreen without a matching resize event.
+ const stopOverlayListener = onWindowControlsOverlayGeometryChange(resize);
+ return () => {
+ window.removeEventListener("resize", resize);
+ stopOverlayListener();
+ };
}, [changeZoom]);
useEffect(() => {
diff --git a/apps/web/src/components/ui/dialog.tsx b/apps/web/src/components/ui/dialog.tsx
index 463cedd79fb..11b27495657 100644
--- a/apps/web/src/components/ui/dialog.tsx
+++ b/apps/web/src/components/ui/dialog.tsx
@@ -76,7 +76,7 @@ function DialogPopup({
className={cn(
bottomStickOnMobile && "max-sm:grid-rows-[1fr_auto] max-sm:p-0 max-sm:pt-12",
variant === "media" &&
- "z-[60] grid-rows-1 place-items-center px-4 py-6 [-webkit-app-region:no-drag]",
+ "z-[60] grid-rows-1 place-items-center px-4 pb-6 pt-[calc(var(--native-titlebar-height,0px)+1.5rem)] [-webkit-app-region:no-drag]",
)}
>
<DialogPrimitive.Popup
diff --git a/apps/web/src/index.css b/apps/web/src/index.css
index 5b4945bf3d3..72afd53fdbe 100644
--- a/apps/web/src/index.css
+++ b/apps/web/src/index.css
@@ -149,6 +149,7 @@ html[data-mobile-composer-route-transition="true"]::view-transition-old(t3-mobil
}
.wco {
+ --native-titlebar-height: env(titlebar-area-height, 0px);
--workspace-topbar-height: max(40px, env(titlebar-area-height, 52px));
--workspace-controls-top: env(titlebar-area-y, 0px);
--workspace-controls-left: calc(env(titlebar-area-x, 0px) + 0.75rem);
diff --git a/apps/web/src/lib/windowControlsOverlay.ts b/apps/web/src/lib/windowControlsOverlay.ts
index fb66b1c1ca5..f2bbdc77c22 100644
--- a/apps/web/src/lib/windowControlsOverlay.ts
+++ b/apps/web/src/lib/windowControlsOverlay.ts
@@ -30,6 +30,25 @@ function getWindowControlsOverlay(): WindowControlsOverlayLike | null {
return (navigator as NavigatorWithWindowControlsOverlay).windowControlsOverlay ?? null;
}
+/** Height of the native caption strip drawn over the page, or 0 when the window has none. */
+export function windowControlsOverlayHeight(): number {
+ const overlay = getWindowControlsOverlay();
+ return overlay?.visible ? overlay.getTitlebarAreaRect().height : 0;
+}
+
+/** Calls `listener` when the native caption strip appears, disappears, or changes size. */
+export function onWindowControlsOverlayGeometryChange(listener: () => void): () => void {
+ const overlay = getWindowControlsOverlay();
+ if (!overlay) {
+ return () => {};
+ }
+
+ overlay.addEventListener("geometrychange", listener);
+ return () => {
+ overlay.removeEventListener("geometrychange", listener);
+ };
+}
+
export function syncDocumentWindowControlsOverlayClass(): () => void {
if (typeof document === "undefined") {
return () => {};
@@ -41,14 +60,7 @@ export function syncDocumentWindowControlsOverlayClass(): () => void {
};
update();
- if (!overlay) {
- return () => {};
- }
-
- overlay.addEventListener("geometrychange", update);
- return () => {
- overlay.removeEventListener("geometrychange", update);
- };
+ return onWindowControlsOverlayGeometryChange(update);
}
function subscribeTitlebarArea(listener: () => void): () => void {