123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767 |
- /*!
- * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
- * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
- * v1.0.0-beta.97
- */
- import { h, Host } from "@stencil/core";
- import { ensureId, focusElement, getSlotted, isCalciteFocusable } from "../../utils/dom";
- import { queryShadowRoot } from "@a11y/focus-trap/shadow";
- import { isFocusable, isHidden } from "@a11y/focus-trap/focusable";
- import { CSS, ICONS, SLOTS, TEXT } from "./resources";
- import { createObserver } from "../../utils/observers";
- import { connectConditionalSlotComponent, disconnectConditionalSlotComponent } from "../../utils/conditionalSlot";
- import { connectOpenCloseComponent, disconnectOpenCloseComponent } from "../../utils/openCloseComponent";
- const isFocusableExtended = (el) => {
- return isCalciteFocusable(el) || isFocusable(el);
- };
- const getFocusableElements = (el) => {
- return queryShadowRoot(el, isHidden, isFocusableExtended);
- };
- /**
- * @slot header - A slot for adding header text.
- * @slot content - A slot for adding the component's content.
- * @slot primary - A slot for adding a primary button.
- * @slot secondary - A slot for adding a secondary button.
- * @slot back - A slot for adding a back button.
- */
- export class Modal {
- constructor() {
- //--------------------------------------------------------------------------
- //
- // Properties
- //
- //--------------------------------------------------------------------------
- /**
- * When `true`, the component is active.
- *
- * @deprecated use `open` instead.
- */
- this.active = false;
- /** When `true`, displays and positions the component. */
- this.open = false;
- /** Passes a function to run before the component closes. */
- this.beforeClose = () => Promise.resolve();
- /** When `true`, disables the component's close button. */
- this.disableCloseButton = false;
- /** When `true`, disables the closing of the component when clicked outside. */
- this.disableOutsideClose = false;
- /** Accessible name for the component's close button. */
- this.intlClose = TEXT.close;
- /** When `true`, disables the default close on escape behavior. */
- this.disableEscape = false;
- /** Specifies the size of the component. */
- this.scale = "m";
- /** Specifies the width of the component. Can use scale sizes or pass a number (displays in pixels). */
- this.width = "m";
- /** Sets the background color of the component's content. */
- this.backgroundColor = "white";
- /**
- * When `true`, disables spacing to the content area slot.
- *
- * @deprecated Use `--calcite-modal-padding` CSS variable instead.
- */
- this.noPadding = false;
- //--------------------------------------------------------------------------
- //
- // Variables
- //
- //--------------------------------------------------------------------------
- this.hasFooter = true;
- /**
- * We use internal variable to make sure initially open modal can transition from closed state when rendered
- *
- * @private
- */
- this.isOpen = false;
- this.mutationObserver = createObserver("mutation", () => this.updateFooterVisibility());
- this.openTransitionProp = "opacity";
- //--------------------------------------------------------------------------
- //
- // Private Methods
- //
- //--------------------------------------------------------------------------
- this.setTransitionEl = (el) => {
- this.transitionEl = el;
- connectOpenCloseComponent(this);
- };
- this.openEnd = () => {
- this.setFocus();
- this.el.removeEventListener("calciteModalOpen", this.openEnd);
- };
- this.handleOutsideClose = () => {
- if (this.disableOutsideClose) {
- return;
- }
- this.close();
- };
- /** Close the modal, first running the `beforeClose` method */
- this.close = () => {
- return this.beforeClose(this.el).then(() => {
- this.open = false;
- this.isOpen = false;
- focusElement(this.previousActiveElement);
- this.removeOverflowHiddenClass();
- });
- };
- this.focusFirstElement = () => {
- focusElement(this.disableCloseButton ? getFocusableElements(this.el)[0] : this.closeButtonEl);
- };
- this.focusLastElement = () => {
- const focusableElements = getFocusableElements(this.el).filter((el) => !el.getAttribute("data-focus-fence"));
- if (focusableElements.length > 0) {
- focusElement(focusableElements[focusableElements.length - 1]);
- }
- else {
- focusElement(this.closeButtonEl);
- }
- };
- this.updateFooterVisibility = () => {
- this.hasFooter = !!getSlotted(this.el, [SLOTS.back, SLOTS.primary, SLOTS.secondary]);
- };
- }
- //--------------------------------------------------------------------------
- //
- // Lifecycle
- //
- //--------------------------------------------------------------------------
- componentWillLoad() {
- // when modal initially renders, if active was set we need to open as watcher doesn't fire
- if (this.open) {
- requestAnimationFrame(() => this.openModal());
- }
- }
- connectedCallback() {
- var _a;
- (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true, subtree: true });
- this.updateFooterVisibility();
- connectConditionalSlotComponent(this);
- connectOpenCloseComponent(this);
- if (this.open) {
- this.active = this.open;
- }
- if (this.active) {
- this.activeHandler(this.active);
- }
- }
- disconnectedCallback() {
- var _a;
- this.removeOverflowHiddenClass();
- (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
- disconnectConditionalSlotComponent(this);
- disconnectOpenCloseComponent(this);
- }
- render() {
- return (h(Host, { "aria-describedby": this.contentId, "aria-labelledby": this.titleId, "aria-modal": "true", role: "dialog" }, h("calcite-scrim", { class: CSS.scrim, onClick: this.handleOutsideClose }), this.renderStyle(), h("div", { class: {
- [CSS.modal]: true,
- [CSS.modalOpen]: this.isOpen
- }, ref: this.setTransitionEl }, h("div", { "data-focus-fence": true, onFocus: this.focusLastElement, tabindex: "0" }), h("div", { class: CSS.header }, this.renderCloseButton(), h("header", { class: CSS.title }, h("slot", { name: CSS.header }))), h("div", { class: {
- content: true,
- "content--spaced": !this.noPadding,
- "content--no-footer": !this.hasFooter
- }, ref: (el) => (this.modalContent = el) }, h("slot", { name: SLOTS.content })), this.renderFooter(), h("div", { "data-focus-fence": true, onFocus: this.focusFirstElement, tabindex: "0" }))));
- }
- renderFooter() {
- return this.hasFooter ? (h("div", { class: CSS.footer, key: "footer" }, h("span", { class: CSS.back }, h("slot", { name: SLOTS.back })), h("span", { class: CSS.secondary }, h("slot", { name: SLOTS.secondary })), h("span", { class: CSS.primary }, h("slot", { name: SLOTS.primary })))) : null;
- }
- renderCloseButton() {
- return !this.disableCloseButton ? (h("button", { "aria-label": this.intlClose, class: CSS.close, key: "button", onClick: this.close, ref: (el) => (this.closeButtonEl = el), title: this.intlClose }, h("calcite-icon", { icon: ICONS.close, scale: this.scale === "s" ? "s" : this.scale === "m" ? "m" : this.scale === "l" ? "l" : null }))) : null;
- }
- renderStyle() {
- const hasCustomWidth = !isNaN(parseInt(`${this.width}`));
- return hasCustomWidth ? (h("style", null, `
- .${CSS.modal} {
- max-width: ${this.width}px !important;
- }
- @media screen and (max-width: ${this.width}px) {
- .${CSS.modal} {
- height: 100% !important;
- max-height: 100% !important;
- width: 100% !important;
- max-width: 100% !important;
- margin: 0 !important;
- border-radius: 0 !important;
- }
- .content {
- flex: 1 1 auto !important;
- max-height: unset !important;
- }
- }
- `)) : null;
- }
- //--------------------------------------------------------------------------
- //
- // Event Listeners
- //
- //--------------------------------------------------------------------------
- handleEscape(event) {
- if (this.open && !this.disableEscape && event.key === "Escape" && !event.defaultPrevented) {
- this.close();
- event.preventDefault();
- }
- }
- //--------------------------------------------------------------------------
- //
- // Public Methods
- //
- //--------------------------------------------------------------------------
- /**
- * Focus the first interactive element.
- *
- * @param el
- * @deprecated use `setFocus` instead.
- */
- async focusElement(el) {
- if (el) {
- el.focus();
- }
- return this.setFocus();
- }
- /**
- * Sets focus on the component.
- *
- * By default, tries to focus on focusable content. If there is none, it will focus on the close button.
- * To focus on the close button, use the `close-button` focus ID.
- *
- * @param focusId
- */
- async setFocus(focusId) {
- const closeButton = this.closeButtonEl;
- return focusElement(focusId === "close-button" ? closeButton : getFocusableElements(this.el)[0] || closeButton);
- }
- /**
- * Sets the scroll top of the component's content.
- *
- * @param top
- * @param left
- */
- async scrollContent(top = 0, left = 0) {
- if (this.modalContent) {
- if (this.modalContent.scrollTo) {
- this.modalContent.scrollTo({ top, left, behavior: "smooth" });
- }
- else {
- this.modalContent.scrollTop = top;
- this.modalContent.scrollLeft = left;
- }
- }
- }
- onBeforeOpen() {
- this.transitionEl.classList.add(CSS.openingActive);
- this.calciteModalBeforeOpen.emit();
- }
- onOpen() {
- this.transitionEl.classList.remove(CSS.openingIdle, CSS.openingActive);
- this.calciteModalOpen.emit();
- }
- onBeforeClose() {
- this.transitionEl.classList.add(CSS.closingActive);
- this.calciteModalBeforeClose.emit();
- }
- onClose() {
- this.transitionEl.classList.remove(CSS.closingIdle, CSS.closingActive);
- this.calciteModalClose.emit();
- }
- activeHandler(value) {
- this.open = value;
- }
- async toggleModal(value) {
- var _a, _b;
- this.active = value;
- if (value) {
- (_a = this.transitionEl) === null || _a === void 0 ? void 0 : _a.classList.add(CSS.openingIdle);
- this.openModal();
- }
- else {
- (_b = this.transitionEl) === null || _b === void 0 ? void 0 : _b.classList.add(CSS.closingIdle);
- this.close();
- }
- }
- /** Open the modal */
- openModal() {
- this.previousActiveElement = document.activeElement;
- this.el.addEventListener("calciteModalOpen", this.openEnd);
- this.open = true;
- this.isOpen = true;
- const titleEl = getSlotted(this.el, SLOTS.header);
- const contentEl = getSlotted(this.el, SLOTS.content);
- this.titleId = ensureId(titleEl);
- this.contentId = ensureId(contentEl);
- document.documentElement.classList.add(CSS.overflowHidden);
- }
- removeOverflowHiddenClass() {
- document.documentElement.classList.remove(CSS.overflowHidden);
- }
- static get is() { return "calcite-modal"; }
- static get encapsulation() { return "shadow"; }
- static get originalStyleUrls() {
- return {
- "$": ["modal.scss"]
- };
- }
- static get styleUrls() {
- return {
- "$": ["modal.css"]
- };
- }
- static get properties() {
- return {
- "active": {
- "type": "boolean",
- "mutable": true,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [{
- "name": "deprecated",
- "text": "use `open` instead."
- }],
- "text": "When `true`, the component is active."
- },
- "attribute": "active",
- "reflect": true,
- "defaultValue": "false"
- },
- "open": {
- "type": "boolean",
- "mutable": true,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "When `true`, displays and positions the component."
- },
- "attribute": "open",
- "reflect": true,
- "defaultValue": "false"
- },
- "beforeClose": {
- "type": "unknown",
- "mutable": false,
- "complexType": {
- "original": "(el: HTMLElement) => Promise<void>",
- "resolved": "(el: HTMLElement) => Promise<void>",
- "references": {
- "HTMLElement": {
- "location": "global"
- },
- "Promise": {
- "location": "global"
- }
- }
- },
- "required": false,
- "optional": true,
- "docs": {
- "tags": [],
- "text": "Passes a function to run before the component closes."
- },
- "defaultValue": "() => Promise.resolve()"
- },
- "disableCloseButton": {
- "type": "boolean",
- "mutable": false,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "When `true`, disables the component's close button."
- },
- "attribute": "disable-close-button",
- "reflect": true,
- "defaultValue": "false"
- },
- "disableOutsideClose": {
- "type": "boolean",
- "mutable": false,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "When `true`, disables the closing of the component when clicked outside."
- },
- "attribute": "disable-outside-close",
- "reflect": true,
- "defaultValue": "false"
- },
- "intlClose": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "string",
- "resolved": "string",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "Accessible name for the component's close button."
- },
- "attribute": "intl-close",
- "reflect": false,
- "defaultValue": "TEXT.close"
- },
- "docked": {
- "type": "boolean",
- "mutable": false,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "When `true`, prevents the component from expanding to the entire screen on mobile devices."
- },
- "attribute": "docked",
- "reflect": true
- },
- "disableEscape": {
- "type": "boolean",
- "mutable": false,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "When `true`, disables the default close on escape behavior."
- },
- "attribute": "disable-escape",
- "reflect": true,
- "defaultValue": "false"
- },
- "scale": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "Scale",
- "resolved": "\"l\" | \"m\" | \"s\"",
- "references": {
- "Scale": {
- "location": "import",
- "path": "../interfaces"
- }
- }
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "Specifies the size of the component."
- },
- "attribute": "scale",
- "reflect": true,
- "defaultValue": "\"m\""
- },
- "width": {
- "type": "any",
- "mutable": false,
- "complexType": {
- "original": "Scale | number",
- "resolved": "\"l\" | \"m\" | \"s\" | number",
- "references": {
- "Scale": {
- "location": "import",
- "path": "../interfaces"
- }
- }
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "Specifies the width of the component. Can use scale sizes or pass a number (displays in pixels)."
- },
- "attribute": "width",
- "reflect": true,
- "defaultValue": "\"m\""
- },
- "fullscreen": {
- "type": "boolean",
- "mutable": false,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "Sets the component to always be fullscreen (overrides `width`)."
- },
- "attribute": "fullscreen",
- "reflect": true
- },
- "color": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "\"red\" | \"blue\"",
- "resolved": "\"blue\" | \"red\"",
- "references": {}
- },
- "required": false,
- "optional": true,
- "docs": {
- "tags": [],
- "text": "Adds a color bar to the top of component for visual impact.\nUse color to add importance to destructive or workflow dialogs."
- },
- "attribute": "color",
- "reflect": true
- },
- "backgroundColor": {
- "type": "string",
- "mutable": false,
- "complexType": {
- "original": "ModalBackgroundColor",
- "resolved": "\"grey\" | \"white\"",
- "references": {
- "ModalBackgroundColor": {
- "location": "import",
- "path": "./interfaces"
- }
- }
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [],
- "text": "Sets the background color of the component's content."
- },
- "attribute": "background-color",
- "reflect": true,
- "defaultValue": "\"white\""
- },
- "noPadding": {
- "type": "boolean",
- "mutable": false,
- "complexType": {
- "original": "boolean",
- "resolved": "boolean",
- "references": {}
- },
- "required": false,
- "optional": false,
- "docs": {
- "tags": [{
- "name": "deprecated",
- "text": "Use `--calcite-modal-padding` CSS variable instead."
- }],
- "text": "When `true`, disables spacing to the content area slot."
- },
- "attribute": "no-padding",
- "reflect": true,
- "defaultValue": "false"
- }
- };
- }
- static get states() {
- return {
- "hasFooter": {},
- "isOpen": {}
- };
- }
- static get events() {
- return [{
- "method": "calciteModalBeforeClose",
- "name": "calciteModalBeforeClose",
- "bubbles": true,
- "cancelable": false,
- "composed": true,
- "docs": {
- "tags": [],
- "text": "Fires when the component is requested to be closed and before the closing transition begins."
- },
- "complexType": {
- "original": "void",
- "resolved": "void",
- "references": {}
- }
- }, {
- "method": "calciteModalClose",
- "name": "calciteModalClose",
- "bubbles": true,
- "cancelable": false,
- "composed": true,
- "docs": {
- "tags": [],
- "text": "Fires when the component is closed and animation is complete."
- },
- "complexType": {
- "original": "void",
- "resolved": "void",
- "references": {}
- }
- }, {
- "method": "calciteModalBeforeOpen",
- "name": "calciteModalBeforeOpen",
- "bubbles": true,
- "cancelable": false,
- "composed": true,
- "docs": {
- "tags": [],
- "text": "Fires when the component is added to the DOM but not rendered, and before the opening transition begins."
- },
- "complexType": {
- "original": "void",
- "resolved": "void",
- "references": {}
- }
- }, {
- "method": "calciteModalOpen",
- "name": "calciteModalOpen",
- "bubbles": true,
- "cancelable": false,
- "composed": true,
- "docs": {
- "tags": [],
- "text": "Fires when the component is open and animation is complete."
- },
- "complexType": {
- "original": "void",
- "resolved": "void",
- "references": {}
- }
- }];
- }
- static get methods() {
- return {
- "focusElement": {
- "complexType": {
- "signature": "(el?: HTMLElement) => Promise<void>",
- "parameters": [{
- "tags": [{
- "name": "param",
- "text": "el"
- }],
- "text": ""
- }],
- "references": {
- "Promise": {
- "location": "global"
- },
- "HTMLElement": {
- "location": "global"
- }
- },
- "return": "Promise<void>"
- },
- "docs": {
- "text": "Focus the first interactive element.",
- "tags": [{
- "name": "param",
- "text": "el"
- }, {
- "name": "deprecated",
- "text": "use `setFocus` instead."
- }]
- }
- },
- "setFocus": {
- "complexType": {
- "signature": "(focusId?: \"close-button\") => Promise<void>",
- "parameters": [{
- "tags": [{
- "name": "param",
- "text": "focusId"
- }],
- "text": ""
- }],
- "references": {
- "Promise": {
- "location": "global"
- }
- },
- "return": "Promise<void>"
- },
- "docs": {
- "text": "Sets focus on the component.\n\nBy default, tries to focus on focusable content. If there is none, it will focus on the close button.\nTo focus on the close button, use the `close-button` focus ID.",
- "tags": [{
- "name": "param",
- "text": "focusId"
- }]
- }
- },
- "scrollContent": {
- "complexType": {
- "signature": "(top?: number, left?: number) => Promise<void>",
- "parameters": [{
- "tags": [{
- "name": "param",
- "text": "top"
- }],
- "text": ""
- }, {
- "tags": [{
- "name": "param",
- "text": "left"
- }],
- "text": ""
- }],
- "references": {
- "Promise": {
- "location": "global"
- }
- },
- "return": "Promise<void>"
- },
- "docs": {
- "text": "Sets the scroll top of the component's content.",
- "tags": [{
- "name": "param",
- "text": "top"
- }, {
- "name": "param",
- "text": "left"
- }]
- }
- }
- };
- }
- static get elementRef() { return "el"; }
- static get watchers() {
- return [{
- "propName": "active",
- "methodName": "activeHandler"
- }, {
- "propName": "open",
- "methodName": "toggleModal"
- }];
- }
- static get listeners() {
- return [{
- "name": "keydown",
- "method": "handleEscape",
- "target": "window",
- "capture": false,
- "passive": false
- }];
- }
- }
|