Skip to content
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

Button: only add fallback aria-labelledby if aria-label isn't present #4459

Closed
wants to merge 2 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
11 changes: 8 additions & 3 deletions packages/react/src/Button/ButtonBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,14 @@ const ButtonBase = forwardRef(
const sxStyles = useMemo(() => {
return merge<BetterSystemStyleObject>(baseStyles, sxProp)
}, [baseStyles, sxProp])

const uuid = useId(id)
const loadingAnnouncementID = `${uuid}-loading-announcement`
const buttonLabelID = ariaLabelledBy || `${uuid}-label`

// generate a fallback aria-labelledby to point to text content,
// but only if the instance does not have an aria-label
const textContentId = `${uuid}-label`
const fallbackAriaLabelledById = props['aria-label'] ? undefined : textContentId

if (__DEV__) {
/**
Expand Down Expand Up @@ -101,7 +106,7 @@ const ButtonBase = forwardRef(
.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={ariaLabelledBy || fallbackAriaLabelledById}
id={id}
onClick={loading ? undefined : onClick}
>
Expand All @@ -117,7 +122,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={textContentId}>
{children}
{count !== undefined && !TrailingVisual && (
<CounterLabel data-component="ButtonCounter" sx={{ml: 2}}>
Expand Down
23 changes: 23 additions & 0 deletions packages/react/src/Button/__tests__/Button.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,27 @@ describe('Button', () => {

expect(container.getByRole('button')).toHaveAccessibleName('content')
})

it('should add be labelled by only text content', () => {
const container = render(<Button trailingVisual={() => 'hello'}>content</Button>)

expect(container.getByRole('button')).toHaveAccessibleName('content')
})

it('should prefer aria-label over text content', () => {
const container = render(<Button aria-label="Custom label">content</Button>)

expect(container.getByRole('button')).toHaveAccessibleName('Custom label')
})

it('should prefer aria-labelledby over text content', () => {
const container = render(
<>
<span id="label">different label</span>
<Button aria-labelledby="label">content</Button>
</>,
)

expect(container.getByRole('button')).toHaveAccessibleName('different label')
})
})
Loading