@@ -207,7 +207,7 @@ export class ActionMenuElement extends HTMLElement {
207
207
if ( dialogInvoker ) {
208
208
const dialog = this . ownerDocument . getElementById ( dialogInvoker . getAttribute ( 'data-show-dialog-id' ) || '' )
209
209
210
- if ( dialog && this . contains ( dialogInvoker ) && this . contains ( dialog ) ) {
210
+ if ( dialog && this . contains ( dialogInvoker ) ) {
211
211
this . #handleDialogItemActivated( event , dialog )
212
212
return
213
213
}
@@ -243,20 +243,33 @@ export class ActionMenuElement extends HTMLElement {
243
243
}
244
244
245
245
#handleDialogItemActivated( event : Event , dialog : HTMLElement ) {
246
- this . querySelector < HTMLElement > ( '.ActionListWrap' ) ! . style . display = 'none'
246
+ if ( this . contains ( dialog ) ) {
247
+ this . querySelector < HTMLElement > ( '.ActionListWrap' ) ! . style . display = 'none'
248
+ }
247
249
const dialog_controller = new AbortController ( )
248
250
const { signal} = dialog_controller
249
251
const handleDialogClose = ( ) => {
250
252
dialog_controller . abort ( )
251
- this . querySelector < HTMLElement > ( '.ActionListWrap' ) ! . style . display = ''
252
- if ( this . #isOpen( ) ) {
253
- this . #hide( )
253
+ if ( this . contains ( dialog ) ) {
254
+ this . querySelector < HTMLElement > ( '.ActionListWrap' ) ! . style . display = ''
255
+ if ( this . #isOpen( ) ) {
256
+ this . #hide( )
257
+ }
254
258
}
255
259
const activeElement = this . ownerDocument . activeElement
256
260
const lostFocus = this . ownerDocument . activeElement === this . ownerDocument . body
257
261
const focusInClosedMenu = this . contains ( activeElement )
258
- if ( lostFocus || focusInClosedMenu ) {
259
- setTimeout ( ( ) => this . invokerElement ?. focus ( ) , 0 )
262
+ const focusInDialog = dialog . contains ( activeElement )
263
+ if ( lostFocus || focusInClosedMenu || focusInDialog ) {
264
+ setTimeout ( ( ) => {
265
+ // if the activeElement has changed after a task, then it's likely
266
+ // that other JS has tried to shift focus. We should respect that
267
+ // focus shift as long as it's not back at the document.
268
+ const newActiveElement = this . ownerDocument . activeElement
269
+ if ( newActiveElement === activeElement || newActiveElement === this . ownerDocument . body ) {
270
+ this . invokerElement ?. focus ( )
271
+ }
272
+ } , 0 )
260
273
}
261
274
}
262
275
// a modal <dialog> element will close all popovers
0 commit comments