import { MediaChromeButton } from './media-chrome-button.js'; import { globalThis } from './utils/server-safe-globals.js'; import { MediaUIEvents, MediaUIAttributes } from './constants.js'; import { t } from './utils/i18n.js'; import { getBooleanAttr, setBooleanAttr, getStringAttr, setStringAttr, } from './utils/element-utils.js'; const enterIcon = ``; const exitIcon = ``; function getSlotTemplateHTML(_attrs: Record) { return /*html*/ ` ${enterIcon} ${exitIcon} `; } function getTooltipContentHTML() { return /*html*/ ` ${t('Start casting')} ${t('Stop casting')} `; } const updateAriaLabel = (el: MediaCastButton) => { const label = el.mediaIsCasting ? t('stop casting') : t('start casting'); el.setAttribute('aria-label', label); }; /** * @slot enter - An element shown when the media is not in casting mode and pressing the button will open the Cast menu. * @slot exit - An element shown when the media is in casting mode and pressing the button will open the Cast menu. * @slot icon - An element for representing enter and exit states in a single icon * * @attr {(unavailable|unsupported)} mediacastunavailable - (read-only) Set if casting is unavailable. * @attr {boolean} mediaiscasting - (read-only) Present if the media is casting. * * @cssproperty [--media-cast-button-display = inline-flex] - `display` property of button. */ class MediaCastButton extends MediaChromeButton { static getSlotTemplateHTML = getSlotTemplateHTML; static getTooltipContentHTML = getTooltipContentHTML; static get observedAttributes() { return [ ...super.observedAttributes, MediaUIAttributes.MEDIA_IS_CASTING, MediaUIAttributes.MEDIA_CAST_UNAVAILABLE, ]; } connectedCallback(): void { super.connectedCallback(); updateAriaLabel(this); } attributeChangedCallback( attrName: string, oldValue: string | null, newValue: string | null ) { super.attributeChangedCallback(attrName, oldValue, newValue); if (attrName === MediaUIAttributes.MEDIA_IS_CASTING) { updateAriaLabel(this); } } /** * @type {boolean} Are we currently casting */ get mediaIsCasting(): boolean { return getBooleanAttr(this, MediaUIAttributes.MEDIA_IS_CASTING); } set mediaIsCasting(value: boolean) { setBooleanAttr(this, MediaUIAttributes.MEDIA_IS_CASTING, value); } /** * @type {string | undefined} Cast unavailability state */ get mediaCastUnavailable(): string | undefined { return getStringAttr(this, MediaUIAttributes.MEDIA_CAST_UNAVAILABLE); } set mediaCastUnavailable(value: string | undefined) { setStringAttr(this, MediaUIAttributes.MEDIA_CAST_UNAVAILABLE, value); } handleClick() { const eventName = this.mediaIsCasting ? MediaUIEvents.MEDIA_EXIT_CAST_REQUEST : MediaUIEvents.MEDIA_ENTER_CAST_REQUEST; this.dispatchEvent( new globalThis.CustomEvent(eventName, { composed: true, bubbles: true }) ); } } if (!globalThis.customElements.get('media-cast-button')) { globalThis.customElements.define('media-cast-button', MediaCastButton); } export default MediaCastButton;