Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(popup): use capturing listener to improve robustness #1285

Merged
merged 1 commit into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 13 additions & 26 deletions src/popup/popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ export default Vue.extend({
mouseInRange: false,
/** mark popup as clicked when mousedown, reset after mouseup */
contentClicked: false,
/**
* mark trigger element as clicked when click,
* reset after click event bubbles to document */
triggerClicked: false,
};
},
computed: {
Expand Down Expand Up @@ -107,7 +103,7 @@ export default Vue.extend({
if (visible) {
this.preventClosing(true);
if (!this.hasDocumentEvent) {
on(document, 'click', this.handleDocumentClick);
on(document, 'click', this.handleDocumentClick, true);
this.hasDocumentEvent = true;
}
// focus trigger esc 隐藏浮层
Expand All @@ -124,7 +120,7 @@ export default Vue.extend({
} else {
this.preventClosing(false);
// destruction is delayed until after animation ends
off(document, 'click', this.handleDocumentClick);
off(document, 'click', this.handleDocumentClick, true);
this.hasDocumentEvent = false;
this.mouseInRange = false;
}
Expand Down Expand Up @@ -158,8 +154,6 @@ export default Vue.extend({
trigger.add('focusout', () => this.handleClose({ trigger: 'trigger-element-blur' }));
} else if (hasTrigger.click) {
trigger.add('click', (e: MouseEvent) => {
// override nested popups with trigger hover due to higher priority
this.visibleState = 0;
this.handleToggle({ e, trigger: 'trigger-element-click' });
// ie9-10 trigger propagation
if (getIEVersion() < 11) {
Expand All @@ -174,20 +168,17 @@ export default Vue.extend({
e.button === 2 && this.handleToggle({ trigger: 'context-menu' });
});
}
if (!hasTrigger['context-menu']) {
trigger.add('click', () => {
this.triggerClicked = true;
});
}
};
updateTrigger();
this.$watch('trigger', updateTrigger);
},
updated() {
(this.$refs.container as any)?.updateContent();
},
destroyed() {
beforeDestroy() {
(this as any).popup?.preventClosing(false);
this.destroyPopper();
off(document, 'click', this.handleDocumentClick, true);
},
methods: {
updatePopper() {
Expand Down Expand Up @@ -238,7 +229,8 @@ export default Vue.extend({
if (!triggerEl || !overlayEl) return;
if (typeof overlayStyle === 'function') {
return overlayStyle(triggerEl, overlayEl);
} if (typeof overlayStyle === 'object') {
}
if (typeof overlayStyle === 'object') {
return overlayStyle;
}
},
Expand Down Expand Up @@ -289,15 +281,17 @@ export default Vue.extend({
this.hasTrigger.click ? 0 : hideTimeout,
);
},
handleDocumentClick() {
if (this.contentClicked || this.triggerClicked) {
this.triggerClicked = false;
// clear the flag if mouseup handler is failed
handleDocumentClick(ev?: MouseEvent) {
if (this.contentClicked) {
// clear the flag after mousedown
setTimeout(() => {
this.contentClicked = false;
});
return;
}
const triggerEl = this.$el as HTMLElement;
// ignore document event when clicking trigger element
if (triggerEl.contains(ev.target as Node)) return;
this.visibleState = 0;
this.emitPopVisible(false, { trigger: 'document' });
},
Expand Down Expand Up @@ -390,13 +384,6 @@ export default Vue.extend({
mousedown: () => {
this.contentClicked = true;
},
mouseup: () => {
// make sure to execute after document click is triggered
setTimeout(() => {
// clear the flag which was set by mousedown
this.contentClicked = false;
});
},
...(hasTrigger.hover && {
mouseenter: this.onMouseEnter,
mouseleave: this.onMouseLeave,
Expand Down
31 changes: 23 additions & 8 deletions src/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ export const trim = (str: string): string => (str || '').replace(/^[\s\uFEFF]+|[

export const on = ((): any => {
if (!isServer && document.addEventListener) {
return (element: Node, event: string, handler: EventListenerOrEventListenerObject): any => {
return (
element: Node,
event: string,
handler: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions,
): any => {
if (element && event && handler) {
element.addEventListener(event, handler, false);
return () => off(element, event, handler);
element.addEventListener(event, handler, options);
return () => off(element, event, handler, options);
}
};
}
Expand All @@ -31,9 +36,14 @@ export const on = ((): any => {

export const off = ((): any => {
if (!isServer && document.removeEventListener) {
return (element: Node, event: string, handler: EventListenerOrEventListenerObject): any => {
return (
element: Node,
event: string,
handler: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions,
): any => {
if (element && event) {
element.removeEventListener(event, handler, false);
element.removeEventListener(event, handler, options);
}
};
}
Expand All @@ -44,14 +54,19 @@ export const off = ((): any => {
};
})();

export function once(element: Node, event: string, handler: EventListenerOrEventListenerObject) {
export function once(
element: Node,
event: string,
handler: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions,
) {
const handlerFn = typeof handler === 'function' ? handler : handler.handleEvent;
const callback = (evt: any) => {
handlerFn(evt);
off(element, event, callback);
off(element, event, callback, options);
};

on(element, event, callback);
on(element, event, callback, options);
}

export function hasClass(el: Element, cls: string): any {
Expand Down