@react-hookz/web
Version:
React hooks done right, for browser and SSR.
14 lines (13 loc) • 568 B
JavaScript
import { useMediaQuery } from '../useMediaQuery/index.js';
/**
* Checks if screen is in `portrait` or `landscape` orientation.
*
* As `Screen Orientation API` is still experimental and not supported by Safari, this
* hook uses CSS3 `orientation` media-query to check screen orientation.
*/
export function useScreenOrientation(options) {
const matches = useMediaQuery('(orientation: portrait)', {
initializeWithValue: options?.initializeWithValue ?? true,
});
return matches === undefined ? undefined : (matches ? 'portrait' : 'landscape');
}