-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(shape): Fix errors related to multi-value shape categories #4547
Changes from 7 commits
bec54cf
b8f8538
95ede35
6307eba
937f539
5904bd1
dfe2347
f76d287
726b27a
d05f6ae
242c352
875b8e3
308b6e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,8 @@ | |
// mdc-shape-resolve-percentage-radius(36px, 50%) => `18px` (i.e., 36px / 2) | ||
// | ||
@function mdc-shape-resolve-percentage-radius($component-height, $radius) { | ||
$radius: mdc-shape-prop-value($radius); | ||
|
||
@if type-of($radius) == "list" { | ||
$radius-value: (); | ||
|
||
|
@@ -70,39 +72,10 @@ | |
} | ||
} | ||
|
||
@function mdc-shape-resolve-percentage-for-corner_($component-height, $radius) { | ||
$radius-value: mdc-shape-prop-corner-value_($radius); | ||
|
||
@if type-of($radius-value) == "number" and unit($radius-value) == "%" { | ||
// Converts the percentage to number without unit. Example: 50% => 50. | ||
$percentage: $radius-value / ($radius-value * 0 + 1); | ||
|
||
@return $component-height * ($percentage / 100); | ||
} @else { | ||
@return $radius-value; | ||
} | ||
} | ||
|
||
// | ||
// Strips unit from number. This is accomplished by dividing the value by itself to cancel out units, while resulting | ||
// in a denominator of 1. | ||
// | ||
// Examples: | ||
// | ||
// 50% => 50 | ||
// | ||
@function mdc-shape-strip-unit_($number) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function was apparently not being used whatsoever, so I removed it. |
||
@if type-of($number) == "number" and not unitless($number) { | ||
@return $number / ($number * 0 + 1); | ||
} | ||
|
||
@return $number; | ||
} | ||
|
||
// | ||
// Returns $radius value of shape category - `large`, `medium` or `small`. | ||
// Otherwise, it returns the $radius itself if valid. | ||
// $radius can be a single value or list of up to 4. | ||
// $radius can be a single value, or a list of up to 4 values. | ||
kfranqueiro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// Examples: | ||
// | ||
|
@@ -114,15 +87,27 @@ | |
@error "Invalid radius: '#{$radius}' is more than 4 values"; | ||
} | ||
|
||
$radius-value: (); | ||
$radius-values: (); | ||
|
||
@each $corner in $radius { | ||
$radius-value: append($radius-value, mdc-shape-prop-corner-value_($corner)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ended up merging the logic of |
||
@for $i from 1 through length($radius) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious, why is this changed from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need the index to correlate between the given radius list and the fully-expanded list of radii for any categories that are referenced. |
||
$corner: nth($radius, $i); | ||
|
||
@if map-has-key($mdc-shape-category-values, $corner) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if I'm OK not supporting something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would result in |
||
// If a category is encountered within a list of radii, apply the category's value for the corresponding corner | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This behavior maximizes compatibility with the existing behavior which clearly expected shape categories to be applicable to individual corners (e.g. |
||
$radius-values: | ||
append($radius-values, nth(mdc-shape-unpack-radius_(map-get($mdc-shape-category-values, $corner)), $i)); | ||
} @else { | ||
$radius-values: append($radius-values, mdc-shape-validate-radius-value_($corner)); | ||
} | ||
} | ||
|
||
@return $radius-value; | ||
@return $radius-values; | ||
} @else { | ||
@return mdc-shape-prop-corner-value_($radius); | ||
@if map-has-key($mdc-shape-category-values, $radius) { | ||
@return map-get($mdc-shape-category-values, $radius); | ||
} @else { | ||
@return mdc-shape-validate-radius-value_($radius); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -147,34 +132,50 @@ | |
@error "Expected masked-corners of length 4 but got '#{length($masked-corners)}'."; | ||
} | ||
|
||
@if length($radius) == 3 { | ||
$radius: nth($radius, 1) nth($radius, 2) nth($radius, 3) nth($radius, 2); | ||
} @else if length($radius) == 2 { | ||
$radius: nth($radius, 1) nth($radius, 2) nth($radius, 1) nth($radius, 2); | ||
} @else if length($radius) == 1 { | ||
$radius: $radius $radius $radius $radius; | ||
} | ||
$radius: mdc-shape-unpack-radius_($radius); | ||
|
||
@return if(nth($masked-corners, 1) == 1, nth($radius, 1), 0) | ||
if(nth($masked-corners, 2) == 1, nth($radius, 2), 0) | ||
if(nth($masked-corners, 3) == 1, nth($radius, 3), 0) | ||
if(nth($masked-corners, 4) == 1, nth($radius, 4), 0); | ||
} | ||
|
||
@function mdc-shape-prop-corner-value_($radius) { | ||
@if map-has-key($mdc-shape-category-values, $radius) { | ||
@return map-get($mdc-shape-category-values, $radius); | ||
} @else if mdc-shape-is-valid-radius-value_($radius) { | ||
// | ||
// Unpacks shorthand values for border-radius (i.e. lists of 1-3 values). | ||
// If a list of 4 values is given, it is returned as-is. | ||
// TODO: This is private for purposes of getting it into a patch; make it public for a future minor/major release. | ||
kfranqueiro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
@function mdc-shape-unpack-radius_($radius) { | ||
@if length($radius) == 4 { | ||
@return $radius; | ||
} @else { | ||
@error "Invalid radius: '#{$radius}' radius is not supported"; | ||
} @else if length($radius) == 3 { | ||
@return nth($radius, 1) nth($radius, 2) nth($radius, 3) nth($radius, 2); | ||
} @else if length($radius) == 2 { | ||
@return nth($radius, 1) nth($radius, 2) nth($radius, 1) nth($radius, 2); | ||
} @else if length($radius) == 1 { | ||
@return $radius $radius $radius $radius; | ||
} | ||
|
||
@return map-get($mdc-shape-category-values, $radius); | ||
@error "Invalid radius: '#{$radius}' is more than 4 values"; | ||
} | ||
|
||
@function mdc-shape-is-valid-radius-value_($radius) { | ||
@function mdc-shape-resolve-percentage-for-corner_($component-height, $radius) { | ||
@if type-of($radius) == "number" and unit($radius) == "%" { | ||
// Converts the percentage to number without unit. Example: 50% => 50. | ||
$percentage: $radius / ($radius * 0 + 1); | ||
|
||
@return $component-height * ($percentage / 100); | ||
} @else { | ||
@return $radius; | ||
} | ||
} | ||
|
||
@function mdc-shape-validate-radius-value_($radius) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function has been rewritten to emit an error, akin to the |
||
$is-number: type-of($radius) == "number"; | ||
|
||
@return $is-number or str_index($radius, "var(") or str_index($radius, "calc("); | ||
@if not ($is-number or str_index($radius, "var(") or str_index($radius, "calc(")) { | ||
@error "Invalid radius: #{$radius}"; | ||
} | ||
|
||
@return $radius; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved this function lower in the file verbatim, to group internal functions together.