Skip to content

Commit 24bcb49

Browse files
committed
use addDelegatedEventListener
1 parent 8315fb6 commit 24bcb49

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

web_src/js/utils/dom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ export function querySingleVisibleElem<T extends HTMLElement>(parent: Element, s
360360
export function addDelegatedEventListener<T extends HTMLElement, E extends Event>(parent: Node, type: string, selector: string, listener: (elem: T, e: E) => Promisable<void>, options?: boolean | AddEventListenerOptions) {
361361
parent.addEventListener(type, (e: Event) => {
362362
const elem = (e.target as HTMLElement).closest(selector);
363-
if (!elem) return;
363+
if (!elem || !parent.contains(elem)) return;
364364
listener(elem as T, e as E);
365365
}, options);
366366
}

web_src/js/webcomponents/overflow-menu.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {throttle} from 'throttle-debounce';
22
import {createTippy} from '../modules/tippy.ts';
3-
import {isDocumentFragmentOrElementNode, toggleClass} from '../utils/dom.ts';
3+
import {addDelegatedEventListener, isDocumentFragmentOrElementNode, toggleClass} from '../utils/dom.ts';
44
import octiconKebabHorizontal from '../../../public/assets/img/svg/octicon-kebab-horizontal.svg';
55

66
window.customElements.define('overflow-menu', class extends HTMLElement {
@@ -20,7 +20,6 @@ window.customElements.define('overflow-menu', class extends HTMLElement {
2020
updateItems = throttle(100, () => {
2121
if (!this.tippyContent) {
2222
const div = document.createElement('div');
23-
div.classList.add('tippy-target');
2423
div.tabIndex = -1; // for initial focus, programmatic focus only
2524
div.addEventListener('keydown', (e) => {
2625
if (e.key === 'Tab') {
@@ -69,7 +68,8 @@ window.customElements.define('overflow-menu', class extends HTMLElement {
6968
}
7069
}
7170
});
72-
this.append(div);
71+
div.classList.add('tippy-target');
72+
this.handleItemClick(div, '.tippy-target > .item');
7373
this.tippyContent = div;
7474
}
7575

@@ -102,21 +102,6 @@ window.customElements.define('overflow-menu', class extends HTMLElement {
102102
const itemRight = item.offsetLeft + item.offsetWidth;
103103
if (menuRight - itemRight < 38) { // roughly the width of .overflow-menu-button with some extra space
104104
this.tippyItems.push(item);
105-
106-
// close tippy when clicking item of tippy
107-
if (!item.hasAttribute('data-tippy-click-added')) {
108-
item.addEventListener('click', () => {
109-
this.button?._tippy.hide();
110-
});
111-
item.setAttribute('data-tippy-click-added', 'true');
112-
}
113-
}
114-
// refresh overflow-button active state
115-
if (!item.hasAttribute('data-button-update-click-added')) {
116-
item.addEventListener('click', () => {
117-
this.updateButtonActivationState();
118-
});
119-
item.setAttribute('data-button-update-click-added', 'true');
120105
}
121106
}
122107
itemFlexSpace?.style.removeProperty('display');
@@ -209,6 +194,14 @@ window.customElements.define('overflow-menu', class extends HTMLElement {
209194
}
210195
});
211196
this.resizeObserver.observe(this);
197+
this.handleItemClick(this, '.overflow-menu-items > .item');
198+
}
199+
200+
handleItemClick(el: Element, selector: string) {
201+
addDelegatedEventListener(el, 'click', selector, () => {
202+
this.button?._tippy.hide();
203+
this.updateButtonActivationState();
204+
});
212205
}
213206

214207
connectedCallback() {

0 commit comments

Comments
 (0)