8

On better browsers: arbitrary media queries and browser UIs | Kilian Valkhof

 3 years ago
source link: https://kilianvalkhof.com/2022/css-html/on-better-browsers-arbitrary-media-queries-and-browser-uis/?ref=sidebar
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

On better browsers: arbitrary media queries and browser UIs

Kilian Valkhof

Building tools that make developers awesome.

Search term

On better browsers: arbitrary media queries and browser UIs

CSS & HTML, 14 September 2022, 3 minute read

This morning Kitty Giraudel tweeted about an imaginary media query that would indicate right- or left-handedness and it made me imagine a future where sites can register support for one or more media features through a browser API, and the browser would offer these options in the UI.

Two years ago I requested something similar to the Chromium team: making it possible to emulate arbitrary media queries in the devtools. At the time I also wrote about that on this blog: I want my own media queries in browsers

That never went anywhere, but this is more or less a continuation of that thought. Combining it with Kitty's imaginary "prefers-main-side", and the idea that browsers should expose media query settings on a per-origin basis like page zoom, here's an idea that would be even more amazing to have in browsers:

Arbitrary media feature support

What if a site could register a media feature:

window.registerMediaFeature({
    name: "prefers-main-side", 
    options: ['right', 'left'],
    default: 'right',
});

After which a browser would:

  • Understand the media feature in your CSS
  • Match the default value and apply the CSS
  • Provide an appropriate toggle UI in the address bar

This doesn’t just give you more ways to offer a customized experience for your visitors, at the same time this frees up real estate on the site itself because it no longer has to reserve space to provide a fully custom UI.

It should work both for known media features, like prefers-color-scheme, completely imagined ones like prefers-main-side, or not-yet-implemented features like prefers-reduced-transparency.

Overwriting known media queries

Furthermore, it should allow overwriting the values of known media features to provide more options. For example, Twitter comes with two dark modes: "dim" and "lights out". prefers-color-scheme only has "light" and "dark", so they would extend prefers-color-scheme with a new value, and update the names to match their brand:

window.registerMediaFeature({
  name: "prefers-color-scheme", 
  options: {
    light: "Default",
    dim: "Dim"
    dark: "Lights out",
  }
  default: 'light',
});

This way, a browser could still match the OS level setting (light or dark) to the options offered by Twitter as well as provide a UI to switch to "Dim", allowing Twitter to get rid of a piece of custom in-page UI that does the same thing.

The browser UI could be exposed in a dropdown similar to how Polypane has been showing it for a while, with big toggles that make it easy to see both which options have been selected as well as which options there are.

The Polypane UI with a list of media feature toggles visible

Browsers could automatically detect usage of regular media features and show those in the same UI, storing the selection per-origin. That way the choices a user made will get remembered next time they visit your site.

To prevent abuse, a website could ask for permission before they're allowed to register media features, or they could be limited to a fixed number of registered media features. Edit: Evan suggested explicitly mentioning that the options would be same-origin restricted to prevent them from being used for fingerprinting.

But that's getting ahead of myself. What do you think of the concept? Reply on Twitter with your thoughts!

Update Looks like Kitty wrote about it themselves too: Dominant hand respecting design and they came up with a very similar API (though I think their use of the CSS global is better!):

CSS.registerMedia({
  name: 'prefers-dominant-hand',
  syntax: 'start | end | no-preference',
  initialValue: 'no-preference',
})

Now we just need to get the browsers on board ;)

Hi, I'm Kilian. I make Polypane, the browser for responsive web development and design. If you're reading this site, that's probably interesting to you. Try it out!

Related Posts

I want my own media queries in browsers
19 October 2020, 4 minute read

We’re on the verge of a whole lot of new CSS media queries becoming available. But if you’re a developer that wants to get a head start and try them out, you’re out of luck. Browsers are prototyping them one by one before making them available and we as developers have to wait.

Digging Deep into Media Queries with Alex Trost of Frontend.horse
4 March 2022, < 1 minute read

On the Frontend horse Livestream (Which I highly recommend you subscribe to!) I joined Alex Trost to take a look at the Frontend.horse site to add support for the many different user preference media queries that exist now: prefers-color-scheme, prefers-reduced-motion, prefers-reduced-data, prefers-contrast and forced-colors.

The complete guide to CSS Media Queries
15 June 2020, < 1 minute read

Media queries are what make modern responsive design possible. With them you can set different styling based on things like a users screen size, device capabilities or user preferences. But how do they work, which ones are there and which ones should you use?

</body


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK