@@ -166,13 +166,15 @@ export default class Popup extends AutoControlledComponent<ReactProps<PopupProps
166
166
private outsideClickSubscription = EventStack . noSubscription
167
167
168
168
private triggerDomElement = null
169
+ // focusable element which has triggered Popup, can be either triggerDomElement or the element inside it
170
+ private triggerFocusableDomElement = null
169
171
private popupDomElement = null
170
172
171
173
private closeTimeoutId
172
174
173
175
protected actionHandlers : AccessibilityActionHandlers = {
174
176
closeAndFocusTrigger : e => {
175
- this . close ( e , ( ) => _ . invoke ( this . triggerDomElement , 'focus' ) )
177
+ this . close ( e , ( ) => _ . invoke ( this . triggerFocusableDomElement , 'focus' ) )
176
178
e . stopPropagation ( )
177
179
} ,
178
180
close : e => this . close ( e ) ,
@@ -474,6 +476,10 @@ export default class Popup extends AutoControlledComponent<ReactProps<PopupProps
474
476
}
475
477
476
478
private trySetOpen ( newValue : boolean , eventArgs : any ) {
479
+ // when new state 'open' === 'true', save the last focused element
480
+ if ( newValue ) {
481
+ this . updateTriggerFocusableDomElement ( )
482
+ }
477
483
this . trySetState ( { open : newValue } )
478
484
_ . invoke ( this . props , 'onOpenChange' , eventArgs , { ...this . props , ...{ open : newValue } } )
479
485
}
@@ -497,4 +503,14 @@ export default class Popup extends AutoControlledComponent<ReactProps<PopupProps
497
503
onClose && onClose ( )
498
504
}
499
505
}
506
+
507
+ /**
508
+ * Save DOM element which had focus before Popup opens.
509
+ * Can be either trigger DOM element itself or the element inside it.
510
+ */
511
+ private updateTriggerFocusableDomElement ( ) {
512
+ this . triggerFocusableDomElement = this . triggerDomElement . contains ( document . activeElement )
513
+ ? document . activeElement
514
+ : this . triggerDomElement
515
+ }
500
516
}
0 commit comments