Skip to content

Commit 0f21bc0

Browse files
committed
Fix popover mousedown and add preventDefault
1 parent 778e2b7 commit 0f21bc0

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

packages/components/src/Popover/Popover.tsx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,15 @@ function useOpenWithoutElement(isOpen: boolean, element: HTMLElement | null) {
220220
return openWithoutElem
221221
}
222222

223+
function isNodeInOrAfter(nodeA: Node, nodeB: Node) {
224+
const relationship = nodeA.compareDocumentPosition(nodeB)
225+
return (
226+
relationship === Node.DOCUMENT_POSITION_FOLLOWING ||
227+
relationship ===
228+
Node.DOCUMENT_POSITION_FOLLOWING + Node.DOCUMENT_POSITION_CONTAINED_BY
229+
)
230+
}
231+
223232
function usePopoverToggle(
224233
{
225234
isOpen: controlledIsOpen = false,
@@ -257,21 +266,16 @@ function usePopoverToggle(
257266
// component triggers a scroll animation resulting in an
258267
// unintentional drag, which closes the popover
259268
if (portalElement && mouseDownTarget) {
260-
const relationship = portalElement.compareDocumentPosition(
261-
mouseDownTarget as Node
262-
)
263-
if (
264-
relationship === Node.DOCUMENT_POSITION_FOLLOWING ||
265-
relationship ===
266-
Node.DOCUMENT_POSITION_FOLLOWING +
267-
Node.DOCUMENT_POSITION_CONTAINED_BY
268-
) {
269+
if (isNodeInOrAfter(portalElement, mouseDownTarget as Node)) {
269270
return
270271
}
271272
}
272273

273274
// User clicked inside the Popover surface/portal
274-
if (portalElement && portalElement.contains(event.target as Node)) {
275+
if (
276+
portalElement &&
277+
isNodeInOrAfter(portalElement, event.target as Node)
278+
) {
275279
return
276280
}
277281

@@ -301,6 +305,7 @@ function usePopoverToggle(
301305
}
302306

303307
event.stopPropagation()
308+
event.preventDefault()
304309
}
305310

306311
function handleMouseDown(event: MouseEvent) {

0 commit comments

Comments
 (0)