Files
outline/app/menus/NotificationMenu.tsx
T
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

73 lines
2.0 KiB
TypeScript

import { t } from "i18next";
import { MoreIcon } from "outline-icons";
import * as React from "react";
import { MenuButton } from "reakit/Menu";
import styled from "styled-components";
import { s, hover } from "@shared/styles";
import ContextMenu from "~/components/ContextMenu";
import Template from "~/components/ContextMenu/Template";
import NudeButton from "~/components/NudeButton";
import { actionToMenuItem, performAction } from "~/actions";
import { navigateToNotificationSettings } from "~/actions/definitions/navigation";
import { markNotificationsAsArchived } from "~/actions/definitions/notifications";
import useActionContext from "~/hooks/useActionContext";
import { useMenuState } from "~/hooks/useMenuState";
import useOnClickOutside from "~/hooks/useOnClickOutside";
import { MenuItem } from "~/types";
const NotificationMenu: React.FC = () => {
const menuRef = React.useRef<HTMLDivElement>(null);
const menu = useMenuState();
const context = useActionContext();
const items: MenuItem[] = React.useMemo(
() => [
actionToMenuItem(markNotificationsAsArchived, context),
{
type: "button",
title: t("Notification settings"),
onClick: () => performAction(navigateToNotificationSettings, context),
},
],
[context]
);
useOnClickOutside(
menuRef,
(event) => {
if (menu.visible) {
event.stopPropagation();
event.preventDefault();
menu.hide();
}
},
{ capture: true }
);
return (
<>
<MenuButton {...menu}>
{(props) => (
<Button {...props}>
<MoreIcon />
</Button>
)}
</MenuButton>
<ContextMenu {...menu} menuRef={menuRef} aria-label={t("Notification")}>
<Template {...menu} items={items} />
</ContextMenu>
</>
);
};
const Button = styled(NudeButton)`
color: ${s("textSecondary")};
&:${hover},
&:active {
color: ${s("text")};
background: ${s("sidebarControlHoverBackground")};
}
`;
export default NotificationMenu;