license import { MDCComponent } from '@material/base/component' ; import { SpecificEventListener } from '@material/base/types' ; import { closest } from '@material/dom/ponyfill' ; import { MDCSnackbarAdapter } from './adapter' ; import { strings } from './constants' ; import { MDCSnackbarFoundation } from './foundation' ; import { MDCSnackbarAnnouncer , MDCSnackbarAnnouncerFactory , MDCSnackbarCloseEventDetail } from './types' ; import * as util from './util' ; const { SURFACE_SELECTOR , LABEL_SELECTOR , ACTION_SELECTOR , DISMISS_SELECTOR , OPENING_EVENT , OPENED_EVENT , CLOSING_EVENT , CLOSED_EVENT , } = strings ; export class MDCSnackbar extends MDCComponent < MDCSnackbarFoundation > { static override attachTo ( root ) { return new MDCSnackbar ( root ) ; } private announce ! : MDCSnackbarAnnouncer ; private actionEl ! : Element ; private labelEl ! : Element ; private surfaceEl ! : Element ; private handleKeyDown ! : SpecificEventListener < 'keydown' > ; private handleSurfaceClick ! : SpecificEventListener < 'click' > ; override initialize ( announcerFactory = ( ) => util . announce ) { this . announce = announcerFactory ( ) ; } override initialSyncWithDOM ( ) { this . surfaceEl = this . root . querySelector ( SURFACE_SELECTOR ) ! ; this . labelEl = this . root . querySelector ( LABEL_SELECTOR ) ! ; this . actionEl = this . root . querySelector ( ACTION_SELECTOR ) ! ; this . handleKeyDown = ( evt ) => { this . foundation . handleKeyDown ( evt ) ; } ; this . handleSurfaceClick = ( evt ) => { const target = evt . target as Element ; if ( this . isActionButton ( target ) ) { this . foundation . handleActionButtonClick ( evt ) ; } else if ( this . isActionIcon ( target ) ) { this . foundation . handleActionIconClick ( evt ) ; } } ; this . registerKeyDownHandler ( this . handleKeyDown ) ; this . registerSurfaceClickHandler ( this . handleSurfaceClick ) ; } override destroy ( ) { super . destroy ( ) ; this . deregisterKeyDownHandler ( this . handleKeyDown ) ; this . deregisterSurfaceClickHandler ( this . handleSurfaceClick ) ; } open ( ) { this . foundation . open ( ) ; } param reason close ( reason = '' ) { this . foundation . close ( reason ) ; } override getDefaultFoundation ( ) { const adapter = { addClass : ( className ) => { this . root . classList . add ( className ) ; } , announce : ( ) => { this . announce ( this . labelEl ) ; } , notifyClosed : ( reason ) => this . emit < MDCSnackbarCloseEventDetail > ( CLOSED_EVENT , reason ? { reason } : { } ) , notifyClosing : ( reason ) => this . emit < MDCSnackbarCloseEventDetail > ( CLOSING_EVENT , reason ? { reason } : { } ) , notifyOpened : ( ) => this . emit ( OPENED_EVENT , { } ) , notifyOpening : ( ) => this . emit ( OPENING_EVENT , { } ) , removeClass : ( className ) => this . root . classList . remove ( className ) , } ; return new MDCSnackbarFoundation ( adapter ) ; } get timeoutMs ( ) : number { return this . foundation . getTimeoutMs ( ) ; } set timeoutMs ( timeoutMs ) { this . foundation . setTimeoutMs ( timeoutMs ) ; } get closeOnEscape ( ) : boolean { return this . foundation . getCloseOnEscape ( ) ; } set closeOnEscape ( closeOnEscape ) { this . foundation . setCloseOnEscape ( closeOnEscape ) ; } get isOpen ( ) : boolean { return this . foundation . isOpen ( ) ; } get labelText ( ) : string { return this . labelEl . textContent ! ; } set labelText ( labelText ) { this . labelEl . textContent = labelText ; } get actionButtonText ( ) : string { return this . actionEl . textContent ! ; } set actionButtonText ( actionButtonText ) { this . actionEl . textContent = actionButtonText ; } private registerKeyDownHandler ( handler ) { this . listen ( 'keydown' , handler ) ; } private deregisterKeyDownHandler ( handler ) { this . unlisten ( 'keydown' , handler ) ; } private registerSurfaceClickHandler ( handler ) { this . surfaceEl . addEventListener ( 'click' , handler as EventListener ) ; } private deregisterSurfaceClickHandler ( handler ) { this . surfaceEl . removeEventListener ( 'click' , handler as EventListener ) ; } private isActionButton ( target ) { return Boolean ( closest ( target , ACTION_SELECTOR ) ) ; } private isActionIcon ( target ) { return Boolean ( closest ( target , DISMISS_SELECTOR ) ) ; } }