Skip to content

Commit 95336b0

Browse files
committed
refactor to allow global controller to be used in Parallax; log deprecation warning if no context is available but global is still used
1 parent bee65a9 commit 95336b0

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/components/Parallax.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,26 @@ export default class Parallax extends Component {
3131
};
3232

3333
componentDidMount() {
34-
// Make sure the provided context is an instance of the controller
35-
if (!(this.context.parallaxController instanceof ParallaxController)) {
34+
// Make sure the provided controller is an instance of the Parallax Controller
35+
const isInstance = this.controller instanceof ParallaxController;
36+
37+
// Throw if neither context or global is available
38+
if (!this.controller && !isInstance) {
3639
throw new Error(
3740
"Must wrap your application's <Parallax /> components in a <ParallaxProvider />."
3841
);
3942
}
4043

44+
// Deprecation warning for >=1.0.0
45+
// If no context is available but the window global is then warn
46+
if (!this.context.parallaxController && window.ParallaxController) {
47+
console.log(
48+
'Calling ParallaxController.init() has been deprecated in favor of using the <ParallaxProvider /> component. For usage details see: https://github.com/jscottsmith/react-scroll-parallax/tree/v1.1.0#usage'
49+
);
50+
}
51+
4152
// create a new parallax element and save the reference
42-
this.element = this.context.parallaxController.createElement({
53+
this.element = this.controller.createElement({
4354
elInner: this._inner,
4455
elOuter: this._outer,
4556
props: {
@@ -56,7 +67,7 @@ export default class Parallax extends Component {
5667
componentWillReceiveProps(nextProps) {
5768
// updates the elements props when changed
5869
if (this.props !== nextProps) {
59-
this.context.parallaxController.updateElement(this.element, {
70+
this.controller.updateElement(this.element, {
6071
props: {
6172
disabled: nextProps.disabled,
6273
offsetXMax: nextProps.offsetXMax,
@@ -69,12 +80,17 @@ export default class Parallax extends Component {
6980
}
7081
// resets element styles when disabled
7182
if (this.props.disabled !== nextProps.disabled && nextProps.disabled) {
72-
this.context.parallaxController.resetElementStyles(this.element);
83+
this.controller.resetElementStyles(this.element);
7384
}
7485
}
7586

7687
componentWillUnmount() {
77-
this.context.parallaxController.removeElement(this.element);
88+
this.controller.removeElement(this.element);
89+
}
90+
91+
get controller() {
92+
// Legacy versions may use the global, not context
93+
return this.context.parallaxController || window.ParallaxController;
7894
}
7995

8096
// refs

0 commit comments

Comments
 (0)