-
Notifications
You must be signed in to change notification settings - Fork 584
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useViewportState): introduces useViewportState
- Loading branch information
Antonio Rù
committed
Jun 13, 2022
1 parent
293e1f4
commit d071fea
Showing
16 changed files
with
114 additions
and
2 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
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
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
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,38 @@ | ||
# useViewportState | ||
|
||
Returns information on the current viewport state | ||
|
||
It's built on top of [useWindowResize](./useWindowResize.md) and [useWindowScroll](./useWindowScroll.md). | ||
|
||
### Why? 💡 | ||
|
||
- takes care of adding the listener for the window resize event. | ||
- takes care of removing the listener when the component will unmount | ||
|
||
### Basic Usage: | ||
|
||
```jsx harmony | ||
import { useState } from 'react'; | ||
import useViewportState from 'beautiful-react-hooks/useViewportState'; | ||
|
||
const WindowSizeReporter = () => { | ||
const { width, height, scrollX, scrollY } = useViewportState(); | ||
|
||
return ( | ||
<DisplayDemo> | ||
<p>window width: {width}</p> | ||
<p>window height: {height}</p> | ||
<p>window scrollX: {scrollX}</p> | ||
<p>window scrollY: {scrollY}</p> | ||
</DisplayDemo> | ||
); | ||
}; | ||
|
||
<WindowSizeReporter /> | ||
``` | ||
|
||
### Mastering the hook | ||
|
||
#### ✅ When to use | ||
|
||
- When in need of reading common information about the current viewport |
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
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,39 @@ | ||
import { useState } from 'react' | ||
import useWindowScroll from './useWindowScroll' | ||
import useWindowResize from './useWindowResize' | ||
import useThrottledCallback from './useThrottledCallback' | ||
import useDidMount from './useDidMount' | ||
|
||
export interface ViewportState { | ||
width: number, | ||
height: number, | ||
scrollX: number, | ||
scrollY: number, | ||
} | ||
|
||
/** | ||
* Returns updated information on the current viewport state | ||
*/ | ||
const useViewportState = (debounceBy: number = 250) => { | ||
const [viewport, setViewport] = useState<ViewportState>({ width: 0, height: 0, scrollY: 0, scrollX: 0 }) | ||
const onScroll = useWindowScroll() | ||
const onResize = useWindowResize() | ||
const onMount = useDidMount() | ||
|
||
const saveInfo = useThrottledCallback(() => { | ||
setViewport({ | ||
width: window.innerWidth, | ||
height: window.innerHeight, | ||
scrollX: window.scrollX, | ||
scrollY: window.scrollY, | ||
}) | ||
}, [setViewport], debounceBy) | ||
|
||
onScroll(saveInfo) | ||
onResize(saveInfo) | ||
onMount(saveInfo) | ||
|
||
return viewport | ||
} | ||
|
||
export default useViewportState |
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,16 @@ | ||
import { cleanup, renderHook } from '@testing-library/react-hooks' | ||
import useViewportState from '../dist/useViewportState' | ||
import assertHook from './utils/assertHook' | ||
|
||
describe('useViewportState', () => { | ||
|
||
beforeEach(() => cleanup()) | ||
|
||
assertHook(useViewportState) | ||
|
||
it('should return an object containing information on the current window state', () => { | ||
const { result } = renderHook(() => useViewportState()) | ||
|
||
expect(result.current).to.be.an('object').that.has.all.keys('width', 'height', 'scrollY', 'scrollX') | ||
}) | ||
}) |