Skip to content

Commit b5dc40c

Browse files
committed
fix: revert autohide on mousedown, add autoHideOnMousedown in config, fix #1015
1 parent 62014c3 commit b5dc40c

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

docs/guide/config.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ export const config: FloatingVueConfig = {
6262
arrowPadding: 0,
6363
// Compute arrow overflow (useful to hide it)
6464
arrowOverflow: true,
65+
/**
66+
* By default, compute autohide on 'click'.
67+
*/
68+
autoHideOnMousedown: false,
6569
// Themes
6670
themes: {
6771
tooltip: {

packages/floating-vue/src/components/Popper.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { placements, Placement } from '../util/popper'
1414
import { SHOW_EVENT_MAP, HIDE_EVENT_MAP } from '../util/events'
1515
import { removeFromArray } from '../util/lang'
1616
import { nextFrame } from '../util/frame'
17-
import { getDefaultConfig, getAllParentThemes } from '../config'
17+
import { getDefaultConfig, getAllParentThemes, config } from '../config'
1818

1919
export type ComputePositionConfig = Parameters<typeof computePosition>[2]
2020

@@ -320,6 +320,7 @@ const createPopper = () => defineComponent({
320320
pendingHide: false,
321321
containsGlobalTarget: false,
322322
isDisposed: true,
323+
mouseDownContains: false,
323324
}
324325
},
325326

@@ -1047,25 +1048,50 @@ const createPopper = () => defineComponent({
10471048

10481049
if (typeof document !== 'undefined' && typeof window !== 'undefined') {
10491050
if (isIOS) {
1050-
document.addEventListener('touchstart', handleGlobalClose, supportsPassive
1051+
const options = supportsPassive
10511052
? {
10521053
passive: true,
10531054
capture: true,
10541055
}
1055-
: true)
1056+
: true
1057+
document.addEventListener('touchstart', (event) => handleGlobalPointerDown(event, true), options)
1058+
document.addEventListener('touchend', (event) => handleGlobalPointerUp(event, true), options)
10561059
} else {
1057-
window.addEventListener('mousedown', handleGlobalClose, true)
1060+
window.addEventListener('mousedown', (event) => handleGlobalPointerDown(event, false), true)
1061+
window.addEventListener('click', (event) => handleGlobalPointerUp(event, false), true)
10581062
}
10591063
window.addEventListener('resize', recomputeAllPoppers)
10601064
}
10611065

1062-
function handleGlobalClose (event: PopperEvent, touch = false) {
1066+
function handleGlobalPointerDown (event: PopperEvent, touch: boolean) {
1067+
if (config.autoHideOnMousedown) {
1068+
handleGlobalClose(event, touch)
1069+
} else {
1070+
// Compute contains only
1071+
for (let i = 0; i < shownPoppers.length; i++) {
1072+
const popper = shownPoppers[i]
1073+
try {
1074+
popper.mouseDownContains = popper.popperNode().contains(event.target)
1075+
} catch (e) {
1076+
// noop
1077+
}
1078+
}
1079+
}
1080+
}
1081+
1082+
function handleGlobalPointerUp (event: PopperEvent, touch: boolean) {
1083+
if (!config.autoHideOnMousedown) {
1084+
handleGlobalClose(event, touch)
1085+
}
1086+
}
1087+
1088+
function handleGlobalClose (event: PopperEvent, touch: boolean) {
10631089
const preventClose: Record<string, true> = {}
10641090

10651091
for (let i = shownPoppers.length - 1; i >= 0; i--) {
10661092
const popper = shownPoppers[i]
10671093
try {
1068-
const contains = popper.containsGlobalTarget = popper.popperNode().contains(event.target)
1094+
const contains = popper.containsGlobalTarget = popper.mouseDownContains || popper.popperNode().contains(event.target)
10691095
popper.pendingHide = false
10701096

10711097
// Delay so that close directive has time to set values (closeAllPopover, closePopover)

packages/floating-vue/src/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ export const config: FloatingVueConfig = {
3131
arrowPadding: 0,
3232
// Compute arrow overflow (useful to hide it)
3333
arrowOverflow: true,
34+
/**
35+
* By default, compute autohide on 'click'.
36+
*/
37+
autoHideOnMousedown: false,
3438
// Themes
3539
themes: {
3640
tooltip: {

0 commit comments

Comments
 (0)