Skip to content

Only sets aria-labelledby to button label node only if loading is true #4450

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/rude-chefs-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@primer/react': patch
---

Button and IconButton only use aria-labelledby to preserve the button label in a loading state OR if aria-labelledBy was explicitly passed.

<!-- Changed components: Button, IconButton -->
9 changes: 5 additions & 4 deletions packages/react/src/Button/ButtonBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ const ButtonBase = forwardRef(
}, [baseStyles, sxProp])
const uuid = useId(id)
const loadingAnnouncementID = `${uuid}-loading-announcement`
const buttonLabelID = ariaLabelledBy || `${uuid}-label`

if (__DEV__) {
/**
Expand Down Expand Up @@ -100,8 +99,10 @@ const ButtonBase = forwardRef(
aria-describedby={[loadingAnnouncementID, ariaDescribedBy]
.filter(descriptionID => Boolean(descriptionID))
.join(' ')}
// aria-labelledby is needed because the accessible name becomes unset when the button is in a loading state
aria-labelledby={buttonLabelID}
// aria-labelledby is needed because the accessible name becomes unset when the button is in a loading state.
// We only set it when the button is in a loading state because it will supercede the aria-label when the screen
// reader announces the button name.
aria-labelledby={loading ? `${uuid}-label` : ariaLabelledBy}
id={id}
onClick={loading ? undefined : onClick}
>
Expand All @@ -117,7 +118,7 @@ const ButtonBase = forwardRef(
{loading && !LeadingVisual && !TrailingVisual && renderVisual(Spinner, loading, 'loadingSpinner')}
{LeadingVisual && renderVisual(LeadingVisual, loading, 'leadingVisual')}
{children && (
<span data-component="text" id={buttonLabelID}>
<span data-component="text" id={loading ? `${uuid}-label` : undefined}>
{children}
{count !== undefined && !TrailingVisual && (
<CounterLabel data-component="ButtonCounter" sx={{ml: 2}}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ exports[`Button respects block prop 1`] = `

<button
aria-describedby="test-button-loading-announcement"
aria-labelledby="test-button-label"
class="c0"
data-block="block"
data-loading="false"
Expand All @@ -278,7 +277,6 @@ exports[`Button respects block prop 1`] = `
>
<span
data-component="text"
id="test-button-label"
>
Block
</span>
Expand Down Expand Up @@ -550,7 +548,6 @@ exports[`Button respects the alignContent prop 1`] = `

<button
aria-describedby="test-button-loading-announcement"
aria-labelledby="test-button-label"
class="c0"
data-loading="false"
data-no-visuals="true"
Expand All @@ -563,7 +560,6 @@ exports[`Button respects the alignContent prop 1`] = `
>
<span
data-component="text"
id="test-button-label"
>
Align start
</span>
Expand Down Expand Up @@ -835,7 +831,6 @@ exports[`Button respects the large size prop 1`] = `

<button
aria-describedby="test-button-loading-announcement"
aria-labelledby="test-button-label"
class="c0"
data-loading="false"
data-no-visuals="true"
Expand All @@ -849,7 +844,6 @@ exports[`Button respects the large size prop 1`] = `
>
<span
data-component="text"
id="test-button-label"
>
Smol
</span>
Expand Down Expand Up @@ -1121,7 +1115,6 @@ exports[`Button respects the small size prop 1`] = `

<button
aria-describedby="test-button-loading-announcement"
aria-labelledby="test-button-label"
class="c0"
data-loading="false"
data-no-visuals="true"
Expand All @@ -1135,7 +1128,6 @@ exports[`Button respects the small size prop 1`] = `
>
<span
data-component="text"
id="test-button-label"
>
Smol
</span>
Expand Down Expand Up @@ -1410,7 +1402,6 @@ exports[`Button styles danger button appropriately 1`] = `

<button
aria-describedby="test-button-loading-announcement"
aria-labelledby="test-button-label"
class="c0"
data-loading="false"
data-no-visuals="true"
Expand All @@ -1423,7 +1414,6 @@ exports[`Button styles danger button appropriately 1`] = `
>
<span
data-component="text"
id="test-button-label"
>
Danger
</span>
Expand Down Expand Up @@ -1707,7 +1697,6 @@ exports[`Button styles invisible button appropriately 1`] = `

<button
aria-describedby="test-button-loading-announcement"
aria-labelledby="test-button-label"
class="c0"
data-loading="false"
data-no-visuals="true"
Expand All @@ -1720,7 +1709,6 @@ exports[`Button styles invisible button appropriately 1`] = `
>
<span
data-component="text"
id="test-button-label"
>
Invisible
</span>
Expand Down Expand Up @@ -1991,7 +1979,6 @@ exports[`Button styles primary button appropriately 1`] = `

<button
aria-describedby="test-button-loading-announcement"
aria-labelledby="test-button-label"
class="c0"
data-loading="false"
data-no-visuals="true"
Expand All @@ -2004,7 +1991,6 @@ exports[`Button styles primary button appropriately 1`] = `
>
<span
data-component="text"
id="test-button-label"
>
Primary
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2681,7 +2681,6 @@ exports[`TextInput renders trailingAction text button 1`] = `
>
<button
aria-describedby=":r0:-loading-announcement"
aria-labelledby=":r0:-label"
className="c4"
data-block={null}
data-loading={false}
Expand All @@ -2696,7 +2695,6 @@ exports[`TextInput renders trailingAction text button 1`] = `
>
<span
data-component="text"
id=":r0:-label"
>
Clear
</span>
Expand Down Expand Up @@ -3249,7 +3247,6 @@ exports[`TextInput renders trailingAction text button with a tooltip 1`] = `
>
<button
aria-describedby=":r2:-loading-announcement :r1:"
aria-labelledby=":r2:-label"
className="c4"
data-block={null}
data-loading={false}
Expand All @@ -3268,7 +3265,6 @@ exports[`TextInput renders trailingAction text button with a tooltip 1`] = `
>
<span
data-component="text"
id=":r2:-label"
>
Clear
</span>
Expand Down