Skip to content

Commit

Permalink
Ignore "outside click" on removed elements (#1193)
Browse files Browse the repository at this point in the history
* ignore "outside click" on removed elements

Co-authored-by: Colin King <me@colinking.co>

* update changelog

Co-authored-by: Colin King <me@colinking.co>
  • Loading branch information
RobinMalfait and colinking committed Mar 4, 2022
1 parent 694fbd5 commit 2414bbd
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
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

0 comments on commit 2414bbd

Please sign in to comment.