Files
outline/app/menus/TableOfContentsMenu.tsx
codegen-sh[bot]andTom Moor d559afe2ce fix: Prevent multiple context menus from being open simultaneously (#9298)
* Fix issue #8026: Prevent multiple context menus from being open simultaneously

- Created useCoordinatedMenuState hook that wraps Reakit's useMenuState
- Enhanced MenuProvider with menu registry and coordination logic
- Expanded MenuProvider scope to wrap entire application
- Updated key menu components to use coordinated menu state:
  - DocumentMenu
  - Template (SubMenu)
  - FilterOptions
  - AccountMenu
  - CollectionMenu
- Ensures only one context menu can be open at a time
- Maintains existing Reakit integration and component structure

* Update all imports, add lint rule

* Update to named export

* fix: Sidebar still needs separate MenuProvider to track isOpen within

* fix: Cannot directly open a menu with another open

---------

Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com>
Co-authored-by: Tom Moor <tom@getoutline.com>
2025-05-24 19:20:00 -04:00

84 lines
2.1 KiB
TypeScript

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 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";
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: "link",
href: `#${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>
),
disabled: true,
});
}
return i;
}, [t, headings, minHeading]);
return (
<>
<MenuButton {...menu}>
{(props) => (
<Button
{...props}
icon={<TableOfContentsIcon />}
borderOnHover
neutral
/>
)}
</MenuButton>
<ContextMenu {...menu} aria-label={t("Table of contents")}>
<Template {...menu} items={items} />
</ContextMenu>
</>
);
}
const HeadingWrapper = styled.div`
max-width: 100%;
white-space: normal;
overflow-wrap: anywhere;
`;
export default observer(TableOfContentsMenu);