Skip to content

Commit 2aaa293

Browse files
authored
Ensure links are triggered inside Popover Panel components (#1153)
* ensure links are triggered inside `Popover Panel` components * update changelog
1 parent f3c70aa commit 2aaa293

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Fixed
1111

1212
- Forward the `ref` to all components ([#1116](https://github.com/tailwindlabs/headlessui/pull/1116))
13+
- Ensure links are triggered inside `Popover Panel` components ([#1153](https://github.com/tailwindlabs/headlessui/pull/1153))
1314

1415
## [Unreleased - @headlessui/vue]
1516

@@ -18,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1819
- Make sure that the input syncs when the combobox closes ([#1137](https://github.com/tailwindlabs/headlessui/pull/1137))
1920
- Ensure that you can close the combobox initially ([#1148](https://github.com/tailwindlabs/headlessui/pull/1148))
2021
- Fix Dialog usage in Tabs ([#1149](https://github.com/tailwindlabs/headlessui/pull/1149))
22+
- Ensure links are triggered inside `Popover Panel` components ([#1153](https://github.com/tailwindlabs/headlessui/pull/1153))
2123

2224
## [@headlessui/react@v1.5.0] - 2022-02-17
2325

packages/@headlessui-react/src/components/popover/popover.test.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,6 +1828,43 @@ describe('Keyboard interactions', () => {
18281828
assertActiveElement(getPopoverButton())
18291829
})
18301830
)
1831+
1832+
it(
1833+
'should close the Popover by pressing `Enter` on a Popover.Button and go to the href of the `a` inside a Popover.Panel',
1834+
suppressConsoleLogs(async () => {
1835+
render(
1836+
<Popover>
1837+
<Popover.Button>Open</Popover.Button>
1838+
<Popover.Panel>
1839+
<Popover.Button as={React.Fragment}>
1840+
<a href="#closed">Close</a>
1841+
</Popover.Button>
1842+
</Popover.Panel>
1843+
</Popover>
1844+
)
1845+
1846+
// Open the popover
1847+
await click(getPopoverButton())
1848+
1849+
let closeLink = getByText('Close')
1850+
1851+
expect(closeLink).not.toHaveAttribute('id')
1852+
expect(closeLink).not.toHaveAttribute('aria-controls')
1853+
expect(closeLink).not.toHaveAttribute('aria-expanded')
1854+
1855+
// The close button should close the popover
1856+
await press(Keys.Enter, closeLink)
1857+
1858+
// Verify it is closed
1859+
assertPopoverPanel({ state: PopoverState.InvisibleUnmounted })
1860+
1861+
// Verify we restored the Open button
1862+
assertActiveElement(getPopoverButton())
1863+
1864+
// Verify that we got redirected to the href
1865+
expect(window.location.hash).toEqual('#closed')
1866+
})
1867+
)
18311868
})
18321869
})
18331870

packages/@headlessui-react/src/components/popover/popover.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ let Button = forwardRefWithAs(function Button<TTag extends ElementType = typeof
335335
case Keys.Space:
336336
case Keys.Enter:
337337
event.preventDefault() // Prevent triggering a *click* event
338-
event.stopPropagation()
338+
// @ts-expect-error
339+
event.target.click?.()
339340
dispatch({ type: ActionTypes.ClosePopover })
340341
state.button?.focus() // Re-focus the original opening Button
341342
break

packages/@headlessui-vue/src/components/popover/popover.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,6 +1989,45 @@ describe('Keyboard interactions', () => {
19891989
assertActiveElement(getPopoverButton())
19901990
})
19911991
)
1992+
1993+
it(
1994+
'should close the Popover by pressing `Enter` on a PopoverButton and go to the href of the `a` inside a PopoverPanel',
1995+
suppressConsoleLogs(async () => {
1996+
renderTemplate(
1997+
html`
1998+
<Popover>
1999+
<PopoverButton>Open</PopoverButton>
2000+
<PopoverPanel>
2001+
<PopoverButton as="template">
2002+
<a href="#closed">Close</a>
2003+
</PopoverButton>
2004+
</PopoverPanel>
2005+
</Popover>
2006+
`
2007+
)
2008+
2009+
// Open the popover
2010+
await click(getPopoverButton())
2011+
2012+
let closeLink = getByText('Close')
2013+
2014+
expect(closeLink).not.toHaveAttribute('id')
2015+
expect(closeLink).not.toHaveAttribute('aria-controls')
2016+
expect(closeLink).not.toHaveAttribute('aria-expanded')
2017+
2018+
// The close button should close the popover
2019+
await press(Keys.Enter, closeLink)
2020+
2021+
// Verify it is closed
2022+
assertPopoverPanel({ state: PopoverState.InvisibleUnmounted })
2023+
2024+
// Verify we restored the Open button
2025+
assertActiveElement(getPopoverButton())
2026+
2027+
// Verify that we got redirected to the href
2028+
expect(window.location.hash).toEqual('#closed')
2029+
})
2030+
)
19922031
})
19932032
})
19942033

packages/@headlessui-vue/src/components/popover/popover.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ export let PopoverButton = defineComponent({
250250
case Keys.Space:
251251
case Keys.Enter:
252252
event.preventDefault() // Prevent triggering a *click* event
253-
event.stopPropagation()
253+
// @ts-expect-error
254+
event.target.click?.()
254255
api.closePopover()
255256
dom(api.button)?.focus() // Re-focus the original opening Button
256257
break

0 commit comments

Comments
 (0)