-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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(slider): refactor the slider to use percent values for the track … #1663
Conversation
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.
Overall looks good, just have some refactoring comments.
<div class="md-slider-track-fill" [style.flexBasis]="percent * 100 + '%'"></div> | ||
<div class="md-slider-ticks-container" [style.marginLeft]="-tickIntervalPercent / 2 * 100 + '%'"> | ||
<div class="md-slider-ticks" [style.marginLeft]="tickIntervalPercent / 2 * 100 + '%'" | ||
[style.backgroundSize]="tickIntervalPercent * 100 + '% 2px'"></div> |
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.
Can we move these expressions into functions?
.md-slider-ticks { | ||
background: repeating-linear-gradient(to right, $md-slider-tick-color, | ||
$md-slider-tick-color $md-slider-tick-size, transparent 0, transparent) repeat; | ||
/* Firefox doesn't draw the gradient correctly with 'to right' |
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.
Use //
style comments
'[attr.aria-valuemax]': 'max', | ||
'[attr.aria-valuemin]': 'min', | ||
'[attr.aria-valuenow]': 'value', | ||
|
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 don't think the blank lines are necessary in the host
config
set step(v) { | ||
// Only set the value to a valid number. v is casted to an any as we know it will come in as a | ||
// string but it is labeled as a number which causes parseFloat to not accept it. | ||
if (isNaN(parseFloat(<any> v))) { |
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.
Since this is used a lot, we should refactor to a common function like coerceNumberProperty
, something like...
export function coerceNumberProperty(value: any, fallbackValue = 0) {
return isNaN(parseFloat(v as any)) ? fallbackValue : Number(v);
)
This would be a slight change, since it will go back to the default when getting an invalid value, but I think that is more sensible behavior than not updating at all.
// Only set the value to a valid number. v is casted to an any as we know it will come in as a | ||
// string but it is labeled as a number which causes parseFloat to not accept it. | ||
if (isNaN(parseFloat(<any> v))) { | ||
/** TODO: internal */ |
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.
You can omit the TODO: internal
comments now
this._controlValueAccessorChangeFn(this.value); | ||
this.snapThumbToValue(); | ||
this._updateTickSeparation(); | ||
this._updateTickIntervalPercent(); |
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.
Add comment saying why you update dimensions and ticks on mouseenter
this._renderer.updateThumbAndFillPosition(this._percent, this._sliderDimensions.width); | ||
setTimeout(() => { | ||
this.isSliding = false; | ||
}, 0); |
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.
You can use requestAnimationFrame
requestAnimationFrame(() => this.isSliding = false);
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 think this is not even necessary anymore now that the position is percent based.
@@ -28,7 +28,9 @@ | |||
|
|||
"property-case": "lower", | |||
|
|||
"declaration-block-no-duplicate-properties": true, | |||
"declaration-block-no-duplicate-properties": [ true, { | |||
"ignore": ["consecutive-duplicates"] |
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.
Let's use consecutive-duplicates-with-different-values
in case someone just accidentally duplicates a line.
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.
So I actually tried that and I got: Invalid value "consecutive-duplicates-with-different-values", maybe we're not using the latest version of stylelint?
…fill and thumb position.
Comments addressed and rebased |
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.
Just a couple last comments
@@ -0,0 +1,4 @@ | |||
/** Coerces a data-bound value (typically a string) to a number. */ | |||
export function coerceNumberProperty(value: any, fallbackValue = 0) { | |||
return isNaN(parseFloat(value as any)) ? fallbackValue : Number(value); |
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.
On second look I realize that parseFloat(value)
and Number(value)
are subtly different in their handling of non-numeric characters. E.g.,
parseFloat('123 boats') == 123
Number('123 boats') === NaN
We should just use one of these- probably Number
:
let numberValue = Number(value);
return isNaN(numberValue) ? fallbackValue : numberValue;
set value(v: number) { | ||
this._value = coerceNumberProperty(v, this._value); | ||
this._percent = this._calculatePercentage(this._value); | ||
this._controlValueAccessorChangeFn(this._value); |
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.
We should only invoke the controlValueAccessorChangeFn
when the value was changed via user interaction, though we could change this in a separate PR since it was already ike this.
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
…fill and thumb position.