Skip to main content

Implementing for Web/PWAs

While designing the plugin's API, we found out that the web already supports screen orientation functionality (except on mobile devices, of course). You might be asking: "What is the purpose of our plugin having a web implementation? Couldn't we programmatically detect if the user is on the web and use the Screen Orientation Web API, otherwise, use the plugin?"

The mantra behind Web Native applications is "write once, run anywhere." This applies to plugins as well; developers using Napacitor plugins ought to be able to use the same plugin class and methods and have them implemented for all platforms.

Therefore, we will be good developer-citizens and wrap the Screen Orientation Web API inside the web implementation of the ScreenOrientation plugin.

The web class​

Open a new file src/plugins/screen-orientation/web.ts. This is where we will write the web implementation of the ScreenOrientation plugin.

Unlike Capacitor, the Napacitor web implementation is a plain class — there is no WebPlugin base class to extend. The class simply implements the plugin interface; the web option of registerPlugin takes care of hooking it up.

Start by declaring the ScreenOrientationWeb class, implementing ScreenOrientationPlugin:

import type { PluginListenerHandle } from '@napacitor/core';
import type {
ScreenOrientationPlugin,
ScreenOrientationResult,
} from './definitions';

export class ScreenOrientationWeb implements ScreenOrientationPlugin {
async orientation(): Promise<ScreenOrientationResult> {
if (typeof screen === 'undefined' || !screen.orientation) {
throw new Error(
'ScreenOrientation API not available in this browser',
);
}
return { type: this.map(screen.orientation.type) };
}

async addListener(
eventName: 'screenOrientationChange',
listenerFunc: (orientation: ScreenOrientationResult) => void,
): Promise<PluginListenerHandle> {
void eventName;
const handler = () =>
listenerFunc({ type: this.map(window.screen.orientation.type) });
window.screen.orientation.addEventListener('change', handler);
return {
remove: async () =>
window.screen.orientation.removeEventListener('change', handler),
};
}

async removeAllListeners(): Promise<void> {
// Listeners are removed individually via each handle's remove().
}

private map(type: string): ScreenOrientationResult['type'] {
return type.includes('portrait') ? 'portrait' : 'landscape';
}
}

A few things to note:

  • Because there is no base class, the web implementation manages its own listeners. addListener must return a handle with an async remove() method; removeAllListeners is only responsible for any state the class itself tracks.
  • @napacitor/core resolves registerPlugin('ScreenOrientation') to this class on the web platform.
  • The 'screenOrientationChange' event name must match the one the C# implementation emits.

Registering the web implementation​

To register ScreenOrientationWeb as our plugin's web implementation, we need to use the second input parameter of registerPlugin(). Open src/plugins/screen-orientation/index.ts and update the declaration of the ScreenOrientation variable like so:

const ScreenOrientation = registerPlugin<ScreenOrientationPlugin>(
'ScreenOrientation',
{
web: () =>
import('./web').then(m => new m.ScreenOrientationWeb()),
},
);

Give it a test drive!​

Test out the web implementation. Serve your application using npm run dev, and you can use your browser's Development Tools to emulate a mobile device in both portrait and landscape screen orientations. You should see the different designs as you manually rotate the orientation using the developer tooling.

One platform implemented, one to go: the C# implementation that runs inside the MAUI host on Android, iOS, macOS, and Windows. Before diving into the C# code, take a look at the C# Plugin Guide for the fundamentals.