-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNodesAnimation.tsx
More file actions
157 lines (145 loc) · 4.98 KB
/
Copy pathNodesAnimation.tsx
File metadata and controls
157 lines (145 loc) · 4.98 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"use client"
import { NodeDisplay, type NodeItem } from "@/components/nodes/NodeDisplay"
import { useInView, useReducedMotion } from "motion/react"
import { useEffect, useRef } from "react"
function NodeRow({ nodes, direction, active }: { nodes: NodeItem[]; direction: "left" | "right"; active: boolean }) {
const animationRef = useRef<HTMLDivElement>(null)
const listRef = useRef<HTMLDivElement>(null)
const groupGap = 16
const velocity = 28
useEffect(() => {
const listElement = listRef.current
if (!listElement) return
const resizeObserver = new ResizeObserver((entries) => {
const entry = entries[0]
if (!entry) return
const loopDistance = Math.round(entry.contentRect.width + groupGap)
if (animationRef.current) {
animationRef.current.style.animationDuration = `${loopDistance / velocity}s`
}
})
resizeObserver.observe(listElement)
return () => resizeObserver.disconnect()
}, [])
return (
<div
ref={animationRef}
className="flex w-max items-start gap-4 will-change-transform"
style={{
animationName: direction === "left" ? "node-marquee-left" : "node-marquee-right",
animationDuration: "0s",
animationTimingFunction: "linear",
animationIterationCount: "infinite",
animationPlayState: active ? "running" : "paused",
}}
>
<div ref={listRef} className="flex items-start gap-4">
{nodes.map((node, index) => (
<NodeDisplay key={`${direction}-${node.color}-${index}`} node={node} />
))}
</div>
<div className="flex items-start gap-4" aria-hidden="true">
{nodes.map((node, index) => (
<NodeDisplay key={`${direction}-clone-${node.color}-${index}`} node={node} />
))}
</div>
</div>
)
}
const defaultNodes: NodeItem[] = [
{
color: "pink",
outline: true,
segments: [
{ type: "text", value: "Convert" },
{ type: "reference", value: "value" },
{ type: "text", value: "to boolean" },
],
},
{
color: "brand",
outline: true,
segments: [
{ type: "text", value: "Use fallback" },
{ type: "literal", value: "false" },
{ type: "text", value: "when empty" },
],
},
{
color: "yellow",
outline: true,
segments: [
{ type: "text", value: "Run node" },
{ type: "node", value: "formatDate" },
{ type: "text", value: "with current input" },
],
},
{
color: "blue",
outline: true,
segments: [{ type: "text", value: "Only continue if status equals approved" }],
},
{
color: "aqua",
outline: true,
segments: [
{ type: "text", value: "Map" },
{ type: "reference", value: "user.email" },
{ type: "text", value: "to contact field" },
],
},
{
color: "blue",
outline: true,
segments: [
{ type: "text", value: "Set timeout to" },
{ type: "literal", value: "30" },
{ type: "text", value: "seconds" },
],
},
{
color: "brand",
outline: true,
segments: [
{ type: "text", value: "Trigger" },
{ type: "node", value: "sendMail" },
{ type: "text", value: "after validation" },
],
},
{
color: "pink",
outline: true,
segments: [{ type: "text", value: "This node only contains plain text" }],
},
{
color: "yellow",
outline: true,
segments: [
{ type: "text", value: "Compare" },
{ type: "reference", value: "invoice.total" },
{ type: "text", value: "with" },
{ type: "literal", value: "1000" },
],
},
]
interface NodesAnimationProps {
nodes?: NodeItem[]
}
export function NodesAnimation({ nodes = defaultNodes }: NodesAnimationProps) {
const containerRef = useRef<HTMLDivElement>(null)
const isInView = useInView(containerRef, { amount: 0.2 })
const prefersReducedMotion = useReducedMotion()
const splitIndex = Math.ceil(nodes.length / 2)
const topRowNodes = nodes.slice(0, splitIndex)
const bottomRowNodes = nodes.slice(splitIndex)
return (
<div ref={containerRef} className="relative flex h-full cursor-default flex-col justify-center gap-3 overflow-hidden">
<div className="relative">
<NodeRow nodes={topRowNodes} direction="left" active={isInView && !prefersReducedMotion} />
</div>
<div className="relative pl-10">
<NodeRow nodes={bottomRowNodes} direction="right" active={isInView && !prefersReducedMotion} />
</div>
</div>
)
}