Last active
December 30, 2021 09:37
-
-
Save Voltra/e58c46d466c293e2918bb8bd23b21863 to your computer and use it in GitHub Desktop.
Dark Mode mixin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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