-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create useViewportSize hook (#933)
- Loading branch information
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/components/utils/useViewportSize/__tests__/useViewportSize.test.tsx
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,25 @@ | ||
import {act, fireEvent, renderHook} from '@testing-library/react'; | ||
|
||
import {useViewportSize} from '..'; | ||
|
||
test('Check useViewportSize correct work', () => { | ||
const view = renderHook(() => useViewportSize()); | ||
|
||
act(() => { | ||
window.innerHeight = 500; | ||
window.innerWidth = 500; | ||
}); | ||
|
||
fireEvent(window, new Event('resize')); | ||
|
||
expect(view.result.current).toEqual({height: 500, width: 500}); | ||
|
||
act(() => { | ||
window.innerHeight = 1200; | ||
window.innerWidth = 1200; | ||
}); | ||
|
||
fireEvent(window, new Event('resize')); | ||
|
||
expect(view.result.current).toEqual({height: 1200, width: 1200}); | ||
}); |
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,2 @@ | ||
export {useViewportSize} from './useViewportSize'; | ||
export type {ViewportSize} from './useViewportSize'; |
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 @@ | ||
import React from 'react'; | ||
|
||
export interface ViewportSize { | ||
width?: number; | ||
height?: number; | ||
} | ||
|
||
const getViewportSize = (): ViewportSize => ({ | ||
width: window?.visualViewport?.width ?? window?.innerWidth ?? undefined, | ||
height: window?.visualViewport?.height ?? window?.innerHeight ?? undefined, | ||
}); | ||
|
||
/** | ||
* A hook to get the size of the viewport when resizing | ||
* | ||
* @return - {width, height} | ||
*/ | ||
export const useViewportSize = (): ViewportSize => { | ||
const [size, setSize] = React.useState<ViewportSize>(getViewportSize()); | ||
|
||
React.useEffect(() => { | ||
const onResize = () => { | ||
let newSize = getViewportSize(); | ||
if (newSize.width === size?.width && newSize.height === size?.height) { | ||
newSize = size; | ||
} | ||
setSize(newSize); | ||
}; | ||
|
||
(window.visualViewport ?? window).addEventListener('resize', onResize); | ||
|
||
return () => { | ||
(window.visualViewport ?? window).removeEventListener('resize', onResize); | ||
}; | ||
}, [size]); | ||
|
||
return size; | ||
}; |