Last active
January 17, 2018 17:31
-
-
Save mistergraphx/c20e6b1cdd97f932ae14ca77a889f85c to your computer and use it in GitHub Desktop.
Deep access in sass maps with dot notation <https://css-tricks.com/snippets/sass/deep-getset-maps/#comment-1608318>
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
@function ns($map, $path) { | |
$keys: (); | |
$separator: '.'; | |
$index : str-index($path, $separator); | |
@while $index != null { | |
$item: str-slice($path, 1, $index - 1); | |
$keys: append($keys, $item); | |
$path: str-slice($path, $index + 1); | |
$index : str-index($path, $separator); | |
} | |
$keys: append($keys, $path); | |
@each $key in $keys { | |
$map: map-get($map, $key); | |
} | |
@return $map; | |
} | |
$config: ( | |
foo: 'foo', | |
bar: ( | |
baz: 'bar.baz', | |
size: ( | |
m: 14px, | |
l: 18px, | |
xl: 270px | |
) | |
) | |
); | |
@function config($path) { | |
@return ns($config, $path); | |
} | |
/// USAGE | |
.a { font-size: config('bar.size.l'); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment