|
| 1 | +// ========================================================================== |
| 2 | +// _units.scss |
| 3 | +// |
| 4 | +// Contains all functions relating to units |
| 5 | +// ========================================================================== |
| 6 | + |
| 7 | +// @include line-height |
| 8 | +// |
| 9 | +// Given a font-size and a line-height (in pixels) |
| 10 | +// Returns a unitless line-height value |
| 11 | +// |
| 12 | +@use 'sass:math'; |
| 13 | + |
| 14 | +@function line-height($font-size: 'body-s', $line-height: '20', $scale: 'default') { |
| 15 | + @if type-of($font-size) == 'number' { |
| 16 | + @return decimal-round(math.div($line-height, $font-size), 2); |
| 17 | + } @else if map-has-key($type, $font-size) { // else try and find the value in our type map |
| 18 | + $key-map: map-get($type, $font-size); |
| 19 | + $font-list: map-get($key-map, $scale); |
| 20 | + |
| 21 | + @if type-of($font-list) == 'list' { |
| 22 | + @return line-height(nth($font-list, 1), nth($font-list, 2)); |
| 23 | + } @else { |
| 24 | + @warn 'No line-height found as part of the $type map for #{ $font-size }'; |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +@function decimal-round($number, $digits: 0, $mode: round) { |
| 30 | + $n: 1; |
| 31 | + |
| 32 | + // $number must be a number |
| 33 | + @if type-of($number) != number { |
| 34 | + @warn '#{ $number } is not a number.'; |
| 35 | + @return $number; |
| 36 | + } |
| 37 | + |
| 38 | + // $digits must be a unitless number |
| 39 | + @if type-of($digits) != number { |
| 40 | + @warn '#{ $digits } is not a number.'; |
| 41 | + @return $number; |
| 42 | + } @else if not unitless($digits) { |
| 43 | + @warn '#{ $digits } has a unit.'; |
| 44 | + @return $number; |
| 45 | + } |
| 46 | + |
| 47 | + @if $digits > 0 { |
| 48 | + @for $i from 1 through $digits { |
| 49 | + $n: $n * 10; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @if $mode == round { |
| 54 | + @return math.div(round($number * $n), $n); |
| 55 | + } @else if $mode == ceil { |
| 56 | + @return math.div(ceil($number * $n), $n); |
| 57 | + } @else if $mode == floor { |
| 58 | + @return math.div(floor($number * $n), $n); |
| 59 | + } @else { |
| 60 | + @warn '#{ $mode } is undefined keyword.'; |
| 61 | + @return $number; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +@function em-to-px($emVal, $base: $font-size-base) { |
| 66 | + @return ($emVal * $base) * 1px; |
| 67 | +} |
| 68 | + |
| 69 | +@function map-to-px($breakpoints, $base: $font-size-base) { |
| 70 | + $newBreakpoints: (); |
| 71 | + |
| 72 | + @each $name, $emValue in $breakpoints { |
| 73 | + $pxValue: em-to-px(strip-units($emValue), $base); |
| 74 | + $newBreakpoints: map-merge($newBreakpoints, ($name: $pxValue)); |
| 75 | + } |
| 76 | + |
| 77 | + @return $newBreakpoints; |
| 78 | +} |
0 commit comments