Let's say that you want to use a z-index value in a project (specially a big one) but you are not sure if there's something else using the same z-index value or a higher one, it could cover up your new cool thing, so what you do?
Easy, just use 9999
and consider this job done 😎 👎🏼
But that is the little devil 😈 on your shoulder winning the battle. So let's try to listen to the angel 👼🏼 this time.
Specify the known z-indexes, for example:
$z-indexes-map: (
'under': -1,
'default': 1,
'dialog': 100,
'alert': 200,
'modal': (
'base': 300,
'header': 301,
'close': 302
)
) !default;
Important: this map should be at maximum of 2 level deep.
Also, let's setup the logging options:
$z-indexes-log: (
// If key is found in the map
'used-key': true,
// If value is found in the map
'used-value': true,
// To warn that the value is less than others from the map
'less-than': true,
// If true an error will be thrown instead of warn
'error': false
) !default;
Check each function file for more information.
To get and check a z-index value from a key in a z-index map.
.class {
z-index: get-zindex('default');
z-index: get-zindex('modal', 'header');
}
// Output
.class {
z-index: 1;
z-index: 301;
}
If no key is found, will warn if used-key
is true.
Property will be omitted:
.class {
z-index: get-zindex('foo');
}
// "No key "foo" found in " ("under": -1, "default": 1, "dialog": 100, "alert": 200, "modal": ("base": 300, "header": 301, "close": 302)) ". Property omitted."
If is a wrong value type. Will warn an omit the property:
.class {
z-index: get-zindex(123);
}
// Value should be string. Property omitted!
To safely use a z-index value from z-index map.
If value is less than other values, will warn if less-than
is true.
Property is persisted:
.class {
z-index: safe-zindex(123);
}
// Output
.class {
z-index: 123;
}
// Warning:
// The value '123' is less than 'alert: 200'.
// The value '123' is less than 'modal.base: 300'.
// The value '123' is less than 'modal.header: 301'.
// The value '123' is less than 'modal.close: 302'.
If value is being used by an existent key it will warn if used-value
is true.
Property is persisted:
.class {
z-index: safe-zindex(1);
}
// Output
.class {
z-index: 1;
}
// Warning:
// The z-index value of '1' is being used by the key 'default'.
If is a wrong value type. Will warn an omit the property:
.class {
z-index: safe-zindex('301');
}
// Warning:
// Property omitted value should be a number
To append all items of a map into :root
as CSS Custom Properties.
In this case, let's use z-indexes-map
map.
You can also pass a prefix for the custom properties or leave it empty.
@include append-custom-properties($prefix: 'z-index', $map: $z-indexes-map)
// Output
:root {
--z-index-under: -1;
--z-index-default: 1;
--z-index-dialog: 100;
--z-index-alert: 200;
--z-index-modal-base: 300;
--z-index-modal-header: 301;
--z-index-modal-close: 302;
}