Repository navigation
Description
Background and Motivation
Microsoft.AspNetCore.Components.Platform (#69304) gives Blazor applications typed access to standard browser APIs. Window.Screen (#69969) reads the size and the color depth of the screen. But an application that changes its layout or behavior when the user rotates the device cannot read the orientation, and it cannot learn when the orientation changes. Today it needs IJSRuntime and its own JavaScript for that.
This proposal adds screen.orientation, the last part of the browser context family (#69872) that #69969 left for later. It covers the type and angle attributes of ScreenOrientation and its change event. The event uses the event listener API from #69922, as the change event of Cookie Store does.
lock() and unlock() are not part of this proposal. They are not Baseline, and Safari does not support them.
The implementation is in #70562.
Usage Examples
builder.Services.AddBrowserPlatform();@using Microsoft.AspNetCore.Components.Platform
@implements IAsyncDisposable
@inject IBrowserPlatform Browser
<div class="@(portrait ? "stacked" : "side-by-side")">
...
</div>
@code {
private bool portrait;
private IAsyncDisposable? changeListener;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
var orientation = Browser.Window.Screen.Orientation;
changeListener = await orientation.AddEventListenerAsync(ScreenOrientation.ChangeEvent, _ => UpdateAsync());
await UpdateAsync();
}
}
private async Task UpdateAsync()
{
var type = await Browser.Window.Screen.Orientation.GetTypeAsync();
portrait = type == OrientationType.PortraitPrimary || type == OrientationType.PortraitSecondary;
StateHasChanged();
}
public async ValueTask DisposeAsync()
{
if (changeListener is not null)
{
await changeListener.DisposeAsync();
}
}
}The API works in Interactive Server and WebAssembly.
Proposed API
namespace Microsoft.AspNetCore.Components.Platform;
+public sealed class ScreenOrientation
+{
+ public static EventType<ScreenOrientation, Event> ChangeEvent { get; }
+ public ValueTask<OrientationType> GetTypeAsync();
+ public ValueTask<ushort> GetAngleAsync();
+ public ValueTask<IAsyncDisposable> AddEventListenerAsync<TEvent>(EventType<ScreenOrientation, TEvent> type, Func<TEvent, Task> listener);
+ public ValueTask<IAsyncDisposable> AddEventListenerAsync<TEvent>(EventType<ScreenOrientation, TEvent> type, Func<TEvent, Task> listener, CancellationToken cancellationToken);
+}
+
+public readonly struct OrientationType : IEquatable<OrientationType>
+{
+ public OrientationType(string value);
+ public static OrientationType PortraitPrimary { get; }
+ public static OrientationType PortraitSecondary { get; }
+ public static OrientationType LandscapePrimary { get; }
+ public static OrientationType LandscapeSecondary { get; }
+ public string Value { get; }
+ public bool Equals(OrientationType other);
+ public override bool Equals(object? obj);
+ public override int GetHashCode();
+ public override string ToString();
+ public static bool operator ==(OrientationType left, OrientationType right);
+ public static bool operator !=(OrientationType left, OrientationType right);
+}
+
+public sealed class Event
+{
+ public Event();
+}
+
+public static class ScreenScreenOrientationExtensions
+{
+ extension(Screen screen)
+ {
+ public ScreenOrientation Orientation { get; }
+ }
+}The names and types follow the Web IDL of Screen Orientation. unsigned short becomes ushort.
screen.orientation is [SameObject], so Screen.Orientation is a synchronous property, like Window.Screen. Every caller gets the same instance, and the DI scope disposes it. The orientation changes while the page is open, so each method reads the current value from the browser.
OrientationType is a readonly struct that wraps the Web IDL string, not an enum. This is the rule for every Web IDL enum in the package (#69991), as for CookieSameSite. A value that a newer browser returns is kept as it is, even if the type does not define it. Unlike CookieSameSite, the Web IDL gives OrientationType no default. So default(OrientationType) holds no string. Its Value throws InvalidOperationException, and its ToString() returns an empty string.
The change event has one key, ScreenOrientation.ChangeEvent, as #69922 describes. ScreenOrientation gets the two AddEventListenerAsync overloads that CookieStore has.
The change event is a plain Event in the specification, with no members of its own. #70529 (the online and offline events) proposes the same empty Event class for such events. This proposal adds the same type on purpose, so that the two proposals and their PRs can be reviewed and merged in either order. The PR that merges second will take the type from the first. Event is listed here only so that this proposal is complete on its own.
The extension class is named <Owner><Type>Extensions, after the type that the property returns, as the porting skill says. So it is ScreenScreenOrientationExtensions.
A browser without screen.orientation, such as Safari before 16.4, makes every method throw NotSupportedException, as Cookie Store does when the browser has no cookieStore.
Alternative Designs
Applications can keep using IJSRuntime with their own JavaScript.
#69969 showed OrientationType as a C# enum. An enum cannot hold a value that a newer browser adds, so #69991 chose the struct for all Web IDL enums.
#69969 also named the extension class ScreenOrientationExtensions. That name reads well, but it breaks the naming rule that every other family follows, so this proposal uses ScreenScreenOrientationExtensions.
ScreenOrientation could read type and angle in one call and return a snapshot. That would save one round trip in Interactive Server. But a snapshot is not the browser API, as #69969 said for Screen.
The change event could have its own event type, such as ScreenOrientationChangeEvent. The specification fires a plain Event, so it uses the shared Event from #70529.
Risks
GetTypeAsync() sits next to object.GetType(). It follows the rule that a readable attribute foo becomes GetFooAsync(), and the return type tells the two apart.
Event is a general name in the Microsoft.AspNetCore.Components.Platform namespace, as #70529 says. It is the Web IDL name, as StorageEvent and CookieChangeEvent are.
Like the other families of the package, the API is unavailable before interactivity.
Source Justifications
- The screen orientation is part of the browser context family: "Screen and screen orientation." (Add browser context and capability APIs #69872)
- Applications read browser information to adapt their behavior: "Expose browser environment information needed by applications to adapt their behavior." (Add browser context and capability APIs #69872)
- The Epic asks for the reaction to a rotated device without polling: "The UI updates by itself when the user changes a setting in another tab, goes offline, or rotates the device. The app does not poll." (Components.Platform typed browser APIs for Blazor #69987)
- The orientation was left out of the Screen proposal for later: "Add browser context and capability APIs #69872 also lists the screen orientation. It will follow in a later PR, after Add Navigator to Microsoft.AspNetCore.Components.Platform #69968 (Navigator) is merged." ([API Proposal] Add the Screen to the typed browser platform APIs for Blazor #69969)
- The orientation is an object below
Screenthat returns the same object: "[SameObject] readonly attribute ScreenOrientation orientation;" (screen-orientation.idl) - The members and their types: "
readonly attribute OrientationType type;", "readonly attribute unsigned short angle;", "attribute EventHandler onchange;" (screen-orientation.idl) - The values of
OrientationType: ""portrait-primary"", ""portrait-secondary"", ""landscape-primary"", ""landscape-secondary"" (screen-orientation.idl) - The feature is for adapting the layout: "With this API, you can adapt an application's layout or behavior in response to changes in orientation." (
screen-orientationin web-features) type,angle, and thechangeevent are Baseline widely available: "baseline: high", "baseline_high_date: 2025-09-27", "api.ScreenOrientation.change_event" (screen-orientationin web-features)- Older Safari has no
screen.orientation: "safari: "16.4"" (screen-orientationin web-features) lock()andunlock()are not Baseline: "baseline: false" (screen-orientation-lockin web-features)- Every Web IDL enum is a struct: "Map every Web IDL enum to a hand-written
readonly structthat wraps the string, whether the application passes it or the browser returns it." (.github/skills/port-components-platform-api/references/type-mapping.md) - The rule for an enum without a default: "The constructor stores the string as it is, so the field is
nullonly indefault(T).Valuethrows for it, butToString()must not" (.github/skills/port-components-platform-api/references/type-mapping.md) - The name of the extension class: "Name the extension class
<Owner><Type>Extensionsafter the type that the property returns" (.github/skills/port-components-platform-api/SKILL.md) - The event key: "Give each event one key, a static
EventType<TTarget, TEvent>property named<Name>Event, on the target type" (.github/skills/port-components-platform-api/SKILL.md) - A target other than
Windowgets its own listener methods: "When the target is notWindow, give it twoAddEventListenerAsync<TEvent>overloads, one with aCancellationTokenand one without" (.github/skills/port-components-platform-api/SKILL.md) - The same
Eventtype for theonlineandofflineevents: "Eventis the .NET type for such an event, and it has no members for now." ([API Proposal] Add the online and offline events to the typed browser platform APIs for Blazor #70529) - The event object copies nothing: "
EventTypes.Create<ScreenOrientation, Event>("change", "[{}]")" (ScreenOrientation.cs) - A missing
screen.orientationthrowsNotSupportedException: "reference = await _jsRuntime.GetValueAsync<IJSObjectReference?>("screen.orientation").ConfigureAwait(false);", "throw new NotSupportedException(NotAvailableMessage, ex);" (ScreenOrientation.cs) - The API works in Interactive Server and WebAssembly: "
ScreenPaths = ["/screen-server", "/screen-wasm"]" (ScreenTests.cs)
Activity