-
Notifications
You must be signed in to change notification settings - Fork 7
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
1 parent
dc05da9
commit 3bbd33f
Showing
2 changed files
with
39 additions
and
20 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,27 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
const isSupported = typeof window !== 'undefined' && 'screen' in window && 'orientation' in window.screen; | ||
|
||
const useOrientation = () => { | ||
const screenOrientation = (isSupported ? window.screen.orientation : {}) as ScreenOrientation; | ||
|
||
const [orientation, setOrientation] = useState(screenOrientation?.type); | ||
|
||
useEffect(() => { | ||
if (!isSupported) { | ||
return; | ||
} | ||
|
||
const handleOrientationChange = () => setOrientation(screenOrientation.type); | ||
|
||
window.addEventListener('orientationchange', handleOrientationChange); | ||
|
||
return () => { | ||
window.removeEventListener('orientationchange', handleOrientationChange); | ||
}; | ||
}, []); | ||
|
||
return orientation; | ||
}; | ||
|
||
export default useOrientation; |