Skip to content

Instantly share code, notes, and snippets.

@GalindoSVQ
Created January 7, 2024 11:14
Show Gist options
  • Save GalindoSVQ/2dd2629213f58a5bd093600ec123bbb0 to your computer and use it in GitHub Desktop.
Save GalindoSVQ/2dd2629213f58a5bd093600ec123bbb0 to your computer and use it in GitHub Desktop.
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;
}
@GalindoSVQ
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment