-
Notifications
You must be signed in to change notification settings - Fork 233
fix(button): update aria-label when label changes #5691
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?
Changes from 4 commits
3543797
47725c8
421653f
52489e7
7ff7359
e090f5b
0509970
605828b
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@spectrum-web-components/button': patch | ||
--- | ||
|
||
**Fixed** aria-label updates when `label` property changes. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -220,18 +220,26 @@ export class ButtonBase extends ObserveSlotText(LikeAnchor(Focusable), '', [ | |
} | ||
} | ||
|
||
private isPendingState(): boolean { | ||
return ( | ||
'pending' in this && (this as { pending: boolean }).pending === true | ||
Rajdeepc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
); | ||
} | ||
|
||
private updateAriaLabel(): void { | ||
if (this.label) { | ||
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. Should we explicitly check for undefined instead because empty quotes would be a valid label ( 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. Good question, I haven't thought of that since this check was also used previously. 🤔 |
||
this.setAttribute('aria-label', this.label); | ||
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. If a user sets private _userSetAriaLabel = false;
connectedCallback() {
super.connectedCallback();
// Detect if aria-label was set by the user before component logic runs
this._userSetAriaLabel = this.hasAttribute('aria-label');
}
attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null) {
if (name === 'aria-label' && oldValue !== newValue) {
this._userSetAriaLabel = true;
}
super.attributeChangedCallback(name, oldValue, newValue);
}
private updateAriaLabel(): void {
if (this._userSetAriaLabel) return; // Don't override user intent
if (this.label) {
this.setAttribute('aria-label', this.label);
} else {
this.removeAttribute('aria-label');
}
} 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. So we want to allow both 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. Theoretically we generally don’t want both to apply at the same time. The implementation should respect a consumer’s explicit
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. I agree that this might be a good improvement, even though there is no need for a consumer to provide both 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. I think keeping that change as out of scope makes sense. I'd like to see us create a follow-up ticket to cover the suggestion though. @Rajdeepc would you be able to take care of that and include your code suggestions? 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. Yes I agree with both of you! Let's create a follow up ticket and unblock this PR for now. |
||
} else { | ||
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 +251,11 @@ export class ButtonBase extends ObserveSlotText(LikeAnchor(Focusable), '', [ | |
this.manageAnchor(); | ||
} | ||
|
||
// Don't override PendingStateController's aria-label changes | ||
mizgaionutalexandru marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (changed.has('label') && !this.isPendingState()) { | ||
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. Both 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. This surfaces multiple conflicts since the Button component has 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. Cool thats something we would like to do in future. Do you mind validating this in screen readers? 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. Sure, I added a screen recording, validating the manual test cases with a screen reader. Let me know if I should add anything else. |
||
} | ||
|
||
if (this.anchorElement) { | ||
// Ensure the anchor element is not focusable directly via tab | ||
this.anchorElement.tabIndex = -1; | ||
|
@@ -259,11 +272,7 @@ export class ButtonBase extends ObserveSlotText(LikeAnchor(Focusable), '', [ | |
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'); | ||
} | ||
this.updateAriaLabel(); | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.