sample-ui-component-library 0.0.4-dev → 0.0.5-dev
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/package.json
CHANGED
|
@@ -5,6 +5,12 @@ import { setDefaultCollapsed, collapseTree, selectNode, flattenTree } from "./he
|
|
|
5
5
|
|
|
6
6
|
import { FileCode, ChevronRight, ChevronDown, Braces, FiletypeScss, FiletypeJs, FiletypePy} from "react-bootstrap-icons";
|
|
7
7
|
import PropTypes from 'prop-types';
|
|
8
|
+
import {
|
|
9
|
+
DndContext,
|
|
10
|
+
DragOverlay,
|
|
11
|
+
useDraggable,
|
|
12
|
+
useDroppable,
|
|
13
|
+
} from "@dnd-kit/core";
|
|
8
14
|
|
|
9
15
|
const INDENT_WIDTH = 20;
|
|
10
16
|
const SELECTED_FILE_COLOR = "#00426b";
|
|
@@ -27,7 +33,8 @@ function useEvent(fn) {
|
|
|
27
33
|
/**
|
|
28
34
|
* Renders a single node in the file tree.
|
|
29
35
|
*/
|
|
30
|
-
const TreeNode = ({node, onRowClick}) => {
|
|
36
|
+
const TreeNode = ({id, node, onRowClick}) => {
|
|
37
|
+
const { attributes, listeners, setNodeRef, transform } = useDraggable({id});
|
|
31
38
|
/**
|
|
32
39
|
* Gets the appropriate icon for the node based on its type and collapsed state.
|
|
33
40
|
* @returns <JSX>
|
|
@@ -72,7 +79,7 @@ const TreeNode = ({node, onRowClick}) => {
|
|
|
72
79
|
}
|
|
73
80
|
|
|
74
81
|
return (
|
|
75
|
-
<div className="file-node-row" style={getRowStyle()} onClick={() => onRowClick(node)}>
|
|
82
|
+
<div className="file-node-row" ref={setNodeRef} {...listeners} {...attributes} style={getRowStyle()} onClick={() => onRowClick(node)}>
|
|
76
83
|
<div className="indent" style={{ width: node.level * INDENT_WIDTH + "px"}} />
|
|
77
84
|
{
|
|
78
85
|
node.type === "folder" ?
|
|
@@ -115,7 +122,7 @@ export const FileBrowser = ({tree, onNodeSelect}) => {
|
|
|
115
122
|
const nodes = collapseTree(treeRef.current);
|
|
116
123
|
const rows = [];
|
|
117
124
|
nodes.forEach((node) => {
|
|
118
|
-
rows.push(<TreeNode key={node.id} node={node} onRowClick={handleFileClick}/>);
|
|
125
|
+
rows.push(<TreeNode key={node.id} node={node} id={node.name} onRowClick={handleFileClick}/>);
|
|
119
126
|
});
|
|
120
127
|
setNodes(rows);
|
|
121
128
|
}
|
|
@@ -2,6 +2,15 @@ import { useEffect } from "react";
|
|
|
2
2
|
import { FileBrowser } from "../components/FileBrowser";
|
|
3
3
|
import { useArgs } from "@storybook/preview-api";
|
|
4
4
|
import { action } from "@storybook/addon-actions";
|
|
5
|
+
import {
|
|
6
|
+
DndContext,
|
|
7
|
+
DragOverlay,
|
|
8
|
+
useDraggable,
|
|
9
|
+
useDroppable,
|
|
10
|
+
PointerSensor,
|
|
11
|
+
useSensor,
|
|
12
|
+
useSensors
|
|
13
|
+
} from "@dnd-kit/core";
|
|
5
14
|
|
|
6
15
|
import FileTree1 from "./data/FileBrowser/Tree1.json"
|
|
7
16
|
import FileTree2 from "./data/FileBrowser/Tree2.json"
|
|
@@ -14,6 +23,38 @@ export default {
|
|
|
14
23
|
argTypes: {}
|
|
15
24
|
};
|
|
16
25
|
|
|
26
|
+
/**
|
|
27
|
+
* Preview for the div being dragged.
|
|
28
|
+
* @returns
|
|
29
|
+
*/
|
|
30
|
+
function DragPreview({ label }) {
|
|
31
|
+
return (
|
|
32
|
+
<div
|
|
33
|
+
style={{
|
|
34
|
+
padding: "6px 12px",
|
|
35
|
+
background: "#2d2d2d",
|
|
36
|
+
color: "white",
|
|
37
|
+
boxShadow: "0 4px 12px rgba(0,0,0,0.3)",
|
|
38
|
+
opacity:0.3
|
|
39
|
+
}}
|
|
40
|
+
>
|
|
41
|
+
{label}
|
|
42
|
+
</div>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Offset for the drag overlay.
|
|
48
|
+
* @returns
|
|
49
|
+
*/
|
|
50
|
+
const offsetOverlay = ({ transform }) => {
|
|
51
|
+
return {
|
|
52
|
+
...transform,
|
|
53
|
+
x: transform.x + 20,
|
|
54
|
+
y: transform.y + 20
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
|
|
17
58
|
|
|
18
59
|
const Template = (args) => {
|
|
19
60
|
const [, updateArgs] = useArgs();
|
|
@@ -26,12 +67,50 @@ const Template = (args) => {
|
|
|
26
67
|
updateArgs({onNodeSelect : onNodeSelect});
|
|
27
68
|
}, []);
|
|
28
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Callback for when drag ends.
|
|
72
|
+
*/
|
|
73
|
+
const handleDragEnd = (event) =>{
|
|
74
|
+
const { active, over } = event;
|
|
75
|
+
console.log("Drag Ended");
|
|
76
|
+
const rect = event.activatorEvent;
|
|
77
|
+
|
|
78
|
+
console.log(over );
|
|
79
|
+
|
|
80
|
+
if (over) {
|
|
81
|
+
console.log("Dragged item:", active.id);
|
|
82
|
+
console.log("Dropped on:", over.id);
|
|
83
|
+
} else {
|
|
84
|
+
console.log("Dropped outside any droppable");
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Callback for when drag is started.
|
|
90
|
+
*/
|
|
91
|
+
const onDragStart = (event) => {
|
|
92
|
+
console.log("Drag Started");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const sensors = useSensors(
|
|
96
|
+
useSensor(PointerSensor, {
|
|
97
|
+
activationConstraint: {
|
|
98
|
+
distance: 8
|
|
99
|
+
}
|
|
100
|
+
})
|
|
101
|
+
);
|
|
102
|
+
|
|
29
103
|
return (
|
|
30
|
-
<
|
|
31
|
-
<div className="
|
|
32
|
-
<
|
|
104
|
+
<DndContext sensors={sensors} onDragStart={onDragStart} onDragEnd={handleDragEnd}>
|
|
105
|
+
<div className="viewerStoryWrapper">
|
|
106
|
+
<div className="file-browser">
|
|
107
|
+
<FileBrowser {...args} />
|
|
108
|
+
</div>
|
|
33
109
|
</div>
|
|
34
|
-
|
|
110
|
+
<DragOverlay modifiers={[offsetOverlay]} dropAnimation={null}>
|
|
111
|
+
<DragPreview label={"preview"}/>
|
|
112
|
+
</DragOverlay>
|
|
113
|
+
</DndContext>
|
|
35
114
|
)
|
|
36
115
|
}
|
|
37
116
|
|