Skip to content

Commit 2dcb548

Browse files
committed
Move target as DOM element
1 parent 2f8595d commit 2dcb548

File tree

4 files changed

+30
-82
lines changed

4 files changed

+30
-82
lines changed

packages/component/src/DOMSetter.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

packages/component/src/EventSpy.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,37 @@ export default class ScrollSpy extends React.Component {
88
}
99

1010
componentDidMount() {
11-
const { current } = this.props.target || {};
11+
const { target } = this.props;
1212

13-
if (current) {
14-
current.addEventListener(this.props.name, this.handleEvent, { passive: true });
15-
this.handleEvent(current);
13+
if (target) {
14+
target.addEventListener(this.props.name, this.handleEvent, { passive: true });
15+
this.handleEvent(target);
1616
}
1717
}
1818

1919
componentDidUpdate(prevProps) {
2020
const { name: prevName, target: prevTarget } = prevProps;
21-
const { current: prevCurrent } = prevTarget || {};
2221
const { name, target } = this.props;
23-
const { current } = target || {};
2422

2523
if (
26-
current !== prevCurrent
24+
target !== prevTarget
2725
|| name !== prevName
2826
) {
29-
if (prevCurrent) {
30-
prevCurrent.removeEventListener(prevName, this.handleEvent);
27+
if (prevTarget) {
28+
prevTarget.removeEventListener(prevName, this.handleEvent);
3129
}
3230

33-
if (current) {
34-
current.addEventListener(name, this.handleEvent, { passive: true });
35-
this.handleEvent(current);
31+
if (target) {
32+
target.addEventListener(name, this.handleEvent, { passive: true });
33+
this.handleEvent(target);
3634
}
3735
}
3836
}
3937

4038
componentWillUnmount() {
41-
const { current } = this.props.target || {};
39+
const { target } = this.props;
4240

43-
current && current.removeEventListener(this.props.name, this.handleEvent);
41+
target && target.removeEventListener(this.props.name, this.handleEvent);
4442
}
4543

4644
handleEvent() {

packages/component/src/ScrollToBottom/Composer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ export default class ScrollToBottomComposer extends React.Component {
5050
<EventSpy
5151
name="scroll"
5252
onEvent={ this.handleScroll }
53-
target={ this.state.target }
53+
target={ this.state.target && this.state.target.current }
5454
/>
5555
{ typeof this.state.scrollTop === 'number' &&
5656
<SpineTo
5757
name="scrollTop"
5858
onEnd={ this.handleScrollEnd }
59-
target={ this.state.target }
59+
target={ this.state.target && this.state.target.current }
6060
value={ this.state.scrollTop }
6161
/>
6262
}

packages/component/src/SpineTo.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,62 +32,59 @@ export default class ScrollTo extends React.Component {
3232

3333
componentDidMount() {
3434
const { name, target } = this.props;
35-
const { current } = target || {};
3635

37-
if (current) {
38-
this.addEventListeners(current);
39-
this.animate(name, current[name], this.props.value, 1);
36+
if (target) {
37+
this.addEventListeners(target);
38+
this.animate(name, target[name], this.props.value, 1);
4039
}
4140
}
4241

4342
componentDidUpdate(prevProps) {
4443
const { target: prevTarget } = prevProps;
4544
const { target } = this.props;
46-
const { current: prevCurrent } = prevTarget || {};
47-
const { current } = target || {};
4845
const scrollChanged = prevProps.value !== this.props.value;
49-
const targetChanged = prevCurrent !== current;
46+
const targetChanged = prevTarget !== target;
5047

5148
if (targetChanged) {
52-
this.removeEventListeners(prevCurrent);
53-
this.addEventListeners(current);
49+
this.removeEventListeners(prevTarget);
50+
this.addEventListeners(target);
5451
}
5552

56-
if ((scrollChanged || targetChanged) && current) {
53+
if ((scrollChanged || targetChanged) && target) {
5754
const { name } = this.props;
5855

59-
this.animate(name, current[name], this.props.value, 1);
56+
this.animate(name, target[name], this.props.value, 1);
6057
}
6158
}
6259

6360
componentWillUnmount() {
64-
this.removeEventListeners(this.props.target && this.props.target.current);
61+
this.removeEventListeners(this.props.target);
6562
cancelAnimationFrame(this.animator);
6663
}
6764

68-
addEventListeners(current) {
69-
current && current.addEventListener('pointerdown', this.handleCancelAnimation, { passive: true });
65+
addEventListeners(target) {
66+
target && target.addEventListener('pointerdown', this.handleCancelAnimation, { passive: true });
7067
}
7168

72-
removeEventListeners(current) {
73-
current && current.removeEventListener('pointerdown', this.handleCancelAnimation);
69+
removeEventListeners(target) {
70+
target && target.removeEventListener('pointerdown', this.handleCancelAnimation);
7471
}
7572

7673
animate(name, from, to, index, start = Date.now()) {
7774
if (typeof to === 'number') {
7875
cancelAnimationFrame(this.animator);
7976

8077
this.animator = requestAnimationFrame(() => {
81-
const { current } = this.props.target || {};
78+
const { target } = this.props;
8279

83-
if (current) {
80+
if (target) {
8481
let nextValue = step(from, to, squareStepper, (Date.now() - start) / 5);
8582

8683
if (Math.abs(to - nextValue) < .5) {
8784
nextValue = to;
8885
}
8986

90-
current[name] = nextValue;
87+
target[name] = nextValue;
9188

9289
if (to === nextValue) {
9390
this.props.onEnd && this.props.onEnd(true);

0 commit comments

Comments
 (0)