-
Notifications
You must be signed in to change notification settings - Fork 668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[css-conditional] CSS.supports for media queries? #5272
Comments
can you explain a little more why you want to determine if the query is supported regardless of whether it matches? Not saying there's not reason to do that, but knowing your reason may help figure out what to do about it. |
The DevTools patch I linked to is an example: we want to show UI to emulate the |
What about let testMedia = str => matchMedia(`${str}, not all and ${str}`).matches;
testMedia("(prefers-this-doesnt-exist: lolwat)"); // false
testMedia("(prefers-color-scheme: light)"); // true
testMedia("(prefers-color-scheme: dark)"); // true
testMedia("(prefers-color-scheme: lolwat)"); // false or let testMedia = str => matchMedia(str).media === str;
testMedia("(prefers-this-doesnt-exist: lolwat)"); // false
testMedia("(prefers-color-scheme: light)"); // true
testMedia("(prefers-color-scheme: dark)"); // true
testMedia("(prefers-color-scheme: lolwat)"); // false Edit: the 2nd suggestion does no longer work because any |
I like it! Much simpler than the workaround I came up with, but also a little more magical / less direct. |
On a similar note (let me know if you want this raised as a separate issue), how can this be detected in pure CSS? For example with So ideally I'd be able to do something like this:
That way I don't have to update every single instance of the font-family use across all my CSS but can just take care of it once, at the font-face definition. But this isn't possible AFAIK, firstly because @supports can't check media items, and secondly because you can't Similarly, I preload my fonts so want to do that conditionally:
But can't because this will only work for those browsers that recognise Is my understanding correct or is there a way to handle this in pure CSS? If not should there be? |
Note the updated #5272 (comment). This is related to #7595 (comment). I just updated my previous blog post accordingly. |
let testMedia = str => matchMedia(`${str}, not all and ${str}`).matches; This suggestion would only cover a small number of possible media queries. Examples where this string interpolation of media queries would not work as expected :
It would only work for
|
Spec: https://drafts.csswg.org/css-conditional-3/#dom-css-supports
Use case: Given a media query like
(prefers-reduced-data: reduce)
, I want to determine whether the host environment supports this query (regardless of whether the query matches). (Currently, one way to do this is to manually write a feature test which injects some CSS with media queries into the page, and then check if it had an effect.)CSS.supports
is super handy for feature detection, but it doesn't support feature-detection for media query expressions.matchMedia
can be used to figure out whether or not a given media query matches, but not whether or not it is supported. E.g.matchMedia('(prefers-this-doesnt-exist: lolwat)')
returns an object withmatches: false
, but there's no indication whether or not it can ever match / is supported in the current implementation.Potential solutions:
CSS.supports()
could be extended to support media queries, i.e.CSS.supports('@media (...)')
. The spec + implementations would have to branch on whether or not theconditionText
parameter looks like a media query, which may not be ideal.matchMedia('(prefers-this-doesnt-exist: lolwat)')
could be extended with asupported: true/false
property. This doesn't seem ideal, since it has nothing to do with matching media.CSS.supportsMedia('(prefers-reduced-data: reduce)')
.Apologies if this has been discussed previously; a quick search didn't reveal anything relevant.
The text was updated successfully, but these errors were encountered: