Skip to content
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

ScrollSnapMixin: an extension to achieve scroll-snap emulation with react-scroll #38

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ Checkout examples
>> Or
>> [Code](https://github.com/fisshy/react-scroll/blob/master/examples/basic/app.js)

> Scroll-snap
>> [Live](#)
>> Or
>> [Code](https://github.com/fisshy/react-scroll/blob/master/examples/scroll-snap/app.js)

### Usage
```js

Expand Down Expand Up @@ -86,7 +91,15 @@ var Link = React.createClass({

```


### Scroll-snap emulation
Mixin added an emulation of [scroll-snap](https://developer.mozilla
.org/en-US/docs/Web/CSS/scroll-snap-type) to your components. This can
be interesting for landing pages. Just include the mixin into parent component.
```
...
mixins: [ScrollSnapMixin],
...
```

### Todo
- [x] Vertical scrolling
Expand Down
4 changes: 2 additions & 2 deletions examples/basic/index.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!doctype html>
<header>
<head>
<title>React Scroll Examples</title>
<link href="app.css" rel="stylesheet"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"
</header>
</head>
<body>
<div id="example">
</div>
Expand Down
3 changes: 2 additions & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<a href="/basic/index.html"> Basic </a>
<a href="/basic/index.html"> Basic </a><br/>
<a href="/scroll-snap/index.html"> ScrollSnap </a>
26 changes: 26 additions & 0 deletions examples/scroll-snap/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* uncomment this if native scroll-snap is needed */
/*@supports (scroll-snap-type: mandatory) or (-webkit-scroll-snap-type: mandatory) {
#wrapper {
height: 100vh;
}
}*/

#wrapper {
overflow-y: auto;
scroll-behavior: smooth;
scroll-snap-type: mandatory;
scroll-snap-points-y: repeat(100vh);
}

.element {
height:100vh;
font-size: 45px;
border-top:1px solid #000;
padding-top:55px;
padding-left:10px;
/*scroll-snap-coordinate: 0 0;*/
}

.active {
color:red !important;
}
84 changes: 84 additions & 0 deletions examples/scroll-snap/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"use strict";

var React = require('react');
var ReactDOM = require('react-dom');
var Scroll = require('react-scroll');

var Link = Scroll.Link;
var Element = Scroll.Element;
var ScrollSnapMixin = Scroll.ScrollSnapMixin;

var Section = React.createClass({
getInitialState() {
return {
overflow: true
}
},

mixins: [ScrollSnapMixin],

handleClick: function () {
if (this.state.overflow) {
document.body.style.overflowY = 'hidden';
this.setState({ overflow: false });
} else {
document.body.style.overflowY = 'scroll';
this.setState({ overflow: true });
}
},

render: function () {
var overflow;

if (this.state.overflow) {
overflow = 'With';
} else {
overflow = 'Without';
}

return (
<div id="wrapper">
<nav className="navbar navbar-default navbar-fixed-top">
<div className="container-fluid">
<div className="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul className="nav navbar-nav">
<li><Link to="test1" spy={true} smooth={true} duration={500} >Test 1</Link></li>
<li><Link to="test2" spy={true} smooth={true} duration={500}>Test 2</Link></li>
<li><Link to="test3" spy={true} smooth={true} duration={500} >Test 3</Link></li>
<li><Link to="test4" spy={true} smooth={true} duration={500}>Test 4</Link></li>
<li><Link to="test5" spy={true} smooth={true} duration={500}>Test 5</Link></li>
<li><a onClick={ this.handleClick }>{overflow} "overflow: hidden"</a></li>
</ul>
</div>
</div>
</nav>

<Element name="test1" className="element" style={{ backgroundColor: '#ffffff' }}>
test 1
</Element>

<Element name="test2" className="element" style={{ backgroundColor: '#008000' }}>
test 2
</Element>

<Element name="test3" className="element" style={{ backgroundColor: '#00C0C0' }}>
test 3
</Element>

<Element name="test4" className="element" style={{ backgroundColor: '#C0C0FF' }}>
test 4
</Element>

<Element name="test5" className="element" style={{ backgroundColor: '#FFC0FF' }}>
test 5
</Element>
</div>
);
}
});


ReactDOM.render(
<Section />,
document.getElementById('example')
);
13 changes: 13 additions & 0 deletions examples/scroll-snap/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<head>
<title>React Scroll Examples</title>
<link href="app.css" rel="stylesheet"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"
</head>
<body>
<div id="example">
</div>
</body>

<script src="/__build__/shared.js"></script>
<script src="/__build__/scroll-snap.js"></script>
1 change: 1 addition & 0 deletions modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ exports.Button = require('./components/Button.js');
exports.Element = require('./components/Element.js');
exports.Helpers = require('./mixins/Helpers.js');
exports.scroller = require('./mixins/scroller.js');
exports.ScrollSnapMixin = require('./mixins/scroll-snap.js');
exports.scrollSpy = require('./mixins/scroll-spy.js');
5 changes: 4 additions & 1 deletion modules/mixins/Helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ var Helpers = {
},

scrollTo : function(to) {
var duration = this.props.duration;

scroller.scrollTo(to, this.props.smooth, this.props.duration, this.props.offset);

setTimeout(function() { scroller.flushTarget() }, duration);
},

onClick: function(event) {
Expand All @@ -45,7 +49,6 @@ var Helpers = {
*/

this.scrollTo(this.props.to);

},

componentDidMount: function() {
Expand Down
Loading