@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')→ theMainDisplayInfoChangedevent (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
| Platform | Implementation | Notes |
|---|---|---|
| Android | Activity.RequestedOrientation | Portrait / Landscape / Unspecified (unlock, falls back to the manifest's global setting) |
| iOS / Mac Catalyst | UIDevice.SetValueForKey("orientation") + AttemptRotationToDeviceOrientation | Legacy 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 |
| Windows | Not supported | Throws NOT_IMPLEMENTED; resize the window or use system display settings instead |
| Web | Screen 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) inInfo.plist; - By default, an iPad allows Multitasking and its orientation cannot be locked — set
UIRequiresFullScreentoYESinInfo.plistto allow locking; - The runtime orientation mechanism may not take effect in some scenarios on iOS 16+; combine with the
Info.plistconfiguration 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'srefreshRateis 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).
| Param | Type |
|---|---|
options | KeepAwakeOptions |
Returns: Promise<void>
lock(options)
Locks the screen orientation.
| Param | Type |
|---|---|
options | LockOptions |
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.).
| Param | Type |
|---|---|
eventName | 'displayChange' |
callback | DisplayChangeCallback |
Returns: Promise<PluginListenerHandle> & PluginListenerHandle
removeAllListeners()
Removes all listeners of this plugin.
Returns: Promise<void>
Interfaces
DisplayInfo
| Prop | Type | Description |
|---|---|---|
width | number | Pixel width. |
height | number | Pixel height. |
density | number | Screen density (1x = 1.0, 2x = 2.0). |
orientation | 'portrait' | 'landscape' | Current orientation. |
rotation | number | Rotation relative to natural orientation (0 / 90 / 180 / 270). |
refreshRate | number | Screen refresh rate in Hz (0 when unknown to the platform). |
KeepAwakeOptions
| Prop | Type | Description |
|---|---|---|
keepAwake | boolean | Whether to keep the screen awake (prevent locking/sleep). |
LockOptions
| Prop | Type | Description |
|---|---|---|
orientation | 'portrait' | 'landscape' | The orientation to lock (invalid values throw INVALID_ARGUMENTS). |