11import { throttle } from 'throttle-debounce' ;
22import { createTippy } from '../modules/tippy.ts' ;
3- import { isDocumentFragmentOrElementNode , toggleClass } from '../utils/dom.ts' ;
3+ import { addDelegatedEventListener , isDocumentFragmentOrElementNode , toggleClass } from '../utils/dom.ts' ;
44import octiconKebabHorizontal from '../../../public/assets/img/svg/octicon-kebab-horizontal.svg' ;
55
66window . 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