UNPKG

@postnord/web-components

Version:

PostNord Web Components

399 lines (398 loc) 15.4 kB
/*! * Built with Stencil * By PostNord. */ import { h, Host, forceUpdate } from "@stencil/core"; import { awaitTopbar, en, reduceMotion } from "../../../index"; import { close as closeIcon } from "pn-design-assets/pn-assets/icons.js"; import { translations } from "./translation"; /** * Present content in a modal that overlays everything on the current page. * Use the prop `open` to toggle the visiblity. * Use the event `modalToggle` to determine when the modal is being closed/opened by the user. * * @slot - The default slot, will be placed in the middle content area. * @slot header - Place the title + lead text in the header. * @slot buttons - Place buttons inside the modal at the bottom. */ export class PnModal { constructor() { this.isClosing = false; this.isOpening = false; this.label = undefined; this.helpertext = undefined; this.language = null; this.open = false; this.persistent = false; this.hideClose = false; this.allowOverflow = false; this.sheet = false; this.maxWidth = null; } mo; standardAnimationDuration = 400; animationDuration = this.standardAnimationDuration; modalTimeout; modalElement; hostElement; handleOpen() { if (this.open) this.openModal(); else { this.closeModal(); /** In the next update, we can remove this one so you can finally stack modals. */ this.close.emit(true); } this.handleOverflow(); clearTimeout(this.modalTimeout); this.isClosing = !this.open && true; if (reduceMotion()) this.animationDuration = 0; else this.animationDuration = this.standardAnimationDuration; this.modalTimeout = setTimeout(() => { this.isClosing = false; this.modalVisiblity.emit({ visible: this.open }); }, this.animationDuration); } handleMaxWidth() { const width = this.maxWidth || '45em'; this.modalElement.style.setProperty('--pn-modal-max-width', width); } /** This event is fired when the modal is toggled. */ modalToggle; /** This event is dispatched after the opening/closing animation has finished playing. */ modalVisiblity; /** * Event fired when the modal is closed, either by clicking on the dark background or by clicking on the close button. * @deprecated Use the new `modalToggle` event instead. */ close; connectedCallback() { this.mo = new MutationObserver(() => forceUpdate(this.hostElement)); this.mo.observe(this.hostElement, { childList: true, subtree: true }); } disconnectedCallback() { if (this.mo) this.mo.disconnect(); } async componentWillLoad() { if (this.language === null) await awaitTopbar(this.hostElement); } componentDidLoad() { this.handleMaxWidth(); if (this.open) this.openModal(); } translate(prop) { return translations?.[prop]?.[this.language || en] || prop; } handleOverflow() { if (!this.allowOverflow) return; const overflow = this.modalElement.scrollHeight > innerHeight * 0.85 ? 'hidden auto' : 'unset'; this.modalElement.style.setProperty('--pn-modal-overflow', overflow); } isSameModal(target) { return this.modalElement.isSameNode(target); } handleKeyboard(event) { if (event.key !== 'Escape') return; event.preventDefault(); event.stopImmediatePropagation(); if (!this.persistent) this.setModalClose(); } clickModalBackground(event) { const { clientY, clientX } = event; const notInsideAnyModal = event.target.localName !== this.modalElement.localName; const notThisModal = !this.isSameModal(event.target); if (notInsideAnyModal || notThisModal) return; const { top, left, height, width } = this.modalElement.getBoundingClientRect(); const isInModal = top <= clientY && clientY <= top + height && left <= clientX && clientX <= left + width; if (!isInModal && !this.persistent) this.toggleOpen(false); } setModalClose() { this.toggleOpen(false); } closeModal() { this.modalElement.close(); } openModal() { this.modalElement.showModal(); } toggleOpen(state) { this.open = state ?? !this.open; } render() { return (h(Host, { key: '6bf631cc88584b24782904a9918091736e27c3d6' }, h("dialog", { key: '543806f18626bd7f8f6983a5c3769f8bf0422544', class: "pn-modal", "data-closing": this.isClosing, "data-sheet": this.sheet, onClose: () => this.setModalClose(), onCancel: () => this.setModalClose(), onToggle: () => this.modalToggle.emit({ open: this.open }), onKeyDown: event => this.handleKeyboard(event), onClick: event => this.clickModalBackground(event), ref: el => (this.modalElement = el) }, !this.hideClose && (h("pn-button", { key: '60350dfcac19aa570b78e4fb82ca2a0a288de1b7', small: true, class: "pn-modal-close-button", icon: closeIcon, iconOnly: true, arialabel: this.translate('CLOSE_MODAL'), appearance: "light", variant: "borderless", type: "button", onPnClick: () => this.setModalClose() })), h("header", { key: 'ba3d857e5f0b57c9567b045843f3b3a1aee9f06d', class: "pn-modal-header" }, this.label && h("h2", { key: '524b36cbeb3f1e0274fb29a3656638c4c221cec9', class: "pn-modal-label" }, this.label), this.helpertext && h("p", { key: '330777c3c194177f7fc811eb45f2ee3a585894fc', class: "pn-modal-text" }, this.helpertext), h("slot", { key: '62dbf64100d4e62974a18752423a781db737333f', name: "header" })), h("section", { key: '57618a994095a9f24c7a142d86fa788cca531062', class: "pn-modal-content" }, h("slot", { key: '14f533135af35a88d1fb03b8e57cd133d3981629' })), h("nav", { key: 'd355d6c0024893a26b1e17bf187a7d0e3142b766', class: "pn-modal-buttons" }, h("slot", { key: '20020f7fd89b706bfaf5825090d6a0470e91b577', name: "buttons" }))))); } static get is() { return "pn-modal"; } static get originalStyleUrls() { return { "$": ["pn-modal.scss"] }; } static get styleUrls() { return { "$": ["pn-modal.css"] }; } static get properties() { return { "label": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Set a label for the modal." }, "attribute": "label", "reflect": false }, "helpertext": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Set a descriptive text for the modal." }, "attribute": "helpertext", "reflect": false }, "language": { "type": "string", "mutable": false, "complexType": { "original": "PnLanguages", "resolved": "\"\" | \"da\" | \"en\" | \"fi\" | \"no\" | \"sv\"", "references": { "PnLanguages": { "location": "import", "path": "@/index", "id": "src/index.ts::PnLanguages" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Set the language." }, "attribute": "language", "reflect": false, "defaultValue": "null" }, "open": { "type": "boolean", "mutable": true, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "Features" }], "text": "Bind to this property if you want to control the visibility of the modal from your own data." }, "attribute": "open", "reflect": true, "defaultValue": "false" }, "persistent": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "Features" }], "text": "Prevent users from closing the modal by clicking on the backdrop or the `ESC` key." }, "attribute": "persistent", "reflect": false, "defaultValue": "false" }, "hideClose": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "Features" }], "text": "Hide the close button. If you disable this, make sure you build your own cancel button." }, "attribute": "hide-close", "reflect": false, "defaultValue": "false" }, "allowOverflow": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Allow overflow if possible. When the modal opens, it will check scrollHeight > offsetHeight.\nDo not use if you have a lot of conditional rendering inside the modal." }, "attribute": "allow-overflow", "reflect": false, "defaultValue": "false" }, "sheet": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "Visual" }], "text": "Use the `sheet` visual. Aligns the modal to the right." }, "attribute": "sheet", "reflect": false, "defaultValue": "false" }, "maxWidth": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "category", "text": "Visual" }], "text": "Define your own max width of the modal. Default is `45em`." }, "attribute": "max-width", "reflect": false, "defaultValue": "null" } }; } static get states() { return { "isClosing": {}, "isOpening": {} }; } static get events() { return [{ "method": "modalToggle", "name": "modalToggle", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "This event is fired when the modal is toggled." }, "complexType": { "original": "{ open: boolean }", "resolved": "{ open: boolean; }", "references": {} } }, { "method": "modalVisiblity", "name": "modalVisiblity", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [], "text": "This event is dispatched after the opening/closing animation has finished playing." }, "complexType": { "original": "{ visible: boolean }", "resolved": "{ visible: boolean; }", "references": {} } }, { "method": "close", "name": "close", "bubbles": true, "cancelable": true, "composed": true, "docs": { "tags": [{ "name": "deprecated", "text": "Use the new `modalToggle` event instead." }], "text": "Event fired when the modal is closed, either by clicking on the dark background or by clicking on the close button." }, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} } }]; } static get elementRef() { return "hostElement"; } static get watchers() { return [{ "propName": "open", "methodName": "handleOpen" }, { "propName": "maxWidth", "methodName": "handleMaxWidth" }]; } } //# sourceMappingURL=pn-modal.js.map