/* The fullscreenelement attribute can be used to say which element to make fullscreen. If none, the button will look for the closest media-container element to the media. If none, the button will make the media fullscreen. */ 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, getStringAttr, setBooleanAttr, setStringAttr, } from './utils/element-utils.js'; const enterFullscreenIcon = ``; const exitFullscreenIcon = ``; function getSlotTemplateHTML(_attrs: Record) { return /*html*/ ` ${enterFullscreenIcon} ${exitFullscreenIcon} `; } function getTooltipContentHTML() { return /*html*/ ` ${t('Enter fullscreen mode')} ${t('Exit fullscreen mode')} `; } const updateAriaLabel = (el: MediaFullscreenButton) => { const label = el.mediaIsFullscreen ? t('exit fullscreen mode') : t('enter fullscreen mode'); el.setAttribute('aria-label', label); }; /** * @slot enter - An element shown when the media is not in fullscreen and pressing the button will trigger entering fullscreen. * @slot exit - An element shown when the media is in fullscreen and pressing the button will trigger exiting fullscreen. * @slot icon - An element for representing enter and exit states in a single icon * * @attr {(unavailable|unsupported)} mediafullscreenunavailable - (read-only) Set if fullscreen is unavailable. * @attr {boolean} mediaisfullscreen - (read-only) Present if the media is fullscreen. * * @cssproperty [--media-fullscreen-button-display = inline-flex] - `display` property of button. */ class MediaFullscreenButton extends MediaChromeButton { static getSlotTemplateHTML = getSlotTemplateHTML; static getTooltipContentHTML = getTooltipContentHTML; static get observedAttributes(): string[] { return [ ...super.observedAttributes, MediaUIAttributes.MEDIA_IS_FULLSCREEN, MediaUIAttributes.MEDIA_FULLSCREEN_UNAVAILABLE, ]; } #lastActionEvent: Event | null = null; 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_IS_FULLSCREEN) { updateAriaLabel(this); } } /** * @type {string | undefined} Fullscreen unavailability state */ get mediaFullscreenUnavailable(): string | undefined { return getStringAttr(this, MediaUIAttributes.MEDIA_FULLSCREEN_UNAVAILABLE); } set mediaFullscreenUnavailable(value: string | undefined) { setStringAttr(this, MediaUIAttributes.MEDIA_FULLSCREEN_UNAVAILABLE, value); } /** * @type {boolean} Whether fullscreen is available */ get mediaIsFullscreen(): boolean { return getBooleanAttr(this, MediaUIAttributes.MEDIA_IS_FULLSCREEN); } set mediaIsFullscreen(value: boolean) { setBooleanAttr(this, MediaUIAttributes.MEDIA_IS_FULLSCREEN, value); } handleClick(e: Event): void { this.#lastActionEvent = e; const isPointerEvent = this.#lastActionEvent instanceof PointerEvent; const event = this.mediaIsFullscreen ? new globalThis.CustomEvent( MediaUIEvents.MEDIA_EXIT_FULLSCREEN_REQUEST, { composed: true, bubbles: true } ) : new globalThis.CustomEvent( MediaUIEvents.MEDIA_ENTER_FULLSCREEN_REQUEST, { composed: true, bubbles: true, detail: isPointerEvent } ); this.dispatchEvent(event); } } if (!globalThis.customElements.get('media-fullscreen-button')) { globalThis.customElements.define( 'media-fullscreen-button', MediaFullscreenButton ); } export default MediaFullscreenButton;