@@ -203,8 +203,25 @@ export const OverlayMixin = (superClass) =>
203203 }
204204 }
205205
206+ /**
207+ * Whether to add global listeners for closing on outside click.
208+ * By default, listeners are not added for a modeless overlay.
209+ *
210+ * @return {boolean }
211+ * @protected
212+ */
213+ _shouldAddGlobalListeners ( ) {
214+ return ! this . modeless ;
215+ }
216+
206217 /** @private */
207218 _addGlobalListeners ( ) {
219+ if ( this . __hasGlobalListeners ) {
220+ return ;
221+ }
222+
223+ this . __hasGlobalListeners = true ;
224+
208225 document . addEventListener ( 'mousedown' , this . _boundMouseDownListener ) ;
209226 document . addEventListener ( 'mouseup' , this . _boundMouseUpListener ) ;
210227 // Firefox leaks click to document on contextmenu even if prevented
@@ -214,6 +231,12 @@ export const OverlayMixin = (superClass) =>
214231
215232 /** @private */
216233 _removeGlobalListeners ( ) {
234+ if ( ! this . __hasGlobalListeners ) {
235+ return ;
236+ }
237+
238+ this . __hasGlobalListeners = false ;
239+
217240 document . removeEventListener ( 'mousedown' , this . _boundMouseDownListener ) ;
218241 document . removeEventListener ( 'mouseup' , this . _boundMouseUpListener ) ;
219242 document . documentElement . removeEventListener ( 'click' , this . _boundOutsideClickListener , true ) ;
@@ -247,13 +270,20 @@ export const OverlayMixin = (superClass) =>
247270
248271 /** @private */
249272 _modelessChanged ( modeless ) {
273+ if ( this . opened ) {
274+ // Add / remove listeners if modeless is changed while opened
275+ if ( this . _shouldAddGlobalListeners ( ) ) {
276+ this . _addGlobalListeners ( ) ;
277+ } else {
278+ this . _removeGlobalListeners ( ) ;
279+ }
280+ }
281+
250282 if ( ! modeless ) {
251283 if ( this . opened ) {
252- this . _addGlobalListeners ( ) ;
253284 this . _enterModalState ( ) ;
254285 }
255286 } else {
256- this . _removeGlobalListeners ( ) ;
257287 this . _exitModalState ( ) ;
258288 }
259289 }
@@ -275,7 +305,7 @@ export const OverlayMixin = (superClass) =>
275305
276306 document . addEventListener ( 'keydown' , this . _boundKeydownListener ) ;
277307
278- if ( ! this . modeless ) {
308+ if ( this . _shouldAddGlobalListeners ( ) ) {
279309 this . _addGlobalListeners ( ) ;
280310 }
281311 } else if ( wasOpened ) {
@@ -290,7 +320,7 @@ export const OverlayMixin = (superClass) =>
290320
291321 document . removeEventListener ( 'keydown' , this . _boundKeydownListener ) ;
292322
293- if ( ! this . modeless ) {
323+ if ( this . _shouldAddGlobalListeners ( ) ) {
294324 this . _removeGlobalListeners ( ) ;
295325 }
296326 }
@@ -473,7 +503,7 @@ export const OverlayMixin = (superClass) =>
473503 }
474504
475505 // Only close modeless overlay on Esc press when it contains focus
476- if ( this . modeless && ! event . composedPath ( ) . includes ( this . $ . overlay ) ) {
506+ if ( ! this . _shouldAddGlobalListeners ( ) && ! event . composedPath ( ) . includes ( this . $ . overlay ) ) {
477507 return ;
478508 }
479509
0 commit comments