@headless-tree/react 1.1.0 → 1.2.1
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/CHANGELOG.md +18 -0
- package/lib/cjs/assistive-tree-description.js +8 -6
- package/lib/cjs/use-tree.js +10 -5
- package/lib/esm/assistive-tree-description.js +8 -6
- package/lib/esm/use-tree.js +11 -6
- package/package.json +7 -1
- package/readme.md +157 -0
- package/src/assistive-tree-description.tsx +6 -5
- package/src/use-tree.tsx +20 -14
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @headless-tree/react
|
|
2
2
|
|
|
3
|
+
## 1.2.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- d925607: fixed `<AssistiveTreeDescription />` component throwing an error if the dnd feature is not included (#126)
|
|
8
|
+
|
|
9
|
+
## 1.2.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- 647a072: Fixed incorrect package.json exports configurations to proper ESM and CJS exports (#104)
|
|
14
|
+
- 349d36e: change package.json["module"] to commonjs to fix inconsistent package definitiuons (#104)
|
|
15
|
+
- e2faf37: Fixed an issue async data loaders that resolve data before the tree is mounted can cause the tree to not render at all
|
|
16
|
+
|
|
17
|
+
Note: When using the `createTree()` API directly instead of going through the React `useTree` API, an additional call
|
|
18
|
+
to `tree.rebuildItems()` afterwards will be necessary. This change is marked as minor release regardless, since `createTree` is
|
|
19
|
+
currently not a publically documented feature.
|
|
20
|
+
|
|
3
21
|
## 1.1.0
|
|
4
22
|
|
|
5
23
|
## 1.0.1
|
|
@@ -27,15 +27,17 @@ const styles = {
|
|
|
27
27
|
clip: "rect(0 0 0 0)",
|
|
28
28
|
};
|
|
29
29
|
const getDefaultLabel = (dnd, assistiveState, hotkeys) => {
|
|
30
|
-
var _a, _b;
|
|
30
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
31
|
+
if (!hotkeys.startDrag)
|
|
32
|
+
return ""; // No hotkey feature configured
|
|
31
33
|
const itemNames = (_b = (_a = dnd === null || dnd === void 0 ? void 0 : dnd.draggedItems) === null || _a === void 0 ? void 0 : _a.map((item) => item.getItemName()).join(", ")) !== null && _b !== void 0 ? _b : "";
|
|
32
34
|
const position = !(dnd === null || dnd === void 0 ? void 0 : dnd.dragTarget)
|
|
33
35
|
? "None"
|
|
34
36
|
: "childIndex" in dnd.dragTarget
|
|
35
37
|
? `${dnd.dragTarget.childIndex} of ${dnd.dragTarget.item.getChildren().length} in ${dnd.dragTarget.item.getItemName()}`
|
|
36
38
|
: `in ${dnd.dragTarget.item.getItemName()}`;
|
|
37
|
-
const navGuide = `Press ${hotkeys.dragUp.hotkey} and ${hotkeys.dragDown.hotkey} to move up or down, ` +
|
|
38
|
-
`${hotkeys.completeDrag.hotkey} to drop, ${hotkeys.cancelDrag.hotkey} to abort.`;
|
|
39
|
+
const navGuide = `Press ${(_c = hotkeys.dragUp) === null || _c === void 0 ? void 0 : _c.hotkey} and ${(_d = hotkeys.dragDown) === null || _d === void 0 ? void 0 : _d.hotkey} to move up or down, ` +
|
|
40
|
+
`${(_e = hotkeys.completeDrag) === null || _e === void 0 ? void 0 : _e.hotkey} to drop, ${(_f = hotkeys.cancelDrag) === null || _f === void 0 ? void 0 : _f.hotkey} to abort.`;
|
|
39
41
|
switch (assistiveState) {
|
|
40
42
|
case core_1.AssistiveDndState.Started:
|
|
41
43
|
return itemNames
|
|
@@ -44,12 +46,12 @@ const getDefaultLabel = (dnd, assistiveState, hotkeys) => {
|
|
|
44
46
|
case core_1.AssistiveDndState.Dragging:
|
|
45
47
|
return itemNames ? `${itemNames}, ${position}` : position;
|
|
46
48
|
case core_1.AssistiveDndState.Completed:
|
|
47
|
-
return `Drag completed. Press ${hotkeys.startDrag.hotkey} to move selected items`;
|
|
49
|
+
return `Drag completed. Press ${(_g = hotkeys.startDrag) === null || _g === void 0 ? void 0 : _g.hotkey} to move selected items`;
|
|
48
50
|
case core_1.AssistiveDndState.Aborted:
|
|
49
|
-
return `Drag cancelled. Press ${hotkeys.startDrag.hotkey} to move selected items`;
|
|
51
|
+
return `Drag cancelled. Press ${(_h = hotkeys.startDrag) === null || _h === void 0 ? void 0 : _h.hotkey} to move selected items`;
|
|
50
52
|
case core_1.AssistiveDndState.None:
|
|
51
53
|
default:
|
|
52
|
-
return `Press ${hotkeys.startDrag.hotkey} to move selected items`;
|
|
54
|
+
return `Press ${(_j = hotkeys.startDrag) === null || _j === void 0 ? void 0 : _j.hotkey} to move selected items`;
|
|
53
55
|
}
|
|
54
56
|
};
|
|
55
57
|
const AssistiveTreeDescription = (_a) => {
|
package/lib/cjs/use-tree.js
CHANGED
|
@@ -6,11 +6,16 @@ const core_1 = require("@headless-tree/core");
|
|
|
6
6
|
const useTree = (config) => {
|
|
7
7
|
const [tree] = (0, react_1.useState)(() => ({ current: (0, core_1.createTree)(config) }));
|
|
8
8
|
const [state, setState] = (0, react_1.useState)(() => tree.current.getState());
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
} })))
|
|
9
|
+
(0, react_1.useEffect)(() => {
|
|
10
|
+
tree.current.rebuildTree();
|
|
11
|
+
}, [tree]); // runs only once after mount
|
|
12
|
+
(0, react_1.useInsertionEffect)(() => {
|
|
13
|
+
tree.current.setConfig((prev) => (Object.assign(Object.assign(Object.assign({}, prev), config), { state: Object.assign(Object.assign({}, state), config.state), setState: (state) => {
|
|
14
|
+
var _a;
|
|
15
|
+
setState(state);
|
|
16
|
+
(_a = config.setState) === null || _a === void 0 ? void 0 : _a.call(config, state);
|
|
17
|
+
} })));
|
|
18
|
+
});
|
|
14
19
|
return tree.current;
|
|
15
20
|
};
|
|
16
21
|
exports.useTree = useTree;
|
|
@@ -21,15 +21,17 @@ const styles = {
|
|
|
21
21
|
clip: "rect(0 0 0 0)",
|
|
22
22
|
};
|
|
23
23
|
const getDefaultLabel = (dnd, assistiveState, hotkeys) => {
|
|
24
|
-
var _a, _b;
|
|
24
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
25
|
+
if (!hotkeys.startDrag)
|
|
26
|
+
return ""; // No hotkey feature configured
|
|
25
27
|
const itemNames = (_b = (_a = dnd === null || dnd === void 0 ? void 0 : dnd.draggedItems) === null || _a === void 0 ? void 0 : _a.map((item) => item.getItemName()).join(", ")) !== null && _b !== void 0 ? _b : "";
|
|
26
28
|
const position = !(dnd === null || dnd === void 0 ? void 0 : dnd.dragTarget)
|
|
27
29
|
? "None"
|
|
28
30
|
: "childIndex" in dnd.dragTarget
|
|
29
31
|
? `${dnd.dragTarget.childIndex} of ${dnd.dragTarget.item.getChildren().length} in ${dnd.dragTarget.item.getItemName()}`
|
|
30
32
|
: `in ${dnd.dragTarget.item.getItemName()}`;
|
|
31
|
-
const navGuide = `Press ${hotkeys.dragUp.hotkey} and ${hotkeys.dragDown.hotkey} to move up or down, ` +
|
|
32
|
-
`${hotkeys.completeDrag.hotkey} to drop, ${hotkeys.cancelDrag.hotkey} to abort.`;
|
|
33
|
+
const navGuide = `Press ${(_c = hotkeys.dragUp) === null || _c === void 0 ? void 0 : _c.hotkey} and ${(_d = hotkeys.dragDown) === null || _d === void 0 ? void 0 : _d.hotkey} to move up or down, ` +
|
|
34
|
+
`${(_e = hotkeys.completeDrag) === null || _e === void 0 ? void 0 : _e.hotkey} to drop, ${(_f = hotkeys.cancelDrag) === null || _f === void 0 ? void 0 : _f.hotkey} to abort.`;
|
|
33
35
|
switch (assistiveState) {
|
|
34
36
|
case AssistiveDndState.Started:
|
|
35
37
|
return itemNames
|
|
@@ -38,12 +40,12 @@ const getDefaultLabel = (dnd, assistiveState, hotkeys) => {
|
|
|
38
40
|
case AssistiveDndState.Dragging:
|
|
39
41
|
return itemNames ? `${itemNames}, ${position}` : position;
|
|
40
42
|
case AssistiveDndState.Completed:
|
|
41
|
-
return `Drag completed. Press ${hotkeys.startDrag.hotkey} to move selected items`;
|
|
43
|
+
return `Drag completed. Press ${(_g = hotkeys.startDrag) === null || _g === void 0 ? void 0 : _g.hotkey} to move selected items`;
|
|
42
44
|
case AssistiveDndState.Aborted:
|
|
43
|
-
return `Drag cancelled. Press ${hotkeys.startDrag.hotkey} to move selected items`;
|
|
45
|
+
return `Drag cancelled. Press ${(_h = hotkeys.startDrag) === null || _h === void 0 ? void 0 : _h.hotkey} to move selected items`;
|
|
44
46
|
case AssistiveDndState.None:
|
|
45
47
|
default:
|
|
46
|
-
return `Press ${hotkeys.startDrag.hotkey} to move selected items`;
|
|
48
|
+
return `Press ${(_j = hotkeys.startDrag) === null || _j === void 0 ? void 0 : _j.hotkey} to move selected items`;
|
|
47
49
|
}
|
|
48
50
|
};
|
|
49
51
|
export const AssistiveTreeDescription = (_a) => {
|
package/lib/esm/use-tree.js
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
import { useState } from "react";
|
|
1
|
+
import { useEffect, useInsertionEffect, useState } from "react";
|
|
2
2
|
import { createTree } from "@headless-tree/core";
|
|
3
3
|
export const useTree = (config) => {
|
|
4
4
|
const [tree] = useState(() => ({ current: createTree(config) }));
|
|
5
5
|
const [state, setState] = useState(() => tree.current.getState());
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} })))
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
tree.current.rebuildTree();
|
|
8
|
+
}, [tree]); // runs only once after mount
|
|
9
|
+
useInsertionEffect(() => {
|
|
10
|
+
tree.current.setConfig((prev) => (Object.assign(Object.assign(Object.assign({}, prev), config), { state: Object.assign(Object.assign({}, state), config.state), setState: (state) => {
|
|
11
|
+
var _a;
|
|
12
|
+
setState(state);
|
|
13
|
+
(_a = config.setState) === null || _a === void 0 ? void 0 : _a.call(config, state);
|
|
14
|
+
} })));
|
|
15
|
+
});
|
|
11
16
|
return tree.current;
|
|
12
17
|
};
|
package/package.json
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@headless-tree/react",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"main": "lib/cjs/index.js",
|
|
5
5
|
"module": "lib/esm/index.js",
|
|
6
6
|
"types": "lib/esm/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
"types": "./lib/esm/index.d.ts",
|
|
9
|
+
"import": "./lib/esm/index.js",
|
|
10
|
+
"default": "./lib/cjs/index.js"
|
|
11
|
+
},
|
|
7
12
|
"sideEffects": false,
|
|
8
13
|
"scripts": {
|
|
9
14
|
"build:cjs": "tsc -m commonjs --outDir lib/cjs",
|
|
@@ -16,6 +21,7 @@
|
|
|
16
21
|
"directory": "packages/react"
|
|
17
22
|
},
|
|
18
23
|
"author": "Lukas Bach <npm@lukasbach.com>",
|
|
24
|
+
"funding": "https://github.com/sponsors/lukasbach",
|
|
19
25
|
"license": "MIT",
|
|
20
26
|
"devDependencies": {
|
|
21
27
|
"@babel/preset-env": "^7.24.0",
|
package/readme.md
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
[](https://headless-tree.lukasbach.com/)
|
|
4
|
+
[](https://discord.gg/KuZ6EezzVw)
|
|
5
|
+
[](https://bsky.app/profile/lukasbach.bsky.social)
|
|
6
|
+
[](https://github.com/sponsors/lukasbach)
|
|
7
|
+
[](https://github.com/lukasbach)
|
|
8
|
+
[](https://www.npmjs.com/package/@headless-tree/core)
|
|
9
|
+
[](https://www.npmjs.com/package/@headless-tree/react)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
Super-easy integration of complex tree components into React. Supports ordered
|
|
13
|
+
and unordered drag-and-drop, extensive keybindings, search, renaming and more.
|
|
14
|
+
Fully customizable and accessible. Headless Tree is the official successor for
|
|
15
|
+
[react-complex-tree](https://github.com/lukasbach/react-complex-tree).
|
|
16
|
+
|
|
17
|
+
It aims to bring the many features of complex tree views, like multi-select,
|
|
18
|
+
drag-and-drop, keyboard navigation, tree search, renaming and more, while
|
|
19
|
+
being unopinionated about the styling and rendering of the tree itself.
|
|
20
|
+
Accessibility is ensured by default, and the integration is extremely
|
|
21
|
+
simple and flexible.
|
|
22
|
+
|
|
23
|
+
The interface gives you a flat list of tree nodes
|
|
24
|
+
that you can easily render yourself, which keeps the complexity of the
|
|
25
|
+
code low and allows you to customize the tree to your needs. This flat
|
|
26
|
+
structure also allows you to virtualize the tree with any virtualization
|
|
27
|
+
library you want. The library automatically provides the necessary
|
|
28
|
+
aria tags to emulate a nested tree structure, so that accessibility
|
|
29
|
+
requirements are met despite the flat structure.
|
|
30
|
+
|
|
31
|
+
Dive into [the Get Started page](https://headless-tree.lukasbach.com/getstarted)
|
|
32
|
+
to find out how to use Headless Tree, or have a look at
|
|
33
|
+
[the samples on the Headless Tree Homepage](https://headless-tree.lukasbach.com/#demogrid)
|
|
34
|
+
to get an idea of what you can do with it.
|
|
35
|
+
|
|
36
|
+
> [!TIP]
|
|
37
|
+
> Headless Tree is now available as Beta! The library is mostly stable and
|
|
38
|
+
> production ready, and will be generally released within two months, once
|
|
39
|
+
> I have collected feedback and fixed any bugs that might arise. I've written
|
|
40
|
+
> [a blog post](https://medium.com/@lukasbach/headless-tree-and-the-future-of-react-complex-tree-fc920700e82a)
|
|
41
|
+
> about the details of the change, and the future of the library.
|
|
42
|
+
>
|
|
43
|
+
> Join
|
|
44
|
+
> [the Discord](https://discord.gg/KuZ6EezzVw) to get involved, and
|
|
45
|
+
> [follow on Bluesky](https://bsky.app/profile/lukasbach.bsky.social) to
|
|
46
|
+
> stay up to date.
|
|
47
|
+
|
|
48
|
+
## Features
|
|
49
|
+
|
|
50
|
+
- [Simple Interface](https://headless-tree.lukasbach.com/?demo=0#demogrid): Easy integration in React with full customizability of DOM
|
|
51
|
+
- [Drag and Drop](https://headless-tree.lukasbach.com/?demo=1#demogrid): Powerful ordered drag-and-drop, that can interact with external drag events
|
|
52
|
+
- [Scalable](https://headless-tree.lukasbach.com/?demo=2#demogrid): Headless Tree remains performant even with large trees
|
|
53
|
+
- [Virtualization Support](https://headless-tree.lukasbach.com/?demo=3#demogrid): Compatible with common virtualization library to support 100k+ items
|
|
54
|
+
- [Hotkeys!](https://headless-tree.lukasbach.com/?demo=4#demogrid): Lots of hotkeys, fully customizable
|
|
55
|
+
- [Search Support](https://headless-tree.lukasbach.com/?demo=5#demogrid): Typeahead anywhere in the tree to quickly search the entire tree
|
|
56
|
+
- [Rename items](https://headless-tree.lukasbach.com/?demo=6#demogrid): Optionally allow users to rename items inside the tree
|
|
57
|
+
- [Manage State](https://headless-tree.lukasbach.com/?demo=7#demogrid): Let Headless Tree manage tree state internally, or manage any part of it yourself
|
|
58
|
+
- [Customize Behavior](https://headless-tree.lukasbach.com/?demo=8#demogrid): Easily overwrite internal behavior like requiring double clicks on items to expand
|
|
59
|
+
- [Customize Logic](https://headless-tree.lukasbach.com/?demo=9#demogrid): Overwrite or expand any internal behavior of Headless Tree
|
|
60
|
+
- [Async Data Support](https://headless-tree.lukasbach.com/?demo=10#demogrid): Use synchronous or asynchronous data sources for your tree. Headless Tree comes with optional caching for async data
|
|
61
|
+
- Free of dependencies
|
|
62
|
+
- Or check out [this comprehensive playground](https://headless-tree.lukasbach.com/?demo=11#demogrid) that has most of the capabilities enabled.
|
|
63
|
+
|
|
64
|
+
## Bundle Size
|
|
65
|
+
|
|
66
|
+
Headless Tree exports individual features in a tree-shaking-friendly
|
|
67
|
+
way, allowing you to only include what you need to keep your bundle size
|
|
68
|
+
small. Listed bundle sizes are based on min+gzipped bundles, and are
|
|
69
|
+
based on the Bundlephobia report as of Headless Tree v0.0.15.
|
|
70
|
+
|
|
71
|
+
| Feature | Bundle Size |
|
|
72
|
+
|------------------------|-------------|
|
|
73
|
+
| Tree Core | 3.1kB |
|
|
74
|
+
| Sync Data Loader | 0.8kB |
|
|
75
|
+
| Async Data Loader | 1.4kB |
|
|
76
|
+
| Selections | 1.1kB |
|
|
77
|
+
| Drag and Drop | 2.8kB |
|
|
78
|
+
| Keyboard Drag and Drop | 2.7kB |
|
|
79
|
+
| Hotkeys | 0.8kB |
|
|
80
|
+
| Tree Search | 1.3kB |
|
|
81
|
+
| Renaming | 0.9kB |
|
|
82
|
+
| Expand All | 0.7kB |
|
|
83
|
+
| React Bindings | 0.4kB |
|
|
84
|
+
|
|
85
|
+
Total bundle size is 9.5kB plus 0.4kB for the React bindings. Note that
|
|
86
|
+
the sum of features is bigger than the total bundle size, because several
|
|
87
|
+
features share code. Tree-shaking will ensure that the minimum amount of
|
|
88
|
+
code is included in your bundle.
|
|
89
|
+
|
|
90
|
+
## Get Started
|
|
91
|
+
|
|
92
|
+
> [!TIP]
|
|
93
|
+
> You can find a comprehensive [get-started guide](https://headless-tree.lukasbach.com/getstarted)
|
|
94
|
+
> on the documentation homepage. The following gives a brief overview.
|
|
95
|
+
|
|
96
|
+
Install Headless Tree via npm:
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
npm install @headless-tree/core @headless-tree/react
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
In your react component, call the `useTree` hook from `@headless-tree/react` with the configuration of
|
|
103
|
+
your tree:
|
|
104
|
+
|
|
105
|
+
```tsx
|
|
106
|
+
import {
|
|
107
|
+
hotkeysCoreFeature,
|
|
108
|
+
selectionFeature,
|
|
109
|
+
syncDataLoaderFeature,
|
|
110
|
+
} from "@headless-tree/core";
|
|
111
|
+
import { useTree } from "@headless-tree/react";
|
|
112
|
+
|
|
113
|
+
const tree = useTree<string>({
|
|
114
|
+
initialState: { expandedItems: ["folder-1"] },
|
|
115
|
+
rootItemId: "folder",
|
|
116
|
+
getItemName: (item) => item.getItemData(),
|
|
117
|
+
isItemFolder: (item) => !item.getItemData().endsWith("item"),
|
|
118
|
+
dataLoader: {
|
|
119
|
+
getItem: (itemId) => itemId,
|
|
120
|
+
getChildren: (itemId) => [
|
|
121
|
+
`${itemId}-folder`,
|
|
122
|
+
`${itemId}-1-item`,
|
|
123
|
+
`${itemId}-2-item`,
|
|
124
|
+
],
|
|
125
|
+
},
|
|
126
|
+
indent: 20,
|
|
127
|
+
features: [syncDataLoaderFeature, selectionFeature, hotkeysCoreFeature],
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Then, render your tree based on the tree instance returned from the hook:
|
|
132
|
+
|
|
133
|
+
```tsx
|
|
134
|
+
<div {...tree.getContainerProps()} className="tree">
|
|
135
|
+
{tree.getItems().map((item) => (
|
|
136
|
+
<button
|
|
137
|
+
{...item.getProps()}
|
|
138
|
+
key={item.getId()}
|
|
139
|
+
style={{ paddingLeft: `${item.getItemMeta().level * 20}px` }}
|
|
140
|
+
>
|
|
141
|
+
<div
|
|
142
|
+
className={cn("treeitem", {
|
|
143
|
+
focused: item.isFocused(),
|
|
144
|
+
expanded: item.isExpanded(),
|
|
145
|
+
selected: item.isSelected(),
|
|
146
|
+
folder: item.isFolder(),
|
|
147
|
+
})}
|
|
148
|
+
>
|
|
149
|
+
{item.getItemName()}
|
|
150
|
+
</div>
|
|
151
|
+
</button>
|
|
152
|
+
))}
|
|
153
|
+
</div>
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Read on in the [get started guide](https://headless-tree.lukasbach.com/getstarted) to learn more about
|
|
157
|
+
how to use Headless Tree, and how to customize it to your needs.
|
|
@@ -22,6 +22,7 @@ const getDefaultLabel = <T,>(
|
|
|
22
22
|
assistiveState: AssistiveDndState,
|
|
23
23
|
hotkeys: HotkeysConfig<T>,
|
|
24
24
|
) => {
|
|
25
|
+
if (!hotkeys.startDrag) return ""; // No hotkey feature configured
|
|
25
26
|
const itemNames =
|
|
26
27
|
dnd?.draggedItems?.map((item) => item.getItemName()).join(", ") ?? "";
|
|
27
28
|
const position = !dnd?.dragTarget
|
|
@@ -30,8 +31,8 @@ const getDefaultLabel = <T,>(
|
|
|
30
31
|
? `${dnd.dragTarget.childIndex} of ${dnd.dragTarget.item.getChildren().length} in ${dnd.dragTarget.item.getItemName()}`
|
|
31
32
|
: `in ${dnd.dragTarget.item.getItemName()}`;
|
|
32
33
|
const navGuide =
|
|
33
|
-
`Press ${hotkeys.dragUp
|
|
34
|
-
`${hotkeys.completeDrag
|
|
34
|
+
`Press ${hotkeys.dragUp?.hotkey} and ${hotkeys.dragDown?.hotkey} to move up or down, ` +
|
|
35
|
+
`${hotkeys.completeDrag?.hotkey} to drop, ${hotkeys.cancelDrag?.hotkey} to abort.`;
|
|
35
36
|
switch (assistiveState) {
|
|
36
37
|
case AssistiveDndState.Started:
|
|
37
38
|
return itemNames
|
|
@@ -40,12 +41,12 @@ const getDefaultLabel = <T,>(
|
|
|
40
41
|
case AssistiveDndState.Dragging:
|
|
41
42
|
return itemNames ? `${itemNames}, ${position}` : position;
|
|
42
43
|
case AssistiveDndState.Completed:
|
|
43
|
-
return `Drag completed. Press ${hotkeys.startDrag
|
|
44
|
+
return `Drag completed. Press ${hotkeys.startDrag?.hotkey} to move selected items`;
|
|
44
45
|
case AssistiveDndState.Aborted:
|
|
45
|
-
return `Drag cancelled. Press ${hotkeys.startDrag
|
|
46
|
+
return `Drag cancelled. Press ${hotkeys.startDrag?.hotkey} to move selected items`;
|
|
46
47
|
case AssistiveDndState.None:
|
|
47
48
|
default:
|
|
48
|
-
return `Press ${hotkeys.startDrag
|
|
49
|
+
return `Press ${hotkeys.startDrag?.hotkey} to move selected items`;
|
|
49
50
|
}
|
|
50
51
|
};
|
|
51
52
|
|
package/src/use-tree.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useState } from "react";
|
|
1
|
+
import { useEffect, useInsertionEffect, useState } from "react";
|
|
2
2
|
import { TreeConfig, TreeState, createTree } from "@headless-tree/core";
|
|
3
3
|
|
|
4
4
|
export const useTree = <T,>(config: TreeConfig<T>) => {
|
|
@@ -7,19 +7,25 @@ export const useTree = <T,>(config: TreeConfig<T>) => {
|
|
|
7
7
|
tree.current.getState(),
|
|
8
8
|
);
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
...
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
tree.current.rebuildTree();
|
|
12
|
+
}, [tree]); // runs only once after mount
|
|
13
|
+
|
|
14
|
+
useInsertionEffect(() => {
|
|
15
|
+
tree.current.setConfig((prev) => ({
|
|
16
|
+
...prev,
|
|
17
|
+
...config,
|
|
18
|
+
state: {
|
|
19
|
+
// ...prev.state,
|
|
20
|
+
...state,
|
|
21
|
+
...config.state,
|
|
22
|
+
},
|
|
23
|
+
setState: (state) => {
|
|
24
|
+
setState(state);
|
|
25
|
+
config.setState?.(state);
|
|
26
|
+
},
|
|
27
|
+
}));
|
|
28
|
+
});
|
|
23
29
|
|
|
24
30
|
return tree.current;
|
|
25
31
|
};
|