1.0.4 • Published 10 months ago

@rileyy29/react-mindmap v1.0.4

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

react-mindmap

Build Downloads Size

A simple base framework/starter package intended to assist developers with the creation and visualisation of interactive MindMaps in React.

Contents

What is this?

This package is a React component that renders and manages interactive Mindmaps. This is a simple tool in early development which allows users to create draggable, dynamic and interactive Mindmaps simply.

Install

This package is installable with npm or yarn. Supporting the latest versions of Node.js.

npm install @rileyy29/react-mindmap
yarn add @rileyy29/react-mindmap

You may also be required to install peer dependencies if you do not already have them installed.

npm install react-draggable
yarn add react-draggable

Usage

Mindmap Props

  • defaultNodes (Array<NodeStructure>, default: [])\ Use the Mindmap in an uncontrolled state, with state being managed by the Mindmap itself.
  • nodes (Array<NodeStructure>, default: [])\ Use the Mindmap in a controlled state, with the state being managed by yourself. This requires the use of onNodesChange for drag operations etc.
  • onNodesChange (Array<NodeStructure>, default: [])\ Specify a function to be invoked when the node set is changed, during drag operations etc.
  • line (SVGProps<SVGPathElement>, default: undefined)\ Specify props to be passed to the mindmap connector line (pathing).
  • noLine (boolean, default: false)\ Specify whether the mindmap connector line (pathing) should be hidden.
  • nodeClassName (string, default: '')\ Specify a classname for the parent element for each Node.
  • draggable (boolean, default: true)\ Specify whether all nodes should be draggable.
  • draggableFn ((node: NodeStructure) => boolean, default: undefined)\ Specify whether a node should be draggable using a per node reference.
  • renderNode ((node: NodeStructure) => ReactNode | ReactElement | JSX.Element, default: undefined, required)\ Specify a renderer for the node.

Examples

Simple Root and Child

This example shows how to create a simple root -> child Mindmap with draggable functionality. By default, a connecting line will be rendered between the nodes. All styling can be customised with the renderNode prop or the line prop.

This is using the Mindmap in a controlled state, if you do not want to control the state of nodes yourself, you can use the defaultNodes prop instead of nodes and onNodesChange.

import { Mindmap, type NodeStructure } from "@rileyy29/react-mindmap";
import { useState } from "react";

export default function Example() {
    const [nodes, setNodes] = useState<NodeStructure[]>([{
        id: 1,
        x: 455,
        y: 0,
        width: 200,
        height: 100,
        type: "ROOT",
        node: { title: 'Root Node' },
        childNodes: [
            {
                id: 2,
                x: 455,
                y: 150,
                width: 200,
                height: 100,
                type: "CHILD",
                node: { title: 'Child Node' },
                childNodes: []
            }
        ]
    }]);

    return <Mindmap draggable={true} renderNode={(node) => <div>{node.node.title}</div>} nodes={nodes} onNodesChange={setNodes} />
}

Multiple Root and Child Node

This example shows how to create a multi root -> multi child Mindmap with draggable functionality. By default, a connecting line will be rendered between the nodes. All styling can be customised with the renderNode prop or the line prop.

This is using the Mindmap in a controlled state, if you do not want to control the state of nodes yourself, you can use the defaultNodes prop instead of nodes and onNodesChange.

import { Mindmap, type NodeStructure } from "@rileyy29/react-mindmap";
import { useState } from "react";

export default function Example() {
    const [nodes, setNodes] = useState<NodeStructure[]>([
        {
            id: 6,
            x: 455,
            y: 0,
            width: 200,
            height: 100,
            type: "ROOT",
            node: { title: 'Root Node 2' },
            childNodes: [
                {
                    id: 10,
                    x: 455,
                    y: 150,
                    width: 200,
                    height: 100,
                    type: "CHILD",
                    node: { title: 'Child Node 1 (Root 2)' },
                    childNodes: []
                }
            ]
        },
        {
            id: 1,
            x: 225,
            y: 0,
            width: 200,
            height: 100,
            type: "ROOT",
            node: { title: 'Root Node' },
            childNodes: [
                {
                    id: 2,
                    x: 225,
                    y: 150,
                    width: 200,
                    height: 100,
                    type: "CHILD",
                    node: { title: 'Child Node 1' },
                    childNodes: [
                        {
                            id: 3,
                            x: 15,
                            y: 280,
                            width: 200,
                            height: 100,
                            type: "CHILD",
                            node: { title: 'Child Node 2' },
                            childNodes: [],
                        },
                        {
                            id: 4,
                            x: 255,
                            y: 300,
                            width: 200,
                            height: 100,
                            type: "CHILD",
                            node: { title: 'Child Node 3' },
                            childNodes: [
                                {
                                    id: 23,
                                    x: 255,
                                    y: 420,
                                    width: 200,
                                    height: 100,
                                    type: "CHILD",
                                    node: { title: 'Child Node 4' },
                                    childNodes: [
                                        {
                                            id: 33,
                                            x: 255,
                                            y: 540,
                                            width: 125,
                                            height: 55,
                                            type: "CHILD",
                                            node: { title: 'Child Node 5' },
                                            childNodes: [],
                                        },
                                        {
                                            id: 36,
                                            x: 390,
                                            y: 540,
                                            width: 125,
                                            height: 55,
                                            type: "CHILD",
                                            node: { title: 'Child Node 6' },
                                            childNodes: [],
                                        },
                                        {
                                            id: 37,
                                            x: 525,
                                            y: 540,
                                            width: 125,
                                            height: 55,
                                            type: "CHILD",
                                            node: { title: 'Child Node 7' },
                                            childNodes: [],
                                        }
                                    ],
                                }
                            ],
                        }
                    ],
                },
            ],
        }
    ]);

    return <Mindmap draggable={true} renderNode={(node) => <div>{node.node.title}</div>} nodes={nodes} onNodesChange={setNodes} />
}

Screenshots

Nodes Screenshot

License

MIT © Riley Rogerson

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago