A powerful, flexible, and easy-to-use React tree component with drag-and-drop functionality built on top of @dnd-kit.
- 🎯 Drag & Drop: Full drag-and-drop support with visual feedback
- 📁 Nested Trees: Support for unlimited nesting levels
- 🎨 Customizable: Completely customizable appearance via CSS
- 🔧 TypeScript: Written in TypeScript with full type safety
- ⚡ Performance: Optimized rendering and smooth animations
- 🧩 Flexible: Works with any data structure
- 📱 Accessible: Built with accessibility in mind
- 🎭 Two Styles: Simple and folder-style tree components included
npm install dnd-dynamic-tree @dnd-kit/core @dnd-kit/sortable @dnd-kit/utilitiesimport { SortableTree, SimpleTreeItemWrapper } from 'dnd-dynamic-tree'
function MyTree() {
const [items, setItems] = useState([
{ id: '1', value: 'Home', children: [{ id: '4', value: 'Documents' }] },
{ id: '2', value: 'Work' },
{ id: '3', value: 'Personal', canHaveChildren: false },
])
const TreeItemComponent = React.forwardRef((props, ref) => (
<SimpleTreeItemWrapper {...props} ref={ref}>
<div>{props.item.value}</div>
</SimpleTreeItemWrapper>
))
return (
<SortableTree
items={items}
onItemsChanged={setItems}
TreeItemComponent={TreeItemComponent}
/>
)
}Check out the Storybook for code samples and play around. You could also play with it on CodeSandbox
- Node.js 18+
- npm or pnpm
-
Clone the repository
git clone https://github.com/shantanusoam/dnd-dynamic-tree.git cd dnd-dynamic-tree -
Install dependencies
npm install
-
Start the development demo
npm run dev
This will start a demo application at
http://localhost:5173where you can see and test the library in action. -
Start Storybook
npm run storybook
This will start Storybook at
http://localhost:6006with interactive examples and documentation.
npm run dev- Start development demonpm run storybook- Start Storybook development servernpm run build- Build the library for productionnpm run build:lib- Build only the librarynpm run build:demo- Build the demo applicationnpm run lint- Run ESLintnpm run lint:fix- Fix ESLint issues
dnd-dynamic-tree/
├── lib/ # Library source code
│ ├── dynamic-Tree/ # Main library components
│ │ ├── index.ts # Main exports
│ │ ├── SortableTree.tsx
│ │ ├── ui/ # UI components
│ │ └── types.ts # TypeScript types
│ └── main.ts # Library entry point
├── src/ # Development/demo code
│ ├── App.tsx # Demo application
│ ├── stories/ # Storybook stories
│ └── index.css
├── dist/ # Built library output
└── package.json
The library includes several example implementations:
import { SortableTree, SimpleTreeItemWrapper } from 'dnd-dynamic-tree'
const SimpleTreeComponent = React.forwardRef((props, ref) => (
<SimpleTreeItemWrapper {...props} ref={ref}>
<div style={{ padding: '8px 12px' }}>
{props.item.value}
</div>
</SimpleTreeItemWrapper>
))
<SortableTree
items={treeData}
onItemsChanged={setTreeData}
TreeItemComponent={SimpleTreeComponent}
indentationWidth={20}
/>import { SortableTree, FolderTreeItemWrapper } from 'dnd-dynamic-tree'
const FolderTreeComponent = React.forwardRef((props, ref) => (
<FolderTreeItemWrapper {...props} ref={ref}>
<div style={{ padding: '8px 12px' }}>
{props.item.value}
</div>
</FolderTreeItemWrapper>
))
<SortableTree
items={treeData}
onItemsChanged={setTreeData}
TreeItemComponent={FolderTreeComponent}
indentationWidth={25}
/>For complete documentation and API reference, check out:
- Storybook: Interactive examples at
http://localhost:6006(after runningnpm run storybook) - Development Demo: Live demo at
http://localhost:5173(after runningnpm run dev) - Source Code: Well-documented TypeScript source in the
lib/directory
- File Explorers: Organize files and folders with drag-and-drop
- Task Management: Manage tasks and projects hierarchically
- Navigation Menus: Create dynamic, reorderable navigation structures
- Content Management: Organize content categories and items
- Data Organization: Any hierarchical data that needs reordering
Contributions are welcome! The development setup is now properly configured:
- Fork the repository
- Clone your fork:
git clone https://github.com/your-username/dnd-dynamic-tree.git - Install dependencies:
npm install - Start development:
npm run dev - Start Storybook:
npm run storybook - Make your changes and test thoroughly
- Submit a pull request
MIT © Shantanu Soam