Skip to content

Commit

Permalink
simplify outside click behaviour
Browse files Browse the repository at this point in the history
Here is a little story. We used to use the `click` event listener on the
window to try and detect whether we clicked outside of the main area we
are working in.

This all worked fine, until we got a bug report that it didn't work
properly on Mobile, especially iOS. After a bit of debugging we switched
this behaviour to use `pointerdown` instead of the `click` event
listener. Worked great! Maybe...

The reason the `click` didn't work was because of another bug fix. In
React if you render a `<form><Dialog></form>` and your `Dialog` contains
a button without a type, (or an input where you press enter) then the
form would submit... even though we portalled the `Dialog` to a
different location, but it bubbled the event up via the SyntethicEvent
System. To fix this, we've added a "simple" `onClick(e) { e.stopPropagation() }`
to make sure that click events didn't leak out.

Alright no worries, but, now that we switched to `pointerdown` we got
another bug report that it didn't work on older iOS devices. Fine, let's
add a `mousedown` next to the `pointerdown` event. Now this works all
great! Maybe...

This doesn't work quite as we expected because it could happen that both
events fire and then the `onClose` of the Dialog component would fire
twice. In fact, there is an open issue about this: #1490 at the time of
writing this commit message.
We tried to only call the close function once by checking if those
events happen within the same "tick", which is not always the case...

Alright, let's ignore that issue for a second, there is another issue
that popped up... If you have a Dialog that is scrollable (because it is
greater than the current viewport) then a wild scrollbar appears (what a
weird Pokémon). The moment you try to click the scrollbar or drag it the
Dialog closes. What in the world...?

Well... turns out that `pointerdown` gets fired if you happen to "click"
(or touch) on the scrollbar. A click event does not get fired. No
worries we can fix this! Maybe...

(Narrator: ... nope ...)

One thing we can try is to measure the scrollbar width, and if you
happen to click near the edge then we ignore this click. You can think
of it like `let safeArea = viewportWidth - scrollBarWidth`. Everything
works great now! Maybe...

Well, let me tell you about macOS and "floating" scrollbars... you can't
measure those... AAAAAAAARGHHHH

Alright, scratch that, let's add an invisible 20px gap all around the
viewport without measuring as a safe area. Nobody will click in the 20px
gap, right, right?! Everything works great now! Maybe...

Mobile devices, yep, Dialogs are used there as well and usually there is
not a lot of room around those Dialogs so you almost always hit the
"safe area". Should we now try and detect the device people are
using...?

/me takes a deep breath...

Inhales... Exhales...

Alright, time to start thinking again... The outside click with a
"simple" click worked on Menu and Listbox not on the Dialog so this
should be enough right?

WAIT A MINUTE

Remember this piece of code from earlier:

```js
onClick(event) {
  event.stopPropagation()
}
```

The click event never ever reaches the `window` so we can't detect the
click outside...

Let's move that code to the `Dialog.Panel` instead of on the `Dialog`
itself, this will make sure that we stop the click event from leaking
if you happen to nest a Dialog in a form and have a submitable
button/input in the `Dialog.Panel`. But if you click outside of the
`Dialog.Panel` the "click" event will bubble to the `window` so that we
can detect a click and check whether it was outside or not.

Time to start cleaning:
  - ☑️ Remove all the scrollbar measuring code...
    - Closing works on mobile now, no more safe area hack
  - ☑️ Remove the pointerdown & mousedown event
    - Outside click doesn't fire twice anymore
  - ☑️ Use a "simple" click event listener
    - We can click the scrollbar and the browser ignores it for us

All issues have been fixed! (Until the next one of course...)
  • Loading branch information
RobinMalfait committed Jun 3, 2022
1 parent d383e67 commit 136c028
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 76 deletions.
57 changes: 19 additions & 38 deletions packages/@headlessui-react/src/hooks/use-outside-click.ts
@@ -1,29 +1,32 @@
import { MutableRefObject, useRef } from 'react'
import { microTask } from '../utils/micro-task'
import { useEvent } from './use-event'
import { MutableRefObject, useEffect, useRef } from 'react'
import { useWindowEvent } from './use-window-event'

type Container = MutableRefObject<HTMLElement | null> | HTMLElement | null
type ContainerCollection = Container[] | Set<Container>
type ContainerInput = Container | ContainerCollection

export enum Features {
None = 1 << 0,
IgnoreScrollbars = 1 << 1,
}

export function useOutsideClick(
containers: ContainerInput | (() => ContainerInput),
cb: (event: MouseEvent | PointerEvent, target: HTMLElement) => void,
features: Features = Features.None
enabled: boolean = true
) {
let called = useRef(false)
let handler = useEvent((event: MouseEvent | PointerEvent) => {
if (called.current) return
called.current = true
microTask(() => {
called.current = false
})
// TODO: remove this once the React bug has been fixed: https://github.com/facebook/react/issues/24657
let enabledRef = useRef(false)
useEffect(
process.env.NODE_ENV === 'test'
? () => {
enabledRef.current = enabled
}
: () => {
requestAnimationFrame(() => {
enabledRef.current = enabled
})
},
[enabled]
)

useWindowEvent('click', (event) => {
if (!enabledRef.current) return

let _containers = (function resolve(containers): ContainerCollection {
if (typeof containers === 'function') {
Expand All @@ -46,25 +49,6 @@ export function useOutsideClick(
// Ignore if the target doesn't exist in the DOM anymore
if (!target.ownerDocument.documentElement.contains(target)) return

// Ignore scrollbars:
// This is a bit hacky, and is only necessary because we are checking for `pointerdown` and
// `mousedown` events. They _are_ being called if you click on a scrollbar. The `click` event
// is not called when clicking on a scrollbar, but we can't use that otherwise it won't work
// on mobile devices where only pointer events are being used.
if ((features & Features.IgnoreScrollbars) === Features.IgnoreScrollbars) {
// TODO: We can calculate this dynamically~is. On macOS if you have the "Automatically based
// on mouse or trackpad" setting enabled, then the scrollbar will float on top and therefore
// you can't calculate its with by checking the clientWidth and scrollWidth of the element.
// Therefore we are currently hardcoding this to be 20px.
let scrollbarWidth = 20

let viewport = target.ownerDocument.documentElement
if (event.clientX > viewport.clientWidth - scrollbarWidth) return
if (event.clientX < scrollbarWidth) return
if (event.clientY > viewport.clientHeight - scrollbarWidth) return
if (event.clientY < scrollbarWidth) return
}

// Ignore if the target exists in one of the containers
for (let container of _containers) {
if (container === null) continue
Expand All @@ -76,7 +60,4 @@ export function useOutsideClick(

return cb(event, target)
})

useWindowEvent('pointerdown', handler)
useWindowEvent('mousedown', handler)
}
41 changes: 3 additions & 38 deletions packages/@headlessui-vue/src/hooks/use-outside-click.ts
Expand Up @@ -7,24 +7,11 @@ type Container = Ref<HTMLElement | null> | HTMLElement | null
type ContainerCollection = Container[] | Set<Container>
type ContainerInput = Container | ContainerCollection

export enum Features {
None = 1 << 0,
IgnoreScrollbars = 1 << 1,
}

export function useOutsideClick(
containers: ContainerInput | (() => ContainerInput),
cb: (event: MouseEvent | PointerEvent, target: HTMLElement) => void,
features: Features = Features.None
cb: (event: MouseEvent | PointerEvent, target: HTMLElement) => void
) {
let called = false
function handle(event: MouseEvent | PointerEvent) {
if (called) return
called = true
microTask(() => {
called = false
})

useWindowEvent('click', (event) => {
let target = event.target as HTMLElement

// Ignore if the target doesn't exist in the DOM anymore
Expand All @@ -46,25 +33,6 @@ export function useOutsideClick(
return [containers]
})(containers)

// Ignore scrollbars:
// This is a bit hacky, and is only necessary because we are checking for `pointerdown` and
// `mousedown` events. They _are_ being called if you click on a scrollbar. The `click` event
// is not called when clicking on a scrollbar, but we can't use that otherwise it won't work
// on mobile devices where only pointer events are being used.
if ((features & Features.IgnoreScrollbars) === Features.IgnoreScrollbars) {
// TODO: We can calculate this dynamically~is. On macOS if you have the "Automatically based
// on mouse or trackpad" setting enabled, then the scrollbar will float on top and therefore
// you can't calculate its with by checking the clientWidth and scrollWidth of the element.
// Therefore we are currently hardcoding this to be 20px.
let scrollbarWidth = 20

let viewport = target.ownerDocument.documentElement
if (event.clientX > viewport.clientWidth - scrollbarWidth) return
if (event.clientX < scrollbarWidth) return
if (event.clientY > viewport.clientHeight - scrollbarWidth) return
if (event.clientY < scrollbarWidth) return
}

// Ignore if the target exists in one of the containers
for (let container of _containers) {
if (container === null) continue
Expand All @@ -75,8 +43,5 @@ export function useOutsideClick(
}

cb(event, target)
}

useWindowEvent('pointerdown', handle)
useWindowEvent('mousedown', handle)
})
}

0 comments on commit 136c028

Please sign in to comment.