UNPKG

@shoelace-style/shoelace

Version:

A forward-thinking library of web components.

400 lines (393 loc) 11.9 kB
import { drawer_styles_default } from "./chunk.7KZHSE62.js"; import { Modal } from "./chunk.CUKKX6LO.js"; import { lockBodyScrolling, unlockBodyScrolling } from "./chunk.RWUUFNUL.js"; import { SlIconButton } from "./chunk.MXRX25LY.js"; import { getAnimation, setDefaultAnimation } from "./chunk.EJDHS3MU.js"; import { waitForEvent } from "./chunk.B4BZKR24.js"; import { animateTo, stopAnimations } from "./chunk.RV6ECJES.js"; import { LocalizeController } from "./chunk.NH3SRVOC.js"; import { o } from "./chunk.2URMUHDY.js"; import { HasSlotController } from "./chunk.NYIIDP5N.js"; import { e } from "./chunk.UZVKBFXH.js"; import { watch } from "./chunk.SJGTYGCD.js"; import { component_styles_default } from "./chunk.K23QWHWK.js"; import { ShoelaceElement, e as e2, n } from "./chunk.QAHCGRIS.js"; import { x } from "./chunk.CXZZ2LVK.js"; import { __decorateClass } from "./chunk.625AWUY7.js"; // src/internal/string.ts function uppercaseFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1); } // src/components/drawer/drawer.component.ts var SlDrawer = class extends ShoelaceElement { constructor() { super(...arguments); this.hasSlotController = new HasSlotController(this, "footer"); this.localize = new LocalizeController(this); this.modal = new Modal(this); this.open = false; this.label = ""; this.placement = "end"; this.contained = false; this.noHeader = false; this.handleDocumentKeyDown = (event) => { if (this.contained) { return; } if (event.key === "Escape" && this.modal.isActive() && this.open) { event.stopImmediatePropagation(); this.requestClose("keyboard"); } }; } firstUpdated() { this.drawer.hidden = !this.open; if (this.open) { this.addOpenListeners(); if (!this.contained) { this.modal.activate(); lockBodyScrolling(this); } } } disconnectedCallback() { var _a; super.disconnectedCallback(); unlockBodyScrolling(this); (_a = this.closeWatcher) == null ? void 0 : _a.destroy(); } requestClose(source) { const slRequestClose = this.emit("sl-request-close", { cancelable: true, detail: { source } }); if (slRequestClose.defaultPrevented) { const animation = getAnimation(this, "drawer.denyClose", { dir: this.localize.dir() }); animateTo(this.panel, animation.keyframes, animation.options); return; } this.hide(); } addOpenListeners() { var _a; if ("CloseWatcher" in window) { (_a = this.closeWatcher) == null ? void 0 : _a.destroy(); if (!this.contained) { this.closeWatcher = new CloseWatcher(); this.closeWatcher.onclose = () => this.requestClose("keyboard"); } } else { document.addEventListener("keydown", this.handleDocumentKeyDown); } } removeOpenListeners() { var _a; document.removeEventListener("keydown", this.handleDocumentKeyDown); (_a = this.closeWatcher) == null ? void 0 : _a.destroy(); } async handleOpenChange() { if (this.open) { this.emit("sl-show"); this.addOpenListeners(); this.originalTrigger = document.activeElement; if (!this.contained) { this.modal.activate(); lockBodyScrolling(this); } const autoFocusTarget = this.querySelector("[autofocus]"); if (autoFocusTarget) { autoFocusTarget.removeAttribute("autofocus"); } await Promise.all([stopAnimations(this.drawer), stopAnimations(this.overlay)]); this.drawer.hidden = false; requestAnimationFrame(() => { const slInitialFocus = this.emit("sl-initial-focus", { cancelable: true }); if (!slInitialFocus.defaultPrevented) { if (autoFocusTarget) { autoFocusTarget.focus({ preventScroll: true }); } else { this.panel.focus({ preventScroll: true }); } } if (autoFocusTarget) { autoFocusTarget.setAttribute("autofocus", ""); } }); const panelAnimation = getAnimation(this, `drawer.show${uppercaseFirstLetter(this.placement)}`, { dir: this.localize.dir() }); const overlayAnimation = getAnimation(this, "drawer.overlay.show", { dir: this.localize.dir() }); await Promise.all([ animateTo(this.panel, panelAnimation.keyframes, panelAnimation.options), animateTo(this.overlay, overlayAnimation.keyframes, overlayAnimation.options) ]); this.emit("sl-after-show"); } else { this.emit("sl-hide"); this.removeOpenListeners(); if (!this.contained) { this.modal.deactivate(); unlockBodyScrolling(this); } await Promise.all([stopAnimations(this.drawer), stopAnimations(this.overlay)]); const panelAnimation = getAnimation(this, `drawer.hide${uppercaseFirstLetter(this.placement)}`, { dir: this.localize.dir() }); const overlayAnimation = getAnimation(this, "drawer.overlay.hide", { dir: this.localize.dir() }); await Promise.all([ animateTo(this.overlay, overlayAnimation.keyframes, overlayAnimation.options).then(() => { this.overlay.hidden = true; }), animateTo(this.panel, panelAnimation.keyframes, panelAnimation.options).then(() => { this.panel.hidden = true; }) ]); this.drawer.hidden = true; this.overlay.hidden = false; this.panel.hidden = false; const trigger = this.originalTrigger; if (typeof (trigger == null ? void 0 : trigger.focus) === "function") { setTimeout(() => trigger.focus()); } this.emit("sl-after-hide"); } } handleNoModalChange() { if (this.open && !this.contained) { this.modal.activate(); lockBodyScrolling(this); } if (this.open && this.contained) { this.modal.deactivate(); unlockBodyScrolling(this); } } /** Shows the drawer. */ async show() { if (this.open) { return void 0; } this.open = true; return waitForEvent(this, "sl-after-show"); } /** Hides the drawer */ async hide() { if (!this.open) { return void 0; } this.open = false; return waitForEvent(this, "sl-after-hide"); } render() { return x` <div part="base" class=${e({ drawer: true, "drawer--open": this.open, "drawer--top": this.placement === "top", "drawer--end": this.placement === "end", "drawer--bottom": this.placement === "bottom", "drawer--start": this.placement === "start", "drawer--contained": this.contained, "drawer--fixed": !this.contained, "drawer--rtl": this.localize.dir() === "rtl", "drawer--has-footer": this.hasSlotController.test("footer") })} > <div part="overlay" class="drawer__overlay" @click=${() => this.requestClose("overlay")} tabindex="-1"></div> <div part="panel" class="drawer__panel" role="dialog" aria-modal="true" aria-hidden=${this.open ? "false" : "true"} aria-label=${o(this.noHeader ? this.label : void 0)} aria-labelledby=${o(!this.noHeader ? "title" : void 0)} tabindex="0" > ${!this.noHeader ? x` <header part="header" class="drawer__header"> <h2 part="title" class="drawer__title" id="title"> <!-- If there's no label, use an invisible character to prevent the header from collapsing --> <slot name="label"> ${this.label.length > 0 ? this.label : String.fromCharCode(65279)} </slot> </h2> <div part="header-actions" class="drawer__header-actions"> <slot name="header-actions"></slot> <sl-icon-button part="close-button" exportparts="base:close-button__base" class="drawer__close" name="x-lg" label=${this.localize.term("close")} library="system" @click=${() => this.requestClose("close-button")} ></sl-icon-button> </div> </header> ` : ""} <slot part="body" class="drawer__body"></slot> <footer part="footer" class="drawer__footer"> <slot name="footer"></slot> </footer> </div> </div> `; } }; SlDrawer.styles = [component_styles_default, drawer_styles_default]; SlDrawer.dependencies = { "sl-icon-button": SlIconButton }; __decorateClass([ e2(".drawer") ], SlDrawer.prototype, "drawer", 2); __decorateClass([ e2(".drawer__panel") ], SlDrawer.prototype, "panel", 2); __decorateClass([ e2(".drawer__overlay") ], SlDrawer.prototype, "overlay", 2); __decorateClass([ n({ type: Boolean, reflect: true }) ], SlDrawer.prototype, "open", 2); __decorateClass([ n({ reflect: true }) ], SlDrawer.prototype, "label", 2); __decorateClass([ n({ reflect: true }) ], SlDrawer.prototype, "placement", 2); __decorateClass([ n({ type: Boolean, reflect: true }) ], SlDrawer.prototype, "contained", 2); __decorateClass([ n({ attribute: "no-header", type: Boolean, reflect: true }) ], SlDrawer.prototype, "noHeader", 2); __decorateClass([ watch("open", { waitUntilFirstUpdate: true }) ], SlDrawer.prototype, "handleOpenChange", 1); __decorateClass([ watch("contained", { waitUntilFirstUpdate: true }) ], SlDrawer.prototype, "handleNoModalChange", 1); setDefaultAnimation("drawer.showTop", { keyframes: [ { opacity: 0, translate: "0 -100%" }, { opacity: 1, translate: "0 0" } ], options: { duration: 250, easing: "ease" } }); setDefaultAnimation("drawer.hideTop", { keyframes: [ { opacity: 1, translate: "0 0" }, { opacity: 0, translate: "0 -100%" } ], options: { duration: 250, easing: "ease" } }); setDefaultAnimation("drawer.showEnd", { keyframes: [ { opacity: 0, translate: "100%" }, { opacity: 1, translate: "0" } ], rtlKeyframes: [ { opacity: 0, translate: "-100%" }, { opacity: 1, translate: "0" } ], options: { duration: 250, easing: "ease" } }); setDefaultAnimation("drawer.hideEnd", { keyframes: [ { opacity: 1, translate: "0" }, { opacity: 0, translate: "100%" } ], rtlKeyframes: [ { opacity: 1, translate: "0" }, { opacity: 0, translate: "-100%" } ], options: { duration: 250, easing: "ease" } }); setDefaultAnimation("drawer.showBottom", { keyframes: [ { opacity: 0, translate: "0 100%" }, { opacity: 1, translate: "0 0" } ], options: { duration: 250, easing: "ease" } }); setDefaultAnimation("drawer.hideBottom", { keyframes: [ { opacity: 1, translate: "0 0" }, { opacity: 0, translate: "0 100%" } ], options: { duration: 250, easing: "ease" } }); setDefaultAnimation("drawer.showStart", { keyframes: [ { opacity: 0, translate: "-100%" }, { opacity: 1, translate: "0" } ], rtlKeyframes: [ { opacity: 0, translate: "100%" }, { opacity: 1, translate: "0" } ], options: { duration: 250, easing: "ease" } }); setDefaultAnimation("drawer.hideStart", { keyframes: [ { opacity: 1, translate: "0" }, { opacity: 0, translate: "-100%" } ], rtlKeyframes: [ { opacity: 1, translate: "0" }, { opacity: 0, translate: "100%" } ], options: { duration: 250, easing: "ease" } }); setDefaultAnimation("drawer.denyClose", { keyframes: [{ scale: 1 }, { scale: 1.01 }, { scale: 1 }], options: { duration: 250 } }); setDefaultAnimation("drawer.overlay.show", { keyframes: [{ opacity: 0 }, { opacity: 1 }], options: { duration: 250 } }); setDefaultAnimation("drawer.overlay.hide", { keyframes: [{ opacity: 1 }, { opacity: 0 }], options: { duration: 250 } }); export { SlDrawer };