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/curvy-goats-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Include current selected menu item in accessible name when using an `aria-label` in `SegmentedControl`
15 changes: 15 additions & 0 deletions packages/react/src/SegmentedControl/SegmentedControl.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,21 @@ describe('SegmentedControl', () => {
expect(spy).toHaveBeenCalledTimes(2)
spy.mockRestore()
})

it('should include the visual text if the user specifies an aria-label', () => {
const {getByLabelText} = render(
<SegmentedControl aria-label="File view" variant={{narrow: 'dropdown'}}>
{segmentData.map(({label}, index) => (
<SegmentedControl.Button selected={index === 2} key={label}>
{label}
</SegmentedControl.Button>
))}
</SegmentedControl>,
)

const menuButton = getByLabelText('Blame, File view')
expect(menuButton).toBeInTheDocument()
})
})

// TODO: uncomment these tests after we fix a11y for the Tooltip component
Expand Down
5 changes: 4 additions & 1 deletion packages/react/src/SegmentedControl/SegmentedControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ const Root: React.FC<React.PropsWithChildren<SegmentedControlProps>> = ({
The aria-label is only provided as a backup when the designer or engineer neglects to show a label for the SegmentedControl.
The best thing to do is to have a visual label who's id is referenced using the `aria-labelledby` prop.
*/}
<ActionMenu.Button aria-label={ariaLabel} leadingVisual={getChildIcon(selectedChild)}>
<ActionMenu.Button
aria-label={ariaLabel && `${getChildText(selectedChild)}, ${ariaLabel}`}
leadingVisual={getChildIcon(selectedChild)}
>
{getChildText(selectedChild)}
</ActionMenu.Button>
<ActionMenu.Overlay aria-labelledby={ariaLabelledby}>
Expand Down