Skip to content

Virtual whiteboard for sketching hand-drawn like diagrams

feat: close MainMenu and Library dropdown on item select (#6152)

David Luzar1db078a3dceb
diff --git a/src/components/ActiveConfirmDialog.tsx b/src/components/ActiveConfirmDialog.tsx
new file mode 100644
index 0000000000..3c79a51900
--- /dev/null
+++ b/src/components/ActiveConfirmDialog.tsx
@@ -0,0 +1,35 @@
+import { atom, useAtom } from "jotai";
+import { actionClearCanvas } from "../actions";
+import { t } from "../i18n";
+import { useExcalidrawActionManager } from "./App";
+import ConfirmDialog from "./ConfirmDialog";
+
+export const activeConfirmDialogAtom = atom<"clearCanvas" | null>(null);
+
+export const ActiveConfirmDialog = () => {
+ const [activeConfirmDialog, setActiveConfirmDialog] = useAtom(
+ activeConfirmDialogAtom,
+ );
+ const actionManager = useExcalidrawActionManager();
+
+ if (!activeConfirmDialog) {
+ return null;
+ }
+
+ if (activeConfirmDialog === "clearCanvas") {
+ return (
+ <ConfirmDialog
+ onConfirm={() => {
+ actionManager.executeAction(actionClearCanvas);
+ setActiveConfirmDialog(null);
+ }}
+ onCancel={() => setActiveConfirmDialog(null)}
+ title={t("clearCanvasDialog.title")}
+ >
+ <p className="clear-canvas__content"> {t("alerts.clearReset")}</p>
+ </ConfirmDialog>
+ );
+ }
+
+ return null;
+};
diff --git a/src/components/LayerUI.tsx b/src/components/LayerUI.tsx
index 140f28e919..3defb29471 100644
--- a/src/components/LayerUI.tsx
+++ b/src/components/LayerUI.tsx
@@ -50,6 +50,7 @@ import { hostSidebarCountersAtom } from "./Sidebar/Sidebar";
import { jotaiScope } from "../jotai";
import { useAtom } from "jotai";
import MainMenu from "./main-menu/MainMenu";
+import { ActiveConfirmDialog } from "./ActiveConfirmDialog";
import { HandButton } from "./HandButton";
import { isHandToolActive } from "../appState";
@@ -394,6 +395,7 @@ const LayerUI = ({
}}
/>
)}
+ <ActiveConfirmDialog />
{renderImageExportDialog()}
{renderJSONExportDialog()}
{appState.pasteDialog.shown && (
diff --git a/src/components/LibraryMenuHeaderContent.tsx b/src/components/LibraryMenuHeaderContent.tsx
index beeb8dd172..43ea968999 100644
--- a/src/components/LibraryMenuHeaderContent.tsx
+++ b/src/components/LibraryMenuHeaderContent.tsx
@@ -187,6 +187,7 @@ export const LibraryMenuHeader: React.FC<{
</DropdownMenu.Trigger>
<DropdownMenu.Content
onClickOutside={() => setIsLibraryMenuOpen(false)}
+ onSelect={() => setIsLibraryMenuOpen(false)}
className="library-menu"
>
{!itemsSelected && (
diff --git a/src/components/dropdownMenu/DropdownMenuContent.tsx b/src/components/dropdownMenu/DropdownMenuContent.tsx
index 873a99d8d5..8ec2b6e631 100644
--- a/src/components/dropdownMenu/DropdownMenuContent.tsx
+++ b/src/components/dropdownMenu/DropdownMenuContent.tsx
@@ -4,16 +4,23 @@ import { Island } from "../Island";
import { useDevice } from "../App";
import clsx from "clsx";
import Stack from "../Stack";
+import React from "react";
+import { DropdownMenuContentPropsContext } from "./common";
const MenuContent = ({
children,
onClickOutside,
className = "",
+ onSelect,
style,
}: {
children?: React.ReactNode;
onClickOutside?: () => void;
className?: string;
+ /**
+ * Called when any menu item is selected (clicked on).
+ */
+ onSelect?: (event: Event) => void;
style?: React.CSSProperties;
}) => {
const device = useDevice();
@@ -24,28 +31,32 @@ const MenuContent = ({
const classNames = clsx(`dropdown-menu ${className}`, {
"dropdown-menu--mobile": device.isMobile,
}).trim();
+
return (
- <div
- ref={menuRef}
- className={classNames}
- style={style}
- data-testid="dropdown-menu"
- >
- {/* the zIndex ensures this menu has higher stacking order,
+ <DropdownMenuContentPropsContext.Provider value={{ onSelect }}>
+ <div
+ ref={menuRef}
+ className={classNames}
+ style={style}
+ data-testid="dropdown-menu"
+ >
+ {/* the zIndex ensures this menu has higher stacking order,
see https://github.com/excalidraw/excalidraw/pull/1445 */}
- {device.isMobile ? (
- <Stack.Col className="dropdown-menu-container">{children}</Stack.Col>
- ) : (
- <Island
- className="dropdown-menu-container"
- padding={2}
- style={{ zIndex: 1 }}
- >
- {children}
- </Island>
- )}
- </div>
+ {device.isMobile ? (
+ <Stack.Col className="dropdown-menu-container">{children}</Stack.Col>
+ ) : (
+ <Island
+ className="dropdown-menu-container"
+ padding={2}
+ style={{ zIndex: 1 }}
+ >
+ {children}
+ </Island>
+ )}
+ </div>
+ </DropdownMenuContentPropsContext.Provider>
);
};
-export default MenuContent;
MenuContent.displayName = "DropdownMenuContent";
+
+export default MenuContent;
diff --git a/src/components/dropdownMenu/DropdownMenuItem.tsx b/src/components/dropdownMenu/DropdownMenuItem.tsx
index 4f8db982d7..9a8e924623 100644
--- a/src/components/dropdownMenu/DropdownMenuItem.tsx
+++ b/src/components/dropdownMenu/DropdownMenuItem.tsx
@@ -1,10 +1,10 @@
import React from "react";
+import {
+ getDrodownMenuItemClassName,
+ useHandleDropdownMenuItemClick,
+} from "./common";
import MenuItemContent from "./DropdownMenuItemContent";
-export const getDrodownMenuItemClassName = (className = "") => {
- return `dropdown-menu-item dropdown-menu-item-base ${className}`.trim();
-};
-
const DropdownMenuItem = ({
icon,
onSelect,
@@ -14,15 +14,17 @@ const DropdownMenuItem = ({
...rest
}: {
icon?: JSX.Element;
- onSelect: () => void;
+ onSelect: (event: Event) => void;
children: React.ReactNode;
shortcut?: string;
className?: string;
-} & React.ButtonHTMLAttributes<HTMLButtonElement>) => {
+} & Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, "onSelect">) => {
+ const handleClick = useHandleDropdownMenuItemClick(rest.onClick, onSelect);
+
return (
<button
{...rest}
- onClick={onSelect}
+ onClick={handleClick}
type="button"
className={getDrodownMenuItemClassName(className)}
title={rest.title ?? rest["aria-label"]}
diff --git a/src/components/dropdownMenu/DropdownMenuItemLink.tsx b/src/components/dropdownMenu/DropdownMenuItemLink.tsx
index 18d3f2c701..fdf87f7a4f 100644
--- a/src/components/dropdownMenu/DropdownMenuItemLink.tsx
+++ b/src/components/dropdownMenu/DropdownMenuItemLink.tsx
@@ -1,20 +1,28 @@
import MenuItemContent from "./DropdownMenuItemContent";
import React from "react";
-import { getDrodownMenuItemClassName } from "./DropdownMenuItem";
+import {
+ getDrodownMenuItemClassName,
+ useHandleDropdownMenuItemClick,
+} from "./common";
+
const DropdownMenuItemLink = ({
icon,
shortcut,
href,
children,
+ onSelect,
className = "",
...rest
}: {
+ href: string;
icon?: JSX.Element;
children: React.ReactNode;
shortcut?: string;
className?: string;
- href: string;
+ onSelect?: (event: Event) => void;
} & React.AnchorHTMLAttributes<HTMLAnchorElement>) => {
+ const handleClick = useHandleDropdownMenuItemClick(rest.onClick, onSelect);
+
return (
<a
{...rest}
@@ -23,6 +31,7 @@ const DropdownMenuItemLink = ({
rel="noreferrer"
className={getDrodownMenuItemClassName(className)}
title={rest.title ?? rest["aria-label"]}
+ onClick={handleClick}
>
<MenuItemContent icon={icon} shortcut={shortcut}>
{children}
diff --git a/src/components/dropdownMenu/common.ts b/src/components/dropdownMenu/common.ts
new file mode 100644
index 0000000000..bc4fcfc0e0
--- /dev/null
+++ b/src/components/dropdownMenu/common.ts
@@ -0,0 +1,31 @@
+import React, { useContext } from "react";
+import { EVENT } from "../../constants";
+import { composeEventHandlers } from "../../utils";
+
+export const DropdownMenuContentPropsContext = React.createContext<{
+ onSelect?: (event: Event) => void;
+}>({});
+
+export const getDrodownMenuItemClassName = (className = "") => {
+ return `dropdown-menu-item dropdown-menu-item-base ${className}`.trim();
+};
+
+export const useHandleDropdownMenuItemClick = (
+ origOnClick:
+ | React.MouseEventHandler<HTMLAnchorElement | HTMLButtonElement>
+ | undefined,
+ onSelect: ((event: Event) => void) | undefined,
+) => {
+ const DropdownMenuContentProps = useContext(DropdownMenuContentPropsContext);
+
+ return composeEventHandlers(origOnClick, (event) => {
+ const itemSelectEvent = new CustomEvent(EVENT.MENU_ITEM_SELECT, {
+ bubbles: true,
+ cancelable: true,
+ });
+ onSelect?.(itemSelectEvent);
+ if (!itemSelectEvent.defaultPrevented) {
+ DropdownMenuContentProps.onSelect?.(itemSelectEvent);
+ }
+ });
+};
diff --git a/src/components/main-menu/DefaultItems.tsx b/src/components/main-menu/DefaultItems.tsx
index 6342b31d22..6e89b598c3 100644
--- a/src/components/main-menu/DefaultItems.tsx
+++ b/src/components/main-menu/DefaultItems.tsx
@@ -28,9 +28,9 @@ import {
} from "../../actions";
import "./DefaultItems.scss";
-import { useState } from "react";
-import ConfirmDialog from "../ConfirmDialog";
import clsx from "clsx";
+import { useSetAtom } from "jotai";
+import { activeConfirmDialogAtom } from "../ActiveConfirmDialog";
export const LoadScene = () => {
// FIXME Hack until we tie "t" to lang state
@@ -122,41 +122,22 @@ export const ClearCanvas = () => {
// FIXME Hack until we tie "t" to lang state
// eslint-disable-next-line
const appState = useExcalidrawAppState();
+ const setActiveConfirmDialog = useSetAtom(activeConfirmDialogAtom);
const actionManager = useExcalidrawActionManager();
- const [showDialog, setShowDialog] = useState(false);
- const toggleDialog = () => setShowDialog(!showDialog);
-
if (!actionManager.isActionEnabled(actionClearCanvas)) {
return null;
}
return (
- <>
- <DropdownMenuItem
- icon={TrashIcon}
- onSelect={toggleDialog}
- data-testid="clear-canvas-button"
- aria-label={t("buttons.clearReset")}
- >
- {t("buttons.clearReset")}
- </DropdownMenuItem>
-
- {/* FIXME this should live outside MainMenu so it stays open
- if menu is closed */}
- {showDialog && (
- <ConfirmDialog
- onConfirm={() => {
- actionManager.executeAction(actionClearCanvas);
- toggleDialog();
- }}
- onCancel={toggleDialog}
- title={t("clearCanvasDialog.title")}
- >
- <p className="clear-canvas__content"> {t("alerts.clearReset")}</p>
- </ConfirmDialog>
- )}
- </>
+ <DropdownMenuItem
+ icon={TrashIcon}
+ onSelect={() => setActiveConfirmDialog("clearCanvas")}
+ data-testid="clear-canvas-button"
+ aria-label={t("buttons.clearReset")}
+ >
+ {t("buttons.clearReset")}
+ </DropdownMenuItem>
);
};
ClearCanvas.displayName = "ClearCanvas";
@@ -171,7 +152,9 @@ export const ToggleTheme = () => {
return (
<DropdownMenuItem
- onSelect={() => {
+ onSelect={(event) => {
+ // do not close the menu when changing theme
+ event.preventDefault();
return actionManager.executeAction(actionToggleTheme);
}}
icon={appState.theme === "dark" ? SunIcon : MoonIcon}
diff --git a/src/components/main-menu/MainMenu.tsx b/src/components/main-menu/MainMenu.tsx
index bf398a38b0..35b40635f0 100644
--- a/src/components/main-menu/MainMenu.tsx
+++ b/src/components/main-menu/MainMenu.tsx
@@ -11,14 +11,25 @@ import * as DefaultItems from "./DefaultItems";
import { UserList } from "../UserList";
import { t } from "../../i18n";
import { HamburgerMenuIcon } from "../icons";
+import { composeEventHandlers } from "../../utils";
-const MainMenu = ({ children }: { children?: React.ReactNode }) => {
+const MainMenu = ({
+ children,
+ onSelect,
+}: {
+ children?: React.ReactNode;
+ /**
+ * Called when any menu item is selected (clicked on).
+ */
+ onSelect?: (event: Event) => void;
+}) => {
const device = useDevice();
const appState = useExcalidrawAppState();
const setAppState = useExcalidrawSetAppState();
const onClickOutside = device.isMobile
? undefined
: () => setAppState({ openMenu: null });
+
return (
<DropdownMenu open={appState.openMenu === "canvas"}>
<DropdownMenu.Trigger
@@ -30,7 +41,12 @@ const MainMenu = ({ children }: { children?: React.ReactNode }) => {
>
{HamburgerMenuIcon}
</DropdownMenu.Trigger>
- <DropdownMenu.Content onClickOutside={onClickOutside}>
+ <DropdownMenu.Content
+ onClickOutside={onClickOutside}
+ onSelect={composeEventHandlers(onSelect, () => {
+ setAppState({ openMenu: null });
+ })}
+ >
{children}
{device.isMobile && appState.collaborators.size > 0 && (
<fieldset className="UserList-Wrapper">
diff --git a/src/constants.ts b/src/constants.ts
index 20b3778e3a..c302ce8772 100644
--- a/src/constants.ts
+++ b/src/constants.ts
@@ -62,6 +62,7 @@ export enum EVENT {
SCROLL = "scroll",
// custom events
EXCALIDRAW_LINK = "excalidraw-link",
+ MENU_ITEM_SELECT = "menu.itemSelect",
}
export const ENV = {
diff --git a/src/packages/excalidraw/CHANGELOG.md b/src/packages/excalidraw/CHANGELOG.md
index 1dd0e0be46..8a32ddcdd2 100644
--- a/src/packages/excalidraw/CHANGELOG.md
+++ b/src/packages/excalidraw/CHANGELOG.md
@@ -11,6 +11,12 @@ The change should be grouped under one of the below section and must contain PR
Please add the latest change on the top under the correct section.
-->
+## Unreleased
+
+### Features
+
+- `MainMenu`, `MainMenu.Item`, and `MainMenu.ItemLink` components now all support `onSelect(event: Event): void` callback. If you call `event.preventDefault()`, it will prevent the menu from closing when an item is selected (clicked on). [#6152](https://github.com/excalidraw/excalidraw/pull/6152)
+
## 0.14.1 (2023-01-16)
### Fixes
diff --git a/src/utils.ts b/src/utils.ts
index 80aa03f1dd..ee8952daa3 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -743,3 +743,22 @@ export const isShallowEqual = <T extends Record<string, any>>(
}
return aKeys.every((key) => objA[key] === objB[key]);
};
+
+// taken from Radix UI
+// https://github.com/radix-ui/primitives/blob/main/packages/core/primitive/src/primitive.tsx
+export const composeEventHandlers = <E>(
+ originalEventHandler?: (event: E) => void,
+ ourEventHandler?: (event: E) => void,
+ { checkForDefaultPrevented = true } = {},
+) => {
+ return function handleEvent(event: E) {
+ originalEventHandler?.(event);
+
+ if (
+ !checkForDefaultPrevented ||
+ !(event as unknown as Event).defaultPrevented
+ ) {
+ return ourEventHandler?.(event);
+ }
+ };
+};