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

Ignore "outside click" on removed elements #1193

Merged
merged 2 commits into from
Mar 4, 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Reset Combobox Input when the value gets reset ([#1181](https://github.com/tailwindlabs/headlessui/pull/1181))
- Fix double `beforeEnter` due to SSR ([#1183](https://github.com/tailwindlabs/headlessui/pull/1183))
- Adjust active {item,option} index ([#1184](https://github.com/tailwindlabs/headlessui/pull/1184))
- Only activate the `Tab` on mouseup ([#1192](https://github.com/tailwindlabs/headlessui/pull/1192))
- Ignore "outside click" on removed elements ([#1193](https://github.com/tailwindlabs/headlessui/pull/1193))

## [Unreleased - @headlessui/vue]

Expand All @@ -36,6 +38,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Adjust active {item,option} index ([#1184](https://github.com/tailwindlabs/headlessui/pull/1184))
- Fix re-focusing element after close ([#1186](https://github.com/tailwindlabs/headlessui/pull/1186))
- Fix `Dialog` cycling ([#553](https://github.com/tailwindlabs/headlessui/pull/553))
- Only activate the `Tab` on mouseup ([#1192](https://github.com/tailwindlabs/headlessui/pull/1192))
- Ignore "outside click" on removed elements ([#1193](https://github.com/tailwindlabs/headlessui/pull/1193))

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

Expand Down
37 changes: 37 additions & 0 deletions packages/@headlessui-react/src/components/dialog/dialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,43 @@ describe('Mouse interactions', () => {
expect(wrapperFn).toHaveBeenCalledTimes(0)
})
)

it(
'should should be possible to click on removed elements without closing the Dialog',
suppressConsoleLogs(async () => {
function Example() {
let [isOpen, setIsOpen] = useState(true)
let wrapper = useRef<HTMLDivElement | null>(null)

return (
<Dialog open={isOpen} onClose={setIsOpen}>
<div ref={wrapper}>
Contents
<button
onMouseDown={() => {
// Remove this button before the Dialog's mousedown listener fires:
wrapper.current?.remove()
}}
>
Inside
</button>
<TabSentinel />
</div>
</Dialog>
)
}
render(<Example />)

// Verify it is open
assertDialog({ state: DialogState.Visible })

// Click the button inside the the Dialog
await click(getByText('Inside'))

// Verify it is still open
assertDialog({ state: DialogState.Visible })
})
)
})

describe('Nesting', () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/@headlessui-react/src/hooks/use-outside-click.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export function useOutsideClick(

let target = event.target as HTMLElement

// Ignore if the target doesn't exist in the DOM anymore
if (!target.ownerDocument.documentElement.contains(target)) return

// Ignore if the target exists in one of the containers
for (let container of _containers) {
if (container === null) continue
let domNode = container instanceof HTMLElement ? container : container.current
Expand Down
38 changes: 38 additions & 0 deletions packages/@headlessui-vue/src/components/dialog/dialog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,44 @@ describe('Mouse interactions', () => {
expect(wrapperFn).toHaveBeenCalledTimes(0)
})
)

it(
'should should be possible to click on removed elements without closing the Dialog',
suppressConsoleLogs(async () => {
renderTemplate({
template: `
<Dialog :open="isOpen" @close="setIsOpen">
<div ref="wrapper">
Contents
<!-- Remove this button before the Dialog's mousedown listener fires: -->
<button @mousedown="wrapper.remove()">Inside</button>
<TabSentinel />
</div>
</Dialog>
`,
setup() {
let isOpen = ref(true)
let wrapper = ref<HTMLDivElement | null>(null)
return {
isOpen,
wrapper,
setIsOpen(value: boolean) {
isOpen.value = value
},
}
},
})

// Verify it is open
assertDialog({ state: DialogState.Visible })

// Click the button inside the the Dialog
await click(getByText('Inside'))

// Verify it is still open
assertDialog({ state: DialogState.Visible })
})
)
})

describe('Nesting', () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/@headlessui-vue/src/hooks/use-outside-click.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export function useOutsideClick(
})

let target = event.target as HTMLElement

// Ignore if the target doesn't exist in the DOM anymore
if (!target.ownerDocument.documentElement.contains(target)) return

let _containers = (() => {
if (Array.isArray(containers)) {
return containers
Expand All @@ -46,6 +50,7 @@ export function useOutsideClick(
return [containers]
})()

// Ignore if the target exists in one of the containers
for (let container of _containers) {
if (container === null) continue
let domNode = container instanceof HTMLElement ? container : dom(container)
Expand Down