index.tsx


Overview

This file implements a React component and a custom hook related to managing a context menu for nodes within a flow diagram or graph interface, leveraging the @xyflow/react library. The primary goal is to provide UI controls—specifically a context menu that appears on right-clicking a node—enabling users to duplicate or delete nodes interactively.

Key functionalities include:

This file is typically integrated within a React Flow environment where nodes represent interactive elements in a visual workflow or graph.


Exports

Interface: INodeContextMenu

export interface INodeContextMenu {
  id: string;
  top: number;
  left: number;
  right?: number;
  bottom?: number;
  [key: string]: unknown;
}

Description

Defines the properties expected by the NodeContextMenu component, specifying the node ID and positioning coordinates to place the context menu relative to the node.

Properties

Property

Type

Description

id

string

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

top

number

The top coordinate (pixels) for menu positioning.

left

number

The left coordinate (pixels) for menu positioning.

right

number (optional)

The right coordinate (pixels), alternative horizontal positioning.

bottom

number (optional)

The bottom coordinate (pixels), alternative vertical positioning.

[key: string]

unknown (optional)

Additional props passed to the menu container element.


Component: NodeContextMenu

export function NodeContextMenu({
  id,
  top,
  left,
  right,
  bottom,
  ...props
}: INodeContextMenu)

Description

A React functional component that renders a context menu for a given node. It provides buttons to duplicate or delete the node. The menu is positioned absolutely on the page based on the top, left, right, and bottom props.

Parameters

Returns

Usage Example

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

Implementation Details


Hook: useHandleNodeContextMenu

export const useHandleNodeContextMenu = (sideWidth: number) => { ... }

Description

A custom React hook managing the state and event handlers related to the node context menu. It tracks the menu's visibility and position, handles right-click events on nodes, and closes the menu on pane clicks.

Parameters

Returns

An object containing:

Property

Type

Description

onNodeContextMenu

NodeMouseHandler

Event handler for right-click on nodes, positions and opens the menu.

menu

INodeContextMenu

Current menu state (position and node id).

onPaneClick

() => void

Handler to close the menu when clicking outside.

ref

React.RefObject

Ref to the container DOM node for bounding calculations.

Usage Example

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

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

Implementation Details

Note: Some commented-out code suggests previous attempts to refine menu positioning based on pane boundaries, currently simplified.


Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

componentDiagram
    component "NodeContextMenu" {
      +duplicateNode()
      +deleteNode()
      props: INodeContextMenu (id, top, left, right?, bottom?, ...)
    }
    component "useHandleNodeContextMenu" {
      +onNodeContextMenu(event, node)
      +onPaneClick()
      state: menu (INodeContextMenu)
      ref: DOM element ref
    }
    component "ReactFlow" {
      +getNode(id)
      +addNodes(node)
      +setNodes(fn)
      +setEdges(fn)
    }

    ReactFlow <.. NodeContextMenu : uses
    useHandleNodeContextMenu ..> NodeContextMenu : provides menu state & handlers
    useHandleNodeContextMenu ..> ReactFlow : event handlers attached to nodes

Summary

This file provides a reusable React UI component and hook to manage a node context menu in a flow-based diagram editor. It allows interactive duplication and deletion of nodes with thoughtful UI positioning and integration with the React Flow management API. The solution balances usability and functionality with clean React hooks and well-defined interfaces.


Additional Notes