I want to use the new CSS feature queries by selector, like so:
:root { --focus-shadow: 0 0 0 2.4px rgba(130, 138, 145, 0.5); }
/* Focus styles even for mouse selection in old browsers */
*:focus { outline: none; box-shadow: var(--focus-shadow); }
/* Newer browsers: Only add focus styles if e.g. navigating with the keyboard */
@supports selector(*:focus-visible) {
*:focus { box-shadow: none; }
*:focus-visible { box-shadow: var(--focus-shadow); }
<a href="#">Focus me</a>
The above is valid CSS (although poorly supported as of June 2020), but Sass doesn't support it in either of its two implementations yet. It's in the works, but it looks like it'll take a while to implement it. Trying to compile the above through Sass throws an error (varies between Sass implementations; see these issues for details on the errors).
In the mean time, I want to trick Sass into compiling my support query as is, without trying to understand it. I tried a few things unsuccessfully. Using unquote()
, this is the most promising attempt:
@mixin supports-selector( $selector ) {
$query-str: #{ unquote( '@supports selector(' ) }#{ unquote( $selector ) }#{ ')' };
#{ unquote( $query-str ) }#{ unquote( '{' ) }
@content;
#{ unquote( '}' ) }
}
:root { --focus-shadow: 0 0 0 2.4px rgba(130, 138, 145, 0.5); }
*:focus { outline: none; box-shadow: var(--focus-shadow); }
@include supports-selector( '*:focus-visible' ) {
*:focus { box-shadow: none; }
*:focus-visible { box-shadow: var(--focus-shadow); }
}
This still throws an error though, as Sass expects an arbitrary string to be a property followed by a colon:
property "#{ unquote( $query-str ) }#{ unquote( '{' ) }" must be followed by a ':'
Not unquoting the @supports
part results in error to the effect that selector(whatever)
is not a valid @supports condition.
I don't know what else to try.
How can I trick Sass into not compiling my mixin?
The @supports CSS at-rule lets you specify declarations that depend on a browser's support for one or more specific CSS features. This is called a feature query. The rule may be placed at the top level of your code or nested inside any other conditional group at-rule.
SCSS syntax is CSS compatible, so you just have to rename your . css file to . scss et voilà! Your first SCSS file has been created, just like that.
Sass Importing Files Just like CSS, Sass also supports the @import directive. The @import directive allows you to include the content of one file in another.
This might work for you - by just escaping the characters, it will still compile to CSS.
The SASS/SCSS processor was getting hung up not on the @supports
, but rather the *:focus-visible
:
:root { --focus-shadow: 0 0 0 2.4px rgba(130, 138, 145, 0.5); }
/* Focus styles even for mouse selection in old browsers */
*:focus { outline: none; box-shadow: var(--focus-shadow); }
/* Newer browsers: Only add focus styles if e.g. navigating with the keyboard */
@supports #{'\(*:focus-visible)'} {
*:focus { box-shadow: none; }
*:focus-visible { box-shadow: var(--focus-shadow); }
}
Compiles to this:
:root {
--focus-shadow: 0 0 0 2.4px rgba(130, 138, 145, 0.5);
}
/* Focus styles even for mouse selection in old browsers */
*:focus {
outline: none;
box-shadow: var(--focus-shadow);
}
/* Newer browsers: Only add focus styles if e.g. navigating with the keyboard */
@supports (*:focus-visible) {
*:focus {
box-shadow: none;
}
*:focus-visible {
box-shadow: var(--focus-shadow);
}
}
SASSMEISTER Link to play with.
Edit: based on your comment, you can still add the selector
:
@supports #{'\selector(*:focus-visible)'} {
*:focus { box-shadow: none; }
*:focus-visible { box-shadow: var(--focus-shadow); }
}
Updated SASSMeister
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With