Created
March 19, 2018 18:57
-
-
Save joedski/00d0fbaa149e99634b88596e99e43aea to your computer and use it in GitHub Desktop.
Simple enum function for doing things like automatic z-indices.
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 enum($keys) { | |
$index-map: (); | |
$running-index: 1; | |
@each $k in $keys { | |
@if type-of($k) == list { | |
// NOTE: We do _not_ check if this value is greater than prior values. | |
// This is intentional. | |
$running-index: nth($k, 2); | |
$k: nth($k, 1); | |
} | |
$index-map: map-merge($index-map, ($k: $running-index)); | |
$running-index: $running-index + 1; | |
} | |
@return $index-map; | |
} | |
// Like typical enums, you can also change the value mid-way through, | |
// and it will carry on incrementing after that. | |
// This does mean you can end up with multiple keys pointing to the same value, | |
// but I don't recommend that. | |
$app-z-indices: enum( | |
'bottom' | |
'middle' | |
('top' 10) | |
'more-top' | |
); | |
@function app-z-index($key) { | |
@if map-has-key($app-z-indices, $key) { | |
@return map-get($app-z-indices, $key); | |
} | |
// throw errors for non-existent keys, that way it's just as good as vars. | |
@error "'#{$key}' is not a known z-index key."; | |
} | |
.foo { | |
/* should be 1 = 'bottom' */ | |
z-index: app-z-index('bottom'); | |
} | |
.bar { | |
/* should be 2 = 'middle' */ | |
z-index: app-z-index('middle'); | |
} | |
.baz { | |
/* should be 10 = 'top' */ | |
z-index: app-z-index('top'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment