Skip to main content

@napacitor/display

Screen display plugin: reads screen metrics, listens for display changes, keeps the screen awake, and supports runtime screen orientation locking.

Installation​

To use npm:

npm install @napacitor/display

To use yarn:

yarn add @napacitor/display

Sync native files:

npx nap sync

Usage​

import { Display } from '@napacitor/display';

// Read screen info
const info = await Display.getInfo();
// { width, height, density, orientation, rotation, refreshRate }

// Listen for display changes (orientation/size/density, etc.)
const handle = await Display.addListener('displayChange', (info) => {
console.log('Display metrics changed:', info);
});
// handle.remove()

// Prevent the device from locking/sleeping
await Display.setKeepAwake({ keepAwake: true });
await Display.setKeepAwake({ keepAwake: false });

// Lock orientation at runtime
await Display.lock({ orientation: 'portrait' });
await Display.lock({ orientation: 'landscape' });

// Unlock orientation, falling back to the global setting
await Display.unlock();

How It Works​

Wraps the APIs described in the .NET MAUI DeviceDisplay documentation:

  • getInfo() → DeviceDisplay.Current.MainDisplayInfo (Width / Height / Density / Orientation / Rotation / RefreshRate);
  • addListener('displayChange') → the MainDisplayInfoChanged event (fires when any screen metric changes);
  • setKeepAwake() → KeepScreenOn (prevents the device from locking/sleeping);
  • lock() / unlock() → runtime orientation locking (see below).

Platform implementations of runtime orientation locking​

PlatformImplementationNotes
AndroidActivity.RequestedOrientationPortrait / Landscape / Unspecified (unlock, falls back to the manifest's global setting)
iOS / Mac CatalystUIDevice.SetValueForKey("orientation") + AttemptRotationToDeviceOrientationLegacy runtime orientation mechanism; may not take effect in some scenarios on iOS 16+; the orientation you lock must be declared in UISupportedInterfaceOrientations in Info.plist
WindowsNot supportedThrows NOT_IMPLEMENTED; resize the window or use system display settings instead
WebScreen Orientation API (screen.orientation.lock)Usually requires fullscreen or an installed PWA, otherwise throws

Permissions​

Android​

No additional configuration required. Orientation locking applies to the current Activity and needs no manifest declaration; for a global orientation restriction, set android:screenOrientation on the main activity in AndroidManifest.xml.

iOS​

  • Runtime orientation locking requires the target orientation to be declared in UISupportedInterfaceOrientations (iPad: UISupportedInterfaceOrientations~ipad) in Info.plist;
  • By default, an iPad allows Multitasking and its orientation cannot be locked — set UIRequiresFullScreen to YES in Info.plist to allow locking;
  • The runtime orientation mechanism may not take effect in some scenarios on iOS 16+; combine with the Info.plist configuration above for hard locks.

Windows​

No setup is required (getInfo / setKeepAwake / displayChange are available; lock / unlock throw NOT_IMPLEMENTED).

Web​

  • setKeepAwake: relies on the Wake Lock API; throws when the browser doesn't support it;
  • lock / unlock: rely on the Screen Orientation API, which usually requires fullscreen or an installed PWA;
  • getInfo's refreshRate is unknowable in browsers, always 0.

API​

getInfo()​

Gets the current screen info.

Returns: Promise<DisplayInfo>

setKeepAwake(options)​

Sets whether the screen stays awake (prevents the device from locking/sleeping).

ParamType
optionsKeepAwakeOptions

Returns: Promise<void>

lock(options)​

Locks the screen orientation.

ParamType
optionsLockOptions

Returns: Promise<void>

unlock()​

Unlocks the orientation, falling back to the global setting.

Returns: Promise<void>

addListener('displayChange', ...)​

Listens for display metric changes (orientation/size/density, etc.).

ParamType
eventName'displayChange'
callbackDisplayChangeCallback

Returns: Promise<PluginListenerHandle> & PluginListenerHandle

removeAllListeners()​

Removes all listeners of this plugin.

Returns: Promise<void>

Interfaces​

DisplayInfo​

PropTypeDescription
widthnumberPixel width.
heightnumberPixel height.
densitynumberScreen density (1x = 1.0, 2x = 2.0).
orientation'portrait' | 'landscape'Current orientation.
rotationnumberRotation relative to natural orientation (0 / 90 / 180 / 270).
refreshRatenumberScreen refresh rate in Hz (0 when unknown to the platform).

KeepAwakeOptions​

PropTypeDescription
keepAwakebooleanWhether to keep the screen awake (prevent locking/sleep).

LockOptions​

PropTypeDescription
orientation'portrait' | 'landscape'The orientation to lock (invalid values throw INVALID_ARGUMENTS).