Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
*/

import * as React from 'react';
import {useLayoutEffect, createRef} from 'react';
import {useLayoutEffect} from 'react';
import {createPortal} from 'react-dom';

import ContextMenuItem from './ContextMenuItem';

import type {
ContextMenuItem as ContextMenuItemType,
ContextMenuPosition,
ContextMenuRef,
} from './types';

import styles from './ContextMenu.css';
Expand Down Expand Up @@ -49,15 +48,13 @@ type Props = {
items: ContextMenuItemType[],
position: ContextMenuPosition,
hide: () => void,
ref?: ContextMenuRef,
};

export default function ContextMenu({
anchorElementRef,
position,
items,
hide,
ref = createRef(),
}: Props): React.Node {
// This works on the assumption that ContextMenu component is only rendered when it should be shown
const anchor = anchorElementRef.current;
Expand All @@ -73,8 +70,21 @@ export default function ContextMenu({
'[data-react-devtools-portal-root]',
);

const hideMenu = portalContainer == null || items.length === 0;
const menuRef = React.useRef<HTMLDivElement | null>(null);

useLayoutEffect(() => {
const menu = ((ref.current: any): HTMLElement);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cast was unsound. ref was passed from

useImperativeHandle(
ref,
() => ({
isShown() {
return shouldShow;
},
hide,
}),
[shouldShow, hide],
);
if (!shouldShow) {
return closedMenuStub;
}
return (
<ContextMenu
anchorElementRef={anchorElementRef}
position={position}
hide={hide}
items={items}
ref={ref}
/>

i.e. the owner both set the ref and let the child override the ref again. It seems to me we just don't need to pass along the ref and let ContextMenu fully own the ref on the div

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotcha

// Match the early-return condition below.
if (hideMenu) {
return;
}
const maybeMenu = menuRef.current;
if (maybeMenu === null) {
throw new Error(
"Can't access context menu element. This is a bug in React DevTools.",
);
}
const menu = (maybeMenu: HTMLDivElement);

function hideUnlessContains(event: Event) {
if (!menu.contains(((event.target: any): Node))) {
Expand All @@ -98,14 +108,14 @@ export default function ContextMenu({

ownerWindow.removeEventListener('resize', hide);
};
}, []);
}, [hideMenu]);

if (portalContainer == null || items.length === 0) {
if (hideMenu) {
return null;
}

return createPortal(
<div className={styles.ContextMenu} ref={ref}>
<div className={styles.ContextMenu} ref={menuRef}>
{items.map(({onClick, content}, index) => (
<ContextMenuItem key={index} onClick={onClick} hide={hide}>
{content}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export default function ContextMenuContainer({
position={position}
hide={hide}
items={items}
ref={ref}
/>
);
}
Loading