Home / File/ useContextMenu.js — react Source File

useContextMenu.js — react Source File

Architecture documentation for useContextMenu.js, a javascript file in the react codebase. 2 imports, 1 dependents.

File javascript BabelCompiler Validation 2 imports 1 dependents 2 functions

Entity Profile

Dependency Diagram

graph LR
  27c1f5e3_f1ca_440b_62aa_b832f761835e["useContextMenu.js"]
  f94c772c_e76f_6e89_bc9b_2d47148e561c["types.js"]
  27c1f5e3_f1ca_440b_62aa_b832f761835e --> f94c772c_e76f_6e89_bc9b_2d47148e561c
  ac587885_e294_a1e9_b13f_5e7b920fdb42["react"]
  27c1f5e3_f1ca_440b_62aa_b832f761835e --> ac587885_e294_a1e9_b13f_5e7b920fdb42
  7eedd21d_ae6a_7447_035b_f34143688483["ContextMenuContainer.js"]
  7eedd21d_ae6a_7447_035b_f34143688483 --> 27c1f5e3_f1ca_440b_62aa_b832f761835e
  style 27c1f5e3_f1ca_440b_62aa_b832f761835e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Copyright (c) Meta Platforms, Inc. and 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 * as React from 'react';
import {useState, useEffect, useCallback} from 'react';

import type {ContextMenuPosition} from './types';

type Payload = {
  shouldShow: boolean,
  position: ContextMenuPosition | null,
  hide: () => void,
};

export default function useContextMenu(anchorElementRef: {
  current: React.ElementRef<any> | null,
}): Payload {
  const [shouldShow, setShouldShow] = useState(false);
  const [position, setPosition] = React.useState<ContextMenuPosition | null>(
    null,
  );

  const hide = useCallback(() => {
    setShouldShow(false);
    setPosition(null);
  }, []);

  useEffect(() => {
    const anchor = anchorElementRef.current;
    if (anchor == null) return;

    function handleAnchorContextMenu(e: MouseEvent) {
      e.preventDefault();
      e.stopPropagation();

      const {pageX, pageY} = e;

      const ownerDocument = anchor?.ownerDocument;
      const portalContainer = ownerDocument?.querySelector(
        '[data-react-devtools-portal-root]',
      );

      if (portalContainer == null) {
        throw new Error(
          "DevTools tooltip root node not found: can't display the context menu",
        );
      }

      // `x` and `y` should be relative to the container, to which these context menus will be portaled
      // we can't use just `pageX` or `pageY` for Fusebox integration, because RDT frontend is inlined with the whole document
      // meaning that `pageY` will have an offset of 27, which is the tab bar height
      // for the browser extension, these will equal to 0
      const {top: containerTop, left: containerLeft} =
        portalContainer.getBoundingClientRect();

      setShouldShow(true);
      setPosition({x: pageX - containerLeft, y: pageY - containerTop});
    }

    anchor.addEventListener('contextmenu', handleAnchorContextMenu);
    return () => {
      anchor.removeEventListener('contextmenu', handleAnchorContextMenu);
    };
  }, [anchorElementRef]);

  return {shouldShow, position, hide};
}

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does useContextMenu.js do?
useContextMenu.js is a source file in the react codebase, written in javascript. It belongs to the BabelCompiler domain, Validation subdomain.
What functions are defined in useContextMenu.js?
useContextMenu.js defines 2 function(s): Payload.hide, hide.
What does useContextMenu.js depend on?
useContextMenu.js imports 2 module(s): react, types.js.
What files import useContextMenu.js?
useContextMenu.js is imported by 1 file(s): ContextMenuContainer.js.
Where is useContextMenu.js in the architecture?
useContextMenu.js is located at packages/react-devtools-shared/src/devtools/ContextMenu/useContextMenu.js (domain: BabelCompiler, subdomain: Validation, directory: packages/react-devtools-shared/src/devtools/ContextMenu).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free