Skip to content

ActionList: Add new prop disableFocusZone #6116

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 2 commits into from
Jun 12, 2025
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/clever-pans-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': minor
---

ActionList: Add new prop `disableFocusZone` to disable the default focus zone provided
25 changes: 25 additions & 0 deletions packages/react/src/ActionList/ActionList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,29 @@ describe('ActionList', () => {
expect(container.querySelector('.leading')).toBeInTheDocument()
expect(container.querySelector('.description')).toBeInTheDocument()
})

it('should not be navigatable with arrow keys if `disableFocusZone` is true', async () => {
const {container} = HTMLRender(
<ActionList role="listbox" aria-label="Select a project" disableFocusZone={true}>
<ActionList.Item role="option">Option 1</ActionList.Item>
<ActionList.Item role="option">Option 2</ActionList.Item>
<ActionList.Item role="option" disabled>
Option 3
</ActionList.Item>
<ActionList.Item role="option">Option 4</ActionList.Item>
<ActionList.Item role="option" inactiveText="Unavailable due to an outage">
Option 5
</ActionList.Item>
</ActionList>,
)

await userEvent.tab() // tab into the story, this should focus on the first button
expect(document.activeElement).toHaveTextContent('Option 1')

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

expect(container.querySelector('li[aria-disabled="true"]')?.nextElementSibling).toHaveTextContent('Option 4')
expect(container.querySelector('li[aria-disabled="true"]')?.nextElementSibling).toHaveAttribute('tabindex', '0')
})
})
13 changes: 11 additions & 2 deletions packages/react/src/ActionList/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ import {BoxWithFallback} from '../internal/components/BoxWithFallback'

export const List = React.forwardRef<HTMLUListElement, ActionListProps>(
(
{variant = 'inset', selectionVariant, showDividers = false, role, sx: sxProp = defaultSxProp, className, ...props},
{
variant = 'inset',
selectionVariant,
showDividers = false,
role,
sx: sxProp = defaultSxProp,
disableFocusZone = false,
Copy link
Member Author

Choose a reason for hiding this comment

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

We could instead add a prop named focusZoneSettings, which allows consumers to customize the focus zone. This would work much like how it does in AnchoredOverlay.

This might not be needed now, but gives consumers more flexibility with the component, and removes the need for the disableFocusZone boolean prop.

className,
...props
},
forwardedRef,
): JSX.Element => {
const [slots, childrenWithoutSlots] = useSlots(props.children, {
Expand All @@ -37,7 +46,7 @@ export const List = React.forwardRef<HTMLUListElement, ActionListProps>(

let enableFocusZone = false
if (enableFocusZoneFromContainer !== undefined) enableFocusZone = enableFocusZoneFromContainer
else if (listRole) enableFocusZone = ['menu', 'menubar', 'listbox'].includes(listRole)
else if (listRole && !disableFocusZone) enableFocusZone = ['menu', 'menubar', 'listbox'].includes(listRole)

useFocusZone({
disabled: !enableFocusZone,
Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/ActionList/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ export type ActionListProps = React.PropsWithChildren<{
* The ARIA role describing the function of `List` component. `listbox` or `menu` are a common values.
*/
role?: AriaRole
/**
* Disables the focus zone for the list if applicable. Focus zone is enabled by default for `menu` and `listbox` roles, or components such as `ActionMenu` and `SelectPanel`.
*/
disableFocusZone?: boolean
className?: string
}> &
SxProp
Expand Down
Loading