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

ActionMenu: Fix keyboard navigation for ActionMenu inside Overlay #2266

Merged
merged 10 commits into from
Aug 31, 2022
5 changes: 5 additions & 0 deletions .changeset/actionmenu-reenable-focus-trap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

ActionMenu: Fix keyboard navigation for ActionMenu inside Overlay by re-enabling focus trap. It was disabled in v35.6.0, that led to a regression for ActionMenu within focus zones (example: AnchoredOverlay)
1 change: 0 additions & 1 deletion src/ActionMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ const Overlay: React.FC<React.PropsWithChildren<MenuOverlayProps>> = ({children,
align={align}
overlayProps={overlayProps}
focusZoneSettings={{focusOutBehavior: 'wrap'}}
focusTrapSettings={{disabled: true}}
>
<div ref={containerRef}>
<ActionListContainerContext.Provider
Expand Down
54 changes: 53 additions & 1 deletion src/stories/ActionMenu/fixtures.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
ActionMenu,
ActionList,
Button,
IconButton
IconButton,
AnchoredOverlay
} from '../..'
import {
ServerIcon,
Expand All @@ -35,6 +36,7 @@ import {
IconProps,
IssueOpenedIcon
} from '@primer/octicons-react'
import {FocusKeys} from '@primer/behaviors'

const meta: Meta = {
title: 'Composite components/ActionMenu/fixtures',
Expand Down Expand Up @@ -788,3 +790,53 @@ export function TabTest(): JSX.Element {
</>
)
}

export function WithinFocusZone(): JSX.Element {
const [overlayOpen, setOverlayOpen] = React.useState(false)

return (
<>
<p>
When ActionMenu is used in a form inside an AnchoredOverlay, it is recommended to use key bindings for Tabs (not
ArrowKeys)
</p>
<p>
Known bug: Pressing Tab on an open menu should close the menu and put the focus on the next element instead of
the anchor.
</p>
<AnchoredOverlay
focusZoneSettings={{bindKeys: FocusKeys.Tab}}
renderAnchor={props => <Button {...props}>open overlay</Button>}
width="medium"
open={overlayOpen}
onOpen={() => setOverlayOpen(true)}
onClose={() => setOverlayOpen(false)}
>
<Box sx={{p: 4}}>
<FormControl sx={{mb: 2}}>
<FormControl.Label>First field</FormControl.Label>
<TextInput />
</FormControl>
<FormControl sx={{mb: 2}}>
<FormControl.Label>Second field</FormControl.Label>
<ActionMenu>
<ActionMenu.Button sx={{mb: 2}}>open menu</ActionMenu.Button>
<ActionMenu.Overlay>
<ActionList>
<ActionList.Item>Item 1</ActionList.Item>
<ActionList.Item>Item 2</ActionList.Item>
<ActionList.Item>Item 3</ActionList.Item>
</ActionList>
</ActionMenu.Overlay>
</ActionMenu>
</FormControl>

<FormControl>
<FormControl.Label>Third field</FormControl.Label>
<TextInput />
</FormControl>
</Box>
</AnchoredOverlay>
</>
)
}