mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
0eac01abcd
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).
52 lines
1.2 KiB
JavaScript
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],
|
|
);
|
|
}
|