@particle-academy/react-fancy 1.8.0 → 1.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.cjs +12 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +12 -6
- package/dist/index.js.map +1 -1
- package/docs/TreeNav.md +178 -0
- package/package.json +1 -1
package/docs/TreeNav.md
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# TreeNav
|
|
2
|
+
|
|
3
|
+
Hierarchical tree navigation for file browsers, folder structures, and nested category lists. Supports expand/collapse, selection state, file/folder icons with extension-based coloring, and controlled/uncontrolled expanded node management.
|
|
4
|
+
|
|
5
|
+
Pairs with `@particle-academy/fancy-code`'s `CodeEditor` for IDE-style layouts.
|
|
6
|
+
|
|
7
|
+
## Import
|
|
8
|
+
|
|
9
|
+
```tsx
|
|
10
|
+
import { TreeNav } from "@particle-academy/react-fancy";
|
|
11
|
+
import type { TreeNodeData } from "@particle-academy/react-fancy";
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Basic Usage
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
const files: TreeNodeData[] = [
|
|
18
|
+
{
|
|
19
|
+
id: "src", label: "src", type: "folder", children: [
|
|
20
|
+
{ id: "app.tsx", label: "App.tsx", ext: "tsx" },
|
|
21
|
+
{ id: "main.ts", label: "main.ts", ext: "ts" },
|
|
22
|
+
{
|
|
23
|
+
id: "components", label: "components", type: "folder", children: [
|
|
24
|
+
{ id: "button.tsx", label: "Button.tsx", ext: "tsx" },
|
|
25
|
+
{ id: "modal.tsx", label: "Modal.tsx", ext: "tsx" },
|
|
26
|
+
],
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
},
|
|
30
|
+
{ id: "pkg", label: "package.json", ext: "json" },
|
|
31
|
+
{ id: "readme", label: "README.md", ext: "md" },
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
<TreeNav
|
|
35
|
+
nodes={files}
|
|
36
|
+
selectedId={selectedFile}
|
|
37
|
+
onSelect={(id, node) => setSelectedFile(id)}
|
|
38
|
+
defaultExpandedIds={["src"]}
|
|
39
|
+
/>
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Props
|
|
43
|
+
|
|
44
|
+
### TreeNav (root)
|
|
45
|
+
|
|
46
|
+
| Prop | Type | Default | Description |
|
|
47
|
+
|------|------|---------|-------------|
|
|
48
|
+
| nodes | `TreeNodeData[]` | - | Tree data (required) |
|
|
49
|
+
| selectedId | `string` | - | Currently selected node ID |
|
|
50
|
+
| onSelect | `(id: string, node: TreeNodeData) => void` | - | Selection callback |
|
|
51
|
+
| expandedIds | `string[]` | - | Controlled expanded node IDs |
|
|
52
|
+
| defaultExpandedIds | `string[]` | - | Initial expanded nodes (uncontrolled) |
|
|
53
|
+
| onExpandedChange | `(ids: string[]) => void` | - | Callback when expanded state changes |
|
|
54
|
+
| defaultExpandAll | `boolean` | `false` | Expand all folders on mount |
|
|
55
|
+
| indentSize | `number` | `16` | Indent per nesting level in px |
|
|
56
|
+
| showIcons | `boolean` | `true` | Show file/folder icons |
|
|
57
|
+
| className | `string` | - | Additional CSS classes |
|
|
58
|
+
|
|
59
|
+
### TreeNodeData
|
|
60
|
+
|
|
61
|
+
| Field | Type | Default | Description |
|
|
62
|
+
|-------|------|---------|-------------|
|
|
63
|
+
| id | `string` | - | Unique identifier (required) |
|
|
64
|
+
| label | `string` | - | Display text (required) |
|
|
65
|
+
| type | `"file" \| "folder"` | - | Node type (auto-detected from children) |
|
|
66
|
+
| ext | `string` | - | File extension for icon coloring (e.g., `"ts"`, `"php"`) |
|
|
67
|
+
| children | `TreeNodeData[]` | - | Nested child nodes |
|
|
68
|
+
| icon | `ReactNode` | - | Custom icon override |
|
|
69
|
+
| disabled | `boolean` | - | Disable the node |
|
|
70
|
+
|
|
71
|
+
## Context Hook
|
|
72
|
+
|
|
73
|
+
Access tree state from custom components inside the tree:
|
|
74
|
+
|
|
75
|
+
```tsx
|
|
76
|
+
import { useTreeNav } from "@particle-academy/react-fancy";
|
|
77
|
+
|
|
78
|
+
function CustomNode() {
|
|
79
|
+
const { selectedId, toggle, expandedIds } = useTreeNav();
|
|
80
|
+
// ...
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
| Property | Type | Description |
|
|
85
|
+
|----------|------|-------------|
|
|
86
|
+
| selectedId | `string \| undefined` | Currently selected node ID |
|
|
87
|
+
| onSelect | `(id, node) => void` | Selection callback |
|
|
88
|
+
| expandedIds | `string[]` | Currently expanded node IDs |
|
|
89
|
+
| toggle | `(id: string) => void` | Toggle a node's expanded state |
|
|
90
|
+
| indentSize | `number` | Current indent size |
|
|
91
|
+
| showIcons | `boolean` | Whether icons are shown |
|
|
92
|
+
|
|
93
|
+
## Expand All
|
|
94
|
+
|
|
95
|
+
```tsx
|
|
96
|
+
<TreeNav nodes={files} defaultExpandAll />
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Controlled Expanded State
|
|
100
|
+
|
|
101
|
+
```tsx
|
|
102
|
+
const [expanded, setExpanded] = useState(["src", "components"]);
|
|
103
|
+
|
|
104
|
+
<TreeNav
|
|
105
|
+
nodes={files}
|
|
106
|
+
expandedIds={expanded}
|
|
107
|
+
onExpandedChange={setExpanded}
|
|
108
|
+
/>
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## No Icons
|
|
112
|
+
|
|
113
|
+
```tsx
|
|
114
|
+
<TreeNav nodes={files} showIcons={false} />
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Custom Indent
|
|
118
|
+
|
|
119
|
+
```tsx
|
|
120
|
+
<TreeNav nodes={files} indentSize={24} />
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## File Icon Colors
|
|
124
|
+
|
|
125
|
+
Icons are automatically colored by file extension:
|
|
126
|
+
|
|
127
|
+
| Extension | Color |
|
|
128
|
+
|-----------|-------|
|
|
129
|
+
| `.ts`, `.tsx` | Blue (#3178c6) |
|
|
130
|
+
| `.js`, `.jsx` | Yellow (#f7df1e) |
|
|
131
|
+
| `.php` | Purple (#777bb4) |
|
|
132
|
+
| `.html`, `.htm` | Orange (#e34c26) |
|
|
133
|
+
| `.css` | Blue (#264de4) |
|
|
134
|
+
| `.json` | Gray (#a1a1aa) |
|
|
135
|
+
| `.md` | Gray (#71717a) |
|
|
136
|
+
| `.yaml`, `.yml` | Red (#cb171e) |
|
|
137
|
+
|
|
138
|
+
Override with the `icon` field on any node:
|
|
139
|
+
|
|
140
|
+
```tsx
|
|
141
|
+
{ id: "special", label: "config", icon: <GearIcon /> }
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## IDE Layout Example
|
|
145
|
+
|
|
146
|
+
Pair with `@particle-academy/fancy-code` for a full IDE:
|
|
147
|
+
|
|
148
|
+
```tsx
|
|
149
|
+
import { TreeNav } from "@particle-academy/react-fancy";
|
|
150
|
+
import { CodeEditor } from "@particle-academy/fancy-code";
|
|
151
|
+
|
|
152
|
+
<div className="flex" style={{ height: 600 }}>
|
|
153
|
+
<div className="w-56 shrink-0 overflow-y-auto border-r p-2">
|
|
154
|
+
<TreeNav
|
|
155
|
+
nodes={fileTree}
|
|
156
|
+
selectedId={activeFile}
|
|
157
|
+
onSelect={(id, node) => openFile(id, node)}
|
|
158
|
+
defaultExpandedIds={["src"]}
|
|
159
|
+
indentSize={12}
|
|
160
|
+
/>
|
|
161
|
+
</div>
|
|
162
|
+
<div className="flex-1">
|
|
163
|
+
<CodeEditor value={code} onChange={setCode} language={lang}>
|
|
164
|
+
<CodeEditor.Toolbar />
|
|
165
|
+
<CodeEditor.Panel />
|
|
166
|
+
<CodeEditor.StatusBar />
|
|
167
|
+
</CodeEditor>
|
|
168
|
+
</div>
|
|
169
|
+
</div>
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Data Attributes
|
|
173
|
+
|
|
174
|
+
| Attribute | Element |
|
|
175
|
+
|-----------|---------|
|
|
176
|
+
| `data-react-fancy-tree-nav` | Root nav element |
|
|
177
|
+
| `data-react-fancy-tree-node` | Each tree node wrapper |
|
|
178
|
+
| `data-react-fancy-tree-node-children` | Children container of an expanded node |
|
package/package.json
CHANGED