diff --git a/app/components/Popover.tsx b/app/components/Popover.tsx index 96ee170130..b2c70b6713 100644 --- a/app/components/Popover.tsx +++ b/app/components/Popover.tsx @@ -1,6 +1,5 @@ +import * as PopoverPrimitive from "@radix-ui/react-popover"; import * as React from "react"; -import { Dialog } from "reakit/Dialog"; -import { Popover as ReakitPopover, PopoverProps } from "reakit/Popover"; import styled from "styled-components"; import breakpoint from "styled-components-breakpoint"; import { depths, s } from "@shared/styles"; @@ -8,8 +7,12 @@ import useKeyDown from "~/hooks/useKeyDown"; import useMobile from "~/hooks/useMobile"; import { fadeAndScaleIn } from "~/styles/animations"; -type Props = PopoverProps & { +type Props = { children: React.ReactNode; + /** Whether the popover is open */ + open?: boolean; + /** Callback when the popover open state changes */ + onOpenChange?: (open: boolean) => void; /** The width of the popover, defaults to 380px. */ width?: number; /** The minimum width of the popover, use instead of width if contents adjusts size. */ @@ -25,34 +28,121 @@ type Props = PopoverProps & { /** The position of the popover on mobile, defaults to "top". */ mobilePosition?: "top" | "bottom"; /** Function to show the popover */ - show: () => void; + show?: () => void; /** Function to hide the popover */ - hide: () => void; + hide?: () => void; + /** Whether the popover is visible */ + visible?: boolean; + /** Custom style for the popover */ + style?: React.CSSProperties; + /** Aria label for the popover */ + "aria-label"?: string; + /** Whether to hide on escape key */ + hideOnEsc?: boolean; + /** Whether to hide on click outside */ + hideOnClickOutside?: boolean; + /** Final focus ref for accessibility */ + unstable_finalFocusRef?: React.RefObject; + /** Reference ref for positioning */ + unstable_referenceRef?: React.RefObject; + /** Legacy reakit props for backward compatibility */ + baseId?: string; + animated?: number | boolean; + animating?: boolean; + setBaseId?: React.Dispatch>; + setVisible?: React.Dispatch>; + setAnimated?: React.Dispatch>; + stopAnimation?: () => void; + modal?: boolean; + unstable_disclosureRef?: React.MutableRefObject; + setModal?: React.Dispatch>; + unstable_popoverRef?: React.RefObject; + unstable_arrowRef?: React.RefObject; + unstable_popoverStyles?: React.CSSProperties; + unstable_arrowStyles?: React.CSSProperties; + unstable_originalPlacement?: any; + unstable_update?: () => boolean; + placement?: any; + place?: React.Dispatch>; + toggle?: () => void; + onClick?: (e: any) => any; + unstable_initialFocusRef?: React.RefObject; + unstable_autoFocusOnShow?: boolean; + unstable_idCountRef?: React.MutableRefObject; }; const Popover = ( { children, + open, + onOpenChange, shrink, width = 380, minWidth, scrollable = true, flex, mobilePosition, + show, + hide, + visible, + style, + hideOnEsc = true, + hideOnClickOutside = true, + unstable_finalFocusRef, + unstable_referenceRef, + // Legacy reakit props - ignored for compatibility + baseId, + animated, + animating, + setBaseId, + setVisible, + setAnimated, + stopAnimation, + modal, + unstable_disclosureRef, + setModal, + unstable_popoverRef, + unstable_arrowRef, + unstable_popoverStyles, + unstable_arrowStyles, + unstable_originalPlacement, + unstable_update, + placement, + place, + toggle, + onClick, + unstable_initialFocusRef, + unstable_autoFocusOnShow, + unstable_idCountRef, ...rest }: Props, ref: React.Ref ) => { const isMobile = useMobile(); - // Custom Escape handler rather than using hideOnEsc from reakit so we can + // Handle legacy reakit API compatibility + const isOpen = open ?? visible ?? false; + const handleOpenChange = React.useCallback( + (newOpen: boolean) => { + if (onOpenChange) { + onOpenChange(newOpen); + } else if (!newOpen && hide) { + hide(); + } else if (newOpen && show) { + show(); + } + }, + [onOpenChange, hide, show] + ); + + // Custom Escape handler rather than using hideOnEsc from radix so we can // prevent default behavior of exiting fullscreen. useKeyDown( "Escape", (event) => { - if (rest.visible && rest.hideOnEsc !== false) { + if (isOpen && hideOnEsc !== false) { event.preventDefault(); - rest.hide(); + handleOpenChange(false); } }, { @@ -62,33 +152,55 @@ const Popover = ( if (isMobile) { return ( - - - {children} - - + + + e.preventDefault()} + onPointerDownOutside={ + hideOnClickOutside ? undefined : (e) => e.preventDefault() + } + {...rest} + > + + {children} + + + + ); } return ( - - - {children} - - + + + e.preventDefault()} + onPointerDownOutside={ + hideOnClickOutside ? undefined : (e) => e.preventDefault() + } + {...rest} + > + + {children} + + + + ); }; @@ -101,8 +213,9 @@ type ContentsProps = { $mobilePosition?: "top" | "bottom"; }; -const StyledPopover = styled(ReakitPopover)` +const StyledPopoverContent = styled(PopoverPrimitive.Content)` z-index: ${depths.modal}; + outline: none; `; const Contents = styled.div` diff --git a/app/editor/components/FindAndReplace.tsx b/app/editor/components/FindAndReplace.tsx index 653fc8ca1e..323940deef 100644 --- a/app/editor/components/FindAndReplace.tsx +++ b/app/editor/components/FindAndReplace.tsx @@ -7,7 +7,6 @@ import { } from "outline-icons"; import * as React from "react"; import { useTranslation } from "react-i18next"; -import { usePopoverState } from "reakit/Popover"; import styled, { useTheme } from "styled-components"; import { depths, s } from "@shared/styles"; import { altDisplay, isModKey, metaDisplay } from "@shared/utils/keyboard"; @@ -20,19 +19,18 @@ import { Portal } from "~/components/Portal"; import { ResizingHeightContainer } from "~/components/ResizingHeightContainer"; import Tooltip from "~/components/Tooltip"; import useKeyDown from "~/hooks/useKeyDown"; -import useOnClickOutside from "~/hooks/useOnClickOutside"; import Desktop from "~/utils/Desktop"; import { useEditor } from "./EditorContext"; type KeyboardShortcutsProps = { - popover: ReturnType; + isOpen: boolean; handleOpen: ({ withReplace }: { withReplace: boolean }) => void; handleCaseSensitive: () => void; handleRegex: () => void; }; function useKeyboardShortcuts({ - popover, + isOpen, handleOpen, handleCaseSensitive, handleRegex, @@ -41,7 +39,7 @@ function useKeyboardShortcuts({ useKeyDown( (ev) => isModKey(ev) && - !popover.visible && + !isOpen && ev.code === "KeyF" && // Keyboard handler is through the AppMenu on Desktop v1.2.0+ !(Desktop.bridge && "onFindInPage" in Desktop.bridge), @@ -54,7 +52,7 @@ function useKeyboardShortcuts({ // Enable/disable case sensitive search useKeyDown( - (ev) => isModKey(ev) && ev.altKey && ev.code === "KeyC" && popover.visible, + (ev) => isModKey(ev) && ev.altKey && ev.code === "KeyC" && isOpen, (ev) => { ev.preventDefault(); handleCaseSensitive(); @@ -64,7 +62,7 @@ function useKeyboardShortcuts({ // Enable/disable regex search useKeyDown( - (ev) => isModKey(ev) && ev.altKey && ev.code === "KeyR" && popover.visible, + (ev) => isModKey(ev) && ev.altKey && ev.code === "KeyR" && isOpen, (ev) => { ev.preventDefault(); handleRegex(); @@ -97,9 +95,6 @@ export default function FindAndReplace({ totalResults, }: Props) { const editor = useEditor(); - const finalFocusRef = React.useRef( - editor.view.dom.parentElement - ); const selectionRef = React.useRef(); const inputRef = React.useRef(null); const inputReplaceRef = React.useRef(null); @@ -110,12 +105,11 @@ export default function FindAndReplace({ const [regexEnabled, setRegex] = React.useState(false); const [searchTerm, setSearchTerm] = React.useState(""); const [replaceTerm, setReplaceTerm] = React.useState(""); - const popover = usePopoverState(); - const { show } = popover; + const [isOpen, setIsOpen] = React.useState(false); React.useEffect(() => { if (open) { - show(); + setIsOpen(true); } }, [open]); @@ -127,16 +121,16 @@ export default function FindAndReplace({ if ("onFindInPage" in Desktop.bridge) { Desktop.bridge.onFindInPage(() => { selectionRef.current = window.getSelection()?.toString(); - show(); + setIsOpen(true); }); } if ("onReplaceInPage" in Desktop.bridge) { Desktop.bridge.onReplaceInPage(() => { setShowReplace(true); - show(); + setIsOpen(true); }); } - }, [show]); + }, []); // Callbacks const selectInputText = React.useCallback(() => { @@ -159,7 +153,7 @@ export default function FindAndReplace({ const shouldShowReplace = !readOnly && withReplace; // If already open, switch focus to corresponding input text. - if (popover.visible) { + if (isOpen) { if (shouldShowReplace) { setShowReplace(true); selectInputReplaceText(); @@ -171,13 +165,13 @@ export default function FindAndReplace({ } selectionRef.current = window.getSelection()?.toString(); - popover.show(); + setIsOpen(true); if (shouldShowReplace) { setShowReplace(true); } }, - [popover, readOnly, selectInputText, selectInputReplaceText] + [isOpen, readOnly, selectInputText, selectInputReplaceText] ); const handleMore = React.useCallback(() => { @@ -213,40 +207,9 @@ export default function FindAndReplace({ }); }, [caseSensitive, editor.commands, searchTerm]); - const handleKeyDown = React.useCallback( - (ev: React.KeyboardEvent) => { - function nextPrevious() { - if (ev.shiftKey) { - editor.commands.prevSearchMatch(); - } else { - editor.commands.nextSearchMatch(); - } - } - - switch (ev.key) { - case "Enter": { - ev.preventDefault(); - nextPrevious(); - return; - } - case "g": { - if (ev.metaKey) { - ev.preventDefault(); - nextPrevious(); - selectInputText(); - } - return; - } - case "F3": { - ev.preventDefault(); - nextPrevious(); - selectInputText(); - return; - } - } - }, - [editor.commands, selectInputText] - ); + const handlePopoverOpenChange = React.useCallback((openState: boolean) => { + setIsOpen(openState); + }, []); const handleReplace = React.useCallback( (ev) => { @@ -295,10 +258,43 @@ export default function FindAndReplace({ [handleReplace] ); - useOnClickOutside(popover.unstable_referenceRef, popover.hide); + const handleKeyDown = React.useCallback( + (ev: React.KeyboardEvent) => { + function nextPrevious() { + if (ev.shiftKey) { + editor.commands.prevSearchMatch(); + } else { + editor.commands.nextSearchMatch(); + } + } + + switch (ev.key) { + case "Enter": { + ev.preventDefault(); + nextPrevious(); + return; + } + case "g": { + if (ev.metaKey) { + ev.preventDefault(); + nextPrevious(); + selectInputText(); + } + return; + } + case "F3": { + ev.preventDefault(); + nextPrevious(); + selectInputText(); + return; + } + } + }, + [editor.commands, selectInputText] + ); useKeyboardShortcuts({ - popover, + isOpen, handleOpen, handleCaseSensitive, handleRegex, @@ -316,7 +312,7 @@ export default function FindAndReplace({ ); React.useEffect(() => { - if (popover.visible) { + if (isOpen) { onOpen(); const startSearchText = selectionRef.current || searchTerm; @@ -339,7 +335,7 @@ export default function FindAndReplace({ editor.commands.clearSearch(); } // eslint-disable-next-line react-hooks/exhaustive-deps - }, [popover.visible]); + }, [isOpen]); const disabled = totalResults === 0; const navigation = ( @@ -370,8 +366,8 @@ export default function FindAndReplace({ return (