Skip to content

Commit

Permalink
Use custom divide() function instead of Dart Sass math module for gre…
Browse files Browse the repository at this point in the history
…ater compatibility
  • Loading branch information
mdo committed Jun 14, 2021
1 parent d41ef89 commit 85f98fe
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
29 changes: 27 additions & 2 deletions scss/_functions.scss
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ $_luminance-list: .0008 .001 .0011 .0013 .0015 .0017 .002 .0022 .0025 .0027 .003
$l1: luminance($background);
$l2: luminance(opaque($background, $foreground));

@return if($l1 > $l2, ($l1 + .05) / ($l2 + .05), ($l2 + .05) / ($l1 + .05));
@return if($l1 > $l2, divide($l1 + .05, $l2 + .05), divide($l2 + .05, $l1 + .05));
}

// Return WCAG2.0 relative luminance
Expand All @@ -137,7 +137,7 @@ $_luminance-list: .0008 .001 .0011 .0013 .0015 .0017 .002 .0022 .0025 .0027 .003
);

@each $name, $value in $rgb {
$value: if($value * .555 < .03928, $value * .555 / 12.92, nth($_luminance-list, $value + 1));
$value: if($value * .555 < .03928, divide($value * .555, 12.92), nth($_luminance-list, $value + 1));
$rgb: map-merge($rgb, ($name: $value));
}

Expand Down Expand Up @@ -219,3 +219,28 @@ $_luminance-list: .0008 .001 .0011 .0013 .0015 .0017 .002 .0022 .0025 .0027 .003

@return if($return-calc == true, calc(#{$value1} - #{$value2}), $value1 + unquote(" - ") + $value2);
}

@function divide($dividend, $divisor, $precision: 10) {
$sign: if($dividend > 0 and $divisor > 0, 1, -1);
$dividend: abs($dividend);
$divisor: abs($divisor);
$quotient: 0;
$remainder: $dividend;
@if $dividend == 0 {
@return 0;
}
@if $divisor == 0 {
@error "Cannot divide by 0";
}
@if $divisor == 1 {
@return $dividend;
}
@while $remainder >= $divisor {
$quotient: $quotient + 1;
$remainder: $remainder - $divisor;
}
@if $remainder > 0 and $precision > 0 {
$remainder: divide($remainder * 10, $divisor, $precision - 1) * .1;
}
@return ($quotient + $remainder) * $sign;
}
5 changes: 4 additions & 1 deletion scss/mixins/_grid.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@use "sass:math";

/// Grid system
//
// Generate semantic grid columns with these mixins.
Expand Down Expand Up @@ -29,7 +31,8 @@
@mixin make-col($size: false, $columns: $grid-columns) {
@if $size {
flex: 0 0 auto;
width: percentage($size / $columns);
width: percentage(divide($size, $columns));

} @else {
flex: 1 1 0;
max-width: 100%;
Expand Down

0 comments on commit 85f98fe

Please sign in to comment.