useOrientation
The useOrientation hook reports the device screen orientation and updates whenever it changes. It uses the Screen Orientation API and falls back to comparing the window width and height in browsers that do not support it.
Usage
Installation
import { useOrientation } from "hookstorm";
Parameters
This hook does not take any parameters.
Return Values
- angle — The rotation angle in degrees (
0,90,180or270). Always0in the fallback mode. - type — The orientation type, for example
portrait-primaryorlandscape-secondary.
Example Usage
import { ReactElement } from "react";
import { useOrientation } from "hookstorm";
export default function OrientationInfo(): ReactElement {
const { type, angle } = useOrientation();
return (
<p>
{type.startsWith("landscape") ? "Landscape" : "Portrait"} ({angle}°)
</p>
);
}
Notes
- During server-side rendering the hook reports
portrait-primarywith an angle of0, then updates on the client. - To only react to the orientation,
useMediaQuerywith(orientation: landscape)is a lighter alternative.