Skip to content
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

Merged
merged 11 commits into from
Nov 8, 2016

Conversation

mmalerba
Copy link
Contributor

…fill and thumb position.

@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Oct 31, 2016
@mmalerba
Copy link
Contributor Author

mmalerba commented Oct 31, 2016

fixes #1389
fixes #1304
fixes #1234

Copy link
Member

@jelbourn jelbourn left a 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>
Copy link
Member

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'
Copy link
Member

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',

Copy link
Member

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))) {
Copy link
Member

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 */
Copy link
Member

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();
Copy link
Member

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);
Copy link
Member

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);

Copy link
Contributor Author

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"]
Copy link
Member

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.

Copy link
Contributor Author

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?

@mmalerba
Copy link
Contributor Author

mmalerba commented Nov 4, 2016

Comments addressed and rebased

Copy link
Member

@jelbourn jelbourn left a 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);
Copy link
Member

@jelbourn jelbourn Nov 7, 2016

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);
Copy link
Member

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.

@jelbourn jelbourn added pr: lgtm action: merge The PR is ready for merge by the caretaker and removed pr: needs review labels Nov 8, 2016
@jelbourn jelbourn merged commit 8815846 into angular:master Nov 8, 2016
rolandjitsu pushed a commit to rolandjitsu/material2 that referenced this pull request Nov 10, 2016
@mmalerba mmalerba deleted the slider-pr branch November 18, 2016 00:34
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants