Skip to content

Commit 89258a9

Browse files
committed
feat: close on mousedown
1 parent 6a49861 commit 89258a9

File tree

1 file changed

+23
-60
lines changed
  • packages/floating-vue/src/components

1 file changed

+23
-60
lines changed

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

+23-60
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,9 @@ const createPopper = () => defineComponent({
317317
randomId: `popper_${[Math.random(), Date.now()].map(n => n.toString(36).substring(2, 10)).join('_')}`,
318318
shownChildren: new Set(),
319319
lastAutoHide: true,
320-
$_pendingHide: false,
321-
$_containsGlobalTarget: false,
322-
$_isDisposed: false,
320+
pendingHide: false,
321+
containsGlobalTarget: false,
322+
isDisposed: true,
323323
}
324324
},
325325

@@ -406,7 +406,6 @@ const createPopper = () => defineComponent({
406406
},
407407

408408
created () {
409-
this.$_isDisposed = true
410409
if (this.autoMinSize) {
411410
console.warn('[floating-vue] `autoMinSize` option is deprecated. Use `autoSize="min"` instead.')
412411
}
@@ -436,7 +435,7 @@ const createPopper = () => defineComponent({
436435
show ({ event = null, skipDelay = false, force = false } = {}) {
437436
if (this.parentPopper?.lockedChild && this.parentPopper.lockedChild !== this) return
438437

439-
this.$_pendingHide = false
438+
this.pendingHide = false
440439
if (force || !this.disabled) {
441440
if (this.parentPopper?.lockedChild === this) {
442441
this.parentPopper.lockedChild = null
@@ -459,7 +458,7 @@ const createPopper = () => defineComponent({
459458

460459
// Abort if child is shown
461460
if (this.shownChildren.size > 0) {
462-
this.$_pendingHide = true
461+
this.pendingHide = true
463462
return
464463
}
465464

@@ -481,16 +480,16 @@ const createPopper = () => defineComponent({
481480
this.parentPopper.lockedChild = null
482481
}
483482

484-
this.$_pendingHide = false
483+
this.pendingHide = false
485484
this.$_scheduleHide(event, skipDelay)
486485

487486
this.$emit('hide')
488487
this.$emit('update:shown', false)
489488
},
490489

491490
init () {
492-
if (!this.$_isDisposed) return
493-
this.$_isDisposed = false
491+
if (!this.isDisposed) return
492+
this.isDisposed = false
494493
this.isMounted = false
495494
this.$_events = []
496495
this.$_preventShow = false
@@ -516,8 +515,8 @@ const createPopper = () => defineComponent({
516515
},
517516

518517
dispose () {
519-
if (this.$_isDisposed) return
520-
this.$_isDisposed = true
518+
if (this.isDisposed) return
519+
this.isDisposed = true
521520
this.$_removeEventListeners()
522521
this.hide({ skipDelay: true })
523522
this.$_detachPopperNode()
@@ -538,7 +537,7 @@ const createPopper = () => defineComponent({
538537
},
539538

540539
async $_computePosition () {
541-
if (this.$_isDisposed || this.positioningDisabled) return
540+
if (this.isDisposed || this.positioningDisabled) return
542541

543542
const options: ComputePositionConfig = {
544543
strategy: this.strategy,
@@ -691,7 +690,7 @@ const createPopper = () => defineComponent({
691690

692691
$_scheduleHide (_event, skipDelay = false) {
693692
if (this.shownChildren.size > 0) {
694-
this.$_pendingHide = true
693+
this.pendingHide = true
695694
return
696695
}
697696
this.$_updateParentShownChildren(false)
@@ -795,7 +794,7 @@ const createPopper = () => defineComponent({
795794

796795
async $_applyHide (skipTransition = false) {
797796
if (this.shownChildren.size > 0) {
798-
this.$_pendingHide = true
797+
this.pendingHide = true
799798
this.$_hideInProgress = false
800799
return
801800
}
@@ -865,7 +864,7 @@ const createPopper = () => defineComponent({
865864
},
866865

867866
$_ensureTeleport () {
868-
if (this.$_isDisposed) return
867+
if (this.isDisposed) return
869868

870869
let container = this.container
871870
// if container is a query, get the relative element
@@ -950,7 +949,7 @@ const createPopper = () => defineComponent({
950949
},
951950

952951
$_refreshListeners () {
953-
if (!this.$_isDisposed) {
952+
if (!this.isDisposed) {
954953
this.$_removeEventListeners()
955954
this.$_addEventListeners()
956955
}
@@ -1010,7 +1009,7 @@ const createPopper = () => defineComponent({
10101009
} else {
10111010
parent.shownChildren.delete(this.randomId)
10121011

1013-
if (parent.$_pendingHide) {
1012+
if (parent.pendingHide) {
10141013
parent.hide()
10151014
}
10161015
}
@@ -1039,10 +1038,6 @@ const createPopper = () => defineComponent({
10391038
}
10401039
return false
10411040
},
1042-
1043-
$_mouseDownContains () {
1044-
// replaced by handleGlobalMousedown
1045-
},
10461041
},
10471042

10481043
render () {
@@ -1052,57 +1047,30 @@ const createPopper = () => defineComponent({
10521047

10531048
if (typeof document !== 'undefined' && typeof window !== 'undefined') {
10541049
if (isIOS) {
1055-
document.addEventListener('touchstart', handleGlobalMousedown, supportsPassive
1056-
? {
1057-
passive: true,
1058-
capture: true,
1059-
}
1060-
: true)
1061-
document.addEventListener('touchend', handleGlobalTouchend, supportsPassive
1050+
document.addEventListener('touchstart', handleGlobalClose, supportsPassive
10621051
? {
10631052
passive: true,
10641053
capture: true,
10651054
}
10661055
: true)
10671056
} else {
1068-
window.addEventListener('mousedown', handleGlobalMousedown, true)
1069-
window.addEventListener('click', handleGlobalClick, true)
1057+
window.addEventListener('mousedown', handleGlobalClose, true)
10701058
}
10711059
window.addEventListener('resize', computePositionAllShownPoppers)
10721060
}
10731061

1074-
function handleGlobalMousedown (event: PopperEvent) {
1075-
for (let i = 0; i < shownPoppers.length; i++) {
1076-
const popper = shownPoppers[i]
1077-
try {
1078-
const popperContent = popper.popperNode()
1079-
popper.$_mouseDownContains = popperContent.contains(event.target)
1080-
} catch (e) {
1081-
// noop
1082-
}
1083-
}
1084-
}
1085-
1086-
function handleGlobalClick (event: PopperEvent) {
1087-
handleGlobalClose(event)
1088-
}
1089-
1090-
function handleGlobalTouchend (event: PopperEvent) {
1091-
handleGlobalClose(event, true)
1092-
}
1093-
10941062
function handleGlobalClose (event: PopperEvent, touch = false) {
10951063
const preventClose: Record<string, true> = {}
10961064

10971065
for (let i = shownPoppers.length - 1; i >= 0; i--) {
10981066
const popper = shownPoppers[i]
10991067
try {
1100-
const contains = popper.$_containsGlobalTarget = isContainingEventTarget(popper, event)
1101-
popper.$_pendingHide = false
1068+
const contains = popper.containsGlobalTarget = popper.popperNode().contains(event.target)
1069+
popper.pendingHide = false
11021070

11031071
// Delay so that close directive has time to set values (closeAllPopover, closePopover)
11041072
requestAnimationFrame(() => {
1105-
popper.$_pendingHide = false
1073+
popper.pendingHide = false
11061074
if (preventClose[popper.randomId]) return
11071075

11081076
if (shouldAutoHide(popper, contains, event)) {
@@ -1119,9 +1087,9 @@ function handleGlobalClose (event: PopperEvent, touch = false) {
11191087
}
11201088

11211089
// Auto hide parents
1122-
let parent = popper.parentPopper
1090+
let parent = popper.parentPopper as PopperInstance
11231091
while (parent) {
1124-
if (shouldAutoHide(parent, parent.$_containsGlobalTarget, event)) {
1092+
if (shouldAutoHide(parent, parent.containsGlobalTarget, event)) {
11251093
parent.$_handleGlobalClose(event, touch)
11261094
} else {
11271095
break
@@ -1136,11 +1104,6 @@ function handleGlobalClose (event: PopperEvent, touch = false) {
11361104
}
11371105
}
11381106

1139-
function isContainingEventTarget (popper: PopperInstance, event: Event): boolean {
1140-
const popperContent = popper.popperNode()
1141-
return popper.$_mouseDownContains || popperContent.contains(event.target)
1142-
}
1143-
11441107
function shouldAutoHide (popper: PopperInstance, contains, event: PopperEvent): boolean {
11451108
return event.closeAllPopover || (event.closePopover && contains) || (getAutoHideResult(popper, event) && !contains)
11461109
}

0 commit comments

Comments
 (0)