diff options
Diffstat (limited to 'src/_functions.scss')
| -rw-r--r-- | src/_functions.scss | 316 |
1 files changed, 197 insertions, 119 deletions
diff --git a/src/_functions.scss b/src/_functions.scss index 9dd14b1..74cc1b5 100644 --- a/src/_functions.scss +++ b/src/_functions.scss | |||
| @@ -10,9 +10,32 @@ | |||
| 10 | //// | 10 | //// |
| 11 | 11 | ||
| 12 | @use 'sass:map'; | 12 | @use 'sass:map'; |
| 13 | @use 'sass:list'; | ||
| 13 | @use 'sass:math'; | 14 | @use 'sass:math'; |
| 15 | @use 'sass:string'; | ||
| 16 | @use 'sass:meta'; | ||
| 14 | @use './vars'; | 17 | @use './vars'; |
| 15 | 18 | ||
| 19 | $numbers: ('0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9); | ||
| 20 | |||
| 21 | $units: ( | ||
| 22 | 'px': 1px, | ||
| 23 | 'cm': 1cm, | ||
| 24 | 'mm': 1mm, | ||
| 25 | '%': 1%, | ||
| 26 | 'ch': 1ch, | ||
| 27 | 'pc': 1pc, | ||
| 28 | 'in': 1in, | ||
| 29 | 'em': 1em, | ||
| 30 | 'rem': 1rem, | ||
| 31 | 'pt': 1pt, | ||
| 32 | 'ex': 1ex, | ||
| 33 | 'vw': 1vw, | ||
| 34 | 'vh': 1vh, | ||
| 35 | 'vmin': 1vmin, | ||
| 36 | 'vmax': 1vmax | ||
| 37 | ); | ||
| 38 | |||
| 16 | /// | 39 | /// |
| 17 | /// Replace a substring with a new string. | 40 | /// Replace a substring with a new string. |
| 18 | /// | 41 | /// |
| @@ -23,13 +46,13 @@ | |||
| 23 | /// @return {string} A string with all instances of $search replaced with $replace | 46 | /// @return {string} A string with all instances of $search replaced with $replace |
| 24 | /// | 47 | /// |
| 25 | @function str-replace($string, $search, $replace) { | 48 | @function str-replace($string, $search, $replace) { |
| 26 | $index: str-index($string, $search); | 49 | $index: string.index($string, $search); |
| 27 | 50 | ||
| 28 | @if $index { | 51 | @if $index { |
| 29 | @return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace); | 52 | @return string.slice($string, 1, $index - 1) + $replace + str-replace(string.slice($string, $index + string.length($search)), $search, $replace); |
| 30 | } | 53 | } |
| 31 | 54 | ||
| 32 | @return $string; | 55 | @return $string; |
| 33 | } | 56 | } |
| 34 | 57 | ||
| 35 | /// | 58 | /// |
| @@ -41,17 +64,17 @@ | |||
| 41 | /// @return {string} | 64 | /// @return {string} |
| 42 | /// | 65 | /// |
| 43 | @function str-implode($list, $glue: '') { | 66 | @function str-implode($list, $glue: '') { |
| 44 | $result: ''; | 67 | $result: ''; |
| 45 | 68 | ||
| 46 | @each $item in $list { | 69 | @each $item in $list { |
| 47 | $result: $result + if(length($item) > 1, str-implode($item, $glue), $item); | 70 | $result: $result + if(list.length($item) > 1, str-implode($item, $glue), $item); |
| 48 | 71 | ||
| 49 | @if $item != nth($list, length($list)) { | 72 | @if $item != list.nth($list, list.length($list)) { |
| 50 | $result: $result + $glue; | 73 | $result: $result + $glue; |
| 51 | } | 74 | } |
| 52 | } | 75 | } |
| 53 | 76 | ||
| 54 | @return $result; | 77 | @return $result; |
| 55 | } | 78 | } |
| 56 | 79 | ||
| 57 | /// | 80 | /// |
| @@ -63,18 +86,18 @@ | |||
| 63 | /// | 86 | /// |
| 64 | /// @return {list} A slice of the list | 87 | /// @return {list} A slice of the list |
| 65 | /// | 88 | /// |
| 66 | @function list-slice($list, $start: 1, $end: length($list)) { | 89 | @function list-slice($list, $start: 1, $end: list.length($list)) { |
| 67 | $result: (); | 90 | $result: (); |
| 68 | 91 | ||
| 69 | @if $end >= $start { | 92 | @if $end >= $start { |
| 70 | @for $i from $start through $end { | 93 | @for $i from $start through $end { |
| 71 | @if $i != 0 { | 94 | @if $i != 0 { |
| 72 | $result: append($result, nth($list, $i), list-separator($list)); | 95 | $result: list.append($result, list.nth($list, $i), list.separator($list)); |
| 73 | } | 96 | } |
| 74 | } | 97 | } |
| 75 | } | 98 | } |
| 76 | 99 | ||
| 77 | @return $result; | 100 | @return $result; |
| 78 | } | 101 | } |
| 79 | 102 | ||
| 80 | /// | 103 | /// |
| @@ -86,15 +109,15 @@ | |||
| 86 | /// @return {list} A list with $value at the beginning, followed by the other items | 109 | /// @return {list} A list with $value at the beginning, followed by the other items |
| 87 | /// | 110 | /// |
| 88 | @function list-prepend($list, $value) { | 111 | @function list-prepend($list, $value) { |
| 89 | $result: append((), $value, list-separator($list)); | 112 | $result: list.append((), $value, list.separator($list)); |
| 90 | 113 | ||
| 91 | @if length($list) > 0 { | 114 | @if list.length($list) > 0 { |
| 92 | @for $i from 1 through length($list) { | 115 | @for $i from 1 through list.length($list) { |
| 93 | $result: append($result, nth($list, $i), list-separator($list)); | 116 | $result: list.append($result, list.nth($list, $i), list.separator($list)); |
| 94 | } | 117 | } |
| 95 | } | 118 | } |
| 96 | 119 | ||
| 97 | @return $result; | 120 | @return $result; |
| 98 | } | 121 | } |
| 99 | 122 | ||
| 100 | /// | 123 | /// |
| @@ -105,15 +128,15 @@ | |||
| 105 | /// @return {list} Teh reversed list | 128 | /// @return {list} Teh reversed list |
| 106 | /// | 129 | /// |
| 107 | @function list-reverse($list) { | 130 | @function list-reverse($list) { |
| 108 | @if length($list) == 0 { | 131 | @if list.length($list) == 0 { |
| 109 | @return $list; | 132 | @return $list; |
| 110 | } | 133 | } |
| 111 | 134 | ||
| 112 | $result: (); | 135 | $result: (); |
| 113 | @for $i from length($list) * -1 through -1 { | 136 | @for $i from list.length($list) * -1 through -1 { |
| 114 | $result: append($result, nth($list, abs($i))); | 137 | $result: list.append($result, list.nth($list, math.abs($i))); |
| 115 | } | 138 | } |
| 116 | @return $result; | 139 | @return $result; |
| 117 | } | 140 | } |
| 118 | 141 | ||
| 119 | /// | 142 | /// |
| @@ -126,52 +149,52 @@ | |||
| 126 | /// | 149 | /// |
| 127 | /// @return {list} Sorted list | 150 | /// @return {list} Sorted list |
| 128 | /// | 151 | /// |
| 129 | @function quicksort($l, $left: 1, $right: length($l)) { | 152 | @function quicksort($l, $left: 1, $right: list.length($l)) { |
| 130 | @if $left < $right { | 153 | @if $left < $right { |
| 131 | $pvr: quicksort-partition($l, $left, $right); | 154 | $pvr: quicksort-partition($l, $left, $right); |
| 132 | $pivot: nth($pvr, 1); | 155 | $pivot: list.nth($pvr, 1); |
| 133 | $l: nth($pvr, 2); | 156 | $l: list.nth($pvr, 2); |
| 134 | $l: quicksort($l, $left, $pivot); | 157 | $l: quicksort($l, $left, $pivot); |
| 135 | $l: quicksort($l, $pivot + 1, $right); | 158 | $l: quicksort($l, $pivot + 1, $right); |
| 136 | } | 159 | } |
| 137 | 160 | ||
| 138 | @return $l; | 161 | @return $l; |
| 139 | } | 162 | } |
| 140 | 163 | ||
| 141 | /// | 164 | /// |
| 142 | /// @access private | 165 | /// @access private |
| 143 | /// | 166 | /// |
| 144 | @function quicksort-partition($l, $left, $right) { | 167 | @function quicksort-partition($l, $left, $right) { |
| 145 | $start: true; | 168 | $start: true; |
| 146 | $i: $left; | 169 | $i: $left; |
| 147 | $j: $right - 1; | 170 | $j: $right - 1; |
| 148 | $pivot: nth($l, $right); | 171 | $pivot: list.nth($l, $right); |
| 149 | 172 | ||
| 150 | @while ($i < $j) or $start { | 173 | @while ($i < $j) or $start { |
| 151 | @while (nth($l, $i) < $pivot) and ($i < $right - 1) { | 174 | @while (list.nth($l, $i) < $pivot) and ($i < $right - 1) { |
| 152 | $i: $i + 1; | 175 | $i: $i + 1; |
| 153 | } | 176 | } |
| 154 | 177 | ||
| 155 | @while (nth($l, $j)>= $pivot) and ($j > $left) { | 178 | @while (list.nth($l, $j)>= $pivot) and ($j > $left) { |
| 156 | $j: $j - 1; | 179 | $j: $j - 1; |
| 157 | } | 180 | } |
| 158 | 181 | ||
| 159 | @if $i < $j { | 182 | @if $i < $j { |
| 160 | $i-val: nth($l, $i); | 183 | $i-val: list.nth($l, $i); |
| 161 | $l: set-nth($l, $i, nth($l, $j)); | 184 | $l: list.set-nth($l, $i, list.nth($l, $j)); |
| 162 | $l: set-nth($l, $j, $i-val); | 185 | $l: list.set-nth($l, $j, $i-val); |
| 163 | } | 186 | } |
| 164 | 187 | ||
| 165 | $start: false; | 188 | $start: false; |
| 166 | } | 189 | } |
| 167 | 190 | ||
| 168 | @if nth($l, $i) > $pivot { | 191 | @if list.nth($l, $i) > $pivot { |
| 169 | $i-val: nth($l, $i); | 192 | $i-val: list.nth($l, $i); |
| 170 | $l: set-nth($l, $i, nth($l, $right)); | 193 | $l: list.set-nth($l, $i, list.nth($l, $right)); |
| 171 | $l: set-nth($l, $right, $i-val); | 194 | $l: list.set-nth($l, $right, $i-val); |
| 172 | } | 195 | } |
| 173 | 196 | ||
| 174 | @return $i $l; | 197 | @return $i $l; |
| 175 | } | 198 | } |
| 176 | 199 | ||
| 177 | /// | 200 | /// |
| @@ -185,10 +208,10 @@ | |||
| 185 | /// @return {any} Either the value assigned to $key or $default | 208 | /// @return {any} Either the value assigned to $key or $default |
| 186 | /// | 209 | /// |
| 187 | @function map-get-default($map, $key, $keys...) { | 210 | @function map-get-default($map, $key, $keys...) { |
| 188 | $default: nth($keys, length($keys)); | 211 | $default: list.nth($keys, list.length($keys)); |
| 189 | $keys: list-slice($keys, 1, length($keys) - 1); | 212 | $keys: list-slice($keys, 1, list.length($keys) - 1); |
| 190 | 213 | ||
| 191 | @return if(map-has-key($map, $key, $keys...), map-get($map, $key, $keys...), $default); | 214 | @return if(map.has-key($map, $key, $keys...), map.get($map, $key, $keys...), $default); |
| 192 | } | 215 | } |
| 193 | 216 | ||
| 194 | /// | 217 | /// |
| @@ -199,29 +222,29 @@ | |||
| 199 | /// @return {string} | 222 | /// @return {string} |
| 200 | /// | 223 | /// |
| 201 | @function map-print($map) { | 224 | @function map-print($map) { |
| 202 | $output: ''; | 225 | $output: ''; |
| 203 | 226 | ||
| 204 | @each $key, $value in $map { | 227 | @each $key, $value in $map { |
| 205 | $value-str: ''; | 228 | $value-str: ''; |
| 206 | 229 | ||
| 207 | @if type-of($value) == map { | 230 | @if meta.type-of($value) == map { |
| 208 | $value-str: '[ ' + map-print($value) + ' ]'; | 231 | $value-str: '[ ' + map-print($value) + ' ]'; |
| 209 | } @else if type-of($value) == list { | 232 | } @else if meta.type-of($value) == list { |
| 210 | $value-str: '[ ' + str-implode($value, ', ') + ' ]'; | 233 | $value-str: '[ ' + str-implode($value, ', ') + ' ]'; |
| 211 | } @else if type-of($value) == string { | 234 | } @else if meta.type-of($value) == string { |
| 212 | $value-str: '\'' + $value + '\''; | 235 | $value-str: '\'' + $value + '\''; |
| 213 | } @else { | 236 | } @else { |
| 214 | $value-str: $value; | 237 | $value-str: $value; |
| 215 | } | 238 | } |
| 216 | 239 | ||
| 217 | @if $output == '' { | 240 | @if $output == '' { |
| 218 | $output: $key + ': ' + $value-str; | 241 | $output: $key + ': ' + $value-str; |
| 219 | } @else { | 242 | } @else { |
| 220 | $output: $output + ', ' + $key + ': ' + $value-str; | 243 | $output: $output + ', ' + $key + ': ' + $value-str; |
| 221 | } | 244 | } |
| 222 | } | 245 | } |
| 223 | 246 | ||
| 224 | @return $output; | 247 | @return $output; |
| 225 | } | 248 | } |
| 226 | 249 | ||
| 227 | /// | 250 | /// |
| @@ -233,35 +256,35 @@ | |||
| 233 | /// @return {bool} `true` if the selector matches at least one suffix, otherwise `false`. | 256 | /// @return {bool} `true` if the selector matches at least one suffix, otherwise `false`. |
| 234 | /// | 257 | /// |
| 235 | @function selector-suffix-match($selector, $suffixes) { | 258 | @function selector-suffix-match($selector, $suffixes) { |
| 236 | $match: true; | 259 | $match: true; |
| 237 | 260 | ||
| 238 | @each $sel in $selector { | 261 | @each $sel in $selector { |
| 239 | @if $match { | 262 | @if $match { |
| 240 | $sel-match: false; | 263 | $sel-match: false; |
| 241 | 264 | ||
| 242 | @each $suffix in $suffixes { | 265 | @each $suffix in $suffixes { |
| 243 | @if not $sel-match { | 266 | @if not $sel-match { |
| 244 | $suf-match: true; | 267 | $suf-match: true; |
| 245 | 268 | ||
| 246 | @for $i from 1 through length($suffix) { | 269 | @for $i from 1 through list.length($suffix) { |
| 247 | @if $suf-match and (nth($sel, -$i) != nth($suffix, -$i)) { | 270 | @if $suf-match and (list.nth($sel, -$i) != list.nth($suffix, -$i)) { |
| 248 | $suf-match: false; | 271 | $suf-match: false; |
| 249 | } | 272 | } |
| 250 | } | 273 | } |
| 251 | 274 | ||
| 252 | @if $suf-match { | 275 | @if $suf-match { |
| 253 | $sel-match: true; | 276 | $sel-match: true; |
| 254 | } | 277 | } |
| 255 | } | 278 | } |
| 256 | } | 279 | } |
| 257 | 280 | ||
| 258 | @if not $sel-match { | 281 | @if not $sel-match { |
| 259 | $match: false; | 282 | $match: false; |
| 260 | } | 283 | } |
| 261 | } | 284 | } |
| 262 | } | 285 | } |
| 263 | 286 | ||
| 264 | @return $match; | 287 | @return $match; |
| 265 | } | 288 | } |
| 266 | 289 | ||
| 267 | /// | 290 | /// |
| @@ -272,7 +295,7 @@ | |||
| 272 | /// @return {number} Unit-less variable | 295 | /// @return {number} Unit-less variable |
| 273 | /// | 296 | /// |
| 274 | @function strip-unit($n) { | 297 | @function strip-unit($n) { |
| 275 | @return math.div($n, $n * 0 + 1); | 298 | @return math.div($n, $n * 0 + 1); |
| 276 | } | 299 | } |
| 277 | 300 | ||
| 278 | /// | 301 | /// |
| @@ -284,7 +307,62 @@ | |||
| 284 | /// @return {number} Pixel value converted to rem | 307 | /// @return {number} Pixel value converted to rem |
| 285 | /// | 308 | /// |
| 286 | @function px-to-rem($size, $base: vars.$root-size) { | 309 | @function px-to-rem($size, $base: vars.$root-size) { |
| 287 | @return math.div($size, $base) * 1rem; | 310 | @return math.div($size, $base) * 1rem; |
| 311 | } | ||
| 312 | |||
| 313 | /// | ||
| 314 | /// Casts a string into a number | ||
| 315 | /// | ||
| 316 | /// @param {string|number} $value | ||
| 317 | /// | ||
| 318 | /// @return {number} | ||
| 319 | /// | ||
| 320 | @function to-number($value) { | ||
| 321 | @if meta.type-of($value) == 'number' { | ||
| 322 | @return $value; | ||
| 323 | } | ||
| 324 | @if meta.type-of($value) != 'string' { | ||
| 325 | @error 'Value for `to-number` should be a number or a string.'; | ||
| 326 | } | ||
| 327 | |||
| 328 | $result: 0; | ||
| 329 | $digits: 0; | ||
| 330 | $minus: string.slice($value, 1, 1) == '-'; | ||
| 331 | |||
| 332 | @for $i from if($minus, 2, 1) through string.length($value) { | ||
| 333 | $character: string.slice($value, $i, $i); | ||
| 334 | |||
| 335 | @if not list.index(map.keys($numbers), $character) and $character != '.' { | ||
| 336 | @return to-length(if($minus, -$result, $result), string.slice($value, $i)); | ||
| 337 | } | ||
| 338 | |||
| 339 | @if $character == '.' { | ||
| 340 | $digits: 1; | ||
| 341 | } @else if $digits == 0 { | ||
| 342 | $result: $result * 10 + map.get($numbers, $character); | ||
| 343 | } @else { | ||
| 344 | $digits: $digits * 10; | ||
| 345 | $result: $result + math.div(map.get($numbers, $character), $digits); | ||
| 346 | } | ||
| 347 | } | ||
| 348 | |||
| 349 | @return if($minus, -$result, $result); | ||
| 350 | } | ||
| 351 | |||
| 352 | /// | ||
| 353 | /// Add $unit to $value | ||
| 354 | /// | ||
| 355 | /// @param {number} $value - Value to add unit to | ||
| 356 | /// @param {string} $unit - String representation of the unit | ||
| 357 | /// | ||
| 358 | /// @return {number} $value expressed in $unit | ||
| 359 | /// | ||
| 360 | @function to-length($value, $unit) { | ||
| 361 | @if not list.index(map.keys($units), $unit) { | ||
| 362 | @error 'Invalid unit `#{$unit}`.'; | ||
| 363 | } | ||
| 364 | |||
| 365 | @return $value * map.get($units, $unit); | ||
| 288 | } | 366 | } |
| 289 | 367 | ||
| 290 | /// | 368 | /// |
| @@ -293,5 +371,5 @@ | |||
| 293 | /// @content | 371 | /// @content |
| 294 | /// | 372 | /// |
| 295 | @mixin execute { | 373 | @mixin execute { |
| 296 | @content; | 374 | @content; |
| 297 | } | 375 | } |
