-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnimatedDataset.js
More file actions
164 lines (152 loc) · 5.05 KB
/
AnimatedDataset.js
File metadata and controls
164 lines (152 loc) · 5.05 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
158
159
160
161
162
163
164
import React from 'react'
import { select } from 'd3-selection'
import 'd3-transition'
import { easeCubic } from 'd3-ease'
import { mapKeys } from './mapKey'
import { parseAttributeName, parseEventName } from './parse'
export function AnimatedDataset({
dataset,
attrs: unparsedAttrs,
tag = 'rect',
init: unparsedInit = {},
events: unparsedEvents = {},
keyFn = d => d.key,
duration = 1000,
delay = 0,
disableAnimation = false,
durationByAttr = {},
delayByAttr = {},
easingByAttr = {},
easing = easeCubic,
children,
}) {
const ref = React.createRef()
const refOldAttrs = React.useRef()
React.useLayoutEffect(() => {
if (!ref.current) return
const attrs = mapKeys(unparsedAttrs, parseAttributeName)
const init = mapKeys(unparsedInit, parseAttributeName)
const events = mapKeys(unparsedEvents, parseEventName)
const durationByAttrParsed = mapKeys(durationByAttr, parseAttributeName)
const delayByAttrParsed = mapKeys(delayByAttr, parseAttributeName)
const easingByAttrParsed = mapKeys(easingByAttr, parseAttributeName)
const attrsList = Object.keys(attrs).filter(a => a !== 'text')
const eventsList = Object.keys(events)
const oldAttrs = refOldAttrs.current || {}
const animate = () => {
select(ref.current)
.selectAll(tag)
.data(dataset, function (d) { return (d && keyFn(d)) || select(this).attr("data-key"); })
.join(
enter => {
const enterEls = enter
.append(tag)
.call(sel => {
attrsList.forEach(a => {
sel.attr(
a,
init.hasOwnProperty(a)
? init[a]
: oldAttrs.hasOwnProperty(a)
? oldAttrs[a]
: attrs[a]
)
})
})
.call(sel => {
eventsList.forEach(event => {
sel.on(event, events[event])
})
})
.call(sel => {
attrsList.forEach(a => {
const tran = disableAnimation
? sel
: sel
.transition(a)
.ease(easingByAttrParsed.hasOwnProperty(a) ? easingByAttrParsed[a] : easing)
.delay(delayByAttrParsed.hasOwnProperty(a) ? delayByAttrParsed[a] : delay)
.duration(
durationByAttrParsed.hasOwnProperty(a)
? durationByAttrParsed[a]
: duration
)
tran.attr(a, attrs[a])
})
})
if (attrs.text) enterEls.text(attrs.text)
return enterEls
},
update => {
const updateEls = update
.call(sel => {
attrsList.forEach(a => {
const tran = disableAnimation
? sel
: sel
.transition(a)
.ease(easingByAttrParsed.hasOwnProperty(a) ? easingByAttrParsed[a] : easing)
.delay(delayByAttrParsed.hasOwnProperty(a) ? delayByAttrParsed[a] : delay)
.duration(
durationByAttrParsed.hasOwnProperty(a) ? durationByAttrParsed[a] : duration
)
tran.attr(a, attrs[a])
}
)
})
if (attrs.text) updateEls.text(attrs.text)
return updateEls;
},
exit =>
exit.call(sel => {
attrsList.forEach(a => {
const tran = disableAnimation
? sel
: sel
.transition(a)
.ease(easingByAttrParsed.hasOwnProperty(a) ? easingByAttrParsed[a] : easing)
.delay(delayByAttrParsed.hasOwnProperty(a) ? delayByAttrParsed[a] : delay)
.duration(
durationByAttrParsed.hasOwnProperty(a) ? durationByAttrParsed[a] : duration
)
tran.attr(a, init.hasOwnProperty(a) ? init[a] : attrs[a]).remove()
})
})
)
refOldAttrs.current = attrs
}
if (disableAnimation) {
animate()
} else {
requestAnimationFrame(animate)
}
}, [
dataset,
unparsedInit,
keyFn,
ref,
tag,
unparsedAttrs,
duration,
disableAnimation,
unparsedEvents,
delay,
easing,
durationByAttr,
delayByAttr,
easingByAttr,
])
return React.createElement(
'g',
{ ref },
// Create the structure first so that react can add the children
children && dataset.map(
(data) => React.createElement(
tag,
{ key: keyFn(data), 'data-key': keyFn(data) },
children && React.Children.toArray(children)
.map(child => React.cloneElement(child, { key: child.toString(), dataset: [data] }))
)
)
)
}