Re: [csswg-drafts] [mediaqueries-5] Script control of (prefers-*) queries (#6517)

The overall API shape looks good to me, moving this to a web preferences API that can operate per-origin seems to solve many more problems that have been mentioned above. It does feel like the right place to solve this problem.

Some observations.

## Fetching computed value?

It looks like this API is strictly for overrides so those wanting to offer a toggle would still need to do an initial check with `matchMedia` to get the current value. Like below:

```js
let currentValue = null;

// We don't know what the initial value is so lets get it.
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
    currentValue = 'dark'
}

button.onClick(() => {
 const newVal = (currentValue === 'dark') ? 'light' : 'dark';
 navigator.preferences.colorScheme.requestOverride(newVal)
  .then(() => {
    // The preference override was successful.
  })
  .catch((error) => {
    // The preference override request was rejected.
  });
})
```


Something like this might be better

```js
button.onClick(() => {
 const newVal = (navigator.preferences.colorScheme.value === 'dark') ? 'light' : 'dark';
 navigator.preferences.colorScheme.requestOverride(newVal)
  .then(() => {
    // The preference override was successful.
  })
  .catch((error) => {
    // The preference override request was rejected.
  });
})
```
It doesn't feel the most ergonomic.
I wonder if the API provides the computed value we could skip the first step entirely.
Looks like this is covered in: https://github.com/WICG/web-preferences-api/issues/7

Although I don't fully understand how the API shape https://github.com/WICG/web-preferences-api/issues/7#issuecomment-1721930664 solves this so it would be good to see an example.

## Should the fulfilled promise return the set value?
For those who are using a toggle, I wonder if the fulfilled promise should return the value that is now set, ergonomically this would make it easy for website authors to pass that value to their `currentSet` state.

## iFrames
I guess there's questions on how to deal with iframes (if we even need to). But that's being discussed here:
https://github.com/WICG/web-preferences-api/issues/8

I guess now that this has moved into a potential browser API and into the WICG, I guess its no longer a CSS WG matter, unless any here feel against the direction this is in.

If people are happy with the control of this being in Web Preferences, then @tabatkins I would be interested in your thoughts on where https://github.com/WICG/web-preferences-apigoes in terms of reviewal, does this go into the Web Apps working group?


-- 
GitHub Notification of comment by jasonwilliams
Please view or discuss this issue at https://github.com/w3c/csswg-drafts/issues/6517#issuecomment-1834350317 using your GitHub account


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Thursday, 30 November 2023 18:38:31 UTC