Skip to content

Commit

Permalink
Merge pull request #11 from 4Catalyzer/customize-scroll-behavior
Browse files Browse the repository at this point in the history
Allow customizing creation of scroll behavior
  • Loading branch information
taion authored Mar 10, 2018
2 parents 5023b9d + 56e3cd5 commit faba271
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ const shouldUpdateScrollByRoute = (prevRenderArgs, { routes }) => {

return true;
};

const render = renderArgs => (
<ScrollManager
shouldUpdateScroll={shouldUpdateScrollByPathname}
renderArgs={renderArgs}
>
{/* ... */}
</ScrollManager>
);
```

You can customize `<ScrollManager>` even further by providing a `createScrollBehavior` callback that creates the scroll behavior object. This allows using a custom subclass of `ScrollBehavior` from scroll-behavior with custom logic. When using a custom `createScrollBehavior` callback, you can continue to specify the `shouldUpdateScroll` callback as above.

```js
const render = renderArgs => (
<ScrollManager
createScrollBehavior={config => new MyScrollBehavior(config)}
renderArgs={renderArgs}
>
{/* ... */}
</ScrollManager>
);
```

[npm-badge]: https://img.shields.io/npm/v/found-scroll.svg
Expand Down
10 changes: 8 additions & 2 deletions src/ScrollManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@ const STORAGE_NAMESPACE = '@@scroll';

const propTypes = {
shouldUpdateScroll: PropTypes.func,
createScrollBehavior: PropTypes.func.isRequired,
renderArgs: PropTypes.shape({
location: PropTypes.object.isRequired,
router: routerShape.isRequired,
}).isRequired,
children: PropTypes.element,
};

const defaultProps = {
createScrollBehavior: config => new ScrollBehavior(config),
};

class ScrollManager extends React.Component {
constructor(props, context) {
super(props, context);

const { renderArgs } = props;
const { createScrollBehavior, renderArgs } = props;
const { router } = renderArgs;

this.scrollBehavior = new ScrollBehavior({
this.scrollBehavior = createScrollBehavior({
addTransitionHook: router.addTransitionHook,
stateStorage: new StateStorage(router, STORAGE_NAMESPACE),
getCurrentLocation: () => this.props.renderArgs.location,
Expand Down Expand Up @@ -65,5 +70,6 @@ class ScrollManager extends React.Component {
}

ScrollManager.propTypes = propTypes;
ScrollManager.defaultProps = defaultProps;

export default ScrollManager;

0 comments on commit faba271

Please sign in to comment.