Skip to content

Commit

Permalink
* Added scroll To by container Id feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eward Somers committed May 28, 2018
1 parent ce00dd2 commit 22e6814
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 18 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ A simple scrolling container for react.

#### ScrollContainer

No api available.
| Prop | Type | What does it do |
| :-------- | :---- | :-------- |
| ContainerId | String | The id of the container to use for navigation. |

#### ScrollTo

| Prop | Type | What does it do |
| :-------- | :---- | :-------- |
| SectionId | String | The id of the section to navigate to. |
| ContainerId | String | The id of the container the element is in. |
| Duraton | Integer | The time it will take the scroll to complete the distance in ms.|
| Offset | Integer | The amount to offset the scrolling to ( 10 means it will scroll 10 pixels higher.)|

Expand All @@ -37,8 +40,10 @@ No api available.

#### Basic usage of the container and sections

Using the containerId ensures you call a unique element in that specific container.

```javascript
<ScrollContainer>
<ScrollContainer id="newContainer">
<div>
<ScrollSection id="section1">
<somecomponent />
Expand All @@ -51,7 +56,7 @@ No api available.
#### Scroll to a section

```javascript
<div onClick={() => ScrollTo('targetId', 10, 500)}>
<div onClick={() => ScrollTo('targetId','newContainer', 10, 500)}>
Click me to navigate to section #1
</div>
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-simple-scroll-container",
"version": "1.0.0",
"version": "1.1.0",
"description": "A simple scroll container for React.",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down
23 changes: 17 additions & 6 deletions src/components/ScrollContainer/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React from 'react'
import PropTypes from 'prop-types'
import { findDOMNode } from 'react-dom'

class ScrollContainer extends React.Component {
static childContextTypes = {
scroll: PropTypes.object,
}
};

elements = {}
elements = {};

handleScroll = () => {
// console.log('isScrolling', this.elements)
Expand All @@ -25,15 +26,20 @@ class ScrollContainer extends React.Component {
// }
// console.log(n.offsetTop, 'offset')
// }
}
};

getNode = (name) => {

return findDOMNode(this.elements[name]);
};

register = (name, ref) => {
this.elements[name] = ref
}
};

unregister = name => {
delete this.elements[name]
}
};

getChildContext() {
return {
Expand All @@ -45,7 +51,12 @@ class ScrollContainer extends React.Component {
}

render() {
return <div onScroll={this.handleScroll}>{React.Children.only(this.props.children)}</div>
return <div id={this.props.containerId} onScroll={this.handleScroll}>{React.Children.only(this.props.children)}</div>
}
}

ScrollContainer.defaultProps = {
containerId: ''
};

export default ScrollContainer
10 changes: 5 additions & 5 deletions src/components/ScrollSection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import PropTypes from 'prop-types'
class ScrollSection extends React.Component {
static contextTypes = {
scroll: PropTypes.object,
}
};

componentDidMount() {
this.context.scroll.register(this.props.sectionId, this._element)
this.context.scroll.register(this.props.sectionId, this._element);
this.props.onMountHandler()
}

componentWillUnmount() {
this.context.scroll.unregister(this.props.sectionId)
this.context.scroll.unregister(this.props.sectionId);
this.props.onUnmountHandler()

}
Expand Down Expand Up @@ -40,14 +40,14 @@ ScrollSection.defaultProps = {
onUnmountHandler: () => null,
onChangeHandler: () => null,
onChildHandler: () => null
}
};

ScrollSection.propTypes = {
onMountHandler: PropTypes.func,
onUnmountHandler: PropTypes.func,
onChangeHandler: PropTypes.func,
onChildHandler: PropTypes.func,
sectionId: PropTypes.string.isRequired
}
};

export default ScrollSection
26 changes: 23 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,34 @@ import scrollIntoView from 'scroll-into-view'
import ScrollSection from './components/ScrollSection/index'
import ScrollContainer from './components/ScrollContainer/index'

const ScrollTo = (sectionId, offset = 0, speed = 500) => {
const node = document.getElementById(sectionId)
window.findReactComponent = function(el) {
for (const key in el) {
if (key.startsWith('__reactInternalInstance$')) {
const fiberNode = el[key];

return fiberNode && fiberNode.return && fiberNode.return.stateNode;
}
}
return null;
};

const ScrollTo = (sectionId, containerId = null, offset = 0, speed = 500) => {
let node = null;

if( containerId !== null) {
const container = document.getElementById(containerId);
const containerReact = findReactComponent(container);
node = containerReact.getNode(sectionId)
} else {
node = document.getElementById(sectionId);

}
scrollIntoView(node, {
time: speed,
align: {
top: offset,
},
})
}
};

export { ScrollContainer, ScrollTo, ScrollSection }

0 comments on commit 22e6814

Please sign in to comment.