license import { FocusTrapInertStrategy } from './focus-trap-inert-strategy' ; import { ConfigurableFocusTrap } from './configurable-focus-trap' ; /**
 * Lightweight FocusTrapInertStrategy that adds a document focus event
 * listener to redirect focus back inside the FocusTrap.
 */ export class EventListenerFocusTrapInertStrategy implements FocusTrapInertStrategy { /** Focus event handler. */ private _listener : ( ( e : FocusEvent ) => void ) | null = null ; /** Adds a document event listener that keeps focus inside the FocusTrap. */ preventFocus ( focusTrap ) { if ( this . _listener ) { focusTrap . _document . removeEventListener ( 'focus' , this . _listener ! , true ) ; } this . _listener = ( e ) => this . _trapFocus ( focusTrap , e ) ; focusTrap . _ngZone . runOutsideAngular ( ( ) => { focusTrap . _document . addEventListener ( 'focus' , this . _listener ! , true ) ; } ) ; } /** Removes the event listener added in preventFocus. */ allowFocus ( focusTrap ) { if ( ! this . _listener ) { return ; } focusTrap . _document . removeEventListener ( 'focus' , this . _listener ! , true ) ; this . _listener = null ; } /**
     * Refocuses the first element in the FocusTrap if the focus event target was outside
     * the FocusTrap.
     *
     * This is an event listener callback. The event listener is added in runOutsideAngular,
     * so all this code runs outside Angular as well.
     */ private _trapFocus ( focusTrap , event ) { const target = event . target as HTMLElement ; const focusTrapRoot = focusTrap . _element ; if ( target && ! focusTrapRoot . contains ( target ) && ! target . closest ?. ( 'div.cdk-overlay-pane' ) ) { setTimeout ( ( ) => { if ( focusTrap . enabled && ! focusTrapRoot . contains ( focusTrap . _document . activeElement ) ) { focusTrap . focusFirstTabbableElement ( ) ; } } ) ; } } }