diff --git a/app/editor/components/FindAndReplace.tsx b/app/editor/components/FindAndReplace.tsx index e4d2ad470f..a204a173ab 100644 --- a/app/editor/components/FindAndReplace.tsx +++ b/app/editor/components/FindAndReplace.tsx @@ -34,6 +34,7 @@ type KeyboardShortcutsProps = { open: boolean; inputRef: React.RefObject; handleOpen: ({ withReplace }: { withReplace: boolean }) => void; + handleClose: () => void; handleCaseSensitive: () => void; handleRegex: () => void; }; @@ -42,18 +43,24 @@ function useKeyboardShortcuts({ open, inputRef, handleOpen, + handleClose, handleCaseSensitive, handleRegex, }: KeyboardShortcutsProps) { - // Open popover + // Toggle popover useKeyDown( (ev) => isModKey(ev) && ev.code === "KeyF" && - (!open || ev.altKey || document.activeElement !== inputRef.current) && // Keyboard handler is through the AppMenu on Desktop v1.2.0+ !(Desktop.bridge && "onFindInPage" in Desktop.bridge), (ev) => { + // Close and fall through to the browser's find when already focused. + if (open && !ev.altKey && document.activeElement === inputRef.current) { + handleClose(); + return; + } + ev.preventDefault(); handleOpen({ withReplace: ev.altKey }); }, @@ -132,6 +139,10 @@ export default function FindAndReplace({ } if ("onFindInPage" in Desktop.bridge) { Desktop.bridge.onFindInPage(() => { + if (document.activeElement === inputRef.current) { + setLocalOpen(false); + return; + } selectionRef.current = window.getSelection()?.toString(); setLocalOpen(true); }); @@ -186,6 +197,8 @@ export default function FindAndReplace({ [localOpen, readOnly, selectInputText, selectInputReplaceText] ); + const handleClose = React.useCallback(() => setLocalOpen(false), []); + const handleMore = React.useCallback(() => { setShowReplace((state) => !state); setTimeout(() => inputReplaceRef.current?.focus(), 100); @@ -341,6 +354,7 @@ export default function FindAndReplace({ open: localOpen, inputRef, handleOpen, + handleClose, handleCaseSensitive, handleRegex, }); @@ -348,8 +362,8 @@ export default function FindAndReplace({ const style: React.CSSProperties = React.useMemo( () => ({ position: "fixed", - top: 0, - right: 0, + top: 60, + right: 16, zIndex: depths.popover, }), [] @@ -448,12 +462,11 @@ export default function FindAndReplace({ width={0} minWidth={isMobile ? undefined : 420} scrollable={false} + align="end" onEscapeKeyDown={handleEscape} onPointerDownOutside={(ev) => ev.preventDefault()} onFocusOutside={(ev) => ev.preventDefault()} style={{ - marginRight: 16, - marginTop: 60, width: isMobile ? "calc(100vw - 32px)" : undefined, }} > @@ -503,10 +516,7 @@ export default function FindAndReplace({ {isMobile && ( {controls} - setLocalOpen(false)} - aria-label={t("Close")} - > +