Skip to content

Add scroll behaviour for container elements #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions src/ScrollBehaviorContainer.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
import React from 'react';
import { Component, PropTypes } from 'react';
import ScrollBehavior from 'scroll-behavior/lib/ScrollBehavior';

export default class ScrollBehaviorContainer extends React.Component {
export default class ScrollBehaviorContainer extends Component {
static propTypes = {
shouldUpdateScroll: React.PropTypes.func,
routerProps: React.PropTypes.object.isRequired,
children: React.PropTypes.node.isRequired,
shouldUpdateScroll: PropTypes.func,
routerProps: PropTypes.object.isRequired,
children: PropTypes.node.isRequired,
};

componentDidMount() {
static childContextTypes = {
scrollBehavior: PropTypes.instanceOf(ScrollBehavior),
};

constructor() {
super();

this.state = {
scrollBehavior: null,
};
}

getChildContext() {
return {
scrollBehavior: this.scrollBehavior,
};
}

componentWillMount() {
const { routerProps } = this.props;

this.scrollBehavior = new ScrollBehavior(
routerProps.router,
() => this.props.routerProps.location
);

// Set state so child context will be updated
this.setState({ scrollBehavior: this.scrollBehavior });

this.onUpdate(null, routerProps);
}

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

let scrollPosition;
if (!shouldUpdateScroll) {
scrollPosition = true;
} else {
scrollPosition = shouldUpdateScroll.call(
this.scrollBehavior, prevRouterProps, routerProps
);
}
this.scrollBehavior.getContainerKeys().forEach(containerKey => {
let scrollPosition;
if (!shouldUpdateScroll) {
scrollPosition = true;
} else {
scrollPosition = shouldUpdateScroll.call(
this.scrollBehavior, prevRouterProps, routerProps, containerKey
);
}

this.scrollBehavior.updateScroll(scrollPosition);
this.scrollBehavior.updateScroll(containerKey, scrollPosition);
});
}

render() {
Expand Down
27 changes: 27 additions & 0 deletions src/ScrollContainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Component, PropTypes } from 'react';
import { findDOMNode } from 'react-dom';
import ScrollBehavior from 'scroll-behavior/lib/ScrollBehavior';

export default class ScrollContainer extends Component {
static propTypes = {
scrollKey: PropTypes.string,
children: PropTypes.element,
};

static contextTypes = {
scrollBehavior: PropTypes.instanceOf(ScrollBehavior),
};

componentDidMount() {
const node = findDOMNode(this);
this.context.scrollBehavior.registerContainer(this.props.scrollKey, node);
}

componentWillUnmount() {
this.context.scrollBehavior.unregisterContainer(this.props.scrollKey);
}

render() {
return this.props.children;
}
}
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';

import ScrollBehaviorContainer from './ScrollBehaviorContainer';
import ScrollContainer from './ScrollContainer';

export default function useScroll(shouldUpdateScroll) {
return {
Expand All @@ -14,3 +15,5 @@ export default function useScroll(shouldUpdateScroll) {
),
};
}

export { ScrollContainer };