-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Conversation
@@ -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%. |
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.
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.
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.
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() |
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.
why not just use
@Input() strokeWidth: number = DEFAULT_STROKE_WIDTH;
Then it will have the default value, otherwise will use the input bound 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 wanted to make a getter/setter because I wanted to avoid that the developer sets the value to 0
or falsy values.
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.
This will still break if the consumer sets it to something like "abc". IMO the getter/setter here is overkill.
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.
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> |
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.
If the value is being set in the style attribute here, should it be removed from the scss file?
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 I removed it?
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.
Yep, I just missed it sorry.
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.
Needs a little bit of cleanup.
@@ -108,6 +108,29 @@ describe('MdProgressSpinner', () => { | |||
expect(progressElement.componentInstance.interdeterminateInterval).toBeFalsy(); | |||
}); | |||
|
|||
it('should allow a custom stoke width', () => { |
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.
stoke->stroke
|
||
fixture.detectChanges(); | ||
|
||
expect(parseInt(pathElement.style.strokeWidth)) |
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.
There's no need to parse the string to an int here. You can just expect it to be '10'
.
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.
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%. |
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.
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() |
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.
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.'); |
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'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.
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 importing might be the best solution here.
af36ecb
to
c9a9e06
Compare
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.
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. */ |
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 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.
Hi there, |
* Adds support for custom `stroke-width` of the progress-spinner elements. Fixes angular#3934
ac419c5
to
608babe
Compare
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. |
stroke-width
of the progress-spinner elements.Fixes #3934