-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor ParallaxProvider to function component (#230)
- Loading branch information
1 parent
a9ebdd5
commit 290dab0
Showing
2 changed files
with
84 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,49 @@ | ||
import React, { Component } from 'react'; | ||
import React, { PropsWithChildren, useEffect, useRef } from 'react'; | ||
|
||
import { ParallaxContext } from '../../context/ParallaxContext'; | ||
import { ParallaxController, ScrollAxis } from 'parallax-controller'; | ||
import { ScrollAxis } from 'parallax-controller'; | ||
import { ParallaxProviderProps } from './types'; | ||
import { createController } from './helpers'; | ||
|
||
export class ParallaxProvider extends Component<ParallaxProviderProps, {}> { | ||
static defaultProps = { | ||
scrollAxis: ScrollAxis.vertical, | ||
}; | ||
|
||
controller: ParallaxController | null; | ||
|
||
constructor(props: ParallaxProviderProps) { | ||
super(props); | ||
this.controller = createController({ | ||
scrollAxis: props.scrollAxis, | ||
export function ParallaxProvider( | ||
props: PropsWithChildren<ParallaxProviderProps> | ||
) { | ||
const controller = useRef( | ||
createController({ | ||
scrollAxis: props.scrollAxis || ScrollAxis.vertical, | ||
scrollContainer: props.scrollContainer, | ||
disabled: props.isDisabled, | ||
}); | ||
} | ||
}) | ||
); | ||
|
||
componentDidUpdate(prevProps: ParallaxProviderProps) { | ||
if ( | ||
prevProps.scrollContainer !== this.props.scrollContainer && | ||
this.props.scrollContainer | ||
) { | ||
this.controller?.updateScrollContainer(this.props.scrollContainer); | ||
// update scroll container | ||
useEffect(() => { | ||
if (props.scrollContainer && controller.current) { | ||
controller.current.updateScrollContainer(props.scrollContainer); | ||
} | ||
}, [props.scrollContainer, controller.current]); | ||
|
||
if (prevProps.isDisabled !== this.props.isDisabled) { | ||
if (this.props.isDisabled) { | ||
this.controller?.disableParallaxController(); | ||
} | ||
if (!this.props.isDisabled) { | ||
this.controller?.enableParallaxController(); | ||
} | ||
// disable/enable parallax | ||
useEffect(() => { | ||
if (props.isDisabled && controller.current) { | ||
controller.current.disableParallaxController(); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
// @ts-ignore | ||
this.controller = this.controller.destroy(); | ||
} | ||
|
||
render() { | ||
const { children } = this.props; | ||
return ( | ||
// @ts-ignore | ||
<ParallaxContext.Provider value={this.controller}> | ||
{children} | ||
</ParallaxContext.Provider> | ||
); | ||
} | ||
if (!props.isDisabled && controller.current) { | ||
controller.current.enableParallaxController(); | ||
} | ||
}, [props.isDisabled, controller.current]); | ||
|
||
// remove the controller when unmounting | ||
useEffect(() => { | ||
return () => { | ||
controller?.current && controller?.current.destroy(); | ||
controller.current = null; | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<ParallaxContext.Provider value={controller.current}> | ||
{props.children} | ||
</ParallaxContext.Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
290dab0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
react-scroll-parallax – ./
react-scroll-parallax-git-master-jscottsmith.vercel.app
react-scroll-parallax.vercel.app
react-scroll-parallax-jscottsmith.vercel.app
react-scroll-parallax.v3.damnthat.tv
react-scroll-parallax.damnthat.tv