From 499a925f006a6fa065e04e836b46d16fd7e81483 Mon Sep 17 00:00:00 2001 From: Adi Dahiya Date: Thu, 27 May 2021 17:04:41 +0000 Subject: [PATCH 1/2] [popover2] fix(ContextMenu2): simpler target positioning logic --- packages/core/src/common/classes.ts | 5 ++- .../core/src/components/collapse/collapse.tsx | 5 +-- packages/core/src/components/tree/_tree.scss | 6 --- packages/core/src/components/tree/tree.tsx | 2 +- packages/popover2/src/contextMenu2.tsx | 38 +++++++------------ packages/popover2/test/contextMenu2Tests.tsx | 3 +- 6 files changed, 21 insertions(+), 38 deletions(-) diff --git a/packages/core/src/common/classes.ts b/packages/core/src/common/classes.ts index fd5cc9cea7..c63d74bae8 100644 --- a/packages/core/src/common/classes.ts +++ b/packages/core/src/common/classes.ts @@ -78,7 +78,10 @@ export const LIST_UNSTYLED = `${NS}-list-unstyled`; export const RTL = `${NS}-rtl`; // layout utilities -/** @see https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block#identifying_the_containing_block */ +/** + * @see https://developer.mozilla.org/en-US/docs/Web/CSS/Containing_block#identifying_the_containing_block + * @deprecated this is no longer needed for ContextMenu2, will be removed in v4.0 + */ export const FIXED_POSITIONING_CONTAINING_BLOCK = `${NS}-fixed-positioning-containing-block`; // components diff --git a/packages/core/src/components/collapse/collapse.tsx b/packages/core/src/components/collapse/collapse.tsx index 107bdb72a9..74e770bdd1 100644 --- a/packages/core/src/components/collapse/collapse.tsx +++ b/packages/core/src/components/collapse/collapse.tsx @@ -185,9 +185,6 @@ export class Collapse extends AbstractPureComponent2 extends React.Component> { public render() { return ( -
+
{this.renderNodes(this.props.contents, [], Classes.TREE_ROOT)}
); diff --git a/packages/popover2/src/contextMenu2.tsx b/packages/popover2/src/contextMenu2.tsx index 202c6e5528..04acc72701 100644 --- a/packages/popover2/src/contextMenu2.tsx +++ b/packages/popover2/src/contextMenu2.tsx @@ -20,6 +20,7 @@ import * as React from "react"; import { Classes as CoreClasses, IOverlayLifecycleProps, + Portal, Props, Utils as CoreUtils, mergeRefs, @@ -69,9 +70,6 @@ export interface ContextMenu2ChildrenProps { /** Popover element rendered by ContextMenu, used to establish a click target to position the menu */ popover: JSX.Element | undefined; - - /** DOM ref for the context menu target, used to calculate menu position on the page */ - ref: React.Ref; } export interface ContextMenu2Props @@ -130,10 +128,12 @@ export const ContextMenu2: React.FC = React.forwardRef(undefined); + // hold a reference to the click mouse event to pass to content/child render functions const [mouseEvent, setMouseEvent] = React.useState>(); const [isOpen, setIsOpen] = React.useState(false); - const containerRef = React.useRef(null); // If disabled prop is changed, we don't want our old context menu to stick around. // If it has just been enabled (disabled = false), then the menu ought to be opened by @@ -155,11 +155,13 @@ export const ContextMenu2: React.FC = React.forwardRef(null); const renderTarget = React.useCallback( ({ ref }: Popover2TargetProps) => ( -
+ +
+ ), [targetOffset], ); @@ -214,14 +216,13 @@ export const ContextMenu2: React.FC = React.forwardRef = React.forwardRef = React.forwardRef = React.forwardRef {
{ctxMenuProps.popover} @@ -162,7 +161,7 @@ describe("ContextMenu2", () => { function openCtxMenu(ctxMenu: ReactWrapper) { ctxMenu .find(`.${TARGET_CLASSNAME}`) - .simulate("contextmenu", { defaultPrevented: false, clientX: 10, clientY: 10 }) + .simulate("contextmenu", { defaultPrevented: false, pageX: 10, pageY: 10 }) .update(); } From b1e9e80c658965a3b4e96b0c07230189a3c3eaba Mon Sep 17 00:00:00 2001 From: Adi Dahiya Date: Thu, 27 May 2021 17:21:51 +0000 Subject: [PATCH 2/2] switch back to clientX/Y --- packages/popover2/src/contextMenu2.tsx | 4 ++-- packages/popover2/test/contextMenu2Tests.tsx | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/popover2/src/contextMenu2.tsx b/packages/popover2/src/contextMenu2.tsx index 04acc72701..0acab8dad7 100644 --- a/packages/popover2/src/contextMenu2.tsx +++ b/packages/popover2/src/contextMenu2.tsx @@ -129,7 +129,7 @@ export const ContextMenu2: React.FC = React.forwardRef(undefined); // hold a reference to the click mouse event to pass to content/child render functions const [mouseEvent, setMouseEvent] = React.useState>(); @@ -216,7 +216,7 @@ export const ContextMenu2: React.FC = React.forwardRef { function openCtxMenu(ctxMenu: ReactWrapper) { ctxMenu .find(`.${TARGET_CLASSNAME}`) - .simulate("contextmenu", { defaultPrevented: false, pageX: 10, pageY: 10 }) + .simulate("contextmenu", { defaultPrevented: false, clientX: 10, clientY: 10 }) .update(); }