Skip to content

Commit

Permalink
fix: allow focus to move from overlay on mousedown (#1244)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgreif committed May 21, 2021
1 parent ff177c8 commit fb2fa6c
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-boxes-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/components": patch
---

Allow focus to move to an input outside an overlay on mousedown
2 changes: 1 addition & 1 deletion src/__tests__/ActionMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe('ActionMenu', () => {
expect(portalRoot).toBeTruthy()
const somethingElse = (await menu.baseElement.querySelector('#something-else')) as HTMLElement
act(() => {
fireEvent.click(somethingElse)
fireEvent.mouseDown(somethingElse)
})
expect(portalRoot?.textContent).toEqual('') // menu items are hidden
})
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/AnchoredOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('AnchoredOverlay', () => {
onCloseCallback={mockCloseCallback}
/>
)
fireEvent.click(anchoredOverlay.baseElement)
fireEvent.mouseDown(anchoredOverlay.baseElement)

expect(mockOpenCallback).toHaveBeenCalledTimes(0)
expect(mockCloseCallback).toHaveBeenCalledTimes(1)
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/DropdownMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe('DropdownMenu', () => {
expect(portalRoot).toBeTruthy()
const somethingElse = (await menu.baseElement.querySelector('#something-else')) as HTMLElement
act(() => {
fireEvent.click(somethingElse)
fireEvent.mouseDown(somethingElse)
})
// portal is closed after click
expect(portalRoot?.textContent).toEqual('') // menu items are hidden
Expand Down
7 changes: 4 additions & 3 deletions src/behaviors/focusTrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,12 @@ export function focusTrap(

// Prevent focus leaving the trap container
document.addEventListener(
'focusin',
(event: FocusEvent) => {
'focus',
event => {
ensureTrapZoneHasFocus(event.target)
},
{signal: wrappingController.signal}
// use capture to ensure we get all events. focus events do not bubble
{signal: wrappingController.signal, capture: true}
)

// focus the first element
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/useOnOutsideClick.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ export const useOnOutsideClick = ({containerRef, ignoreClickRefs, onClickOutside
)

useEffect(() => {
document.addEventListener('click', onOutsideClickInternal)
// use capture to ensure we get all events
document.addEventListener('mousedown', onOutsideClickInternal, {capture: true})
return () => {
document.removeEventListener('click', onOutsideClickInternal)
document.removeEventListener('mousedown', onOutsideClickInternal, {capture: true})
}
}, [onOutsideClickInternal])
}
2 changes: 2 additions & 0 deletions src/stories/DropdownMenu.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {theme, ThemeProvider} from '..'
import {ItemInput} from '../ActionList/List'
import BaseStyles from '../BaseStyles'
import {DropdownMenu, DropdownButton} from '../DropdownMenu'
import TextInput from '../TextInput'

const meta: Meta = {
title: 'Composite components/DropdownMenu',
Expand Down Expand Up @@ -34,6 +35,7 @@ export function FavoriteColorStory(): JSX.Element {
return (
<>
<h1>Favorite Color</h1>
<TextInput placeholder="Input for focus testing" mb={2} />
<div id="favorite-color-label">Please select your favorite color:</div>
<DropdownMenu
renderAnchor={({children, 'aria-labelledby': ariaLabelledBy, ...anchorProps}) => (
Expand Down

1 comment on commit fb2fa6c

@vercel
Copy link

@vercel vercel bot commented on fb2fa6c May 21, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.