index.tsx


Overview

This file defines a React component and a custom hook that together implement a contextual menu for nodes within a flowchart or graph interface using the @xyflow/react library. The context menu allows users to perform node-specific actions such as duplicating and deleting nodes. The hook useHandleNodeContextMenu manages the state and positioning of the context menu when a user right-clicks on a node.

In summary, this file enhances node interaction within a React Flow environment by providing a convenient in-place menu for node operations, improving user experience in graph manipulation scenarios.


Exports

interface INodeContextMenu

Describes the properties related to the node context menu positioning and identification.

Property

Type

Description

id

string

The unique identifier of the node for which the menu is shown

top

number

The vertical position (in pixels) where the menu should appear

left

number

The horizontal position (in pixels) where the menu should appear

right?

number (optional)

Optional right offset for positioning the menu

bottom?

number (optional)

Optional bottom offset for positioning the menu

[key: string]

unknown

Additional arbitrary properties


NodeContextMenu Component

A React functional component that renders a context menu for a specific node in the flow.

Signature

function NodeContextMenu(props: INodeContextMenu): JSX.Element

Props

Description

Usage Example

<NodeContextMenu
  id="node-1"
  top={100}
  left={200}
/>

This renders the menu at position (100px from top, 200px from left) for the node with id "node-1".


useHandleNodeContextMenu Hook

A custom React hook (marked as deprecated) that manages the state and interaction logic for showing the node context menu on right-click.

Signature

function useHandleNodeContextMenu(sideWidth: number): {
  onNodeContextMenu: NodeMouseHandler,
  menu: INodeContextMenu,
  onPaneClick: () => void,
  ref: React.RefObject<any>
}

Parameters

Returns

Description

Usage Example

const sideWidth = 200;
const { onNodeContextMenu, menu, onPaneClick, ref } = useHandleNodeContextMenu(sideWidth);

// In render:
<div ref={ref} onClick={onPaneClick}>
  <ReactFlow onNodeContextMenu={onNodeContextMenu}>
    {/* flow nodes and edges */}
    {menu.id && (
      <NodeContextMenu {...menu} />
    )}
  </ReactFlow>
</div>

Important Implementation Details


Interaction with Other Parts of the System


File Structure and Workflow Diagram

componentDiagram
    component "NodeContextMenu" as NCM
    component "useHandleNodeContextMenu Hook" as Hook
    component "React Flow Graph" as RFG
    component "CSS Module (index.less)" as CSS

    Hook <..> NCM : provides menu state & handlers
    NCM --> RFG : uses useReactFlow API
    Hook --> RFG : sets event handlers (onNodeContextMenu)
    NCM --> CSS : styled by

Summary

This file is a React component and hook module that implements a node-specific context menu within a React Flow graph UI. It allows users to duplicate or delete nodes interactively through a contextual popup menu positioned near the user's click. The hook manages menu state and positioning logic, while the component renders the menu and invokes node operations through the React Flow API. This enhances node manipulation UX in flowchart or graph-based applications built with @xyflow/react.


If you require further details or integration guidance, please let me know!