UNPKG

media-chrome

Version:

Custom elements (web components) for making audio and video player controls that look great in your website or app.

109 lines (105 loc) 3.7 kB
import { MediaChromeButton } from "./media-chrome-button.js"; import { globalThis, document } 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 = `<svg aria-hidden="true" viewBox="0 0 26 24"> <path d="M16 3v2.5h3.5V9H22V3h-6ZM4 9h2.5V5.5H10V3H4v6Zm15.5 9.5H16V21h6v-6h-2.5v3.5ZM6.5 15H4v6h6v-2.5H6.5V15Z"/> </svg>`; const exitFullscreenIcon = `<svg aria-hidden="true" viewBox="0 0 26 24"> <path d="M18.5 6.5V3H16v6h6V6.5h-3.5ZM16 21h2.5v-3.5H22V15h-6v6ZM4 17.5h3.5V21H10v-6H4v2.5Zm3.5-11H4V9h6V3H7.5v3.5Z"/> </svg>`; const slotTemplate = document.createElement("template"); slotTemplate.innerHTML = /*html*/ ` <style> :host([${MediaUIAttributes.MEDIA_IS_FULLSCREEN}]) slot[name=icon] slot:not([name=exit]) { display: none !important; } ${/* Double negative, but safer if display doesn't equal 'block' */ ""} :host(:not([${MediaUIAttributes.MEDIA_IS_FULLSCREEN}])) slot[name=icon] slot:not([name=enter]) { display: none !important; } :host([${MediaUIAttributes.MEDIA_IS_FULLSCREEN}]) slot[name=tooltip-enter], :host(:not([${MediaUIAttributes.MEDIA_IS_FULLSCREEN}])) slot[name=tooltip-exit] { display: none; } </style> <slot name="icon"> <slot name="enter">${enterFullscreenIcon}</slot> <slot name="exit">${exitFullscreenIcon}</slot> </slot> `; const tooltipContent = ( /*html*/ ` <slot name="tooltip-enter">${t("Enter fullscreen mode")}</slot> <slot name="tooltip-exit">${t("Exit fullscreen mode")}</slot> ` ); const updateAriaLabel = (el) => { const label = el.mediaIsFullscreen ? t("exit fullscreen mode") : t("enter fullscreen mode"); el.setAttribute("aria-label", label); }; class MediaFullscreenButton extends MediaChromeButton { static get observedAttributes() { return [ ...super.observedAttributes, MediaUIAttributes.MEDIA_IS_FULLSCREEN, MediaUIAttributes.MEDIA_FULLSCREEN_UNAVAILABLE ]; } constructor(options = {}) { super({ slotTemplate, tooltipContent, ...options }); } connectedCallback() { super.connectedCallback(); updateAriaLabel(this); } attributeChangedCallback(attrName, oldValue, newValue) { super.attributeChangedCallback(attrName, oldValue, newValue); if (attrName === MediaUIAttributes.MEDIA_IS_FULLSCREEN) { updateAriaLabel(this); } } /** * @type {string | undefined} Fullscreen unavailability state */ get mediaFullscreenUnavailable() { return getStringAttr(this, MediaUIAttributes.MEDIA_FULLSCREEN_UNAVAILABLE); } set mediaFullscreenUnavailable(value) { setStringAttr(this, MediaUIAttributes.MEDIA_FULLSCREEN_UNAVAILABLE, value); } /** * @type {boolean} Whether fullscreen is available */ get mediaIsFullscreen() { return getBooleanAttr(this, MediaUIAttributes.MEDIA_IS_FULLSCREEN); } set mediaIsFullscreen(value) { setBooleanAttr(this, MediaUIAttributes.MEDIA_IS_FULLSCREEN, value); } handleClick() { const eventName = this.mediaIsFullscreen ? MediaUIEvents.MEDIA_EXIT_FULLSCREEN_REQUEST : MediaUIEvents.MEDIA_ENTER_FULLSCREEN_REQUEST; this.dispatchEvent( new globalThis.CustomEvent(eventName, { composed: true, bubbles: true }) ); } } if (!globalThis.customElements.get("media-fullscreen-button")) { globalThis.customElements.define( "media-fullscreen-button", MediaFullscreenButton ); } var media_fullscreen_button_default = MediaFullscreenButton; export { media_fullscreen_button_default as default };