mirror of
https://github.com/outline/outline.git
synced 2026-12-02 09:42:07 +03:00
inline menu
This commit is contained in:
1 parent
e8019855dc
commit
f28ce8f0cd
6 files changed
+276
-60
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { createContext, useContext, useMemo } from "react";
|
||||
|
||||
type MenuVariant = "dropdown" | "context";
|
||||
type MenuVariant = "dropdown" | "context" | "inline";
|
||||
|
||||
const MenuContext = createContext<{
|
||||
variant: MenuVariant;
|
||||
|
||||
@@ -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<typeof ContextMenuPrimitive.Root>;
|
||||
React.ComponentPropsWithoutRef<typeof ContextMenuPrimitive.Root> & {
|
||||
children: React.ReactNode;
|
||||
};
|
||||
|
||||
const Menu = ({ children, ...rest }: MenuProps) => {
|
||||
const { variant } = useMenuContext();
|
||||
const isMobile = useMobile();
|
||||
|
||||
if (variant === "inline") {
|
||||
return isMobile ? (
|
||||
<Drawer open={true} modal={false}>
|
||||
{children}
|
||||
</Drawer>
|
||||
) : (
|
||||
<>{children}</>
|
||||
);
|
||||
}
|
||||
|
||||
const Root =
|
||||
variant === "dropdown"
|
||||
@@ -68,15 +87,76 @@ MenuTrigger.displayName = "MenuTrigger";
|
||||
type ContentProps = React.ComponentPropsWithoutRef<
|
||||
typeof DropdownMenuPrimitive.Content
|
||||
> &
|
||||
React.ComponentPropsWithoutRef<typeof ContextMenuPrimitive.Content>;
|
||||
React.ComponentPropsWithoutRef<typeof ContextMenuPrimitive.Content> & {
|
||||
pos?: {
|
||||
top: number;
|
||||
left: number;
|
||||
};
|
||||
};
|
||||
|
||||
const MenuContent = React.forwardRef<
|
||||
| React.ElementRef<typeof DropdownMenuPrimitive.Content>
|
||||
| React.ElementRef<typeof ContextMenuPrimitive.Content>,
|
||||
| React.ElementRef<typeof ContextMenuPrimitive.Content>
|
||||
| HTMLDivElement,
|
||||
ContentProps
|
||||
>((props, ref) => {
|
||||
const { variant } = useMenuContext();
|
||||
const isMobile = useMobile();
|
||||
const { children, ...rest } = props;
|
||||
const contentRef = React.useRef<React.ElementRef<typeof DrawerContent>>(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 ? (
|
||||
<DrawerContent
|
||||
ref={contentRef}
|
||||
aria-label={rest["aria-label"]}
|
||||
aria-describedby={undefined}
|
||||
onAnimationStart={disablePointerEvents}
|
||||
onAnimationEnd={enablePointerEvents}
|
||||
>
|
||||
<DrawerTitle>{rest["aria-label"] || "Menu"}</DrawerTitle>
|
||||
<StyledScrollable hiddenScrollbars>{children}</StyledScrollable>
|
||||
</DrawerContent>
|
||||
) : (
|
||||
<ReactPortal>
|
||||
<InlineMenuContentWrapper
|
||||
ref={ref as React.Ref<HTMLDivElement>}
|
||||
{...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}
|
||||
</InlineMenuContentWrapper>
|
||||
</ReactPortal>
|
||||
);
|
||||
}
|
||||
|
||||
const Portal =
|
||||
variant === "dropdown"
|
||||
@@ -203,7 +283,8 @@ type MenuGroupProps = {
|
||||
|
||||
const MenuGroup = React.forwardRef<
|
||||
| React.ElementRef<typeof DropdownMenuPrimitive.Group>
|
||||
| React.ElementRef<typeof ContextMenuPrimitive.Group>,
|
||||
| React.ElementRef<typeof ContextMenuPrimitive.Group>
|
||||
| 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}
|
||||
<Components.MenuLabel>{label}</Components.MenuLabel>
|
||||
{selected !== undefined && (
|
||||
<Components.SelectedIconWrapper aria-hidden>
|
||||
{selected ? <CheckmarkIcon size={18} /> : null}
|
||||
</Components.SelectedIconWrapper>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
|
||||
// For inline variant, render button directly without Radix Item wrapper
|
||||
if (variant === "inline") {
|
||||
const button = (
|
||||
<Components.MenuButton
|
||||
ref={ref as React.Ref<HTMLButtonElement>}
|
||||
disabled={disabled}
|
||||
$dangerous={dangerous}
|
||||
$active={active}
|
||||
onClick={onClick}
|
||||
onMouseEnter={() => setActive(true)}
|
||||
onMouseLeave={() => setActive(false)}
|
||||
>
|
||||
{buttonContent}
|
||||
</Components.MenuButton>
|
||||
);
|
||||
|
||||
return tooltip ? (
|
||||
<Tooltip content={tooltip} placement="bottom">
|
||||
<div>{button}</div>
|
||||
</Tooltip>
|
||||
) : (
|
||||
<>{button}</>
|
||||
);
|
||||
}
|
||||
|
||||
const Item =
|
||||
variant === "dropdown"
|
||||
? DropdownMenuPrimitive.Item
|
||||
@@ -272,13 +392,7 @@ const MenuButton = React.forwardRef<
|
||||
$dangerous={dangerous}
|
||||
onClick={onClick}
|
||||
>
|
||||
{icon}
|
||||
<Components.MenuLabel>{label}</Components.MenuLabel>
|
||||
{selected !== undefined && (
|
||||
<Components.SelectedIconWrapper aria-hidden>
|
||||
{selected ? <CheckmarkIcon size={18} /> : null}
|
||||
</Components.SelectedIconWrapper>
|
||||
)}
|
||||
{buttonContent}
|
||||
</Components.MenuButton>
|
||||
</Item>
|
||||
);
|
||||
@@ -375,11 +489,16 @@ type MenuSeparatorProps = React.ComponentPropsWithoutRef<
|
||||
|
||||
const MenuSeparator = React.forwardRef<
|
||||
| React.ElementRef<typeof DropdownMenuPrimitive.Separator>
|
||||
| React.ElementRef<typeof ContextMenuPrimitive.Separator>,
|
||||
| React.ElementRef<typeof ContextMenuPrimitive.Separator>
|
||||
| HTMLHRElement,
|
||||
MenuSeparatorProps
|
||||
>((props, ref) => {
|
||||
const { variant } = useMenuContext();
|
||||
|
||||
if (variant === "inline") {
|
||||
return <Components.MenuSeparator ref={ref as React.Ref<HTMLHRElement>} />;
|
||||
}
|
||||
|
||||
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,
|
||||
|
||||
@@ -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 && (
|
||||
<Background align={props.align}>{props.children}</Background>
|
||||
<Background
|
||||
align={props.align}
|
||||
inline={props.inline}
|
||||
className="background"
|
||||
>
|
||||
{props.children}
|
||||
</Background>
|
||||
)}
|
||||
</Wrapper>
|
||||
</Portal>
|
||||
@@ -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" &&
|
||||
`
|
||||
|
||||
@@ -265,6 +265,7 @@ export function SelectionToolbar(props: Props) {
|
||||
|
||||
return (
|
||||
<FloatingToolbar
|
||||
inline={rowIndex !== undefined}
|
||||
align={align}
|
||||
active={isActive}
|
||||
ref={menuRef}
|
||||
@@ -301,7 +302,12 @@ export function SelectionToolbar(props: Props) {
|
||||
onClickOutside={handleClickOutsideLinkEditor}
|
||||
/>
|
||||
) : activeToolbar === Toolbar.Menu && items.length ? (
|
||||
<ToolbarMenu items={items} {...rest} />
|
||||
<ToolbarMenu
|
||||
items={items}
|
||||
inline={rowIndex !== undefined}
|
||||
containerRef={menuRef}
|
||||
{...rest}
|
||||
/>
|
||||
) : null}
|
||||
</FloatingToolbar>
|
||||
);
|
||||
|
||||
@@ -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<HTMLDivElement | null>;
|
||||
};
|
||||
|
||||
/*
|
||||
* Renders an inline menu in the floating toolbar, which does not require a trigger.
|
||||
*/
|
||||
function InLineMenu(props: {
|
||||
active: boolean;
|
||||
item: MenuItem;
|
||||
containerRef?: React.MutableRefObject<HTMLDivElement | null>;
|
||||
}) {
|
||||
const { item, containerRef } = props;
|
||||
const { commands, view } = useEditor();
|
||||
const fallbackRef = useRef<HTMLDivElement | null>(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 (
|
||||
<MenuProvider variant="inline">
|
||||
<Menu>
|
||||
<MenuContent
|
||||
pos={pos}
|
||||
align="end"
|
||||
onCloseAutoFocus={handleCloseAutoFocus}
|
||||
>
|
||||
<EventBoundary>{toMenuItems(items)}</EventBoundary>
|
||||
</MenuContent>
|
||||
</Menu>
|
||||
</MenuProvider>
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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" ? (
|
||||
<MediaDimension key={index} />
|
||||
) : item.children ? (
|
||||
<ToolbarDropdown
|
||||
active={isActive && !item.label}
|
||||
item={item}
|
||||
/>
|
||||
inline ? (
|
||||
<InLineMenu
|
||||
active={isActive && !item.label}
|
||||
item={item}
|
||||
containerRef={containerRef}
|
||||
/>
|
||||
) : (
|
||||
<ToolbarDropdown
|
||||
active={isActive && !item.label}
|
||||
item={item}
|
||||
/>
|
||||
)
|
||||
) : (
|
||||
<Toolbar.Button asChild>
|
||||
<ToolbarButton
|
||||
@@ -161,6 +182,43 @@ function ToolbarMenu(props: Props) {
|
||||
);
|
||||
}
|
||||
|
||||
const mapMenuItems = (
|
||||
children: MenuItem[],
|
||||
commands: Record<string, Function>,
|
||||
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;
|
||||
|
||||
@@ -2,7 +2,6 @@ import {
|
||||
TrashIcon,
|
||||
InsertAboveIcon,
|
||||
InsertBelowIcon,
|
||||
MoreIcon,
|
||||
TableHeaderRowIcon,
|
||||
TableSplitCellsIcon,
|
||||
TableMergeCellsIcon,
|
||||
@@ -40,7 +39,7 @@ export default function tableRowMenuItems(
|
||||
|
||||
return [
|
||||
{
|
||||
icon: <MoreIcon />,
|
||||
skipIcon: true,
|
||||
children: [
|
||||
{
|
||||
name: "toggleHeaderRow",
|
||||
|
||||
Reference in new issue
Block a user