Rooks
HooksBrowser APIs

useOrientation

Returns the current screen orientation and listens for orientation changes.

About

Track the Screen Orientation API's viewport angle and orientation type.


Examples

import React from "react";
import { useOrientation } from "rooks";

export default function App() {
  const orientation = useOrientation();

  if (!orientation) {
    return <div>Reading screen orientation…</div>;
  }

  return (
    <div>
      <h1>Device Orientation</h1>
      <div>
        <strong>Angle:</strong> {orientation.angle}°
      </div>
      <div>
        <strong>Type:</strong> {orientation.type}
      </div>

      <div style={{ marginTop: "20px" }}>
        <p>Try rotating your device to see the orientation change!</p>

        {orientation.type.includes("portrait") && (
          <div style={{ color: "blue" }}>📱 Portrait mode detected</div>
        )}

        {orientation.type.includes("landscape") && (
          <div style={{ color: "green" }}>🔄 Landscape mode detected</div>
        )}
      </div>
    </div>
  );
}
import { useOrientation } from "rooks";

function OrientationCard() {
  const orientation = useOrientation();

  if (!orientation) {
    return null;
  }

  return (
    <div>
      <p>Current orientation: {orientation.type}</p>
      <p>Angle: {orientation.angle}°</p>
      <div
        style={{
          width: 120,
          height: 120,
          display: "grid",
          placeItems: "center",
          border: "1px solid #d1d5db",
          transform: `rotate(${orientation.angle}deg)`,
          transition: "transform 150ms ease",
        }}
      >
        Preview
      </div>
    </div>
  );
}

Parameters

This hook takes no arguments.

Return value

Return valueTypeDescription
orientationScreenOrientation | nullCurrent window.screen.orientation, or null during server rendering and hydration.

ScreenOrientation Properties

PropertyTypeDescription
anglenumberThe orientation angle in degrees (0, 90, 180, or 270)
typestringThe orientation type (e.g., "portrait-primary", "landscape-primary", "portrait-secondary", etc.)

Behavior and lifecycle

The hook reads window.screen.orientation through useSyncExternalStore, subscribes to its change event, and removes the listener on cleanup. It returns the platform object itself, whose angle and type fields change over time.

Compatibility and accessibility

The implementation does not feature-detect screen.orientation: on an unsupported client, reading or subscribing can throw rather than returning null. The server snapshot is null, so keep the initial fallback hydration-safe. Screen orientation describes the display viewport, not a person's physical posture; never make content or controls available in only one orientation.

On this page