license import { MDCFoundation } from '@material/base/foundation' ; import { MDCIconButtonToggleAdapter } from './adapter' ; import { cssClasses , strings } from './constants' ; export class MDCIconButtonToggleFoundation extends MDCFoundation < MDCIconButtonToggleAdapter > { /**
     * Whether the icon button has an aria label that changes depending on
     * toggled state.
     */ private hasToggledAriaLabel : boolean = false ; static override get cssClasses ( ) { return cssClasses ; } static override get strings ( ) { return strings ; } static override get defaultAdapter ( ) : MDCIconButtonToggleAdapter { return { addClass : ( ) => undefined , hasClass : ( ) => false , notifyChange : ( ) => undefined , removeClass : ( ) => undefined , getAttr : ( ) => null , setAttr : ( ) => undefined , } ; } constructor ( adapter ? ) { super ( { ... MDCIconButtonToggleFoundation . defaultAdapter , ... adapter } ) ; } override init ( ) { const ariaLabelOn = this . adapter . getAttr ( strings . DATA_ARIA_LABEL_ON ) ; const ariaLabelOff = this . adapter . getAttr ( strings . DATA_ARIA_LABEL_OFF ) ; if ( ariaLabelOn && ariaLabelOff ) { if ( this . adapter . getAttr ( strings . ARIA_PRESSED ) !== null ) { throw new Error ( 'MDCIconButtonToggleFoundation: Button should not set ' + '`aria-pressed` if it has a toggled aria label.' ) ; } this . hasToggledAriaLabel = true ; } else { this . adapter . setAttr ( strings . ARIA_PRESSED , String ( this . isOn ( ) ) ) ; } } handleClick ( ) { this . toggle ( ) ; this . adapter . notifyChange ( { isOn : this . isOn ( ) } ) ; } isOn ( ) { return this . adapter . hasClass ( cssClasses . ICON_BUTTON_ON ) ; } toggle ( isOn = ! this . isOn ( ) ) { if ( isOn ) { this . adapter . addClass ( cssClasses . ICON_BUTTON_ON ) ; } else { this . adapter . removeClass ( cssClasses . ICON_BUTTON_ON ) ; } if ( this . hasToggledAriaLabel ) { const ariaLabel = isOn ? this . adapter . getAttr ( strings . DATA_ARIA_LABEL_ON ) : this . adapter . getAttr ( strings . DATA_ARIA_LABEL_OFF ) ; this . adapter . setAttr ( strings . ARIA_LABEL , ariaLabel || '' ) ; } else { this . adapter . setAttr ( strings . ARIA_PRESSED , `${ isOn }` ) ; } } } export default MDCIconButtonToggleFoundation ;