@@ -50,12 +50,12 @@ class ScrollInfoProvider extends Component {
5050 hasScrolled : prevHasScrolled ,
5151 } = this . state ;
5252
53- // Set to zero on first render and mount for cross-browser compatibility --
53+ // Set currentScroll to zero on mount for cross-browser compatibility --
5454 // Some browsers populate the cached window.pageOffset at different points of the component lifecycle.
55- // Chrome mounts with the cached window.pageOffset, while Safari and FireFox both only
56- // populate it on first scroll event -- which is triggered by the browser in some cases .
57- // The presence of a timestamp indicates that it wasn't first render or first mount ,
58- // but a true requestAnimationFrame scroll event. Keep at zero otherwise.
55+ // Chrome mounts with cached window.pageOffset coordinates even before firing the respective cached scroll event.
56+ // Neither Safari and FireFox populate these coordinates until this cached scroll (natively triggered by the browser) .
57+ // The presence of a timestamp indicates that the caller of this method was not componentDidMount ,
58+ // but rather a true scroll event via requestAnimationFrame . Keep at zero otherwise.
5959 const hasScrolled = prevHasScrolled || Boolean ( timestamp ) ;
6060 const currentScrollX = hasScrolled ? window . pageXOffset : 0 ;
6161 const currentScrollY = hasScrolled ? window . pageYOffset : 0 ;
@@ -69,9 +69,8 @@ class ScrollInfoProvider extends Component {
6969 const yPercentage = ( currentScrollY / ( document . body . scrollHeight - window . innerHeight ) ) * 100 ;
7070 const totalPercentage = ( xPercentage + yPercentage ) / 2 ;
7171
72- /* eslint-disable no-nested-ternary */
73- const xDirection = xDifference > 0 ? 'right' : xDifference < 0 ? 'left' : prevXDirection ;
74- const yDirection = yDifference > 0 ? 'down' : yDifference < 0 ? 'up' : prevYDirection ;
72+ const xDirection = xDifference > 0 ? 'right' : xDifference < 0 ? 'left' : prevXDirection ; // eslint-disable-line no-nested-ternary
73+ const yDirection = yDifference > 0 ? 'down' : yDifference < 0 ? 'up' : prevYDirection ; // eslint-disable-line no-nested-ternary
7574
7675 this . setState ( {
7776 x : currentScrollX ,
@@ -97,16 +96,18 @@ class ScrollInfoProvider extends Component {
9796
9897 return (
9998 < ScrollInfoContext . Provider value = { { scrollInfo } } >
100- { children }
99+ { children && children }
101100 </ ScrollInfoContext . Provider >
102101 ) ;
103102 }
104103}
105104
106- ScrollInfoProvider . defaultProps = { } ;
105+ ScrollInfoProvider . defaultProps = {
106+ children : undefined ,
107+ } ;
107108
108109ScrollInfoProvider . propTypes = {
109- children : PropTypes . node . isRequired ,
110+ children : PropTypes . node ,
110111} ;
111112
112113export default ScrollInfoProvider ;
0 commit comments