@@ -237,8 +237,25 @@ export const OverlayMixin = (superClass) =>
237237 }
238238 }
239239
240+ /**
241+ * Whether to add global listeners for closing on outside click.
242+ * By default, listeners are not added for a modeless overlay.
243+ *
244+ * @return {boolean }
245+ * @protected
246+ */
247+ _shouldAddGlobalListeners ( ) {
248+ return ! this . modeless ;
249+ }
250+
240251 /** @private */
241252 _addGlobalListeners ( ) {
253+ if ( this . __hasGlobalListeners ) {
254+ return ;
255+ }
256+
257+ this . __hasGlobalListeners = true ;
258+
242259 document . addEventListener ( 'mousedown' , this . _boundMouseDownListener ) ;
243260 document . addEventListener ( 'mouseup' , this . _boundMouseUpListener ) ;
244261 // Firefox leaks click to document on contextmenu even if prevented
@@ -248,6 +265,12 @@ export const OverlayMixin = (superClass) =>
248265
249266 /** @private */
250267 _removeGlobalListeners ( ) {
268+ if ( ! this . __hasGlobalListeners ) {
269+ return ;
270+ }
271+
272+ this . __hasGlobalListeners = false ;
273+
251274 document . removeEventListener ( 'mousedown' , this . _boundMouseDownListener ) ;
252275 document . removeEventListener ( 'mouseup' , this . _boundMouseUpListener ) ;
253276 document . documentElement . removeEventListener ( 'click' , this . _boundOutsideClickListener , true ) ;
@@ -281,13 +304,20 @@ export const OverlayMixin = (superClass) =>
281304
282305 /** @private */
283306 _modelessChanged ( modeless ) {
307+ if ( this . opened ) {
308+ // Add / remove listeners if modeless is changed while opened
309+ if ( this . _shouldAddGlobalListeners ( ) ) {
310+ this . _addGlobalListeners ( ) ;
311+ } else {
312+ this . _removeGlobalListeners ( ) ;
313+ }
314+ }
315+
284316 if ( ! modeless ) {
285317 if ( this . opened ) {
286- this . _addGlobalListeners ( ) ;
287318 this . _enterModalState ( ) ;
288319 }
289320 } else {
290- this . _removeGlobalListeners ( ) ;
291321 this . _exitModalState ( ) ;
292322 }
293323 setOverlayStateAttribute ( this , 'modeless' , modeless ) ;
@@ -326,7 +356,7 @@ export const OverlayMixin = (superClass) =>
326356
327357 document . addEventListener ( 'keydown' , this . _boundKeydownListener ) ;
328358
329- if ( ! this . modeless ) {
359+ if ( this . _shouldAddGlobalListeners ( ) ) {
330360 this . _addGlobalListeners ( ) ;
331361 }
332362 } else if ( wasOpened ) {
@@ -341,7 +371,7 @@ export const OverlayMixin = (superClass) =>
341371
342372 document . removeEventListener ( 'keydown' , this . _boundKeydownListener ) ;
343373
344- if ( ! this . modeless ) {
374+ if ( this . _shouldAddGlobalListeners ( ) ) {
345375 this . _removeGlobalListeners ( ) ;
346376 }
347377 }
@@ -522,7 +552,7 @@ export const OverlayMixin = (superClass) =>
522552 }
523553
524554 // Only close modeless overlay on Esc press when it contains focus
525- if ( this . modeless && ! event . composedPath ( ) . includes ( this . $ . overlay ) ) {
555+ if ( ! this . _shouldAddGlobalListeners ( ) && ! event . composedPath ( ) . includes ( this . $ . overlay ) ) {
526556 return ;
527557 }
528558
0 commit comments