Last active
July 29, 2022 12:03
-
-
Save bafxyz/503fdd9dd3b6a2cdd6808b59ae15109f to your computer and use it in GitHub Desktop.
Standard Sass theming solution
This file contains 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
/* | |
* Implementation of themes with standart sass solution not compatible with css-modules and css-in-js | |
* https://medium.com/@dmitriy.borodiy/easy-color-theming-with-scss-bc38fd5734d1 | |
* | |
* $themes: ( | |
* light: ( | |
* elementBackgroundColor: var(--element-bg), | |
* elementColor: var(--element-color) | |
* ), | |
* dark: ( | |
* elementBackgroundColor: #561558, | |
* elementColor: #2b0a38 | |
* ), | |
* ); | |
* | |
* .Element { | |
* @include themify($themes) { | |
* color: themed('elementColor'); | |
* background-color: themed('elementBackgroundColor'); | |
* } | |
* } | |
*/ | |
/* | |
* Implementation of themes | |
*/ | |
@mixin themify($themes) { | |
@each $theme, $map in $themes { | |
.theme-#{$theme} & { | |
$theme-map: () !global; | |
@each $key, $submap in $map { | |
$value: map-get(map-get($themes, $theme), '#{$key}'); | |
$theme-map: map-merge($theme-map, ($key: $value)) !global; | |
} | |
@content; | |
$theme-map: null !global; | |
} | |
} | |
} | |
@function themed($key) { | |
@return map-get($theme-map, $key); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment