chore: Move TableOfContentsMenu & BreadcrumbMenu to Radix (#9882)

* mobile toc menu

* indent heading levels, filter > H3

* breadcrumb menu

* tiny
This commit is contained in:
Hemachandar authored and GitHub committed 2025-08-09 21:59:46 -04:00
1 parent 00d7239601
commit c2f84466df
10 files changed
+280 -267

No files matched your search

+1 -1
View File
@@ -268,7 +268,7 @@ export function actionV2ToMenuItem(
switch (action.type) {
case "action": {
const title = resolve<string>(action.name, context);
const visible = resolve<boolean>(action.visible, context);
const visible = resolve<boolean>(action.visible, context) ?? true;
const disabled = resolve<boolean>(action.disabled, context);
const icon =
!!action.icon && action.iconInContextMenu !== false
+65 -31
View File
@@ -6,55 +6,89 @@ import { s, ellipsis } from "@shared/styles";
import Flex from "~/components/Flex";
import BreadcrumbMenu from "~/menus/BreadcrumbMenu";
import { undraggableOnDesktop } from "~/styles";
import { MenuInternalLink } from "~/types";
import { InternalLinkActionV2, MenuInternalLink } from "~/types";
import { actionV2ToMenuItem } from "~/actions";
import useActionContext from "~/hooks/useActionContext";
import { useComputed } from "~/hooks/useComputed";
type TopLevelAction =
| InternalLinkActionV2
| { type: "menu"; actions: InternalLinkActionV2[] };
type Props = React.PropsWithChildren<{
items: MenuInternalLink[];
actions: InternalLinkActionV2[];
max?: number;
highlightFirstItem?: boolean;
}>;
function Breadcrumb(
{ items, highlightFirstItem, children, max = 2 }: Props,
{ actions, highlightFirstItem, children, max = 2 }: Props,
ref: React.RefObject<HTMLDivElement> | null
) {
const totalItems = items.length;
const topLevelItems: MenuInternalLink[] = [...items];
let overflowItems;
const actionContext = useActionContext({ isContextMenu: true });
const visibleActions = useComputed(
() =>
actions.filter((action) =>
typeof action.visible === "function"
? action.visible(actionContext)
: (action.visible ?? true)
),
[actions, actionContext]
);
const totalVisibleActions = visibleActions.length;
const topLevelActions: TopLevelAction[] = [...visibleActions];
// chop middle breadcrumbs and present a "..." menu instead
if (totalItems > max) {
if (totalVisibleActions > max) {
const halfMax = Math.floor(max / 2);
overflowItems = topLevelItems.splice(halfMax, totalItems - max);
const menuActions = topLevelActions.splice(
halfMax,
totalVisibleActions - max
) as InternalLinkActionV2[];
topLevelItems.splice(halfMax, 0, {
to: "",
type: "route",
title: <BreadcrumbMenu items={overflowItems as MenuInternalLink[]} />,
topLevelActions.splice(halfMax, 0, {
type: "menu",
actions: menuActions,
});
}
const toBreadcrumb = React.useCallback(
(action: TopLevelAction, index: number) => {
if (action.type === "menu") {
return <BreadcrumbMenu key="menu" actions={action.actions} />;
}
const item = actionV2ToMenuItem(
action,
actionContext
) as MenuInternalLink;
return (
<>
{item.icon}
<Item
to={item.to}
$withIcon={!!item.icon}
$highlight={!!highlightFirstItem && index === 0}
>
{item.title}
</Item>
</>
);
},
[actionContext, highlightFirstItem]
);
return (
<Flex justify="flex-start" align="center" ref={ref}>
{topLevelItems.map((item, index) => (
<React.Fragment
key={
(typeof item.to === "string" ? item.to : item.to.pathname) || index
}
>
{item.icon}
{item.to ? (
<Item
to={item.to}
$withIcon={!!item.icon}
$highlight={!!highlightFirstItem && index === 0}
>
{item.title}
</Item>
) : (
item.title
)}
{index !== topLevelItems.length - 1 || !!children ? <Slash /> : null}
{topLevelActions.map((action, index) => (
<React.Fragment key={action.type === "menu" ? "menu" : `item-${index}`}>
{toBreadcrumb(action, index)}
{index !== topLevelActions.length - 1 || !!children ? (
<Slash />
) : null}
</React.Fragment>
))}
{children}
+21 -28
View File
@@ -3,9 +3,10 @@ import * as React from "react";
import { useTranslation } from "react-i18next";
import Collection from "~/models/Collection";
import CollectionIcon from "~/components/Icons/CollectionIcon";
import { MenuInternalLink } from "~/types";
import { archivePath, collectionPath } from "~/utils/routeHelpers";
import Breadcrumb from "./Breadcrumb";
import { createInternalLinkActionV2 } from "~/actions";
import { ActiveCollectionSection } from "~/actions/sections";
type Props = {
collection: Collection;
@@ -14,32 +15,24 @@ type Props = {
export const CollectionBreadcrumb: React.FC<Props> = ({ collection }) => {
const { t } = useTranslation();
const items = React.useMemo(() => {
const collectionNode: MenuInternalLink = {
type: "route",
title: collection.name,
icon: <CollectionIcon collection={collection} expanded />,
to: collectionPath(collection.path),
};
const actions = React.useMemo(
() => [
createInternalLinkActionV2({
name: t("Archive"),
section: ActiveCollectionSection,
icon: <ArchiveIcon />,
visible: collection.isArchived,
to: archivePath(),
}),
createInternalLinkActionV2({
name: collection.name,
section: ActiveCollectionSection,
icon: <CollectionIcon collection={collection} expanded />,
to: collectionPath(collection.path),
}),
],
[collection, t]
);
const category: MenuInternalLink | undefined = collection.isArchived
? {
type: "route",
icon: <ArchiveIcon />,
title: t("Archive"),
to: archivePath(),
}
: undefined;
const output = [];
if (category) {
output.push(category);
}
output.push(collectionNode);
return output;
}, [collection, t]);
return <Breadcrumb items={items} highlightFirstItem />;
return <Breadcrumb actions={actions} highlightFirstItem />;
};
+79 -90
View File
@@ -11,8 +11,9 @@ import CollectionIcon from "~/components/Icons/CollectionIcon";
import { useLocationSidebarContext } from "~/hooks/useLocationSidebarContext";
import usePolicy from "~/hooks/usePolicy";
import useStores from "~/hooks/useStores";
import { MenuInternalLink } from "~/types";
import { archivePath, settingsPath, trashPath } from "~/utils/routeHelpers";
import { createInternalLinkActionV2 } from "~/actions";
import { ActiveDocumentSection } from "~/actions/sections";
type Props = {
children?: React.ReactNode;
@@ -27,46 +28,12 @@ type Props = {
maxDepth?: number;
};
function useCategory(document: Document): MenuInternalLink | null {
const { t } = useTranslation();
if (document.isDeleted) {
return {
type: "route",
icon: <TrashIcon />,
title: t("Trash"),
to: trashPath(),
};
}
if (document.isArchived) {
return {
type: "route",
icon: <ArchiveIcon />,
title: t("Archive"),
to: archivePath(),
};
}
if (document.template) {
return {
type: "route",
icon: <ShapesIcon />,
title: t("Templates"),
to: settingsPath("templates"),
};
}
return null;
}
function DocumentBreadcrumb(
{ document, children, onlyText, reverse = false, maxDepth }: Props,
ref: React.RefObject<HTMLDivElement> | null
) {
const { collections } = useStores();
const { t } = useTranslation();
const category = useCategory(document);
const sidebarContext = useLocationSidebarContext();
const collection = document.collectionId
? collections.get(document.collectionId)
@@ -78,69 +45,91 @@ function DocumentBreadcrumb(
void document.loadRelations({ withoutPolicies: true });
}, [document]);
let collectionNode: MenuInternalLink | undefined;
if (collection && can.readDocument) {
collectionNode = {
type: "route",
title: collection.name,
icon: <CollectionIcon collection={collection} expanded />,
to: {
pathname: collection.path,
state: { sidebarContext },
},
};
} else if (document.isCollectionDeleted) {
collectionNode = {
type: "route",
title: t("Deleted Collection"),
icon: undefined,
to: "",
};
}
const path = document.pathTo.slice(0, -1);
const items = React.useMemo(() => {
const output: MenuInternalLink[] = [];
const actions = React.useMemo(() => {
if (depth === 0) {
return output;
return [];
}
if (category) {
output.push(category);
}
if (collectionNode) {
output.push(collectionNode);
}
path.forEach((node: NavigationNode) => {
const title = node.title || t("Untitled");
output.push({
type: "route",
title: node.icon ? (
<>
<StyledIcon value={node.icon} color={node.color} /> {title}
</>
) : (
title
),
to: {
pathname: node.url,
state: { sidebarContext },
},
});
});
const outputActions = [
createInternalLinkActionV2({
name: t("Trash"),
section: ActiveDocumentSection,
icon: <TrashIcon />,
visible: document.isDeleted,
to: trashPath(),
}),
createInternalLinkActionV2({
name: t("Archive"),
section: ActiveDocumentSection,
icon: <ArchiveIcon />,
visible: document.isArchived,
to: archivePath(),
}),
createInternalLinkActionV2({
name: t("Templates"),
section: ActiveDocumentSection,
icon: <ShapesIcon />,
visible: document.template,
to: settingsPath("templates"),
}),
createInternalLinkActionV2({
name: collection?.name,
section: ActiveDocumentSection,
icon: collection ? (
<CollectionIcon collection={collection} expanded />
) : undefined,
visible: !!(collection && can.readDocument),
to: collection
? {
pathname: collection.path,
state: { sidebarContext },
}
: "",
}),
createInternalLinkActionV2({
name: t("Deleted Collection"),
section: ActiveDocumentSection,
visible: document.isCollectionDeleted,
to: "",
}),
...path.map((node) => {
const title = node.title || t("Untitled");
return createInternalLinkActionV2({
name: node.icon ? (
<>
<StyledIcon value={node.icon} color={node.color} /> {title}
</>
) : (
title
),
section: ActiveDocumentSection,
to: {
pathname: node.url,
state: { sidebarContext },
},
});
}),
];
return reverse
? depth !== undefined
? output.slice(-depth)
: output
? outputActions.slice(-depth)
: outputActions
: depth !== undefined
? output.slice(0, depth)
: output;
}, [t, path, category, sidebarContext, collectionNode, reverse, depth]);
? outputActions.slice(0, depth)
: outputActions;
}, [
t,
document,
collection,
can.readDocument,
sidebarContext,
path,
reverse,
depth,
]);
if (!collections.isLoaded) {
return null;
@@ -176,7 +165,7 @@ function DocumentBreadcrumb(
}
return (
<Breadcrumb items={items} ref={ref} highlightFirstItem>
<Breadcrumb actions={actions} ref={ref} highlightFirstItem>
{children}
</Breadcrumb>
);
+11 -17
View File
@@ -1,27 +1,21 @@
import { useTranslation } from "react-i18next";
import ContextMenu from "~/components/ContextMenu";
import OverflowMenuButton from "~/components/ContextMenu/OverflowMenuButton";
import Template from "~/components/ContextMenu/Template";
import { useMenuState } from "~/hooks/useMenuState";
import { MenuInternalLink } from "~/types";
import { DropdownMenu } from "~/components/Menu/DropdownMenu";
import { OverflowMenuButton } from "~/components/Menu/OverflowMenuButton";
import { useMenuAction } from "~/hooks/useMenuAction";
import { InternalLinkActionV2 } from "~/types";
type Props = {
items: MenuInternalLink[];
actions: InternalLinkActionV2[];
};
export default function BreadcrumbMenu({ items }: Props) {
export default function BreadcrumbMenu({ actions }: Props) {
const { t } = useTranslation();
const menu = useMenuState({
modal: true,
placement: "bottom",
});
const rootAction = useMenuAction(actions);
return (
<>
<OverflowMenuButton aria-label={t("Show path to document")} {...menu} />
<ContextMenu {...menu} aria-label={t("Path to document")}>
<Template {...menu} items={items} />
</ContextMenu>
</>
<DropdownMenu action={rootAction} ariaLabel={t("Show path to document")}>
<OverflowMenuButton />
</DropdownMenu>
);
}
+57 -58
View File
@@ -2,87 +2,86 @@ import { observer } from "mobx-react";
import { TableOfContentsIcon } from "outline-icons";
import { useMemo } from "react";
import { useTranslation } from "react-i18next";
import { MenuButton } from "reakit/Menu";
import styled from "styled-components";
import { createActionV2, createActionV2Group } from "~/actions";
import { ActiveDocumentSection } from "~/actions/sections";
import Button from "~/components/Button";
import ContextMenu from "~/components/ContextMenu";
import Template from "~/components/ContextMenu/Template";
import { useDocumentContext } from "~/components/DocumentContext";
import { useMenuState } from "~/hooks/useMenuState";
import { MenuItem } from "~/types";
import { DropdownMenu } from "~/components/Menu/DropdownMenu";
import { useMenuAction } from "~/hooks/useMenuAction";
function TableOfContentsMenu() {
const { headings } = useDocumentContext();
const menu = useMenuState({
modal: true,
unstable_preventOverflow: true,
unstable_fixed: true,
unstable_flip: true,
});
const { t } = useTranslation();
const minHeading = headings.reduce(
(memo, heading) => (heading.level < memo ? heading.level : memo),
Infinity
);
const items: MenuItem[] = useMemo(() => {
const i = [
{
type: "heading",
title: t("Contents"),
},
...headings.map((heading) => ({
type: "button",
onClick: () =>
requestAnimationFrame(() =>
requestAnimationFrame(
() => (window.location.hash = `#${heading.id}`)
)
),
title: <HeadingWrapper>{t(heading.title)}</HeadingWrapper>,
level: heading.level - minHeading,
})),
] as MenuItem[];
if (i.length === 1) {
i.push({
type: "link",
href: "#",
title: (
<HeadingWrapper>
{t("Headings you add to the document will appear here")}
</HeadingWrapper>
const headingActions = useMemo(
() =>
headings
.filter((heading) => heading.level < 4)
.map((heading) =>
createActionV2({
name: (
<HeadingWrapper $level={heading.level - minHeading}>
{t(heading.title)}
</HeadingWrapper>
),
section: ActiveDocumentSection,
perform: () =>
requestAnimationFrame(() =>
requestAnimationFrame(
() => (window.location.hash = `#${heading.id}`)
)
),
})
),
disabled: true,
});
[t, headings, minHeading]
);
const actions = useMemo(() => {
let childActions = headingActions;
if (!childActions.length) {
childActions = [
createActionV2({
name: (
<HeadingWrapper>
{t("Headings you add to the document will appear here")}
</HeadingWrapper>
),
section: ActiveDocumentSection,
disabled: true,
perform: () => {},
}),
];
}
return i;
}, [t, headings, minHeading]);
return [
createActionV2Group({
name: t("Contents"),
actions: childActions,
}),
];
}, [t, headingActions]);
const rootAction = useMenuAction(actions);
return (
<>
<MenuButton {...menu}>
{(props) => (
<Button
{...props}
icon={<TableOfContentsIcon />}
borderOnHover
neutral
/>
)}
</MenuButton>
<ContextMenu {...menu} aria-label={t("Table of contents")}>
<Template {...menu} items={items} />
</ContextMenu>
</>
<DropdownMenu action={rootAction} ariaLabel={t("Table of contents")}>
<Button icon={<TableOfContentsIcon />} borderOnHover neutral />
</DropdownMenu>
);
}
const HeadingWrapper = styled.div`
const HeadingWrapper = styled.div<{ $level?: number }>`
max-width: 100%;
white-space: normal;
overflow-wrap: anywhere;
margin-left: ${({ $level }) => `${12 * ($level ?? 0)}px`};
`;
export default observer(TableOfContentsMenu);
@@ -2,8 +2,9 @@ import * as React from "react";
import Icon from "@shared/components/Icon";
import { NavigationNode } from "@shared/types";
import Breadcrumb from "~/components/Breadcrumb";
import { MenuInternalLink } from "~/types";
import { sharedModelPath } from "~/utils/routeHelpers";
import { createInternalLinkActionV2 } from "~/actions";
import { ActiveDocumentSection } from "~/actions/sections";
type Props = {
children?: React.ReactNode;
@@ -47,23 +48,24 @@ const PublicBreadcrumb: React.FC<Props> = ({
sharedTree,
children,
}: Props) => {
const items: MenuInternalLink[] = React.useMemo(
const actions = React.useMemo(
() =>
pathToDocument(sharedTree, documentId)
.slice(1, -1)
.map((item) => ({
...item,
icon: item.icon ? (
<Icon value={item.icon} color={item.color} />
) : undefined,
title: item.title,
type: "route",
to: sharedModelPath(shareId, item.url),
})),
.map((item) =>
createInternalLinkActionV2({
name: item.title,
section: ActiveDocumentSection,
icon: item.icon ? (
<Icon value={item.icon} color={item.color} />
) : undefined,
to: sharedModelPath(shareId, item.url),
})
),
[sharedTree, shareId, documentId]
);
return <Breadcrumb items={items}>{children}</Breadcrumb>;
return <Breadcrumb actions={actions}>{children}</Breadcrumb>;
};
export default PublicBreadcrumb;
+16 -13
View File
@@ -1,6 +1,6 @@
import { observer } from "mobx-react";
import { CopyIcon, InternetIcon, ReplaceIcon } from "outline-icons";
import { useEffect, useCallback } from "react";
import { useEffect, useCallback, useMemo } from "react";
import { Controller, useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { useParams } from "react-router-dom";
@@ -29,6 +29,8 @@ import { ActionRow } from "./components/ActionRow";
import { CopyButton } from "./components/CopyButton";
import ImageInput from "./components/ImageInput";
import SettingRow from "./components/SettingRow";
import { createInternalLinkActionV2 } from "~/actions";
import { NavigationSection } from "~/actions/sections";
type Props = {
oauthClient: OAuthClient;
@@ -77,6 +79,18 @@ const Application = observer(function Application({ oauthClient }: Props) {
},
});
const breadcrumbActions = useMemo(
() => [
createInternalLinkActionV2({
name: t("Applications"),
section: NavigationSection,
icon: <InternetIcon />,
to: settingsPath("applications"),
}),
],
[t]
);
const handleSubmit = useCallback(
async (data: FormData) => {
try {
@@ -118,18 +132,7 @@ const Application = observer(function Application({ oauthClient }: Props) {
return (
<Scene
title={oauthClient.name}
left={
<Breadcrumb
items={[
{
type: "route",
title: t("Applications"),
to: settingsPath("applications"),
icon: <InternetIcon />,
},
]}
/>
}
left={<Breadcrumb actions={breadcrumbActions} />}
actions={<OAuthClientMenu oauthClient={oauthClient} showEdit={false} />}
>
<form onSubmit={formHandleSubmit(handleSubmit)}>
@@ -1,6 +1,8 @@
import { SettingsIcon } from "outline-icons";
import * as React from "react";
import { useTranslation } from "react-i18next";
import { createInternalLinkActionV2 } from "~/actions";
import { NavigationSection } from "~/actions/sections";
import Breadcrumb from "~/components/Breadcrumb";
import Scene from "~/components/Scene";
import { settingsPath } from "~/utils/routeHelpers";
@@ -11,22 +13,20 @@ export function IntegrationScene({
}: React.ComponentProps<typeof Scene>) {
const { t } = useTranslation();
const breadcrumbActions = React.useMemo(
() => [
createInternalLinkActionV2({
name: t("Integrations"),
section: NavigationSection,
icon: <SettingsIcon />,
to: settingsPath("integrations"),
}),
],
[t]
);
return (
<Scene
left={
<Breadcrumb
items={[
{
type: "route",
title: t("Integrations"),
icon: <SettingsIcon />,
to: settingsPath("integrations"),
},
]}
/>
}
{...rest}
>
<Scene left={<Breadcrumb actions={breadcrumbActions} />} {...rest}>
{children}
</Scene>
);
+1 -2
View File
@@ -556,7 +556,6 @@
"Integrations": "Integrations",
"API key": "API key",
"Show path to document": "Show path to document",
"Path to document": "Path to document",
"Sort in sidebar": "Sort in sidebar",
"A-Z sort": "A-Z sort",
"Z-A sort": "Z-A sort",
@@ -583,8 +582,8 @@
"Delete app": "Delete app",
"Revision options": "Revision options",
"Share options": "Share options",
"Contents": "Contents",
"Headings you add to the document will appear here": "Headings you add to the document will appear here",
"Contents": "Contents",
"Table of contents": "Table of contents",
"Change name": "Change name",
"Change email": "Change email",