Skip to content

Commit c0ebc88

Browse files
fix(web): return focus to the composer after closing a media preview (pingdotgg#9513)
Co-authored-by: Claude Code <[email protected]>
1 parent 522ebe6 commit c0ebc88

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

‎apps/web/src/components/chat/ExpandedImageDialog.tsx‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { memo, useCallback, useEffect, useState, type ReactNode } from "react";
1+
import { memo, useCallback, useEffect, useRef, useState, type ReactNode } from "react";
22
import { createPortal } from "react-dom";
33
import { ChevronLeftIcon, ChevronRightIcon, XIcon } from "lucide-react";
44
import { Button } from "../ui/button";
@@ -9,6 +9,7 @@ import { OpenMediaLink } from "../media/OpenMediaLink";
99
import { MediaActions, type MediaActionSource } from "../media/MediaActions";
1010
import { MediaVideoPlayer } from "../media/MediaVideoPlayer";
1111
import { isContextMenuOpen } from "../../contextMenuFallback";
12+
import { composerFloatingLayerProps } from "./composerEventScope";
1213

1314
interface ExpandedImageDialogProps {
1415
preview: ExpandedImagePreview;
@@ -79,6 +80,20 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
7980
setImageOffset((current) => current + direction);
8081
}, []);
8182

83+
// The element that opened the preview gets focus back on close. Without
84+
// this a close button click leaves focus on the unmounted dialog, and the
85+
// composer that owned the opener reads that as a blur and rests.
86+
const openerRef = useRef<Element | null>(null);
87+
useEffect(() => {
88+
openerRef.current = document.activeElement;
89+
return () => {
90+
const opener = openerRef.current;
91+
if (opener instanceof HTMLElement && opener.isConnected) {
92+
opener.focus({ preventScroll: true });
93+
}
94+
};
95+
}, []);
96+
8297
useEffect(() => {
8398
const onKeyDown = (event: globalThis.KeyboardEvent) => {
8499
if (event.defaultPrevented || isContextMenuOpen()) {
@@ -115,6 +130,7 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
115130

116131
return createPortal(
117132
<div
133+
{...composerFloatingLayerProps}
118134
className="fixed inset-0 z-[60] flex items-center justify-center bg-black/75 px-4 py-6 [-webkit-app-region:no-drag]"
119135
role="dialog"
120136
aria-modal="true"

0 commit comments

Comments
 (0)