-
Notifications
You must be signed in to change notification settings - Fork 234
fix: base-button, update aria label on updated event. This also updated attr on slotchange event #5674
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
base: main
Are you sure you want to change the base?
fix: base-button, update aria label on updated event. This also updated attr on slotchange event #5674
Changes from all commits
58799cc
87f684e
44902dd
d125203
2efcb88
fca159c
100ab97
4143a71
ec3bf52
ed134ec
2d2515f
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -220,18 +220,45 @@ export class ButtonBase extends ObserveSlotText(LikeAnchor(Focusable), '', [ | |||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* @label property is empty within changed propertyValues on slot change event. | ||||||||||
* Need to check actualy @label property value instead. | ||||||||||
*/ | ||||||||||
protected updateAriaLabel(): void { | ||||||||||
if ( | ||||||||||
'pending' in this && | ||||||||||
(this as { pending: boolean }).pending === true | ||||||||||
) { | ||||||||||
return; | ||||||||||
} | ||||||||||
Comment on lines
+228
to
+233
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. Checking for pending state |
||||||||||
|
||||||||||
// check if label exists and is not empty | ||||||||||
if (this.label && this.label !== '') { | ||||||||||
// prevent update if the label is the same as the aria-label | ||||||||||
if (this.label !== this.getAttribute('aria-label')) { | ||||||||||
this.setAttribute('aria-label', this.label); | ||||||||||
} | ||||||||||
} else { | ||||||||||
// if dev set aria-label instead of label, don't remove it, and throw warning | ||||||||||
if ( | ||||||||||
!this.hasAttribute('label') && | ||||||||||
this.hasAttribute('aria-label') | ||||||||||
) { | ||||||||||
console.warn( | ||||||||||
this, | ||||||||||
'DO NOT set aria-label attribute on sp-button, use label prop instead.' | ||||||||||
); | ||||||||||
return; | ||||||||||
} | ||||||||||
this.removeAttribute('aria-label'); | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
protected override firstUpdated(changed: PropertyValues): void { | ||||||||||
super.firstUpdated(changed); | ||||||||||
if (!this.hasAttribute('tabindex')) { | ||||||||||
this.setAttribute('tabindex', '0'); | ||||||||||
} | ||||||||||
if (changed.has('label')) { | ||||||||||
if (this.label) { | ||||||||||
this.setAttribute('aria-label', this.label); | ||||||||||
} else { | ||||||||||
this.removeAttribute('aria-label'); | ||||||||||
} | ||||||||||
} | ||||||||||
this.manageAnchor(); | ||||||||||
this.addEventListener('keydown', this.handleKeydown); | ||||||||||
this.addEventListener('keypress', this.handleKeypress); | ||||||||||
|
@@ -243,6 +270,8 @@ export class ButtonBase extends ObserveSlotText(LikeAnchor(Focusable), '', [ | |||||||||
this.manageAnchor(); | ||||||||||
} | ||||||||||
|
||||||||||
this.updateAriaLabel(); | ||||||||||
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.
Suggested change
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. Done. |
||||||||||
|
||||||||||
if (this.anchorElement) { | ||||||||||
// Ensure the anchor element is not focusable directly via tab | ||||||||||
this.anchorElement.tabIndex = -1; | ||||||||||
|
@@ -256,14 +285,4 @@ export class ButtonBase extends ObserveSlotText(LikeAnchor(Focusable), '', [ | |||||||||
this.anchorElement.addEventListener('focus', this.proxyFocus); | ||||||||||
} | ||||||||||
} | ||||||||||
protected override update(changes: PropertyValues): void { | ||||||||||
super.update(changes); | ||||||||||
if (changes.has('label')) { | ||||||||||
if (this.label) { | ||||||||||
this.setAttribute('aria-label', this.label); | ||||||||||
} else { | ||||||||||
this.removeAttribute('aria-label'); | ||||||||||
} | ||||||||||
} | ||||||||||
Comment on lines
-261
to
-267
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. In update it is still useful to track label changes. 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. Done. |
||||||||||
} | ||||||||||
} |
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.
Only update
aria-label
if we have a label property and we're not in apending
state. You need to account for these scenarios too. A minor update to your code would be.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.
Done.
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 seem to cause the issue. I reverted my original solution.