From 3dc3e8372a518fc59624c0d6dae9eceea489c3c4 Mon Sep 17 00:00:00 2001 From: "codegen-sh[bot]" <131295404+codegen-sh[bot]@users.noreply.github.com> Date: Sun, 1 Jun 2025 02:21:30 +0000 Subject: [PATCH] Migrate Popover and Guide components from reakit/Dialog to Radix UI - Replace reakit/Dialog imports with @radix-ui/react-dialog and @radix-ui/react-popover - Update Popover component to use Radix Popover for desktop and Dialog for mobile - Migrate Guide component to use Radix Dialog with proper overlay and content structure - Create compatibility layer with usePopoverState hook and PopoverDisclosure component - Update Collaborators component to use new compatibility imports - Add placement conversion from reakit format to Radix side/align format - Update styled components to use Radix data-state attributes instead of data-enter - Maintain backward compatibility for existing component usage patterns --- app/components/Collaborators.tsx | 3 +- app/components/Guide.tsx | 78 ++++++--------- app/components/Popover.tsx | 137 ++++++++++++++++++++------- app/components/PopoverDisclosure.tsx | 51 ++++++++++ app/hooks/usePopoverState.ts | 29 ++++++ package.json | 2 + yarn.lock | 55 +++++++++++ 7 files changed, 274 insertions(+), 81 deletions(-) create mode 100644 app/components/PopoverDisclosure.tsx create mode 100644 app/hooks/usePopoverState.ts diff --git a/app/components/Collaborators.tsx b/app/components/Collaborators.tsx index c87d1f4fbe..a7dae83cae 100644 --- a/app/components/Collaborators.tsx +++ b/app/components/Collaborators.tsx @@ -5,14 +5,15 @@ import uniq from "lodash/uniq"; import { observer } from "mobx-react"; import { useState, useMemo, useEffect, useCallback } from "react"; import { useTranslation } from "react-i18next"; -import { usePopoverState, PopoverDisclosure } from "reakit/Popover"; import Document from "~/models/Document"; import { AvatarSize, AvatarWithPresence } from "~/components/Avatar"; import DocumentViews from "~/components/DocumentViews"; import Facepile from "~/components/Facepile"; import NudeButton from "~/components/NudeButton"; import Popover from "~/components/Popover"; +import PopoverDisclosure from "~/components/PopoverDisclosure"; import useCurrentUser from "~/hooks/useCurrentUser"; +import usePopoverState from "~/hooks/usePopoverState"; import useStores from "~/hooks/useStores"; type Props = { diff --git a/app/components/Guide.tsx b/app/components/Guide.tsx index 439dbab581..46b1816904 100644 --- a/app/components/Guide.tsx +++ b/app/components/Guide.tsx @@ -1,9 +1,8 @@ +import * as Dialog from "@radix-ui/react-dialog"; import * as React from "react"; -import { Dialog, DialogBackdrop, useDialogState } from "reakit/Dialog"; import styled from "styled-components"; import { depths, s } from "@shared/styles"; import Scrollable from "~/components/Scrollable"; -import usePrevious from "~/hooks/usePrevious"; type Props = { children?: React.ReactNode; @@ -18,47 +17,25 @@ const Guide: React.FC = ({ title = "Untitled", onRequestClose, ...rest -}: Props) => { - const dialog = useDialogState({ - animated: 250, - }); - const wasOpen = usePrevious(isOpen); - - React.useEffect(() => { - if (!wasOpen && isOpen) { - dialog.show(); - } - - if (wasOpen && !isOpen) { - dialog.hide(); - } - }, [dialog, wasOpen, isOpen]); - - return ( - - {(backdropProps) => ( - - - {(dialogProps) => ( - - - {title &&
{title}
} - {children} -
-
- )} -
-
- )} -
- ); -}; +}: Props) => ( + !open && onRequestClose()}> + + + + + + {title &&
{title}
} + {children} +
+
+
+
+
+
+); const Header = styled.h1` font-size: 18px; @@ -66,7 +43,7 @@ const Header = styled.h1` margin-bottom: 1em; `; -const Backdrop = styled.div` +const StyledOverlay = styled(Dialog.Overlay)` position: fixed; top: 0; left: 0; @@ -77,12 +54,12 @@ const Backdrop = styled.div` transition: opacity 200ms ease-in-out; opacity: 0; - &[data-enter] { + &[data-state="open"] { opacity: 1; } `; -const Scene = styled.div` +const StyledContent = styled(Dialog.Content)` position: absolute; top: 0; right: 0; @@ -100,12 +77,19 @@ const Scene = styled.div` transform: translateX(16px); transition: transform 250ms ease, opacity 250ms ease; - &[data-enter] { + &[data-state="open"] { opacity: 1; transform: translateX(0px); } `; +const Scene = styled.div` + width: 100%; + display: flex; + justify-content: center; + align-items: flex-start; +`; + const Content = styled(Scrollable)` width: 100%; padding: 16px; diff --git a/app/components/Popover.tsx b/app/components/Popover.tsx index 96ee170130..3ee557bcbc 100644 --- a/app/components/Popover.tsx +++ b/app/components/Popover.tsx @@ -1,6 +1,6 @@ +import * as Dialog from "@radix-ui/react-dialog"; +import * as Popover 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 +8,10 @@ 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 visible */ + visible?: boolean; /** 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,20 +27,36 @@ 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 to hide on escape key, defaults to true */ + hideOnEsc?: boolean; + /** Whether to hide on click outside, defaults to true */ + hideOnClickOutside?: boolean; + /** Aria label for accessibility */ + "aria-label"?: string; + /** Placement for the popover */ + placement?: string; + /** Gutter spacing */ + gutter?: number; }; -const Popover = ( +const PopoverComponent = ( { children, + visible = false, shrink, width = 380, minWidth, scrollable = true, flex, mobilePosition, + hideOnEsc = true, + hideOnClickOutside = true, + hide, + placement, + gutter, ...rest }: Props, ref: React.Ref @@ -50,9 +68,9 @@ const Popover = ( useKeyDown( "Escape", (event) => { - if (rest.visible && rest.hideOnEsc !== false) { + if (visible && hideOnEsc !== false) { event.preventDefault(); - rest.hide(); + hide?.(); } }, { @@ -62,33 +80,86 @@ const Popover = ( if (isMobile) { return ( - - - {children} - - + !open && hide?.()}> + + + + + {children} + + + + ); } + // Convert reakit placement to Radix side + const getSide = (placementProp?: string) => { + if (!placementProp) { + return "bottom"; + } + if (placementProp.includes("top")) { + return "top"; + } + if (placementProp.includes("bottom")) { + return "bottom"; + } + if (placementProp.includes("left")) { + return "left"; + } + if (placementProp.includes("right")) { + return "right"; + } + return "bottom"; + }; + + const getAlign = (placementProp?: string) => { + if (!placementProp) { + return "center"; + } + if (placementProp.includes("start")) { + return "start"; + } + if (placementProp.includes("end")) { + return "end"; + } + return "center"; + }; + return ( - - - {children} - - + !open && hide?.()}> + + + + {children} + + + + ); }; @@ -101,7 +172,7 @@ type ContentsProps = { $mobilePosition?: "top" | "bottom"; }; -const StyledPopover = styled(ReakitPopover)` +const StyledPopoverContent = styled(Popover.Content)` z-index: ${depths.modal}; `; @@ -142,4 +213,4 @@ const Contents = styled.div` `}; `; -export default React.forwardRef(Popover); +export default React.forwardRef(PopoverComponent); diff --git a/app/components/PopoverDisclosure.tsx b/app/components/PopoverDisclosure.tsx new file mode 100644 index 0000000000..7d72ad447d --- /dev/null +++ b/app/components/PopoverDisclosure.tsx @@ -0,0 +1,51 @@ +import * as Popover from "@radix-ui/react-popover"; +import * as React from "react"; + +type PopoverProps = { + onClick?: (event: React.MouseEvent) => void; + [key: string]: unknown; +}; + +type Props = { + children: React.ReactNode | ((props: PopoverProps) => React.ReactNode); + show?: () => void; + hide?: () => void; + toggle?: () => void; + visible?: boolean; +} & React.ComponentProps; + +/** + * A compatibility component that provides a similar API to reakit's PopoverDisclosure + * but works with Radix UI Popover. + */ +const PopoverDisclosure = React.forwardRef( + ({ children, show, hide, toggle, visible, onClick, ...props }, ref) => { + const handleClick = React.useCallback( + (event: React.MouseEvent) => { + if (onClick) { + onClick(event); + } + if (toggle) { + toggle(); + } + }, + [onClick, toggle] + ); + + // If children is a function, call it with popover props for backward compatibility + const content = + typeof children === "function" + ? children({ onClick: handleClick, ...props }) + : children; + + return ( + + {content} + + ); + } +); + +PopoverDisclosure.displayName = "PopoverDisclosure"; + +export default PopoverDisclosure; diff --git a/app/hooks/usePopoverState.ts b/app/hooks/usePopoverState.ts new file mode 100644 index 0000000000..3ce16bf17d --- /dev/null +++ b/app/hooks/usePopoverState.ts @@ -0,0 +1,29 @@ +import { useState, useCallback } from "react"; + +/** + * A compatibility hook that provides a similar API to reakit's usePopoverState + * but works with our Radix-based Popover component. + */ +export function usePopoverState(options?: { + gutter?: number; + placement?: string; + visible?: boolean; +}) { + const [visible, setVisible] = useState(options?.visible ?? false); + + const show = useCallback(() => setVisible(true), []); + const hide = useCallback(() => setVisible(false), []); + const toggle = useCallback(() => setVisible((prev) => !prev), []); + + return { + visible, + show, + hide, + toggle, + // For backward compatibility with existing code + placement: options?.placement, + gutter: options?.gutter, + }; +} + +export default usePopoverState; diff --git a/package.json b/package.json index ca07d4b060..433443dd15 100644 --- a/package.json +++ b/package.json @@ -86,6 +86,8 @@ "@octokit/auth-app": "^6.1.3", "@outlinewiki/koa-passport": "^4.2.1", "@outlinewiki/passport-azure-ad-oauth2": "^0.1.0", + "@radix-ui/react-dialog": "^1.1.14", + "@radix-ui/react-popover": "^1.1.14", "@radix-ui/react-select": "^2.1.4", "@radix-ui/react-tooltip": "^1.2.7", "@radix-ui/react-visually-hidden": "^1.2.2", diff --git a/yarn.lock b/yarn.lock index cd324c00a0..873de198cc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3365,6 +3365,26 @@ aria-hidden "^1.2.4" react-remove-scroll "^2.6.3" +"@radix-ui/react-dialog@^1.1.14": + version "1.1.14" + resolved "https://registry.yarnpkg.com/@radix-ui/react-dialog/-/react-dialog-1.1.14.tgz#4c69c80c258bc6561398cfce055202ea11075107" + integrity sha512-+CpweKjqpzTmwRwcYECQcNYbI8V9VSQt0SNFKeEBLgfucbsLssU6Ppq7wUdNXEGb573bMjFhVjKVll8rmV6zMw== + dependencies: + "@radix-ui/primitive" "1.1.2" + "@radix-ui/react-compose-refs" "1.1.2" + "@radix-ui/react-context" "1.1.2" + "@radix-ui/react-dismissable-layer" "1.1.10" + "@radix-ui/react-focus-guards" "1.1.2" + "@radix-ui/react-focus-scope" "1.1.7" + "@radix-ui/react-id" "1.1.1" + "@radix-ui/react-portal" "1.1.9" + "@radix-ui/react-presence" "1.1.4" + "@radix-ui/react-primitive" "2.1.3" + "@radix-ui/react-slot" "1.2.3" + "@radix-ui/react-use-controllable-state" "1.2.2" + aria-hidden "^1.2.4" + react-remove-scroll "^2.6.3" + "@radix-ui/react-direction@1.1.0": version "1.1.0" resolved "https://registry.yarnpkg.com/@radix-ui/react-direction/-/react-direction-1.1.0.tgz#a7d39855f4d077adc2a1922f9c353c5977a09cdc" @@ -3408,6 +3428,11 @@ resolved "https://registry.yarnpkg.com/@radix-ui/react-focus-guards/-/react-focus-guards-1.1.1.tgz#8635edd346304f8b42cae86b05912b61aef27afe" integrity sha512-pSIwfrT1a6sIoDASCSpFwOasEwKTZWDw/iBdtnqKO7v6FeOzYJ7U53cPzYFVR3geGGXgVHaH+CdngrrAzqUGxg== +"@radix-ui/react-focus-guards@1.1.2": + version "1.1.2" + resolved "https://registry.yarnpkg.com/@radix-ui/react-focus-guards/-/react-focus-guards-1.1.2.tgz#4ec9a7e50925f7fb661394460045b46212a33bed" + integrity sha512-fyjAACV62oPV925xFCrH8DR5xWhg9KYtJT4s3u54jxp+L/hbpTY2kIeEFFbFe+a/HCE94zGQMZLIpVTPVZDhaA== + "@radix-ui/react-focus-scope@1.1.1": version "1.1.1" resolved "https://registry.yarnpkg.com/@radix-ui/react-focus-scope/-/react-focus-scope-1.1.1.tgz#5c602115d1db1c4fcfa0fae4c3b09bb8919853cb" @@ -3426,6 +3451,15 @@ "@radix-ui/react-primitive" "2.0.2" "@radix-ui/react-use-callback-ref" "1.1.0" +"@radix-ui/react-focus-scope@1.1.7": + version "1.1.7" + resolved "https://registry.yarnpkg.com/@radix-ui/react-focus-scope/-/react-focus-scope-1.1.7.tgz#dfe76fc103537d80bf42723a183773fd07bfb58d" + integrity sha512-t2ODlkXBQyn7jkl6TNaw/MtVEVvIGelJDCG41Okq/KwUsJBwQ4XVZsHAVUkK4mBv3ewiAS3PGuUWuY2BoK4ZUw== + dependencies: + "@radix-ui/react-compose-refs" "1.1.2" + "@radix-ui/react-primitive" "2.1.3" + "@radix-ui/react-use-callback-ref" "1.1.1" + "@radix-ui/react-id@1.1.0": version "1.1.0" resolved "https://registry.yarnpkg.com/@radix-ui/react-id/-/react-id-1.1.0.tgz#de47339656594ad722eb87f94a6b25f9cffae0ed" @@ -3440,6 +3474,27 @@ dependencies: "@radix-ui/react-use-layout-effect" "1.1.1" +"@radix-ui/react-popover@^1.1.14": + version "1.1.14" + resolved "https://registry.yarnpkg.com/@radix-ui/react-popover/-/react-popover-1.1.14.tgz#5496d1986f0287cdfc77e73f70a887e4cb77ad08" + integrity sha512-ODz16+1iIbGUfFEfKx2HTPKizg2MN39uIOV8MXeHnmdd3i/N9Wt7vU46wbHsqA0xoaQyXVcs0KIlBdOA2Y95bw== + dependencies: + "@radix-ui/primitive" "1.1.2" + "@radix-ui/react-compose-refs" "1.1.2" + "@radix-ui/react-context" "1.1.2" + "@radix-ui/react-dismissable-layer" "1.1.10" + "@radix-ui/react-focus-guards" "1.1.2" + "@radix-ui/react-focus-scope" "1.1.7" + "@radix-ui/react-id" "1.1.1" + "@radix-ui/react-popper" "1.2.7" + "@radix-ui/react-portal" "1.1.9" + "@radix-ui/react-presence" "1.1.4" + "@radix-ui/react-primitive" "2.1.3" + "@radix-ui/react-slot" "1.2.3" + "@radix-ui/react-use-controllable-state" "1.2.2" + aria-hidden "^1.2.4" + react-remove-scroll "^2.6.3" + "@radix-ui/react-popper@1.2.1": version "1.2.1" resolved "https://registry.yarnpkg.com/@radix-ui/react-popper/-/react-popper-1.2.1.tgz#2fc66cfc34f95f00d858924e3bee54beae2dff0a"