1 | // Foundation for Sites by ZURB
|
2 | // foundation.zurb.com
|
3 | // Licensed under MIT Open Source
|
4 |
|
5 | ////
|
6 | /// @group functions
|
7 | ////
|
8 |
|
9 | /// Determine if a value is not falsey, in CSS terms. Falsey values are `null`, `none`, `0` with any unit, or an empty list.
|
10 | ///
|
11 | /// @param {Mixed} $val - Value to check.
|
12 | ///
|
13 | /// @returns {Boolean} `true` if `$val` is not falsey.
|
14 | @function has-value($val) {
|
15 | @if $val == null or $val == none {
|
16 | @return false;
|
17 | }
|
18 | @if type-of($val) == 'number' and strip-unit($val) == 0 {
|
19 | @return false;
|
20 | }
|
21 | @if type-of($val) == 'list' and length($val) == 0 {
|
22 | @return false;
|
23 | }
|
24 | @return true;
|
25 | }
|
26 |
|
27 | /// Determine a top/right/bottom/right value on a padding, margin, etc. property, no matter how many values were passed in. Use this function if you need to know the specific side of a value, but don't know if the value is using a shorthand format.
|
28 | ///
|
29 | /// @param {List|Number} $val - Value to analyze. Should be a shorthand sizing property, e.g. "1em 2em 1em"
|
30 | /// @param {Keyword} $side - Side to return. Should be `top`, `right`, `bottom`, or `left`.
|
31 | ///
|
32 | /// @returns {Number} A single value based on `$val` and `$side`.
|
33 | @function get-side($val, $side) {
|
34 | $length: length($val);
|
35 |
|
36 | @if $length == 1 {
|
37 | @return $val;
|
38 | }
|
39 | @if $length == 2 {
|
40 | @return map-get((
|
41 | top: nth($val, 1),
|
42 | bottom: nth($val, 1),
|
43 | left: nth($val, 2),
|
44 | right: nth($val, 2),
|
45 | ), $side);
|
46 | }
|
47 | @if $length == 3 {
|
48 | @return map-get((
|
49 | top: nth($val, 1),
|
50 | left: nth($val, 2),
|
51 | right: nth($val, 2),
|
52 | bottom: nth($val, 3),
|
53 | ), $side);
|
54 | }
|
55 | @if $length == 4 {
|
56 | @return map-get((
|
57 | top: nth($val, 1),
|
58 | right: nth($val, 2),
|
59 | bottom: nth($val, 3),
|
60 | left: nth($val, 4),
|
61 | ), $side);
|
62 | }
|
63 | }
|
64 |
|
65 | /// Given border $val, find a specific element of the border, which is $elem. The possible values for $elem are width, style, and color.
|
66 | ///
|
67 | /// @param {List} $val - Border value to find a value in.
|
68 | /// @param {Keyword} $elem - Border component to extract.
|
69 | ///
|
70 | /// @returns {Mixed} If the value exists, returns the value. If the value is not in the border definition, the function will return a 0px width, solid style, or black border.
|
71 | @function get-border-value($val, $elem) {
|
72 | // Find the width, style, or color and return it
|
73 | @each $v in $val {
|
74 | $type: type-of($v);
|
75 | @if $elem == width and $type == 'number' {
|
76 | @return $v;
|
77 | }
|
78 | @if $elem == style and $type == 'string' {
|
79 | @return $v;
|
80 | }
|
81 | @if $elem == color and $type == 'color' {
|
82 | @return $v;
|
83 | }
|
84 | }
|
85 |
|
86 | // Defaults
|
87 | $defaults: (
|
88 | width: 0,
|
89 | style: solid,
|
90 | color: #000,
|
91 | );
|
92 |
|
93 | @return map-get($defaults, $elem);
|
94 | }
|
95 |
|
96 | /// Finds a value in a nested map.
|
97 | /// @link https://css-tricks.com/snippets/sass/deep-getset-maps/ Deep Get/Set in Maps
|
98 | ///
|
99 | /// @param {Map} $map - Map to pull a value from.
|
100 | /// @param {String} $keys... - Keys to use when looking for a value.
|
101 | /// @returns {Mixed} The value found in the map.
|
102 | @function map-deep-get($map, $keys...) {
|
103 | @each $key in $keys {
|
104 | $map: map-get($map, $key);
|
105 | }
|
106 | @return $map;
|
107 | }
|
108 |
|
109 | /// Casts a map into a list.
|
110 | /// @link http://hugogiraudel.com/2014/04/28/casting-map-into-list/
|
111 | ///
|
112 | /// @param {Map} $map - Map to pull a value from.
|
113 | ///
|
114 | /// @returns {List} Depending on the flag, returns either $keys or $values or both.
|
115 | @function map-to-list($map, $keep: 'both') {
|
116 | $keep: if(index('keys' 'values', $keep), $keep, 'both');
|
117 |
|
118 | @if type-of($map) == 'map' {
|
119 | $keys: ();
|
120 | $values: ();
|
121 |
|
122 | @each $key, $val in $map {
|
123 | $keys: append($keys, $key);
|
124 | $values: append($values, $val);
|
125 | }
|
126 |
|
127 | @if $keep == 'keys' {
|
128 | @return $keys;
|
129 | }
|
130 | @else if $keep == 'values' {
|
131 | @return $values;
|
132 | }
|
133 | @else {
|
134 | @return zip($keys, $values);
|
135 | }
|
136 | }
|
137 |
|
138 | @return if(type-of($map) != 'list', ($value,), $map);
|
139 |
|
140 | }
|
141 |
|
142 | /// Safely return a value from a map.
|
143 | ///
|
144 | /// @param {Map} $map - Map to retrieve a value from.
|
145 | /// @param {String} $key - Name of the map key.
|
146 | ///
|
147 | /// @returns {List} Found value.
|
148 | @function map-safe-get($map, $key) {
|
149 | @if (type-of($map) == 'map' or (type-of($map) == 'list' and length($map) == 0)) {
|
150 | @if (map-has-key($map, $key)) {
|
151 | @return map-get($map, $key);
|
152 | }
|
153 | @else {
|
154 | @error 'Key: `#{$key}` is not available in `#{$map}`';
|
155 | }
|
156 | }
|
157 | @else {
|
158 | @error '`#{$map}` is not a valid map';
|
159 | }
|
160 | }
|