Skip to content

Commit

Permalink
fix(popper): add stopPropagation for wheel event (#1245)
Browse files Browse the repository at this point in the history
  • Loading branch information
shenjunjian authored Jan 4, 2024
1 parent 4ba3f5d commit 2133162
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/renderless/src/common/deps/popper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ const getOffsetRect = (el: HTMLElement) => {
return elementRect
}

const stopFn = (ev: Event) => {
ev.stopPropagation()
}
interface PopperOptions {
arrowOffset: number
arrowElement: string
Expand Down Expand Up @@ -366,6 +369,8 @@ class Popper {
update() {
let data = { instance: this, styles: {} } as unknown as UpdateData

this.stopEventBubble() // 每次更新都检查

this.popperOuterSize = null as unknown as { width: number; height: number }
data.placement = data._originalPlacement = this._options.placement
data.offsets = this._getRefPopOffsets(this._popper, this._reference, data.placement)
Expand All @@ -377,6 +382,14 @@ class Popper {
typeof this.state.updateCallback === 'function' && this.state.updateCallback(data)
}

// 阻止popper的mousewheel等事件冒泡。 通过 onxxx 绑定,是为了避免重复绑定事件
stopEventBubble() {
if (!this._popper) return

if (!this._popper.onmousewheel) this._popper.onmousewheel = stopFn
if (!this._popper.onwheel) this._popper.onwheel = stopFn
}

/** 按顺序执行Modifiers, 如果传入终点modifier,则执行到指定位置 */
runModifiers(data: UpdateData, modifiers: Function[], ends?: Function) {
let modifiersToRun = modifiers.slice()
Expand All @@ -385,7 +398,7 @@ class Popper {
if (ends !== undefined) {
modifiersToRun = this._options.modifierFns.slice(
0,
_options.modifierFns.findIndex((m) => m == ends)
_options.modifierFns.findIndex((m) => m === ends)
)
}

Expand Down

0 comments on commit 2133162

Please sign in to comment.