@@ -36,6 +36,7 @@ export const ContextMenuMixin = (superClass) =>
3636 value : false ,
3737 notify : true ,
3838 readOnly : true ,
39+ sync : true ,
3940 } ,
4041
4142 /**
@@ -135,7 +136,7 @@ export const ContextMenuMixin = (superClass) =>
135136 this . _boundOpen = this . open . bind ( this ) ;
136137 this . _boundClose = this . close . bind ( this ) ;
137138 this . _boundPreventDefault = this . _preventDefault . bind ( this ) ;
138- this . _boundOnGlobalContextMenu = this . _onGlobalContextMenu . bind ( this ) ;
139+ this . __onGlobalContextMenu = this . __onGlobalContextMenu . bind ( this ) ;
139140 }
140141
141142 /** @protected */
@@ -144,6 +145,7 @@ export const ContextMenuMixin = (superClass) =>
144145
145146 this . __boundOnScroll = this . __onScroll . bind ( this ) ;
146147 window . addEventListener ( 'scroll' , this . __boundOnScroll , true ) ;
148+ document . documentElement . addEventListener ( 'contextmenu' , this . __onGlobalContextMenu , true ) ;
147149
148150 // Restore opened state if overlay was opened when disconnecting
149151 if ( this . __restoreOpened ) {
@@ -156,10 +158,16 @@ export const ContextMenuMixin = (superClass) =>
156158 super . disconnectedCallback ( ) ;
157159
158160 window . removeEventListener ( 'scroll' , this . __boundOnScroll , true ) ;
161+ document . documentElement . removeEventListener ( 'contextmenu' , this . __onGlobalContextMenu , true ) ;
159162
160- // Close overlay and memorize opened state
163+ // Memorize opened state and defer close so that DOM moves (disconnect
164+ // followed by reconnect within the same task) do not close the overlay.
161165 this . __restoreOpened = this . opened ;
162- this . close ( ) ;
166+ setTimeout ( ( ) => {
167+ if ( ! this . isConnected ) {
168+ this . close ( ) ;
169+ }
170+ } ) ;
163171 }
164172
165173 /** @protected */
@@ -290,13 +298,7 @@ export const ContextMenuMixin = (superClass) =>
290298 }
291299
292300 /** @private */
293- _openedChanged ( opened , oldOpened ) {
294- if ( opened ) {
295- document . documentElement . addEventListener ( 'contextmenu' , this . _boundOnGlobalContextMenu , true ) ;
296- } else if ( oldOpened ) {
297- document . documentElement . removeEventListener ( 'contextmenu' , this . _boundOnGlobalContextMenu , true ) ;
298- }
299-
301+ _openedChanged ( opened ) {
300302 this . __setListenOnUserSelect ( opened ) ;
301303 }
302304
@@ -630,7 +632,10 @@ export const ContextMenuMixin = (superClass) =>
630632 }
631633
632634 /** @private */
633- _onGlobalContextMenu ( e ) {
635+ __onGlobalContextMenu ( e ) {
636+ if ( ! this . opened ) {
637+ return ;
638+ }
634639 if ( ! e . shiftKey ) {
635640 const isTouchDevice = isAndroid || isIOS ;
636641 if ( ! isTouchDevice ) {
0 commit comments