# diagram-mind-map

> Mind-map diagram with custom user handles to add, branch, and delete nodes.

**Framework:** react  **Component:** Diagram  **Variant:** mind-map

## Get this item

**If you are an agent, fetch the JSON.** Source is inlined, so one request is enough and no tooling is required:

```
GET https://ai.syncfusion.com/r/react/ts/diagram-mind-map.json
```

**Install Package(s)**

```bash
npm install @syncfusion/ej2-react-diagrams @syncfusion/ej2-data
```

**Notes**

- Syncfusion release: 2026 Volume 3 (v35.1.37)
- The Syncfusion package is licensed. The composition in this file is source you own and edit. See https://ai.syncfusion.com/licensing.md

## Source files

### src/components/diagram-mind-map/Diagram.tsx

```tsx
import {
  // React Diagram components and utilities
  MindMap as MindMapModule,
  HierarchicalTree,
  ConnectorConstraints,
  DiagramConstraints,
  ToolBase,
  type MouseEventArgs,
  randomId,
  type ISelectionChangeEventArgs,
  PointPort,
  type UserHandleModel,
  SelectorConstraints,
  SnapConstraints,
  type PointPortModel,
  PortVisibility,
  DiagramComponent,
  NodeConstraints,
  type NodeModel,
  type ConnectorModel,
  Node,
  Connector,
  Diagram,
  DiagramTools,
  Inject,
  DataBinding,
  type Side,
  type MarginModel,
  type HorizontalAlignment,
  type VerticalAlignment,
  type TextModel
} from "@syncfusion/ej2-react-diagrams";

// Importing base components and data management utilities
import { DataManager, Query } from "@syncfusion/ej2-data";
import { mindMap } from "../datasource/diagram-mind-map-data";

// Interface for defining mind-map node information
export interface EmployeeInfo {
  branch: string;
  color: string;
  Left: string;
  Right: string;
  Root: string;
  Label: string;
}

// Initialize data manager with mindMap data and query
const items: DataManager = new DataManager(
  mindMap as unknown as JSON[],
  new Query().take(7)
);

let diagramInstance: Diagram;
// React component for MindMap
function MindMap() {
  //creation of the Ports
  function getPort(): PointPortModel[] {
    return [
      {
        id: "port1",
        offset: { x: 0, y: 0.5 },
        visibility: PortVisibility.Hidden,
        style: { fill: "black" }
      },
      {
        id: "port2",
        offset: { x: 1, y: 0.5 },
        visibility: PortVisibility.Hidden,
        style: { fill: "black" }
      }
    ];
  }

  // Function to add a new node
  function addNode(): NodeModel {
    const obj: NodeModel = {};
    obj.id = randomId();
    obj.data = { Label: "Node", branch: "Right" } as EmployeeInfo;
    return obj;
  }

  // Function to add a connector
  function addConnector(source: NodeModel, target: NodeModel): ConnectorModel {
    const connector: ConnectorModel = {};
    connector.id = randomId();
    connector.sourceID = source.id;
    connector.targetID = target.id;
    return connector;
  }

  // Function to get custom tool based on action (leftHandle, rightHandle, delete)
  function getTool(action: string): ToolBase {
    let tool!: ToolBase;
    if (action === "leftHandle") {
      tool = new LeftExtendTool(diagramInstance.commandHandler);
    } else if (action === "rightHandle") {
      tool = new RightExtendTool(diagramInstance.commandHandler);
    } else if (action === "delete") {
      tool = new DeleteClick(diagramInstance.commandHandler);
    }
    return tool;
  }

  // Custom tool class for left extension tool
  class LeftExtendTool extends ToolBase {
    public mouseDown(_args: MouseEventArgs): void {
      super.mouseDown(_args);
      this.inAction = true;
    }
    public mouseUp(_args: MouseEventArgs): void {
      if (this.inAction) {
        const selectedObject: any = this.commandHandler.getSelectedObject();
        if (selectedObject[0]) {
          if (selectedObject[0] instanceof Node) {
            const node: NodeModel = addNode();
            if ((selectedObject[0].data as EmployeeInfo).branch === "Root") {
              (node.data as EmployeeInfo).branch = "Right";
            } else if (
              (selectedObject[0].data as EmployeeInfo).branch === "Right" ||
              (selectedObject[0].data as EmployeeInfo).branch === "subRight"
            ) {
              (node.data as EmployeeInfo).branch = "subRight";
            }
            getTextEditValue(selectedObject[0], node);
          }
        }
      }
    }
  }

  // Custom tool class for right extension tool
  class RightExtendTool extends ToolBase {
    //mouseDown event
    public mouseDown(_args: MouseEventArgs): void {
      super.mouseDown(_args);
      this.inAction = true;
    }
    //mouseUp event
    public mouseUp(_args: MouseEventArgs): void {
      if (this.inAction) {
        const selectedObject: any = this.commandHandler.getSelectedObject();
        if (selectedObject[0]) {
          if (selectedObject[0] instanceof Node) {
            const node: NodeModel = addNode();
            if ((selectedObject[0].data as EmployeeInfo).branch === "Root") {
              (node.data as EmployeeInfo).branch = "Left";
            } else if (
              (selectedObject[0].data as EmployeeInfo).branch === "Left" ||
              (selectedObject[0].data as EmployeeInfo).branch === "subLeft"
            ) {
              (node.data as EmployeeInfo).branch = "subLeft";
            }
            getTextEditValue(selectedObject[0], node);
          }
        }
      }
    }
  }

  // Custom tool class for delete tool
  class DeleteClick extends ToolBase {
    //mouseDown event
    public mouseDown(_args: MouseEventArgs): void {
      super.mouseDown(_args);
      this.inAction = true;
    }
    //mouseup event
    public mouseUp(_args: MouseEventArgs): void {
      if (this.inAction) {
        const selectedObject: any = this.commandHandler.getSelectedObject();
        if (selectedObject[0]) {
          if (selectedObject[0] instanceof Node) {
            const node: Node = selectedObject[0] as Node;
            this.removeSubChild(node);
          }
          diagramInstance.doLayout();
        }
      }
    }
    //Function to Remove the subchild Elements
    private removeSubChild(node: Node): void {
      for (let i: number = node.outEdges.length - 1; i >= 0; i--) {
        const connector: Connector = diagramInstance.getObject(
          node.outEdges[i]
        ) as Connector;
        const childNode: Node = diagramInstance.getObject(
          connector.targetID ?? ""
        ) as Node;
        if (childNode.outEdges.length > 0) {
          this.removeSubChild(childNode);
        } else {
          diagramInstance.remove(childNode);
        }
      }
      diagramInstance.remove(node);
    }
  }

  //Function to hide the require userhandle.
  function hideUserHandle(name: string): void {
    const handles = diagramInstance.selectedItems.userHandles ?? [];
    for (const handle of handles) {
      if (handle.name === name) {
        handle.visible = false;
      }
    }
  }

  // Definition of left arrow path,right arrow path,delete icon path for user handle
  const leftarrow: string =
    "M11.924,6.202 L4.633,6.202 L4.633,9.266 L0,4.633 L4.632,0 L4.632,3.551 L11.923,3.551 L11.923,6.202Z";
  const rightarrow: string =
    "M0,3.063 L7.292,3.063 L7.292,0 L11.924,4.633 L7.292,9.266 L7.292,5.714 L0.001,5.714 L0.001,3.063Z";
  const deleteicon: string =
    "M 7.04 22.13 L 92.95 22.13 L 92.95 88.8 C 92.95 91.92 91.55 94.58 88.76" +
    "96.74 C 85.97 98.91 82.55 100 78.52 100 L 21.48 100 C 17.45 100 14.03 98.91 11.24 96.74 C 8.45 94.58 7.04" +
    "91.92 7.04 88.8 z M 32.22 0 L 67.78 0 L 75.17 5.47 L 100 5.47 L 100 16.67 L 0 16.67 L 0 5.47 L 24.83 5.47 z";

  // Definition of user handles with their respective properties
  const leftuserhandle: UserHandleModel = setUserHandle(
    "leftHandle",
    leftarrow,
    "Left",
    1,
    { top: 0, bottom: 0, left: 0, right: 10 },
    "Left",
    "Top"
  );
  const rightuserhandle: UserHandleModel = setUserHandle(
    "rightHandle",
    rightarrow,
    "Right",
    1,
    { top: 0, bottom: 0, left: 10, right: 0 },
    "Right",
    "Top"
  );
  const deleteuserhandle: UserHandleModel = setUserHandle(
    "delete",
    deleteicon,
    "Top",
    0.5,
    { top: 0, bottom: 10, left: 0, right: 0 },
    "Center",
    "Center"
  );
  const handle: UserHandleModel[] = [
    leftuserhandle,
    rightuserhandle,
    deleteuserhandle
  ];

  //set and creation of the Userhandle.
  function setUserHandle(
    name: string,
    pathData: string,
    side: Side,
    offset: number,
    margin: MarginModel,
    horizontalAlign: HorizontalAlignment,
    verticalAlign: VerticalAlignment
  ): UserHandleModel {
    return {
      name: name,
      pathData: pathData,
      backgroundColor: "black",
      pathColor: "white",
      side: side,
      offset: offset,
      margin: margin,
      horizontalAlignment: horizontalAlign,
      verticalAlignment: verticalAlign
    };
  }

  // Function to handle text edit value and add a new node and connector
  function getTextEditValue(selectObject: any, node: any): void {
    const connector: any = addConnector(selectObject, node);
    diagramInstance.clearSelection();
    const newNode = diagramInstance.add(node);
    diagramInstance.add(connector);
    diagramInstance.doLayout();
    diagramInstance.bringIntoView(newNode.wrapper.bounds);
    const selectedNodes = diagramInstance.selectedItems.nodes ?? [];
    diagramInstance.select([(diagramInstance.nameTable as Record<string, any>)[newNode.id ?? ""]]);
    if (selectedNodes.length > 0) {
      diagramInstance.startTextEdit(selectedNodes[0]);
    }
  }

  //Change the Position of the UserHandle.
  function changeUserHandlePosition(change: string): void {
    const handles = diagramInstance.selectedItems.userHandles ?? [];
    for (const handle of handles) {
      if (handle.name === "delete" && change === "leftHandle") {
        applyHandle(
          handle,
          "Left",
          1,
          { top: 0, bottom: 0, left: 0, right: 10 },
          "Left",
          "Top"
        );
      } else if (handle.name === "delete" && change === "rightHandle") {
        applyHandle(
          handle,
          "Right",
          1,
          { top: 0, bottom: 0, left: 10, right: 0 },
          "Right",
          "Top"
        );
      }
    }
  }
  //function to set the value for UserHandle element.
  function applyHandle(
    handle: UserHandleModel,
    side: Side,
    offset: number,
    margin: MarginModel,
    horizontalAlignment: HorizontalAlignment,
    verticalAlignment: VerticalAlignment
  ): void {
    handle.side = side;
    handle.offset = offset;
    handle.margin = margin;
    handle.horizontalAlignment = horizontalAlignment;
    handle.verticalAlignment = verticalAlignment;
  }

  return (
    <div className="control-pane">
      <div className="control-section">
        <div  style={{ width: "100%" }}>
          <DiagramComponent
            ref={(diagram: DiagramComponent | null) => { diagramInstance = diagram as Diagram; }}
            id="diagram"
            style={{ width: "74%", height: "550px", float: "left" }}
            width={"100%"}
            height={"550px"}
            constraints={
              DiagramConstraints.Default & ~DiagramConstraints.UndoRedo
            }
            snapSettings={{ constraints: SnapConstraints.None }}
            tool={DiagramTools.SingleSelect}
            layout={{
              type: "MindMap",
              orientation:'Horizontal',
              getBranch: (node: NodeModel) => {
                return ((node as Node).data as EmployeeInfo).branch;
              },
              horizontalSpacing: 50
            }}
            //Selectionchange event for Node and connector
            selectionChange={(arg: ISelectionChangeEventArgs) => {
              if (arg.state === "Changing") {
                if (arg.newValue[0] instanceof Node) {
                  const handles = diagramInstance.selectedItems.userHandles ?? [];
                  for (const handle of handles) {
                    handle.visible = true;
                  }
                  if (
                    ((arg.newValue[0] as Node).data as EmployeeInfo)
                      .branch === "Left" ||
                    ((arg.newValue[0] as Node).data as EmployeeInfo)
                      .branch === "subLeft"
                  ) {
                    hideUserHandle("leftHandle");
                    changeUserHandlePosition("leftHandle");
                  } else if (
                    ((arg.newValue[0] as Node).data as EmployeeInfo)
                      .branch === "Right" ||
                    ((arg.newValue[0] as Node).data as EmployeeInfo)
                      .branch === "subRight"
                  ) {
                    hideUserHandle("rightHandle");
                    changeUserHandlePosition("rightHandle");
                  } else if (
                    ((arg.newValue[0] as Node).data as EmployeeInfo)
                      .branch === "Root"
                  ) {
                    hideUserHandle("delete");
                  }
                } else {
                  hideUserHandle("leftHandle");
                  hideUserHandle("rightHandle");
                  hideUserHandle("delete");
                }
              }
            }}
            selectedItems={{
              constraints: SelectorConstraints.UserHandle,
              userHandles: handle
            }}
            dataSourceSettings={{
              id: "id",
              parentId: "parentId",
              dataSource: items,
              root: String(1)
            }}
            //sets node default value
            getNodeDefaults={(obj: Node) => {
              obj.constraints =
                NodeConstraints.Default & ~NodeConstraints.Drag;
              if (
                (obj.data as EmployeeInfo).branch === "Left" ||
                (obj.data as EmployeeInfo).branch === "Right" ||
                (obj.data as EmployeeInfo).branch === "Root"
              ) {
                obj.shape = { type: "Basic", shape: "Ellipse" };
                obj.borderColor = "black";
                obj.style = {
                  fill:
                    (obj.data as EmployeeInfo).branch === "Root"
                      ? "#E74C3C"
                      : "#F39C12",
                  strokeColor: "none",
                  strokeWidth: 2
                };
                obj.annotations = [
                  {
                    content: (obj.data as EmployeeInfo).Label,
                    margin: { left: 10, right: 10, top: 10, bottom: 10 },
                    style: { color: "white" }
                  }
                ];
                const port: PointPortModel[] = getPort();
                for (let i: number = 0; i < port.length; i++) {
                  obj.ports.push(new PointPort(obj, "ports", port[i], true));
                }
              } else {
                let color: string;
                if (
                  (obj.data as EmployeeInfo).branch === "Right" ||
                  (obj.data as EmployeeInfo).branch === "subRight"
                ) {
                  color = "#8E44AD";
                } else {
                  color = "#3498DB";
                }
                obj.shape = { type: "Basic", shape: "Rectangle" };
                obj.style = { fill: color, strokeWidth: 0 };
                obj.minWidth = 100;
                obj.height = 4;
                const port: PointPortModel[] = getPort();
                for (let i: number = 0; i < port.length; i++) {
                  obj.ports.push(new PointPort(obj, "ports", port[i], true));
                }
                obj.annotations = [
                  {
                    content: (obj.data as EmployeeInfo).Label,
                    offset: { x: 0.5, y: 0 },
                    verticalAlignment: "Bottom"
                  }
                ];
                (obj.shape as TextModel).margin = {
                  left: 0,
                  right: 0,
                  top: 0,
                  bottom: 0
                };
              }
              return obj;
            }}
            //sets connector default value
            getConnectorDefaults={(
              connector: ConnectorModel,
              diagram: Diagram
            ) => {
              connector.type = "Bezier";
              connector.targetDecorator = { shape: "None" };
              const sourceNode: Node = diagram.getObject(
                connector.sourceID ?? ""
              ) as Node;
              const targetNode: Node = diagram.getObject(
                connector.targetID ?? ""
              ) as Node;
              if (
                (targetNode.data as EmployeeInfo).branch === "Right" ||
                (targetNode.data as EmployeeInfo).branch === "subRight"
              ) {
                connector.sourcePortID = sourceNode.ports[0].id;
                connector.targetPortID = targetNode.ports[1].id;
                connector.style = { strokeWidth: 5, strokeColor: "#8E44AD" };
              } else if (
                (targetNode.data as EmployeeInfo).branch === "Left" ||
                (targetNode.data as EmployeeInfo).branch === "subLeft"
              ) {
                connector.sourcePortID = sourceNode.ports[1].id;
                connector.targetPortID = targetNode.ports[0].id;
                connector.style = { strokeWidth: 5, strokeColor: "#3498DB" };
              }
              if (connector.constraints !== undefined) {
                connector.constraints &= ~ConnectorConstraints.Select;
              }
              return connector;
            }}
            created={() => {
              diagramInstance.fitToPage();
            }}
            getCustomTool={getTool}
            scrollSettings={{
              //Sets the scroll padding
              padding: { left: 50, right: 50 }
            }}
          >
            <Inject
              services={[DataBinding, MindMapModule, HierarchicalTree]}
            />
          </DiagramComponent>
        </div>
      </div>
    </div>
  );
}
export default MindMap;

```
