Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/hot-melons-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

Banner: Should prefer aria-labelled-by over aria-label
19 changes: 19 additions & 0 deletions packages/react/src/Banner/Banner.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ describe('Banner', () => {
expect(screen.getByRole('region')).toHaveAttribute('aria-label', 'Test')
})

it('should prefer aria-labelledby over aria-label and not set both', () => {
render(
<Banner aria-label="Override" aria-labelledby="my-banner-title">
<Banner.Title id="my-banner-title">Explicit Banner Title</Banner.Title>
</Banner>,
)

const region = screen.getByRole('region', {name: 'Explicit Banner Title'})
expect(region).toHaveAttribute('aria-labelledby', 'my-banner-title')
expect(region).not.toHaveAttribute('aria-label')
})

it('should only set aria-label when aria-labelledby is not provided', () => {
render(<Banner aria-label="Custom Label" title="test" />)
const region = screen.getByRole('region')
expect(region).toHaveAttribute('aria-label', 'Custom Label')
expect(region).not.toHaveAttribute('aria-labelledby')
})

it('should default the title to a h2', () => {
render(<Banner title="test" />)
expect(screen.getByRole('heading', {level: 2})).toBeInTheDocument()
Expand Down
4 changes: 3 additions & 1 deletion packages/react/src/Banner/Banner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const labels: Record<BannerVariant, string> = {
export const Banner = React.forwardRef<HTMLElement, BannerProps>(function Banner(
{
'aria-label': label,
'aria-labelledby': labelledBy,
children,
className,
description,
Expand Down Expand Up @@ -131,7 +132,8 @@ export const Banner = React.forwardRef<HTMLElement, BannerProps>(function Banner
return (
<section
{...rest}
aria-label={label ?? labels[variant]}
aria-labelledby={labelledBy}
aria-label={labelledBy ? undefined : (label ?? labels[variant])}
className={clsx(className, classes.Banner)}
data-dismissible={onDismiss ? '' : undefined}
data-title-hidden={hideTitle ? '' : undefined}
Expand Down
Loading