@ii_elif_ii/react-node-tree
v1.0.8
Published
Animated React node tree component with configurable layouts and connection lines.
Maintainers
Readme
@ii_elif_ii/react-node-tree
Animated React tree renderer with configurable layout and SVG connection lines.
Install
npm install @ii_elif_ii/react-node-treePeer dependencies:
reactreact-dom
Quick Start
import * as React from "react";
import { NodeTree, type TreeNode } from "@ii_elif_ii/react-node-tree";
import "@ii_elif_ii/react-node-tree/styles.css";
const tree: TreeNode[] = [
{
id: "root",
render: ({ node }) => <div className="rounded border p-3">{node.id}</div>,
children: {
layout: "row",
nodes: [
{ id: "child-1", render: ({ node }) => <div>{node.id}</div> },
{ id: "child-2", render: ({ node }) => <div>{node.id}</div> },
],
},
},
];
export function Demo() {
return (
<NodeTree
nodeTree={tree}
layout={{ direction: "down", root: "stack", gap: 64 }}
connection={{ color: "rgba(255,255,255)", width: 1, opacity: 0.15 }}
animation={{ durationMs: 2000 }}
className={{ frame: "my-node-frame" }}
nodeFrame={{ style: { borderRadius: 12 } }}
/>
);
}Terminology
NodeTree: the React component that renders the full tree.TreeNode: the node data model used to build your tree.
API
TreeNode<TData = unknown>
type TreeNode<TData = unknown> = {
id: string;
data?: TData;
render: (context: TreeNodeRenderContext<TData>) => React.ReactNode;
children?: {
layout?: "stack" | "row";
nodes: TreeNode<TData>[];
};
};TreeNodeRenderContext<TData = unknown>
node: currentTreeNodeindex: index among siblingsdepth: depth from root (0for root)parentId: parent id (undefinedfor root nodes)path: ancestor path ending with current node idisLeaf: whether the node has no childrenchildCount: number of direct childrenisNodeAnimationDone: whether this node's enter animation finished
NodeTreeProps
nodeTree: TreeNode[]root nodes to renderclassName?: NodeTreeClassNameOptionsslot classes (root,canvas,renderer,frame,connections)layout?: NodeTreeLayoutOptionsconnection?: NodeTreeConnectionOptionsanimation?: NodeTreeAnimationOptionsnodeFrame?: NodeTreeFrameOptionsdebug?: boolean- all standard
divattributes except plainclassName(this package uses slot-basedclassName)
NodeTreeLayoutOptions
align?: "center" | "start" | "end" | { x, y }direction?: "down" | "right"root?: "stack" | "row"gap?: numberpadding?: numbercontainerPadding?: number
NodeTreeConnectionOptions
color?: stringwidth?: numberopacity?: number
NodeTreeAnimationOptions
durationMs?: number
NodeTreeFrameOptions
style?: React.CSSProperties
Exports
Component
NodeTree
Types
TreeNodeTreeNodeChildrenTreeNodeRenderContextTreeNodeEdgeNodeTreePropsNodeTreeLayoutOptionsNodeTreeConnectionOptionsNodeTreeAnimationOptionsNodeTreeClassNameOptionsNodeTreeFrameOptionsNodeFrameProps
