Skip to content

Instantly share code, notes, and snippets.

@stowball
Last active November 11, 2022 01:35
Show Gist options
  • Save stowball/cbdf5a99aa766bae72f70fdbc5d86a22 to your computer and use it in GitHub Desktop.
Save stowball/cbdf5a99aa766bae72f70fdbc5d86a22 to your computer and use it in GitHub Desktop.
Complex theming through custom string interpolation of map variables in Sass https://twitter.com/stowball/status/870245991663804416
@function color($keys, $function-name: "color") {
@return get($colors, $keys, $function-name);
}
@function c($keys) {
@return color($keys, "c");
}
@function get($original-map, $keys: null, $stack-trace-name: null) {
$map: $original-map;
@if ($keys) {
@each $key in $keys {
$map: map-get($map, $key);
}
}
@if ($map == null) {
$error: log-error("No map found for #{unquote($stack-trace-name)}(#{$keys}). Map contains #{$original-map}");
}
@return $map;
}
@function log-error($message) {
$message: "***** " + $message + " *****";
@warn $message;
@error $message;
@return null;
}
@mixin theme($css-property, $css-value, $theme-classes: t) {
@each $selector in & {
@each $class in $theme-classes {
@each $theme, $theme-properties in c(themes) {
$value: $css-value;
@each $theme-name, $theme-value in $theme-properties {
$rgba-value: "rgba(#{red($theme-value)}, #{green($theme-value)}, #{blue($theme-value)}";
$value: str-replace($value, "rgba(${#{$theme-name}}", $rgba-value);
$value: str-replace($value, "${#{$theme-name}}", $theme-value);
}
@at-root .#{$class}-#{join($theme, $selector)} {
#{$css-property}: unquote($value);
}
}
}
}
}
$colors: (
themes: (
nrl: (
primary: #1d3c34,
tint: #142c25,
tint-rm: #0b1c17,
secondary: #006c3c,
highlight: #fedb00,
),
broncos: (
primary: #6c1d45,
tint: #521633,
tint-rm: #3d0d21,
secondary: #92426b,
highlight: #f8cc0d,
),
bulldogs: (
primary: #00468b,
tint: #00306f,
tint-rm: #001843,
secondary: #0072bc,
highlight: #0090f0,
),
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment