From d788fe4aa83858a7a39df4b4f6e2905ba90f7d60 Mon Sep 17 00:00:00 2001 From: Alexandr Date: Wed, 1 Sep 2021 17:46:51 +0200 Subject: [PATCH] #1238 Closing ellipses menu when opening another menu (#1240) --- .../ellipsisMenu/EllipsisMenu.stories.tsx | 66 ++++++++++++------- src/components/ellipsisMenu/EllipsisMenu.tsx | 23 ++++++- 2 files changed, 64 insertions(+), 25 deletions(-) diff --git a/src/components/ellipsisMenu/EllipsisMenu.stories.tsx b/src/components/ellipsisMenu/EllipsisMenu.stories.tsx index e34446c417..c91ca0cf11 100644 --- a/src/components/ellipsisMenu/EllipsisMenu.stories.tsx +++ b/src/components/ellipsisMenu/EllipsisMenu.stories.tsx @@ -21,6 +21,7 @@ import EllipsisMenu from "./EllipsisMenu"; import { action } from "@storybook/addon-actions"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faTimes } from "@fortawesome/free-solid-svg-icons"; +import { Card } from "react-bootstrap"; const toggleVariantOptions = [ "primary", @@ -54,30 +55,49 @@ export default { }, } as ComponentMeta; -const Template: ComponentStory = (args) => ( +const items = [ + { + title: "Action", + action: action("Action"), + }, + { + title: "Another ation", + action: action("Another ation"), + }, + { + title: ( + <> + +   Something dangerous + + ), + action: action("Something dangerous"), + className: "text-danger", + }, +]; + +const SingleMenuTemplate: ComponentStory = (args) => ( ); -export const Default = Template.bind({}); -Default.args = { - items: [ - { - title: "Action", - action: action("Action"), - }, - { - title: "Another ation", - action: action("Another ation"), - }, - { - title: ( - <> - -   Something dangerous - - ), - action: action("Something dangerous"), - className: "text-danger", - }, - ], +export const SingleMenu = SingleMenuTemplate.bind({}); +SingleMenu.args = { + items, +}; + +const MultipleMenuTemplate: ComponentStory = (args) => ( + + +
+ +
+
+ +
+
+
+); +export const MultipleMenu = MultipleMenuTemplate.bind({}); +MultipleMenu.args = { + items, }; diff --git a/src/components/ellipsisMenu/EllipsisMenu.tsx b/src/components/ellipsisMenu/EllipsisMenu.tsx index 93e384b7f1..fcde516349 100644 --- a/src/components/ellipsisMenu/EllipsisMenu.tsx +++ b/src/components/ellipsisMenu/EllipsisMenu.tsx @@ -32,8 +32,28 @@ const EllipsisMenu: React.FunctionComponent<{ variant?: string; items: Item[]; }> = ({ variant = "light", items }) => { - const onToggle = (isOpen: boolean, event: SyntheticEvent) => { + const onToggle = ( + isOpen: boolean, + event: SyntheticEvent, + metadata: { + source: "select" | "click" | "rootClose" | "keydown"; + } + ) => { event.stopPropagation(); + + if (metadata.source === "click" && isOpen) { + try { + // The click on this toggle doesn't go beyond the component, + // hence no other element knows that the click happened. + // Simulating the click on the body will let other menus know user clicked somewhere. + document.body.click(); + } catch (error: unknown) { + console.debug( + "EllipsisMenu. Failed to trigger closing other menus", + error + ); + } + } }; return ( @@ -41,7 +61,6 @@ const EllipsisMenu: React.FunctionComponent<{ - {items .filter((x) => !x.hide)