index.tsx


Overview

This file defines React components and hooks for managing a custom context menu associated with nodes in a React Flow graph interface. It enables users to perform actions such as duplicating or deleting nodes directly from a contextual popup menu that appears on right-clicking a node.

The primary exports include:

This file leverages the useReactFlow hook from the @xyflow/react library to interact with the graph state (nodes and edges). The context menu is styled via a CSS module (index.less).


Detailed Explanations

Interface: INodeContextMenu

Defines the properties passed to the NodeContextMenu component and used to position the context menu in the viewport.

Property

Type

Description

id

string

Unique identifier of the node for which the menu shows.

top

number

CSS top position for the menu in pixels.

left

number

CSS left position for the menu in pixels.

right?

number

Optional CSS right position for the menu.

bottom?

number

Optional CSS bottom position for the menu.

[key: string]: unknown

Allows additional arbitrary properties.


Component: NodeContextMenu

A functional React component that renders a context menu for a node, allowing duplication and deletion.

Props

Functionality

Return

JSX markup for a styled div containing:

Usage Example

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

This renders the context menu positioned at (top: 100px, left: 200px) for the node with ID "node-1".


Hook: useHandleNodeContextMenu(sideWidth: number)

A custom React hook to manage the state and behavior of the node context menu.

Parameters

Returns

An object containing:

Property

Type

Description

menu

INodeContextMenu

Current state of the context menu (position & node id).

onNodeContextMenu

(event, node) => void

Event handler to open and position the context menu on right-click.

onPaneClick

() => void

Event handler to close the context menu on pane clicks.

ref

React.RefObject<any>

Ref for the container DOM element to calculate boundaries.

Behavior Details

Usage Example

const sideWidth = 240; // e.g., width of sidebar
const { onNodeContextMenu, menu, onPaneClick, ref } = useHandleNodeContextMenu(sideWidth);

return (
  <div ref={ref} onClick={onPaneClick}>
    <ReactFlow onNodeContextMenu={onNodeContextMenu}>
      {/* graph content */}
    </ReactFlow>
    {menu.id && (
      <NodeContextMenu {...menu} />
    )}
  </div>
);

Important Implementation Details


Interaction with Other Parts of the System


Mermaid Component Diagram

componentDiagram
    component NodeContextMenu {
        +duplicateNode()
        +deleteNode()
        +render()
    }
    component useHandleNodeContextMenu {
        +onNodeContextMenu(event, node)
        +onPaneClick()
        +menu: INodeContextMenu
        +ref: React.RefObject
    }
    NodeContextMenu <.. useHandleNodeContextMenu : uses props
    useHandleNodeContextMenu --> ReactFlow : interacts via event handlers

Summary

This file provides a reusable React context menu component and corresponding hook to enable node-specific interactions (duplicate/delete) in a React Flow graph. It abstracts the complexity of positioning and state management, allowing parent components to easily integrate node context menus with minimal setup. The reliance on @xyflow/react hooks ensures seamless synchronization with the graph's internal state.


End of Documentation for index.tsx