1- import { memo , useCallback , useEffect , useState , type ReactNode } from "react" ;
1+ import { memo , useCallback , useEffect , useRef , useState , type ReactNode } from "react" ;
22import { createPortal } from "react-dom" ;
33import { ChevronLeftIcon , ChevronRightIcon , XIcon } from "lucide-react" ;
44import { Button } from "../ui/button" ;
@@ -9,6 +9,7 @@ import { OpenMediaLink } from "../media/OpenMediaLink";
99import { MediaActions , type MediaActionSource } from "../media/MediaActions" ;
1010import { MediaVideoPlayer } from "../media/MediaVideoPlayer" ;
1111import { isContextMenuOpen } from "../../contextMenuFallback" ;
12+ import { composerFloatingLayerProps } from "./composerEventScope" ;
1213
1314interface 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