Skip to content

Commit

Permalink
ignore "outside click" on removed elements
Browse files Browse the repository at this point in the history
Co-authored-by: Colin King <[email protected]>
  • Loading branch information
RobinMalfait and colinking committed Mar 4, 2022
1 parent 694fbd5 commit cad9d90
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
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 cad9d90

Please sign in to comment.