Skip to content

Commit 123d53f

Browse files
committed
Smoothens mounting/unmounting transitions for nodes & links
1 parent 0d308cd commit 123d53f

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

src/Link/index.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,25 @@ import './style.css';
55

66
export default class Link extends React.PureComponent {
77

8-
constructor(props) {
9-
super(props);
10-
this.expandPath = this.expandPath.bind(this);
8+
componentDidMount() {
9+
this.applyOpacity(1);
1110
}
1211

13-
componentDidMount() {
14-
console.log('mounted:\n', `${this.props.linkData.source.name}-->${this.props.linkData.target.name}`);
15-
select(this.link)
16-
.transition()
17-
.duration(500)
18-
.attr('d', this.expandPath());
12+
componentWillLeave(done) {
13+
this.applyOpacity(0, done);
1914
}
2015

21-
componentWillLeave() {
22-
const { linkData } = this.props;
23-
console.log('Leaving:\n', `${linkData.source.name}-->${linkData.target.name}`);
24-
select(this.link)
25-
.transition()
26-
.duration(500)
27-
.attr('d', this.collapsePath());
16+
applyOpacity(opacity, done = () => {}) {
17+
const { enabled, duration } = this.props.transitions;
18+
if (enabled) {
19+
select(this.link)
20+
.transition()
21+
.duration(duration)
22+
.style('opacity', opacity)
23+
.each('end', done);
24+
} else {
25+
done();
26+
}
2827
}
2928

3029
diagonalPath(linkData, orientation) {
@@ -57,11 +56,13 @@ export default class Link extends React.PureComponent {
5756
}
5857

5958
render() {
59+
const { transitions } = this.props;
6060
return (
6161
<path
6262
ref={(l) => { this.link = l; }}
6363
className="linkBase"
64-
d={this.collapsePath()}
64+
style={transitions.enabled ? undefined : { opacity: 1 }}
65+
d={this.expandPath()}
6566
/>
6667
);
6768
}
@@ -77,4 +78,5 @@ Link.propTypes = {
7778
'diagonal',
7879
'elbow',
7980
]).isRequired,
81+
transitions: PropTypes.object.isRequired,
8082
};

src/Node/index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,35 +42,38 @@ export default class Node extends React.Component {
4242
return transform;
4343
}
4444

45-
applyTransform(transform, done = () => {}) {
45+
applyTransform(transform, opacity = 1, done = () => {}) {
4646
const { enabled, duration } = this.props.transitions;
4747
if (enabled) {
4848
select(this.node)
4949
.transition()
5050
.duration(duration)
5151
.attr('transform', transform)
52+
.style('opacity', opacity)
5253
.each('end', done);
5354
} else {
5455
done();
5556
}
5657
}
5758

59+
handleClick() {
60+
this.props.onClick(this.props.nodeData.id);
61+
}
62+
5863
componentWillLeave(done) {
5964
const { parent } = this.props.nodeData;
6065
const originX = parent ? parent.x : 0;
6166
const originY = parent ? parent.y : 0;
6267
const transform = this.setTransformOrientation(originX, originY);
6368

64-
this.applyTransform(transform, done);
65-
}
66-
67-
handleClick() {
68-
this.props.onClick(this.props.nodeData.id);
69+
this.applyTransform(transform, 0, done);
70+
// this.applyOpacity(0, done);
6971
}
7072

7173
render() {
7274
const { nodeData, depthFactor, transitions } = this.props;
73-
const transform = transitions.enabled ?
75+
76+
const transform = transitions.enabled && this.state.transform ?
7477
this.state.transform :
7578
this.setTransformOrientation(nodeData.x, nodeData.y);
7679

@@ -82,6 +85,7 @@ export default class Node extends React.Component {
8285
<g
8386
id={nodeData.id}
8487
ref={(n) => { this.node = n; }}
88+
style={transitions.enabled ? undefined : { opacity: 1 }}
8589
className={nodeData._children ? 'nodeBase' : 'leafNodeBase'}
8690
transform={transform}
8791
onClick={this.handleClick}

src/Tree/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ export default class Tree extends React.Component {
212212
render() {
213213
const { orientation, translate, pathFunc, depthFactor, transitions } = this.props;
214214
const { nodes, links } = this.generateTree();
215+
215216
return (
216217
<div className="treeContainer">
217218
<svg width="100%" height="100%">
@@ -239,6 +240,7 @@ export default class Tree extends React.Component {
239240
orientation={orientation}
240241
pathFunc={pathFunc}
241242
linkData={linkData}
243+
transitions={transitions}
242244
/>
243245
)}
244246
</TransitionGroup>

0 commit comments

Comments
 (0)