Created
January 7, 2024 11:14
-
-
Save GalindoSVQ/2dd2629213f58a5bd093600ec123bbb0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import * as React from 'react'; | |
export default function useOrientation() { | |
const [orientation, setOrientation] = React.useState({ | |
angle: 0, | |
type: 'UNKNOWN', | |
}); | |
React.useLayoutEffect(() => { | |
const handleChange = () => { | |
const { angle, type } = window.screen.orientation; | |
setOrientation({ | |
angle, | |
type, | |
}); | |
}; | |
if (window.screen?.orientation) { | |
handleChange(); | |
window.screen.orientation.addEventListener('change', handleChange); | |
} | |
return () => { | |
if (window.screen?.orientation) { | |
window.screen.orientation.removeEventListener('change', handleChange); | |
} | |
}; | |
}, []); | |
return orientation; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackblitz.com/edit/stackblitz-starters-qhuscc?file=src%2FuseOrientation.ts