@@ -10,6 +10,7 @@ import {
1010import { useCallback , useEffect , useRef , useState } from "react" ;
1111
1212import { useComposerDraftStore } from "~/composerDraftStore" ;
13+ import { captureDocumentFocus , restoreDocumentFocus } from "~/lib/documentFocus" ;
1314import { previewAnnotationScreenshotFile } from "~/lib/previewAnnotation" ;
1415import { ensureLocalApi } from "~/localApi" ;
1516import {
@@ -468,8 +469,7 @@ export function PreviewView({ threadRef, tabId: requestedTabId, configuredUrls,
468469 // focus into the guest webContents. We restore it when the pick
469470 // resolves so the user's typing context isn't lost — otherwise after
470471 // every pick they'd have to click back into the textarea.
471- const previouslyFocused =
472- typeof document !== "undefined" ? ( document . activeElement as HTMLElement | null ) : null ;
472+ const previouslyFocused = captureDocumentFocus ( ) ;
473473 pickActiveRef . current = true ;
474474 setPickActive ( true ) ;
475475 void ( async ( ) => {
@@ -499,17 +499,7 @@ export function PreviewView({ threadRef, tabId: requestedTabId, configuredUrls,
499499 // Best-effort: restore focus to whatever the user had before the
500500 // pick stole it into the guest webContents. Skip if the previously-
501501 // focused element was unmounted or is no longer focusable.
502- if (
503- previouslyFocused &&
504- previouslyFocused . isConnected &&
505- typeof previouslyFocused . focus === "function"
506- ) {
507- try {
508- previouslyFocused . focus ( { preventScroll : true } ) ;
509- } catch {
510- // Some elements throw on .focus() (detached iframes, etc.).
511- }
512- }
502+ restoreDocumentFocus ( previouslyFocused ) ;
513503 }
514504 } ) ( ) ;
515505 } , [ addImage , addPreviewAnnotation , tabId , threadRef ] ) ;
0 commit comments