diff --git a/app/components/primitives/Menu/MenuContext.tsx b/app/components/primitives/Menu/MenuContext.tsx index 08b14cf38b..8245741f8c 100644 --- a/app/components/primitives/Menu/MenuContext.tsx +++ b/app/components/primitives/Menu/MenuContext.tsx @@ -1,6 +1,6 @@ import { createContext, useContext, useMemo } from "react"; -type MenuVariant = "dropdown" | "context"; +type MenuVariant = "dropdown" | "context" | "inline"; const MenuContext = createContext<{ variant: MenuVariant; diff --git a/app/components/primitives/Menu/index.tsx b/app/components/primitives/Menu/index.tsx index a49721c7be..30db1b0c31 100644 --- a/app/components/primitives/Menu/index.tsx +++ b/app/components/primitives/Menu/index.tsx @@ -3,17 +3,36 @@ import * as DropdownMenuPrimitive from "@radix-ui/react-dropdown-menu"; import * as Components from "../components/Menu"; import type { LocationDescriptor } from "history"; import * as React from "react"; +import styled from "styled-components"; +import breakpoint from "styled-components-breakpoint"; import Tooltip from "~/components/Tooltip"; import { CheckmarkIcon } from "outline-icons"; import { useMenuContext } from "./MenuContext"; +import useMobile from "~/hooks/useMobile"; +import { Drawer, DrawerContent, DrawerTitle } from "../Drawer"; +import Scrollable from "~/components/Scrollable"; +import { Portal as ReactPortal } from "~/components/Portal"; type MenuProps = React.ComponentPropsWithoutRef< typeof DropdownMenuPrimitive.Root > & - React.ComponentPropsWithoutRef; + React.ComponentPropsWithoutRef & { + children: React.ReactNode; + }; const Menu = ({ children, ...rest }: MenuProps) => { const { variant } = useMenuContext(); + const isMobile = useMobile(); + + if (variant === "inline") { + return isMobile ? ( + + {children} + + ) : ( + <>{children} + ); + } const Root = variant === "dropdown" @@ -68,15 +87,76 @@ MenuTrigger.displayName = "MenuTrigger"; type ContentProps = React.ComponentPropsWithoutRef< typeof DropdownMenuPrimitive.Content > & - React.ComponentPropsWithoutRef; + React.ComponentPropsWithoutRef & { + pos?: { + top: number; + left: number; + }; + }; const MenuContent = React.forwardRef< | React.ElementRef - | React.ElementRef, + | React.ElementRef + | HTMLDivElement, ContentProps >((props, ref) => { const { variant } = useMenuContext(); + const isMobile = useMobile(); const { children, ...rest } = props; + const contentRef = React.useRef>(null); + + const enablePointerEvents = React.useCallback(() => { + if (contentRef.current) { + contentRef.current.style.pointerEvents = "auto"; + } + }, []); + + const disablePointerEvents = React.useCallback(() => { + if (contentRef.current) { + contentRef.current.style.pointerEvents = "none"; + } + }, []); + + if (variant === "inline") { + const contentProps = { + maxHeightVar: "--inline-menu-max-height", + transformOriginVar: "--inline-menu-transform-origin", + }; + const { pos } = props; + + return isMobile ? ( + + {rest["aria-label"] || "Menu"} + {children} + + ) : ( + + } + {...contentProps} + hiddenScrollbars + style={{ + ["--inline-menu-max-height" as string]: "85vh", + ["--inline-menu-transform-origin" as string]: "top left", + position: "fixed", + height: "fit-content", + top: pos ? pos.top - window.scrollY + 50 : undefined, + left: pos ? pos.left - 30 : undefined, + margin: "10px", + zIndex: 1000, + }} + > + {children} + + + ); + } const Portal = variant === "dropdown" @@ -203,7 +283,8 @@ type MenuGroupProps = { const MenuGroup = React.forwardRef< | React.ElementRef - | React.ElementRef, + | React.ElementRef + | HTMLDivElement, MenuGroupProps >((props, ref) => { const { variant } = useMenuContext(); @@ -249,6 +330,7 @@ const MenuButton = React.forwardRef< MenuButtonProps >((props, ref) => { const { variant } = useMenuContext(); + const [active, setActive] = React.useState(false); const { label, icon, @@ -260,6 +342,44 @@ const MenuButton = React.forwardRef< ...rest } = props; + // Common button content + const buttonContent = ( + <> + {icon} + {label} + {selected !== undefined && ( + + {selected ? : null} + + )} + + ); + + // For inline variant, render button directly without Radix Item wrapper + if (variant === "inline") { + const button = ( + } + disabled={disabled} + $dangerous={dangerous} + $active={active} + onClick={onClick} + onMouseEnter={() => setActive(true)} + onMouseLeave={() => setActive(false)} + > + {buttonContent} + + ); + + return tooltip ? ( + +
{button}
+
+ ) : ( + <>{button} + ); + } + const Item = variant === "dropdown" ? DropdownMenuPrimitive.Item @@ -272,13 +392,7 @@ const MenuButton = React.forwardRef< $dangerous={dangerous} onClick={onClick} > - {icon} - {label} - {selected !== undefined && ( - - {selected ? : null} - - )} + {buttonContent} ); @@ -375,11 +489,16 @@ type MenuSeparatorProps = React.ComponentPropsWithoutRef< const MenuSeparator = React.forwardRef< | React.ElementRef - | React.ElementRef, + | React.ElementRef + | HTMLHRElement, MenuSeparatorProps >((props, ref) => { const { variant } = useMenuContext(); + if (variant === "inline") { + return } />; + } + const Separator = variant === "dropdown" ? DropdownMenuPrimitive.Separator @@ -419,6 +538,24 @@ const MenuLabel = React.forwardRef< }); MenuLabel.displayName = "MenuLabel"; +// Styled wrapper for inline menu content with mobile-specific overrides +const InlineMenuContentWrapper = styled(Components.MenuContent)` + ${breakpoint("mobile", "tablet")` + // On mobile/tablet, remove width constraints and make it full-width + min-width: auto; + max-width: none; + width: 100%; + box-shadow: none; + border-radius: 0; + padding: 0; + `} +`; + +// Styled scrollable for mobile drawer content +const StyledScrollable = styled(Scrollable)` + max-height: 75vh; +`; + export { Menu, MenuTrigger, diff --git a/app/editor/components/FloatingToolbar.tsx b/app/editor/components/FloatingToolbar.tsx index 1bf7b06fe5..539b727bca 100644 --- a/app/editor/components/FloatingToolbar.tsx +++ b/app/editor/components/FloatingToolbar.tsx @@ -20,6 +20,7 @@ import { RowSelection } from "@shared/editor/selection/RowSelection"; import { isTableSelected } from "@shared/editor/queries/table"; type Props = { + inline?: boolean; align?: "start" | "end" | "center"; active?: boolean; children: React.ReactNode; @@ -36,7 +37,7 @@ const defaultPosition = { visible: false, }; -function usePosition({ +export function usePosition({ menuRef, active, align = "center", @@ -303,7 +304,13 @@ const FloatingToolbar = React.forwardRef(function FloatingToolbar_( }} > {props.children && ( - {props.children} + + {props.children} + )} @@ -368,13 +375,22 @@ const MobileBackground = styled.div` } `; -const Background = styled.div<{ align: Props["align"] }>` +const Background = styled.div<{ + align: Props["align"]; + inline: Props["inline"]; +}>` position: relative; background-color: ${s("menuBackground")}; box-shadow: ${s("menuShadow")}; border-radius: 4px; height: 36px; + ${(props) => + props.inline && + ` + display: none; + `} + ${(props) => props.align === "start" && ` diff --git a/app/editor/components/SelectionToolbar.tsx b/app/editor/components/SelectionToolbar.tsx index 9b446c8974..513631e296 100644 --- a/app/editor/components/SelectionToolbar.tsx +++ b/app/editor/components/SelectionToolbar.tsx @@ -265,6 +265,7 @@ export function SelectionToolbar(props: Props) { return ( ) : activeToolbar === Toolbar.Menu && items.length ? ( - + ) : null} ); diff --git a/app/editor/components/ToolbarMenu.tsx b/app/editor/components/ToolbarMenu.tsx index 7ef38f011d..7a4eadab53 100644 --- a/app/editor/components/ToolbarMenu.tsx +++ b/app/editor/components/ToolbarMenu.tsx @@ -1,4 +1,4 @@ -import { useCallback, useMemo } from "react"; +import { useCallback, useMemo, useRef } from "react"; import styled from "styled-components"; import breakpoint from "styled-components-breakpoint"; import * as Toolbar from "@radix-ui/react-toolbar"; @@ -17,11 +17,55 @@ import { MenuProvider } from "~/components/primitives/Menu/MenuContext"; import { Menu, MenuTrigger } from "~/components/primitives/Menu"; import { useTranslation } from "react-i18next"; import EventBoundary from "@shared/components/EventBoundary"; +import { usePosition } from "./FloatingToolbar"; type Props = { items: MenuItem[]; + inline?: boolean; + containerRef?: React.MutableRefObject; }; +/* + * Renders an inline menu in the floating toolbar, which does not require a trigger. + */ +function InLineMenu(props: { + active: boolean; + item: MenuItem; + containerRef?: React.MutableRefObject; +}) { + const { item, containerRef } = props; + const { commands, view } = useEditor(); + const fallbackRef = useRef(null); + const pos = usePosition({ + menuRef: containerRef || fallbackRef, + active: true, + }); + + const items: TMenuItem[] = useMemo( + () => + item.children ? mapMenuItems(item.children, commands, view.state) : [], + [item.children, commands, view.state] + ); + + const handleCloseAutoFocus = useCallback((ev: Event) => { + ev.stopImmediatePropagation(); + }, []); + + return ( + + + + {toMenuItems(items)} + + + + ); +} + /* * Renders a dropdown menu in the floating toolbar. */ @@ -31,41 +75,10 @@ function ToolbarDropdown(props: { active: boolean; item: MenuItem }) { const { item } = props; const { state } = view; - const items: TMenuItem[] = useMemo(() => { - const handleClick = (menuItem: MenuItem) => () => { - if (!menuItem.name) { - return; - } - - if (commands[menuItem.name]) { - commands[menuItem.name]( - typeof menuItem.attrs === "function" - ? menuItem.attrs(state) - : menuItem.attrs - ); - } else if (menuItem.onClick) { - menuItem.onClick(); - } - }; - - return item.children - ? item.children.map((child) => { - if (child.name === "separator") { - return { type: "separator", visible: child.visible }; - } - return { - type: "button", - title: child.label, - icon: child.icon, - dangerous: child.dangerous, - visible: child.visible, - selected: - child.active !== undefined ? child.active(state) : undefined, - onClick: handleClick(child), - }; - }) - : []; - }, [item.children, commands, state]); + const items: TMenuItem[] = useMemo( + () => (item.children ? mapMenuItems(item.children, commands, state) : []), + [item.children, commands, state] + ); const handleCloseAutoFocus = useCallback((ev: Event) => { ev.stopImmediatePropagation(); @@ -94,7 +107,7 @@ function ToolbarDropdown(props: { active: boolean; item: MenuItem }) { function ToolbarMenu(props: Props) { const { commands, view } = useEditor(); - const { items } = props; + const { items, inline, containerRef } = props; const { state } = view; const handleClick = (item: MenuItem) => () => { @@ -136,10 +149,18 @@ function ToolbarMenu(props: Props) { {item.name === "dimensions" ? ( ) : item.children ? ( - + inline ? ( + + ) : ( + + ) ) : ( , + state: any +): TMenuItem[] => { + const handleClick = (menuItem: MenuItem) => () => { + if (!menuItem.name) { + return; + } + + if (commands[menuItem.name]) { + commands[menuItem.name]( + typeof menuItem.attrs === "function" + ? menuItem.attrs(state) + : menuItem.attrs + ); + } else if (menuItem.onClick) { + menuItem.onClick(); + } + }; + + return children.map((child) => { + if (child.name === "separator") { + return { type: "separator", visible: child.visible }; + } + return { + type: "button", + title: child.label, + icon: child.icon, + dangerous: child.dangerous, + visible: child.visible, + selected: child.active !== undefined ? child.active(state) : undefined, + onClick: handleClick(child), + }; + }); +}; + const FlexibleWrapper = styled.div` color: ${s("textSecondary")}; overflow: hidden; diff --git a/app/editor/menus/tableRow.tsx b/app/editor/menus/tableRow.tsx index 1a349598f2..21b51da73b 100644 --- a/app/editor/menus/tableRow.tsx +++ b/app/editor/menus/tableRow.tsx @@ -2,7 +2,6 @@ import { TrashIcon, InsertAboveIcon, InsertBelowIcon, - MoreIcon, TableHeaderRowIcon, TableSplitCellsIcon, TableMergeCellsIcon, @@ -40,7 +39,7 @@ export default function tableRowMenuItems( return [ { - icon: , + skipIcon: true, children: [ { name: "toggleHeaderRow",