@@ -5,6 +5,11 @@ const useRootMargin = offset => {
55 return `-${ offset * 100 } % 0px -${ 100 - offset * 100 } % 0px` ;
66}
77
8+ const useProgressRootMargin = ( direction , offset ) => {
9+ if ( direction === 'down' ) return `${ offset * 100 } % 0px -${ 100 - offset * 100 } % 0px`
10+ return `-${ offset * 100 } % 0px ${ offset * 100 } % 0px` ;
11+ }
12+
813const Step = props => {
914 const {
1015 children,
@@ -16,31 +21,48 @@ const Step = props => {
1621 onStepProgress,
1722 offset,
1823 scrollamaId,
24+ progressThreshold,
1925 } = props ;
26+
27+ const scrollTop = document . documentElement . scrollTop ;
28+ const direction = lastScrollTop < scrollTop ? 'down' : 'up' ;
29+
30+ const progressRootMargin = useProgressRootMargin ( direction , offset ) ;
2031 const rootMargin = useRootMargin ( offset ) ;
21- const [ ref , entry ] = useIntersectionObserver ( {
32+
33+ const [ node , setNode ] = React . useState ( null ) ;
34+ const [ isIntersecting , setIsIntersecting ] = React . useState ( false ) ;
35+
36+ const [ entry ] = useIntersectionObserver ( {
2237 rootMargin,
2338 threshold : 0 ,
39+ nodeRef : node ,
40+ } ) ;
41+
42+ const [ scrollProgressEntry ] = useIntersectionObserver ( {
43+ rootMargin : progressRootMargin ,
44+ threshold : progressThreshold ,
45+ nodeRef : node ,
2446 } ) ;
25- const [ isIntersecting , setIsIntersecting ] = React . useState ( false ) ;
2647
27- const handleScroll = ( ) => {
28- const { height, top } = entry . target . getBoundingClientRect ( ) ;
29- const progress = Math . min ( 1 , Math . max ( 0 , ( window . innerHeight * offset - top ) / height ) ) ;
3048
31- onStepProgress &&
49+ React . useEffect ( ( ) => {
50+ if ( isIntersecting ) {
51+ const { height, top } = scrollProgressEntry . target . getBoundingClientRect ( ) ;
52+ const progress = Math . min ( 1 , Math . max ( 0 , ( window . innerHeight * offset - top ) / height ) ) ;
53+
54+ onStepProgress &&
3255 onStepProgress ( {
3356 progress,
3457 scrollamaId,
3558 data,
36- element : entry . target ,
37- entry,
59+ element : scrollProgressEntry . target ,
60+ entry : scrollProgressEntry ,
3861 } ) ;
39- } ;
62+ }
63+ } , [ scrollProgressEntry ] ) ;
4064
4165 React . useEffect ( ( ) => {
42- const scrollTop = document . documentElement . scrollTop ;
43- const direction = lastScrollTop < scrollTop ? 'down' : 'up' ;
4466 if ( entry && ! entry . isIntersecting && isIntersecting ) {
4567 onStepExit ( { element : entry . target , scrollamaId, data, entry, direction } ) ;
4668 setIsIntersecting ( false ) ;
@@ -50,17 +72,11 @@ const Step = props => {
5072 onStepEnter ( { element : entry . target , scrollamaId, data, entry, direction} ) ;
5173 handleSetLastScrollTop ( scrollTop )
5274 }
53- if ( entry && entry . isIntersecting && onStepProgress ) {
54- document . addEventListener ( 'scroll' , handleScroll ) ;
55- return ( ) => {
56- document . removeEventListener ( 'scroll' , handleScroll ) ;
57- } ;
58- }
5975 } , [ entry ] ) ;
6076
6177 return React . cloneElement ( React . Children . only ( children ) , {
6278 'data-react-scrollama-id' : scrollamaId ,
63- ref,
79+ ref : setNode ,
6480 entry,
6581 } ) ;
6682} ;
0 commit comments