Skip to content

Commit 5111f4e

Browse files
committed
add ParallaxProvider component that uses context to pass children a reference to controller instance
1 parent 6ed52be commit 5111f4e

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/components/ParallaxProvider.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import React, { Component } from 'react';
2+
import PropTypes from 'prop-types';
3+
import { ParallaxController } from 'react-scroll-parallax';
4+
5+
export default class ParallaxProvider extends Component {
6+
static propTypes = {
7+
children: PropTypes.node.isRequired,
8+
};
9+
10+
static childContextTypes = {
11+
parallaxController: PropTypes.object,
12+
};
13+
14+
getChildContext() {
15+
// Passes down the reference to the controller
16+
const { parallaxController } = this;
17+
return { parallaxController };
18+
}
19+
20+
componentWillMount() {
21+
// Don't initialize on the server
22+
const isServer = typeof window === 'undefined';
23+
24+
if (!isServer) {
25+
// Must not be the server so kick it off...
26+
this.parallaxController = ParallaxController.init();
27+
}
28+
}
29+
30+
componentWillUnmount() {
31+
if (this.parallaxController) {
32+
// Remove scroll and resize listener if the provider is unmounted
33+
this.parallaxController.destroy();
34+
}
35+
}
36+
37+
render() {
38+
const { children } = this.props;
39+
40+
return children;
41+
}
42+
}

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export Parallax from './components/Parallax';
2+
export ParallaxProvider from './components/ParallaxProvider';
23
export ParallaxController from './libs/ParallaxController';

0 commit comments

Comments
 (0)