Skip to content

Commit 062aeb0

Browse files
authored
Merge pull request #27 from Mesoptier/feature/scroll-container
Add scroll behaviour for container elements
2 parents eaa1164 + 0c4f18c commit 062aeb0

File tree

3 files changed

+68
-15
lines changed

3 files changed

+68
-15
lines changed

src/ScrollBehaviorContainer.js

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,42 @@
1-
import React from 'react';
1+
import { Component, PropTypes } from 'react';
22
import ScrollBehavior from 'scroll-behavior/lib/ScrollBehavior';
33

4-
export default class ScrollBehaviorContainer extends React.Component {
4+
export default class ScrollBehaviorContainer extends Component {
55
static propTypes = {
6-
shouldUpdateScroll: React.PropTypes.func,
7-
routerProps: React.PropTypes.object.isRequired,
8-
children: React.PropTypes.node.isRequired,
6+
shouldUpdateScroll: PropTypes.func,
7+
routerProps: PropTypes.object.isRequired,
8+
children: PropTypes.node.isRequired,
99
};
1010

11-
componentDidMount() {
11+
static childContextTypes = {
12+
scrollBehavior: PropTypes.instanceOf(ScrollBehavior),
13+
};
14+
15+
constructor() {
16+
super();
17+
18+
this.state = {
19+
scrollBehavior: null,
20+
};
21+
}
22+
23+
getChildContext() {
24+
return {
25+
scrollBehavior: this.scrollBehavior,
26+
};
27+
}
28+
29+
componentWillMount() {
1230
const { routerProps } = this.props;
1331

1432
this.scrollBehavior = new ScrollBehavior(
1533
routerProps.router,
1634
() => this.props.routerProps.location
1735
);
1836

37+
// Set state so child context will be updated
38+
this.setState({ scrollBehavior: this.scrollBehavior });
39+
1940
this.onUpdate(null, routerProps);
2041
}
2142

@@ -37,16 +58,18 @@ export default class ScrollBehaviorContainer extends React.Component {
3758
onUpdate(prevRouterProps, routerProps) {
3859
const { shouldUpdateScroll } = this.props;
3960

40-
let scrollPosition;
41-
if (!shouldUpdateScroll) {
42-
scrollPosition = true;
43-
} else {
44-
scrollPosition = shouldUpdateScroll.call(
45-
this.scrollBehavior, prevRouterProps, routerProps
46-
);
47-
}
61+
this.scrollBehavior.getContainerKeys().forEach(containerKey => {
62+
let scrollPosition;
63+
if (!shouldUpdateScroll) {
64+
scrollPosition = true;
65+
} else {
66+
scrollPosition = shouldUpdateScroll.call(
67+
this.scrollBehavior, prevRouterProps, routerProps, containerKey
68+
);
69+
}
4870

49-
this.scrollBehavior.updateScroll(scrollPosition);
71+
this.scrollBehavior.updateScroll(containerKey, scrollPosition);
72+
});
5073
}
5174

5275
render() {

src/ScrollContainer.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Component, PropTypes } from 'react';
2+
import { findDOMNode } from 'react-dom';
3+
import ScrollBehavior from 'scroll-behavior/lib/ScrollBehavior';
4+
5+
export default class ScrollContainer extends Component {
6+
static propTypes = {
7+
scrollKey: PropTypes.string,
8+
children: PropTypes.element,
9+
};
10+
11+
static contextTypes = {
12+
scrollBehavior: PropTypes.instanceOf(ScrollBehavior),
13+
};
14+
15+
componentDidMount() {
16+
const node = findDOMNode(this);
17+
this.context.scrollBehavior.registerContainer(this.props.scrollKey, node);
18+
}
19+
20+
componentWillUnmount() {
21+
this.context.scrollBehavior.unregisterContainer(this.props.scrollKey);
22+
}
23+
24+
render() {
25+
return this.props.children;
26+
}
27+
}

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22

33
import ScrollBehaviorContainer from './ScrollBehaviorContainer';
4+
import ScrollContainer from './ScrollContainer';
45

56
export default function useScroll(shouldUpdateScroll) {
67
return {
@@ -14,3 +15,5 @@ export default function useScroll(shouldUpdateScroll) {
1415
),
1516
};
1617
}
18+
19+
export { ScrollContainer };

0 commit comments

Comments
 (0)