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 { getStringAttr, setStringAttr } from './utils/element-utils.js';
const offIcon = ``;
const lowIcon = ``;
const highIcon = ``;
function getSlotTemplateHTML(_attrs: Record) {
return /*html*/ `
${offIcon}
${lowIcon}
${lowIcon}
${highIcon}
`;
}
function getTooltipContentHTML() {
return /*html*/ `
${t('Mute')}
${t('Unmute')}
`;
}
const updateAriaLabel = (el: MediaMuteButton) => {
const muted = el.mediaVolumeLevel === 'off';
const label = muted ? t('unmute') : t('mute');
el.setAttribute('aria-label', label);
};
/**
* @slot off - An element shown when the media is muted or the media’s volume is 0.
* @slot low - An element shown when the media’s volume is “low” (less than 50% / 0.5).
* @slot medium - An element shown when the media’s volume is “medium” (between 50% / 0.5 and 75% / 0.75).
* @slot high - An element shown when the media’s volume is “high” (75% / 0.75 or greater).
* @slot icon - An element for representing all states in a single icon
*
* @attr {string} mediavolumelevel - (read-only) Set to the media volume level.
*
* @cssproperty [--media-mute-button-display = inline-flex] - `display` property of button.
*/
class MediaMuteButton extends MediaChromeButton {
static getSlotTemplateHTML = getSlotTemplateHTML;
static getTooltipContentHTML = getTooltipContentHTML;
static get observedAttributes(): string[] {
return [...super.observedAttributes, MediaUIAttributes.MEDIA_VOLUME_LEVEL];
}
connectedCallback(): void {
super.connectedCallback();
updateAriaLabel(this);
}
attributeChangedCallback(
attrName: string,
oldValue: string | null,
newValue: string | null
): void {
super.attributeChangedCallback(attrName, oldValue, newValue);
if (attrName === MediaUIAttributes.MEDIA_VOLUME_LEVEL) {
updateAriaLabel(this);
}
}
/**
* @type {string | undefined}
*/
get mediaVolumeLevel(): string | undefined {
return getStringAttr(this, MediaUIAttributes.MEDIA_VOLUME_LEVEL);
}
set mediaVolumeLevel(value: string | undefined) {
setStringAttr(this, MediaUIAttributes.MEDIA_VOLUME_LEVEL, value);
}
handleClick(): void {
const eventName: string =
this.mediaVolumeLevel === 'off'
? MediaUIEvents.MEDIA_UNMUTE_REQUEST
: MediaUIEvents.MEDIA_MUTE_REQUEST;
this.dispatchEvent(
new globalThis.CustomEvent(eventName, { composed: true, bubbles: true })
);
}
}
if (!globalThis.customElements.get('media-mute-button')) {
globalThis.customElements.define('media-mute-button', MediaMuteButton);
}
export default MediaMuteButton;