-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
113 additions
and
5 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
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# `useScroll` | ||
|
||
React sensor hook that re-renders on when scroll position in a DOM element changes. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import {useScroll} from 'react-use'; | ||
|
||
const Demo = () => { | ||
const element = React.useRef(null); | ||
const {x, y} = useScroll(element); | ||
|
||
return ( | ||
<div ref={element}> | ||
<div>x: {x}</div> | ||
<div>y: {y}</div> | ||
</div> | ||
); | ||
}; | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as React from 'react'; | ||
import {storiesOf} from '@storybook/react'; | ||
import {useScroll} from '..'; | ||
import ShowDocs from '../util/ShowDocs'; | ||
|
||
const Demo = () => { | ||
const element = React.useRef(null); | ||
const {x, y} = useScroll(element); | ||
|
||
return ( | ||
<> | ||
<div>x: {x}</div> | ||
<div>y: {y}</div> | ||
<div | ||
ref={element} | ||
style={{ | ||
width: '400px', | ||
height: '400px', | ||
backgroundColor: 'whitesmoke', | ||
overflow: 'scroll' | ||
}}> | ||
|
||
<div style={{width: '2000px', height: '2000px'}}> | ||
Scroll me | ||
</div> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
storiesOf('Sensors/useScroll', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useScroll.md')} />) | ||
.add('Demo', () => | ||
<Demo/> | ||
) |
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
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
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import {useState, useEffect, useRef} from 'react'; | ||
import {isClient} from './util'; | ||
|
||
export interface State { | ||
x: number; | ||
y: number; | ||
} | ||
|
||
const useScroll = (ref): State => { | ||
const frame = useRef(0); | ||
const [state, setState] = useState<State>({ | ||
x: isClient ? window.scrollX : 0, | ||
y: isClient ? window.scrollY : 0 | ||
}); | ||
|
||
useEffect(() => { | ||
const handler = () => { | ||
|
||
frame.current = requestAnimationFrame(() => { | ||
setState({ | ||
x: ref.current.scrollLeft, | ||
y: ref.current.scrollTop | ||
}); | ||
}); | ||
} | ||
|
||
if (ref && ref.current) { | ||
ref.current.addEventListener('scroll', handler, { | ||
capture: false, | ||
passive: true | ||
}); | ||
} | ||
|
||
return () => { | ||
if (frame.current) { | ||
cancelAnimationFrame(frame.current); | ||
} | ||
|
||
if (ref && ref.current) { | ||
ref.current.removeEventListener('scroll', handler); | ||
} | ||
}; | ||
}, [ref]); | ||
|
||
return state; | ||
} | ||
|
||
export default useScroll |