1
0
mirror of https://github.com/locomotivemtl/locomotive-boilerplate.git synced 2026-01-15 00:55:08 +08:00
Files
locomotive-boilerplate/assets/styles/tools/_functions.scss

139 lines
3.9 KiB
SCSS
Raw Normal View History

2022-05-12 16:15:44 +02:00
// ==========================================================================
// Tools / Functions
// ==========================================================================
// Converts the given pixel value to its EM quivalent.
//
// @param {Number} $size - The pixel value to convert.
// @param {Number} $base [$font-size] - The assumed base font size.
// @return {Number} Scalable pixel value in EMs.
2022-05-12 16:15:44 +02:00
@function em($size, $base: $font-size) {
@if (type-of($size) == number) {
@if (unit($size) != "px") {
@error "`#{$size}` needs to be a pixel value.";
}
} @else {
@error "`#{$size}` needs to be a number.";
}
@if (type-of($base) == number) {
@if (unit($base) != "px") {
@error "`#{$base}` needs to be a pixel value.";
}
} @else {
@error "`#{$base}` needs to be a number.";
}
@return ($size / $base) * 1em;
}
// Converts the given pixel value to its REM quivalent.
//
// @param {Number} $size - The pixel value to convert.
// @param {Number} $base [$font-size] - The assumed base font size.
// @return {Number} Scalable pixel value in REMs.
2022-05-12 16:15:44 +02:00
@function rem($size, $base: $font-size) {
@if (type-of($size) == number) {
@if (unit($size) != "px") {
@error "`#{$size}` needs to be a pixel value.";
}
} @else {
@error "`#{$size}` needs to be a number.";
}
@if (type-of($base) == number) {
@if (unit($base) != "px") {
@error "`#{$base}` needs to be a pixel value.";
}
} @else {
@error "`#{$base}` needs to be a number.";
}
@return ($size / $base) * 1rem;
}
2022-05-20 11:44:42 +02:00
// A function helper to avoid having to type `map-get($layers, ...)`
//
// @link on http://css-tricks.com/handling-z-index/
// @param {string} $layer The name of the z-index
// @param {number} $var The modifier if needed
// @return {number} The corresponding z-index based on the $layers var
@function z($layer, $var: 0) {
@if not map-has-key($layers, $layer) {
@error "No z-index found in $layers map for `#{$layer}`. Property omitted.";
}
$value: map-get($layers, $layer);
@return $value + $var;
}
// Converts a number to a percentage.
//
// @alias percentage()
// @link http://sassdoc.com/annotations/#alias
// @param {Number} $number - The value to convert.
// @return {Number} A percentage.
2022-05-12 16:15:44 +02:00
@function span($number) {
@return percentage($number);
}
// Checks if a list contains a value(s).
//
// @link https://github.com/thoughtbot/bourbon/blob/master/core/bourbon/validators/_contains.scss
// @param {List} $list - The list to check against.
// @param {List} $values - A single value or list of values to check for.
// @return {Boolean}
// @access private
2022-05-12 16:15:44 +02:00
@function list-contains(
$list,
$values...
) {
@each $value in $values {
@if type-of(index($list, $value)) != "number" {
@return false;
}
}
@return true;
}
// Resolve whether a rule is important or not.
//
// @param {Boolean} $flag - Whether a rule is important (TRUE) or not (FALSE).
// @return {String|Null} Returns `!important` or NULL.
2022-05-12 16:15:44 +02:00
@function important($flag: false) {
@if ($flag == true) {
@return !important;
} @elseif ($important == false) {
@return null;
} @else {
2022-05-12 16:15:44 +02:00
@error "`#{$flag}` needs to be `true` or `false`.";
}
}
// Determine if the current context is for a WYSIWYG editor.
//
// @requires {String} $context - The global context of the stylesheet.
// @return {Boolean} If the $context is set to "editor".
2022-05-12 16:15:44 +02:00
@function is-editor() {
@return ('editor' == $context);
}
// Determine if the current context is for the front-end.
//
// @requires {String} $context - The global context of the stylesheet.
// @return {Boolean} If the $context is set to "frontend".
2022-05-12 16:15:44 +02:00
@function is-frontend() {
@return ('frontend' == $context);
}
$context: 'frontend' !default;