Skip to content

Commit da179ca

Browse files
authored
trigger "outside click" behaviour on mousedown (#212)
Fixes: #95
1 parent 80402e7 commit da179ca

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ export function Listbox<
186186
if (!event.defaultPrevented) buttonRef.current?.focus({ preventScroll: true })
187187
}
188188

189-
window.addEventListener('click', handler)
190-
return () => window.removeEventListener('click', handler)
189+
window.addEventListener('mousedown', handler)
190+
return () => window.removeEventListener('mousedown', handler)
191191
}, [listboxState, optionsRef, buttonRef, d, dispatch])
192192

193193
const propsBag = React.useMemo<ListboxRenderPropArg>(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ export function Menu<TTag extends React.ElementType = typeof DEFAULT_MENU_TAG>(
162162
if (!event.defaultPrevented) buttonRef.current?.focus({ preventScroll: true })
163163
}
164164

165-
window.addEventListener('click', handler)
166-
return () => window.removeEventListener('click', handler)
165+
window.addEventListener('mousedown', handler)
166+
return () => window.removeEventListener('mousedown', handler)
167167
}, [menuState, itemsRef, buttonRef, dispatch])
168168

169169
const propsBag = React.useMemo(() => ({ open: menuState === MenuStates.Open }), [menuState])

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ export const Listbox = defineComponent({
171171
if (!event.defaultPrevented) buttonRef.value?.focus({ preventScroll: true })
172172
}
173173

174-
window.addEventListener('click', handler)
175-
onUnmounted(() => window.removeEventListener('click', handler))
174+
window.addEventListener('mousedown', handler)
175+
onUnmounted(() => window.removeEventListener('mousedown', handler))
176176
})
177177

178178
// @ts-expect-error Types of property 'dataRef' are incompatible.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ export const Menu = defineComponent({
147147
if (!event.defaultPrevented) buttonRef.value?.focus({ preventScroll: true })
148148
}
149149

150-
window.addEventListener('click', handler)
151-
onUnmounted(() => window.removeEventListener('click', handler))
150+
window.addEventListener('mousedown', handler)
151+
onUnmounted(() => window.removeEventListener('mousedown', handler))
152152
})
153153

154154
// @ts-expect-error Types of property 'dataRef' are incompatible.

0 commit comments

Comments
 (0)