Skip to content

Instantly share code, notes, and snippets.

@Voltra
Last active December 30, 2021 09:37
Show Gist options
  • Select an option

  • Save Voltra/e58c46d466c293e2918bb8bd23b21863 to your computer and use it in GitHub Desktop.

Select an option

Save Voltra/e58c46d466c293e2918bb8bd23b21863 to your computer and use it in GitHub Desktop.
Dark Mode mixin
@use "sass:selector";
@mixin darkMode($applySelf: false) {
$selector: &;
@if ($applySelf) {
@content;
}
@at-root {
// SASS properly nests selectors downwards, but not "upwards", so we need to use selector.nest to make it work in all scenarios
$darkByPref: selector.nest("html:not(.colorScheme.-light)", $selector);
$darkByChoice: selector.nest("html.colorScheme.-dark", $selector);
@media (prefers-color-scheme: dark) {
// Only use system preferences if no choice (for light mode) has been made
#{$darkByPref} {
@content;
}
}
#{$darkByChoice} {
@content;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment