license /**
 * Priorities for the announce function.
 */ export enum AnnouncerPriority { POLITE = 'polite' , ASSERTIVE = 'assertive' } /**
 * Options for the announce function.
 */ export interface AnnouncerMessageOptions { priority ? : AnnouncerPriority ; ownerDocument ? : Document ; } /**
 * Data attribute added to live region element.
 */ export const DATA_MDC_DOM_ANNOUNCE = 'data-mdc-dom-announce' ; /**
 * Announces the given message with optional priority, defaulting to "polite"
 */ export function announce ( message , options ? ) { Announcer . getInstance ( ) . say ( message , options ) ; } class Announcer { private static instance : Announcer ; private readonly liveRegions : Map < Document , Map < AnnouncerPriority , Element > > ; static getInstance ( ) { if ( ! Announcer . instance ) { Announcer . instance = new Announcer ( ) ; } return Announcer . instance ; } private constructor ( ) { this . liveRegions = new Map ( ) ; } say ( message , options ? ) { const priority = options ?. priority ?? AnnouncerPriority . POLITE ; const ownerDocument = options ?. ownerDocument ?? document ; const liveRegion = this . getLiveRegion ( priority , ownerDocument ) ; liveRegion . textContent = '' ; setTimeout ( ( ) => { liveRegion . textContent = message ; ownerDocument . addEventListener ( 'click' , clearLiveRegion ) ; } , 1 ) ; function clearLiveRegion ( ) { liveRegion . textContent = '' ; ownerDocument . removeEventListener ( 'click' , clearLiveRegion ) ; } } private getLiveRegion ( priority , ownerDocument ) { let documentLiveRegions = this . liveRegions . get ( ownerDocument ) ; if ( ! documentLiveRegions ) { documentLiveRegions = new Map ( ) ; this . liveRegions . set ( ownerDocument , documentLiveRegions ) ; } const existingLiveRegion = documentLiveRegions . get ( priority ) ; if ( existingLiveRegion && ownerDocument . body . contains ( existingLiveRegion as Node ) ) { return existingLiveRegion ; } const liveRegion = this . createLiveRegion ( priority , ownerDocument ) ; documentLiveRegions . set ( priority , liveRegion ) ; return liveRegion ; } private createLiveRegion ( priority , ownerDocument ) { const el = ownerDocument . createElement ( 'div' ) ; el . style . position = 'absolute' ; el . style . top = '-9999px' ; el . style . left = '-9999px' ; el . style . height = '1px' ; el . style . overflow = 'hidden' ; el . setAttribute ( 'aria-atomic' , 'true' ) ; el . setAttribute ( 'aria-live' , priority ) ; el . setAttribute ( DATA_MDC_DOM_ANNOUNCE , 'true' ) ; ownerDocument . body . appendChild ( el ) ; return el ; } }