File tree Expand file tree Collapse file tree 5 files changed +92
-0
lines changed Expand file tree Collapse file tree 5 files changed +92
-0
lines changed Original file line number Diff line number Diff line change 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
47
- [ ` useSize ` ] ( ./docs/useSize.md ) &mdash ; tracks some HTML element's dimensions.
48
+ - [ ` useWindowScroll ` ] ( ./docs/useWindowScroll.md ) &mdash ; tracks ` Window ` scroll position. [ ![ ] [ img-demo ]] ( https://streamich.github.io/react-use/?path=/story/sensors-usewindowscroll--docs )
48
49
- [ ` useWindowSize ` ] ( ./docs/useWindowSize.md ) &mdash ; tracks ` Window ` dimensions. [ ![ ] [ img-demo ]] ( https://codesandbox.io/s/m7ln22668 )
49
50
<br />
50
51
<br />
Original file line number Diff line number Diff line change
1
+ # ` useWindowSize `
2
+
3
+ React sensor hook that re-renders on window scroll
4
+
5
+ ## Usage
6
+
7
+ ``` jsx
8
+ import {useWindowScroll } from ' react-use' ;
9
+
10
+ const Demo = () => {
11
+ const {x , y } = useWindowScroll ();
12
+
13
+ return (
14
+ < div>
15
+ < div> x: {x}< / div>
16
+ < div> y: {y}< / div>
17
+ < / div>
18
+ );
19
+ };
20
+ ```
Original file line number Diff line number Diff line change
1
+ import * as React from 'react' ;
2
+ import { storiesOf } from '@storybook/react' ;
3
+ import { useWindowScroll } from '..' ;
4
+ import ShowDocs from '../util/ShowDocs' ;
5
+
6
+ const Demo = ( ) => {
7
+ const { x, y} = useWindowScroll ( ) ;
8
+
9
+ return (
10
+ < div style = { {
11
+ width : "200vw" ,
12
+ height : "200vh"
13
+ } } >
14
+ < div style = { {
15
+ position : "fixed" ,
16
+ left : 0 ,
17
+ right : 0 } }
18
+ >
19
+ < div > x: { x } </ div >
20
+ < div > y: { y } </ div >
21
+ </ div >
22
+ </ div >
23
+ ) ;
24
+ } ;
25
+
26
+ storiesOf ( 'Sensors/useWindowScroll' , module )
27
+ . add ( 'Docs' , ( ) => < ShowDocs md = { require ( '../../docs/useWindowScroll.md' ) } /> )
28
+ . add ( 'Demo' , ( ) =>
29
+ < Demo />
30
+ )
Original file line number Diff line number Diff line change @@ -47,6 +47,7 @@ import useTween from './useTween';
47
47
import useUnmount from './useUnmount' ;
48
48
import useUpdate from './useUpdate' ;
49
49
import useVideo from './useVideo' ;
50
+ import useWindowScroll from './useWindowScroll' ;
50
51
import useWindowSize from './useWindowSize' ;
51
52
import useWait from './useWait' ;
52
53
import useUpdateEffect from './useUpdateEffect'
@@ -101,6 +102,7 @@ export {
101
102
useUnmount ,
102
103
useUpdate ,
103
104
useVideo ,
105
+ useWindowScroll ,
104
106
useWindowSize ,
105
107
useWait ,
106
108
useUpdateEffect
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 useWindowScroll = ( ) : 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
+ cancelAnimationFrame ( frame . current )
19
+
20
+ frame . current = requestAnimationFrame ( ( ) => {
21
+ setState ( {
22
+ x : window . scrollX ,
23
+ y : window . scrollY
24
+ } )
25
+ } )
26
+ }
27
+
28
+ window . addEventListener ( 'scroll' , handler , {
29
+ capture : false ,
30
+ passive : true
31
+ } )
32
+
33
+ return ( ) => window . removeEventListener ( 'scroll' , handler )
34
+ } , [ ] )
35
+
36
+ return state
37
+ }
38
+
39
+ export default useWindowScroll
You can’t perform that action at this time.
0 commit comments