Skip to content

[mediaqueries-5] Simplify the Script Control of User Preferences API #14936

Backlog

Description

@jasonwilliams

Follow-up to #14493. At TPAC 2026 the group agreed to keep the Web Preferences API and keep iterating on it, but asked for a smaller API surface scoped to prefers-* media features. This is the proposed replacement for the current PreferenceManager / PreferenceObject design:

enum PreferenceKind {
  "prefers-color-scheme",
  "prefers-contrast",
  "prefers-reduced-motion",
  "prefers-reduced-transparency"
};

[SecureContext]
partial interface Navigator {
  Promise<undefined> setPreferenceOverride(PreferenceKind kind, CSSOMString value);
  Promise<undefined> clearPreferenceOverride(PreferenceKind kind);
  CSSOMString? getPreferenceOverride(PreferenceKind kind);
};

Example

A "System / Light / Dark" toggle. Existing @media (prefers-color-scheme: dark) CSS needs no changes.

const select = document.querySelector("#theme");

// Sync getter: the toggle shows the right state on first render.
select.value = navigator.getPreferenceOverride("prefers-color-scheme") ?? "system";

select.addEventListener("change", async () => {
  if (select.value === "system") {
    await navigator.clearPreferenceOverride("prefers-color-scheme");
  } else {
    await navigator.setPreferenceOverride("prefers-color-scheme", select.value);
  }
});

Semantics

  • setPreferenceOverride() sets a per-origin override for the given media feature. It rejects with TypeError if the value isn't valid for that feature, and with NotAllowedError if the UA declines (e.g. in a cross-origin iframe or a sandboxed frame). It resolves once the override has been applied to the document, so matchMedia(), styles and the used color scheme reflect it.
  • clearPreferenceOverride() removes the override and resolves under the same conditions. Clearing when no override is set is a no-op.
  • getPreferenceOverride() returns only an override set through this API, or null. It never exposes the user's underlying preference. See [mediaqueries-5] Ability to retrieve the original value of an overridden preference #11580
  • getPreferenceOverride() is synchronous and always agrees with media query evaluation. While a set or clear is pending, it returns the previous value; the getter and matchMedia() switch at the same moment, and the promise resolves after that.
  • Cross-origin iframes: the top-level document's overrides apply to media query evaluation in cross-origin iframes. In those frames, getPreferenceOverride() returns null, and setPreferenceOverride() / clearPreferenceOverride() reject with NotAllowedError. See [mediaqueries-5] Preference override interaction with iframes #11577
  • If the UA does not support the media feature named by kind, setPreferenceOverride() and clearPreferenceOverride() reject with NotSupportedError, and getPreferenceOverride() returns null.

Why the setters are async: per-origin overrides may need to be persisted or coordinated outside the content process. The promise isn't there to allow a permission prompt; the old requestOverride() name implied one, which is why it was renamed.

What's removed and why:

  • PreferenceManager, the five PreferenceObjects and their EventTargets. We have put together a more minimal API.
  • validValues: value support can be detected with matchMedia(), and sites generally hard-code their toggle options anyway.
  • Change events: preference changes from the UA or OS settings are observable with matchMedia().onchange. Known limitation: no event fires when an override is set to the value the media query already resolves to. Sites that need to sync toggle UI across documents can use BroadcastChannel.
  • prefers-reduced-data: this can be added later as an enum value without changing the interface.

Open questions

  1. Should clearing use clearPreferenceOverride(kind) or setPreferenceOverride(kind, null)?
    1. Proposed: we should use a clearPreferenceOverrife(kind) method which matches other APIs on navigator
  2. In a cross-origin iframe, does the top-level document's override take precedence over one the iframe's own origin has set?
    1. Proposed: the top-level document's override wins, for visual consistency.

Activity

  1. fantasai commented on Dec 3, 2026

    @fantasai
    Collaborator

    In a cross-origin iframe, does the top-level document's override take precedence over one the iframe's own origin has set?

    I think the iframe's own origin should win here. If the iframe wants to inherit from its embedder, it should not set the override.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions