@@ -106,6 +106,7 @@ export class ActiveOverlay extends LitElement {
106106 public overlayContent ! : HTMLElement ;
107107 public overlayContentTip ?: HTMLElement ;
108108 public trigger ! : HTMLElement ;
109+ public returnFocusElement ?: HTMLSpanElement ;
109110
110111 private placeholder ?: Comment ;
111112 private popper ?: Instance ;
@@ -160,6 +161,7 @@ export class ActiveOverlay extends LitElement {
160161 const firstFocusable = this . querySelector (
161162 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
162163 ) as HTMLElement ;
164+ /* istanbul ignore else */
163165 if ( firstFocusable ) {
164166 firstFocusable . focus ( ) ;
165167 this . removeAttribute ( 'tabindex' ) ;
@@ -182,6 +184,19 @@ export class ActiveOverlay extends LitElement {
182184 return [ styles ] ;
183185 }
184186
187+ public feature ( ) : void {
188+ this . tabIndex = 0 ;
189+ if ( this . interaction === 'modal' ) {
190+ this . slot = 'open' ;
191+ }
192+ }
193+
194+ public obscure ( ) : void {
195+ if ( this . interaction === 'modal' ) {
196+ this . removeAttribute ( 'slot' ) ;
197+ }
198+ }
199+
185200 public firstUpdated ( changedProperties : PropertyValues ) : void {
186201 super . firstUpdated ( changedProperties ) ;
187202
@@ -223,10 +238,7 @@ export class ActiveOverlay extends LitElement {
223238 this . state = 'visible' ;
224239 } ) ;
225240
226- this . tabIndex = 0 ;
227- if ( this . interaction === 'modal' ) {
228- this . slot = 'open' ;
229- }
241+ this . feature ( ) ;
230242 this . updateOverlayPosition ( )
231243 . then ( ( ) => this . applyContentAnimation ( 'spOverlayFadeIn' ) )
232244 . then ( ( ) => {
@@ -296,6 +308,14 @@ export class ActiveOverlay extends LitElement {
296308 this . popper = undefined ;
297309 }
298310
311+ if ( this . returnFocusElement ) {
312+ this . returnFocusElement . remove ( ) ;
313+ this . trigger . removeEventListener (
314+ 'keydown' ,
315+ this . handleInlineTriggerKeydown
316+ ) ;
317+ }
318+
299319 this . returnOverlayContent ( ) ;
300320 this . state = 'disposed' ;
301321 }
@@ -392,6 +412,21 @@ export class ActiveOverlay extends LitElement {
392412 this . schedulePositionUpdate ( ) ;
393413 }
394414
415+ public handleInlineTriggerKeydown = ( event : KeyboardEvent ) : void => {
416+ const { code, shiftKey } = event ;
417+ /* istanbul ignore if */
418+ if ( code !== 'Tab' ) return ;
419+ if ( shiftKey ) {
420+ this . tabbingAway = true ;
421+ this . dispatchEvent ( new Event ( 'close' ) ) ;
422+ return ;
423+ }
424+
425+ event . stopPropagation ( ) ;
426+ event . preventDefault ( ) ;
427+ this . focus ( ) ;
428+ } ;
429+
395430 public connectedCallback ( ) : void {
396431 super . connectedCallback ( ) ;
397432 this . schedulePositionUpdate ( ) ;
0 commit comments