Files
react/packages/react-devtools-shared/src/devtools/ContextMenu/useContextMenu.js
T
Brian Vaughn 0eac01abcd Added missing Flow type coverage to DevTools context menu (#17733)
The  param should probably be a generic type, but I'm not sure how to satisfy Flow with the current top-level  Map. At least this adds basic coverage (which was missing before, oops).
2019-12-29 08:44:24 -08:00

52 lines
1.2 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import {useContext, useEffect} from 'react';
import {RegistryContext} from './Contexts';
import type {ElementRef} from 'react';
export default function useContextMenu({
data,
id,
ref,
}: {|
data: Object,
id: string,
ref: ElementRef<HTMLElement>,
|}) {
const {showMenu} = useContext(RegistryContext);
useEffect(
() => {
if (ref.current !== null) {
const handleContextMenu = event => {
event.preventDefault();
event.stopPropagation();
const pageX =
event.pageX || (event.touches && event.touches[0].pageX);
const pageY =
event.pageY || (event.touches && event.touches[0].pageY);
showMenu({data, id, pageX, pageY});
};
const trigger = ref.current;
trigger.addEventListener('contextmenu', handleContextMenu);
return () => {
trigger.removeEventListener('contextmenu', handleContextMenu);
};
}
},
[data, id, showMenu],
);
}