-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNodes.tsx
89 lines (83 loc) · 2.79 KB
/
Nodes.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import React from "react"
import { useTransition, animated } from "react-spring"
import Node from "./Node"
import { NodeType } from "."
import { getTopLeft, findCollapsedParent, getX0, getY0 } from "@utils"
type NodesProps = {
nodes: NodeType[]
onNodeClick: (node: NodeType) => void
handleMouseOver: (node: NodeType) => (e: React.MouseEvent) => void
hideTooltip: () => void,
}
const transformString = ({ top, left }: { top: number; left: number }) =>
`translate(${left}, ${top})`
export default function Nodes(props: NodesProps) {
const { nodes, onNodeClick, handleMouseOver, hideTooltip } = props
const transitions = useTransition(nodes, {
//const transitions = useTransition(nodes, node => node.data.attributes.id, {
key: node => node.data.attributes.id,
from: node => {
const parentTopLeft = node.parent
? getTopLeft(node.parent)
: { top: 0, left: 0 }
return {
opacity: 0,
transform: transformString(parentTopLeft),
}
},
enter: node => {
const topLeft = getTopLeft(node)
return {
opacity: 1,
transform: transformString(topLeft),
}
},
update: node => {
const topLeft = getTopLeft(node)
return {
opacity: 1,
transform: transformString(topLeft),
}
},
leave: node => {
let collapsedParentPosition = {
top: 0,
left: 0,
}
if (node.parent) {
const collapsedParent = findCollapsedParent(node.parent)
collapsedParentPosition = {
top: collapsedParent
? getX0(collapsedParent)
: node.parent.x,
left: collapsedParent
? getY0(collapsedParent)
: node.parent.y,
}
}
return {
opacity: 0,
transform: transformString(collapsedParentPosition),
}
},
})
return (
<>
{transitions((style, item) => (
<animated.g
opacity={style.opacity}
transform={style.transform}
onClick={() => onNodeClick(item)}
style={{
cursor: "pointer",
pointerEvents: style.opacity.to(v => v < 0.5 ? "none" : "all")
}}
onMouseOver={handleMouseOver(item)}
onMouseOut={hideTooltip}
>
<Node node={item} />
</animated.g>
))}
</>
)
}