File tree Expand file tree Collapse file tree 6 files changed +113
-5
lines changed Expand file tree Collapse file tree 6 files changed +113
-5
lines changed Original file line number Diff line number Diff line change 44
44
- [ ` useMotion ` ] ( ./docs/useMotion.md ) &mdash ; tracks state of device's motion sensor.
45
45
- [ ` useNetwork ` ] ( ./docs/useNetwork.md ) &mdash ; tracks state of user's internet connection.
46
46
- [ ` useOrientation ` ] ( ./docs/useOrientation.md ) &mdash ; tracks state of device's screen orientation.
47
+ - [ ` useScroll ` ] ( ./docs/useScroll.md ) &mdash ; some HTML element's scroll position. [ ![ ] [ img-demo ]] ( https://streamich.github.io/react-use/?path=/story/sensors-usescroll--docs )
48
+
47
49
- [ ` useSize ` ] ( ./docs/useSize.md ) &mdash ; tracks some HTML element's dimensions.
48
50
- [ ` useWindowScroll ` ] ( ./docs/useWindowScroll.md ) &mdash ; tracks ` Window ` scroll position. [ ![ ] [ img-demo ]] ( https://streamich.github.io/react-use/?path=/story/sensors-usewindowscroll--docs )
49
51
- [ ` useWindowSize ` ] ( ./docs/useWindowSize.md ) &mdash ; tracks ` Window ` dimensions. [ ![ ] [ img-demo ]] ( https://codesandbox.io/s/m7ln22668 )
Original file line number Diff line number Diff line change
1
+ # ` useScroll `
2
+
3
+ React sensor hook that re-renders on when scroll position in a DOM element changes.
4
+
5
+ ## Usage
6
+
7
+ ``` jsx
8
+ import {useScroll } from ' react-use' ;
9
+
10
+ const Demo = () => {
11
+ const element = React .useRef (null );
12
+ const {x , y } = useScroll (element);
13
+
14
+ return (
15
+ < div ref= {element}>
16
+ < div> x: {x}< / div>
17
+ < div> y: {y}< / div>
18
+ < / div>
19
+ );
20
+ };
21
+ ```
Original file line number Diff line number Diff line change
1
+ import * as React from 'react' ;
2
+ import { storiesOf } from '@storybook/react' ;
3
+ import { useScroll } from '..' ;
4
+ import ShowDocs from '../util/ShowDocs' ;
5
+
6
+ const Demo = ( ) => {
7
+ const element = React . useRef ( null ) ;
8
+ const { x, y} = useScroll ( element ) ;
9
+
10
+ return (
11
+ < >
12
+ < div > x: { x } </ div >
13
+ < div > y: { y } </ div >
14
+ < div
15
+ ref = { element }
16
+ style = { {
17
+ width : '400px' ,
18
+ height : '400px' ,
19
+ backgroundColor : 'whitesmoke' ,
20
+ overflow : 'scroll'
21
+ } } >
22
+
23
+ < div style = { { width : '2000px' , height : '2000px' } } >
24
+ Scroll me
25
+ </ div >
26
+ </ div >
27
+ </ >
28
+ ) ;
29
+ } ;
30
+
31
+ storiesOf ( 'Sensors/useScroll' , module )
32
+ . add ( 'Docs' , ( ) => < ShowDocs md = { require ( '../../docs/useScroll.md' ) } /> )
33
+ . add ( 'Demo' , ( ) =>
34
+ < Demo />
35
+ )
Original file line number Diff line number Diff line change @@ -8,14 +8,14 @@ const Demo = () => {
8
8
9
9
return (
10
10
< div style = { {
11
- width : " 200vw" ,
12
- height : " 200vh"
11
+ width : ' 200vw' ,
12
+ height : ' 200vh'
13
13
} } >
14
14
< div style = { {
15
- position : " fixed" ,
15
+ position : ' fixed' ,
16
16
left : 0 ,
17
- right : 0 } }
18
- >
17
+ right : 0
18
+ } } >
19
19
< div > x: { x } </ div >
20
20
< div > y: { y } </ div >
21
21
</ div >
Original file line number Diff line number Diff line change @@ -35,6 +35,7 @@ import useOutsideClick from './useOutsideClick';
35
35
import usePromise from './usePromise' ;
36
36
import useRaf from './useRaf' ;
37
37
import useRefMounted from './useRefMounted' ;
38
+ import useScroll from './useScroll' ;
38
39
import useSessionStorage from './useSessionStorage' ;
39
40
import useSetState from './useSetState' ;
40
41
import useSize from './useSize' ;
@@ -90,6 +91,7 @@ export {
90
91
usePromise ,
91
92
useRaf ,
92
93
useRefMounted ,
94
+ useScroll ,
93
95
useSessionStorage ,
94
96
useSetState ,
95
97
useSize ,
Original file line number Diff line number Diff line change
1
+ import { useState , useEffect , useRef } from 'react' ;
2
+ import { isClient } from './util' ;
3
+
4
+ export interface State {
5
+ x : number ;
6
+ y : number ;
7
+ }
8
+
9
+ const useScroll = ( ref ) : State => {
10
+ const frame = useRef ( 0 ) ;
11
+ const [ state , setState ] = useState < State > ( {
12
+ x : isClient ? window . scrollX : 0 ,
13
+ y : isClient ? window . scrollY : 0
14
+ } ) ;
15
+
16
+ useEffect ( ( ) => {
17
+ const handler = ( ) => {
18
+
19
+ frame . current = requestAnimationFrame ( ( ) => {
20
+ setState ( {
21
+ x : ref . current . scrollLeft ,
22
+ y : ref . current . scrollTop
23
+ } ) ;
24
+ } ) ;
25
+ }
26
+
27
+ if ( ref && ref . current ) {
28
+ ref . current . addEventListener ( 'scroll' , handler , {
29
+ capture : false ,
30
+ passive : true
31
+ } ) ;
32
+ }
33
+
34
+ return ( ) => {
35
+ if ( frame . current ) {
36
+ cancelAnimationFrame ( frame . current ) ;
37
+ }
38
+
39
+ if ( ref && ref . current ) {
40
+ ref . current . removeEventListener ( 'scroll' , handler ) ;
41
+ }
42
+ } ;
43
+ } , [ ref ] ) ;
44
+
45
+ return state ;
46
+ }
47
+
48
+ export default useScroll
You can’t perform that action at this time.
0 commit comments