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

fix: adds the MouseEventHandler type to close in Popover #1696

Merged
merged 4 commits into from
Aug 2, 2022
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
4 changes: 4 additions & 0 deletions packages/@headlessui-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Ensure controlled `Tabs` don't change automagically ([#1680](https://github.com/tailwindlabs/headlessui/pull/1680))
- Don't scroll lock when a Transition + Dialog is mounted but hidden ([#1681](https://github.com/tailwindlabs/headlessui/pull/1681))

## Changed

- Allow `Popover` `close` to be passed directly to `onClick` handlers ([#1696](https://github.com/tailwindlabs/headlessui/pull/1696))

## [1.6.6] - 2022-07-07

### Fixed
Expand Down
42 changes: 42 additions & 0 deletions packages/@headlessui-react/src/components/popover/popover.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,48 @@ describe('Rendering', () => {
})
)

it(
'should expose a close function that closes the popover and takes an event',
suppressConsoleLogs(async () => {
function Example() {
return (
<>
<Popover>
{({ close }) => (
<>
<Popover.Button>Trigger</Popover.Button>
<Popover.Panel>
<button onClick={close}>Close me</button>
</Popover.Panel>
</>
)}
</Popover>
</>
)
}

render(<Example />)

// Focus the button
await focus(getPopoverButton())

// Ensure the button is focused
assertActiveElement(getPopoverButton())

// Open the popover
await click(getPopoverButton())

// Ensure we can click the close button
await click(getByText('Close me'))

// Ensure the popover is closed
assertPopoverPanel({ state: PopoverState.InvisibleUnmounted })

// Ensure the Popover.Button got the restored focus
assertActiveElement(getByText('Trigger'))
})
)

describe('refs', () => {
it(
'should be possible to get a ref to the Popover',
Expand Down
39 changes: 27 additions & 12 deletions packages/@headlessui-react/src/components/popover/popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import React, {
MouseEvent as ReactMouseEvent,
MutableRefObject,
Ref,
MouseEventHandler,
} from 'react'

import { Props } from '../../types'
Expand Down Expand Up @@ -44,6 +45,8 @@ import { useEvent } from '../../hooks/use-event'
import { useTabDirection, Direction as TabDirection } from '../../hooks/use-tab-direction'
import { microTask } from '../../utils/micro-task'

type MouseEvent<T> = Parameters<MouseEventHandler<T>>[0]

enum PopoverStates {
Open,
Closed,
Expand Down Expand Up @@ -128,7 +131,9 @@ function usePopoverContext(component: string) {
}

let PopoverAPIContext = createContext<{
close(focusableElement?: HTMLElement | MutableRefObject<HTMLElement | null>): void
close(
focusableElement?: HTMLElement | MutableRefObject<HTMLElement | null> | MouseEvent<HTMLElement>
): void
isPortalled: boolean
} | null>(null)
PopoverAPIContext.displayName = 'PopoverAPIContext'
Expand Down Expand Up @@ -176,7 +181,9 @@ function stateReducer(state: StateDefinition, action: Actions) {
let DEFAULT_POPOVER_TAG = 'div' as const
interface PopoverRenderPropArg {
open: boolean
close(focusableElement?: HTMLElement | MutableRefObject<HTMLElement | null>): void
close(
focusableElement?: HTMLElement | MutableRefObject<HTMLElement | null> | MouseEvent<HTMLElement>
): void
}

let PopoverRoot = forwardRefWithAs(function Popover<
Expand Down Expand Up @@ -271,19 +278,27 @@ let PopoverRoot = forwardRefWithAs(function Popover<
popoverState === PopoverStates.Open
)

let close = useEvent((focusableElement?: HTMLElement | MutableRefObject<HTMLElement | null>) => {
dispatch({ type: ActionTypes.ClosePopover })
let close = useEvent(
(
focusableElement?:
| HTMLElement
| MutableRefObject<HTMLElement | null>
| MouseEvent<HTMLElement>
) => {
dispatch({ type: ActionTypes.ClosePopover })

let restoreElement = (() => {
if (!focusableElement) return button
if (focusableElement instanceof HTMLElement) return focusableElement
if (focusableElement.current instanceof HTMLElement) return focusableElement.current
let restoreElement = (() => {
if (!focusableElement) return button
if (focusableElement instanceof HTMLElement) return focusableElement
if ('current' in focusableElement && focusableElement.current instanceof HTMLElement)
return focusableElement.current

return button
})()
return button
})()

restoreElement?.focus()
})
restoreElement?.focus()
}
)

let api = useMemo<ContextType<typeof PopoverAPIContext>>(
() => ({ close, isPortalled }),
Expand Down