11import React , { Component } from 'react' ;
22import PropTypes from 'prop-types' ;
33import { offsetMin , offsetMax } from '../utils/propValidation' ;
4+ import { ParallaxController } from 'react-scroll-parallax' ;
45
56export default class Parallax extends Component {
67 static defaultProps = {
@@ -25,15 +26,20 @@ export default class Parallax extends Component {
2526 tag : PropTypes . string . isRequired ,
2627 } ;
2728
29+ static contextTypes = {
30+ parallaxController : PropTypes . object , // not required because this could be rendered on the server.
31+ } ;
32+
2833 componentDidMount ( ) {
29- // add this Parallax element to the global listener
30- if ( typeof ParallaxController === 'undefined' ) {
34+ // Make sure the provided context is an instance of the controller
35+ if ( ! ( this . context . parallaxController instanceof ParallaxController ) ) {
3136 throw new Error (
32- ' Must initialize the ParallaxController before adding React Parallax components.'
37+ " Must wrap your application's <Parallax /> components in a <ParallaxProvider />."
3338 ) ;
3439 }
40+
3541 // create a new parallax element and save the reference
36- this . element = ParallaxController . createElement ( {
42+ this . element = this . context . parallaxController . createElement ( {
3743 elInner : this . _inner ,
3844 elOuter : this . _outer ,
3945 props : {
@@ -50,7 +56,7 @@ export default class Parallax extends Component {
5056 componentWillReceiveProps ( nextProps ) {
5157 // updates the elements props when changed
5258 if ( this . props !== nextProps ) {
53- ParallaxController . updateElement ( this . element , {
59+ this . context . parallaxController . updateElement ( this . element , {
5460 props : {
5561 disabled : nextProps . disabled ,
5662 offsetXMax : nextProps . offsetXMax ,
@@ -63,12 +69,12 @@ export default class Parallax extends Component {
6369 }
6470 // resets element styles when disabled
6571 if ( this . props . disabled !== nextProps . disabled && nextProps . disabled ) {
66- ParallaxController . resetElementStyles ( this . element ) ;
72+ this . context . parallaxController . resetElementStyles ( this . element ) ;
6773 }
6874 }
6975
7076 componentWillUnmount ( ) {
71- ParallaxController . removeElement ( this . element ) ;
77+ this . context . parallaxController . removeElement ( this . element ) ;
7278 }
7379
7480 // refs
0 commit comments