Skip to content

ActionList: Add role="option" if role="listbox" is present #6049

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

Merged
merged 4 commits into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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/soft-webs-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

ActionList: Ensure `role="option"` is added when `role="listbox"` is used; allow disabled items to remain focusable
32 changes: 30 additions & 2 deletions packages/react/src/ActionList/ActionList.features.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,35 @@ export const MultiSelect = () => {
)
}

export const ListBoxMultiSelect = () => {
export const ListboxSingleSelect = () => {
const [selectedIndice, setSelectedIndice] = React.useState<number>(0)
const handleSelect = (index: number) => {
setSelectedIndice(index)
}

return (
<ActionList selectionVariant="single" role="listbox" aria-label="Projects">
{projects.map((project, index) => (
<ActionList.Item
key={index}
selected={selectedIndice === index}
aria-checked={selectedIndice === index}
onSelect={() => handleSelect(index)}
disabled={index === 3 ? true : undefined}
role="option"
Copy link
Preview

Copilot AI May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Since role="option" is now inferred when ActionList has role="listbox", this explicit prop in the story is redundant and could be removed for clarity.

Suggested change
role="option"

Copilot uses AI. Check for mistakes.

>
<ActionList.LeadingVisual>
<TableIcon />
</ActionList.LeadingVisual>
{project.name}
<ActionList.Description variant="block">{project.scope}</ActionList.Description>
</ActionList.Item>
))}
</ActionList>
)
}

export const ListboxMultiSelect = () => {
const [selectedIndices, setSelectedIndices] = React.useState<number[]>([0])
const handleSelect = (index: number) => {
if (selectedIndices.includes(index)) {
Expand All @@ -339,7 +367,7 @@ export const ListBoxMultiSelect = () => {
}
}
return (
<ActionList role="menu" selectionVariant="multiple" aria-label="Project">
<ActionList role="menu" selectionVariant="multiple" aria-label="Projects">
{projects.map((project, index) => (
<ActionList.Item
key={index}
Expand Down
4 changes: 3 additions & 1 deletion packages/react/src/ActionList/ActionList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ describe('ActionList', () => {
expect(document.activeElement).toHaveTextContent('Option 2')

await userEvent.keyboard('{ArrowDown}')
expect(document.activeElement).not.toHaveTextContent('Option 3') // option 3 is disabled
expect(document.activeElement).toHaveTextContent('Option 3')

await userEvent.keyboard('{ArrowDown}')
expect(document.activeElement).toHaveTextContent('Option 4')

await userEvent.keyboard('{ArrowDown}')
Expand Down
17 changes: 16 additions & 1 deletion packages/react/src/ActionList/Item.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ function SingleSelectListStory(): JSX.Element {
{projects.map((project, index) => (
<ActionList.Item
key={index}
role="option"
selected={index === selectedIndex}
onSelect={() => setSelectedIndex(index)}
disabled={project.disabled}
Expand Down Expand Up @@ -193,6 +192,7 @@ describe('ActionList.Item', () => {
await userEvent.tab() // get focus on first element
await userEvent.keyboard('{ArrowDown}')
await userEvent.keyboard('{ArrowDown}')
await userEvent.keyboard('{ArrowDown}')
expect(inactiveOption).toHaveFocus()
expect(document.activeElement).toHaveAccessibleDescription(projects[3].inactiveText as string)
})
Expand Down Expand Up @@ -220,6 +220,7 @@ describe('ActionList.Item', () => {
await userEvent.keyboard('{ArrowDown}')
await userEvent.keyboard('{ArrowDown}')
await userEvent.keyboard('{ArrowDown}')
await userEvent.keyboard('{ArrowDown}')
expect(inactiveOption).toHaveFocus()
expect(document.activeElement).toHaveAccessibleDescription(projects[5].inactiveText as string)
})
Expand Down Expand Up @@ -420,4 +421,18 @@ describe('ActionList.Item', () => {
expect(button.parentElement?.tagName).toBe('LI')
expect(button.textContent).toBe('Item 5')
})

it('should add `role="option"` if `role="listbox"` and `selectionVariant` is present', async () => {
const {getAllByRole} = HTMLRender(
<ActionList role="listbox" selectionVariant="single">
<ActionList.Item>Item 1</ActionList.Item>
<ActionList.Item>Item 2</ActionList.Item>
<ActionList.Item>Item 3</ActionList.Item>
<ActionList.Item>Item 4</ActionList.Item>
</ActionList>,
)
const options = getAllByRole('option')
expect(options[0]).toBeInTheDocument()
expect(options).toHaveLength(4)
})
})
7 changes: 3 additions & 4 deletions packages/react/src/ActionList/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ export const Item = React.forwardRef<HTMLLIElement, ActionListItemProps>(
if (selectionVariant === 'single') inferredItemRole = 'menuitemradio'
else if (selectionVariant === 'multiple') inferredItemRole = 'menuitemcheckbox'
else inferredItemRole = 'menuitem'
} else if (container === 'SelectPanel' && listRole === 'listbox') {
if (selectionVariant !== undefined) inferredItemRole = 'option'
} else if ((container === 'SelectPanel' && listRole === 'listbox') || listRole === 'listbox') {
Copy link
Preview

Copilot AI May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conditional can be simplified to if (listRole === 'listbox'), since the extra SelectPanel check is redundant.

Suggested change
} else if ((container === 'SelectPanel' && listRole === 'listbox') || listRole === 'listbox') {
} else if (listRole === 'listbox') {

Copilot uses AI. Check for mistakes.

if (selectionVariant !== undefined && !role) inferredItemRole = 'option'
}

const itemRole = role || inferredItemRole
Expand Down Expand Up @@ -325,8 +325,7 @@ export const Item = React.forwardRef<HTMLLIElement, ActionListItemProps>(

let focusable

// if item is disabled and is of type (menuitem*, option) it should remain focusable, if inactive, apply the same rules
if ((disabled && !inferredItemRole) || showInactiveIndicator) {
if (showInactiveIndicator) {
focusable = true
}

Expand Down
Loading