Skip to content

feat(progress-spinner): add support for custom stroke-width #4113

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

Merged
merged 1 commit into from
Apr 21, 2017

Conversation

devversion
Copy link
Member

  • Adds support for custom stroke-width of the progress-spinner elements.

Fixes #3934

@devversion devversion requested a review from crisbeto April 15, 2017 17:45
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Apr 15, 2017
@@ -33,8 +33,8 @@ $mat-progress-spinner-viewport-size: 100px !default;
path {
fill: transparent;

// Stroke width of 10px defines stroke as 10% of the viewBox.
stroke-width: $mat-progress-spinner-stroke-width;
// Stroke width is by default 10px, which is relatively to the viewBox size 10%.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It currently is centered properly in the viewBox in the getSvgArc method, we define pathRadius as 40 and radius as 50. If we allow the strokeWidth to be set we will need to make a corresponding adjustment to the pathRadius.

Thought we need to keep in mind getSvgArc is called ~60 times/sec, so ideally we don't want to recalculate the pathRadius value every time if we can avoid it, though it is a pretty trivial calculation compared to determining easing values.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. In theory we could cache the pathRadius when the stroke width is updated through a setter. But I feel like it's not worth passing a cached value to the getSvgArc function (due to the trivial subtraction).

Let me know what you think. Happy to implement the caching if desired.

@@ -103,6 +107,11 @@ export class MdProgressSpinner implements OnDestroy {
this._cleanupIndeterminateAnimation();
}

/** Stroke width of the progress spinner. By default uses 10px as stroke width. */
@Input()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just use

@Input() strokeWidth: number = DEFAULT_STROKE_WIDTH;

Then it will have the default value, otherwise will use the input bound value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to make a getter/setter because I wanted to avoid that the developer sets the value to 0 or falsy values.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will still break if the consumer sets it to something like "abc". IMO the getter/setter here is overkill.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. We can just set the default stroke-width as initial value.

@@ -5,5 +5,5 @@
-->
<svg viewBox="0 0 100 100"
preserveAspectRatio="xMidYMid meet">
<path></path>
<path [style.strokeWidth]="strokeWidth"></path>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the value is being set in the style attribute here, should it be removed from the scss file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I removed it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I just missed it sorry.

Copy link
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a little bit of cleanup.

@@ -108,6 +108,29 @@ describe('MdProgressSpinner', () => {
expect(progressElement.componentInstance.interdeterminateInterval).toBeFalsy();
});

it('should allow a custom stoke width', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stoke->stroke


fixture.detectChanges();

expect(parseInt(pathElement.style.strokeWidth))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to parse the string to an int here. You can just expect it to be '10'.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a need for that. Safari 9 adds the px suffix to the strokeWidth value. We could have also done it through toContain but parseInt and toBe is more safe here.

@@ -25,6 +25,9 @@ const endIndeterminate = 80;
/* Maximum angle for the arc. The angle can't be exactly 360, because the arc becomes hidden. */
const MAX_ANGLE = 359.99 / 100;

// Stroke width is by default 10px, which is relatively to the viewBox size 10%.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure whether this comment is very helpful. We'd also need to keep it updated if we change the default value. Something like "Default stroke width as a percentage of the viewBox" might be better.

@@ -103,6 +107,11 @@ export class MdProgressSpinner implements OnDestroy {
this._cleanupIndeterminateAnimation();
}

/** Stroke width of the progress spinner. By default uses 10px as stroke width. */
@Input()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will still break if the consumer sets it to something like "abc". IMO the getter/setter here is overkill.

fixture.detectChanges();

expect(parseInt(pathElement.style.strokeWidth))
.toBe(10, 'Expected stroke width to fallback to 10px.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether we should be testing the exact default value here since it'll start failing if we change it. I suggest either importing the DEFAULT_STROKE_WIDTH and checking against it, or just checking that it has a valid stroke width in the first place.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think importing might be the best solution here.

@devversion devversion force-pushed the feat/progress-spinner-stroke branch 2 times, most recently from af36ecb to c9a9e06 Compare April 16, 2017 12:43
Copy link
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, one minor nit.

@@ -73,6 +75,9 @@ export class MdProgressSpinner implements OnDestroy {
private _value: number;
private _color: string = 'primary';

/** Stroke width of the progress spinner. By default uses 10px as stroke width. */
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 that this should have the By default uses 10px as stroke width. since the default may change and I doubt that we'd remember to update the comment.

@devversion devversion added the action: merge The PR is ready for merge by the caretaker label Apr 17, 2017
@wiwski
Copy link

wiwski commented Apr 20, 2017

Hi there,
Any news about this merge ?

* Adds support for custom `stroke-width` of the progress-spinner elements.

Fixes angular#3934
@devversion devversion force-pushed the feat/progress-spinner-stroke branch from ac419c5 to 608babe Compare April 20, 2017 14:48
@kara kara merged commit b846a27 into angular:master Apr 21, 2017
@devversion devversion deleted the feat/progress-spinner-stroke branch November 11, 2017 10:20
@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 7, 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.

New @Input in progress-spinner component for controlling the stroke-width
6 participants