calcite-modal.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.82
  5. */
  6. import { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client';
  7. import { f as focusElement, b as getSlotted, i as ensureId, j as isCalciteFocusable } from './dom.js';
  8. import { c as createObserver } from './observers.js';
  9. import { c as connectConditionalSlotComponent, d as disconnectConditionalSlotComponent } from './conditionalSlot.js';
  10. import { d as defineCustomElement$4 } from './icon.js';
  11. import { d as defineCustomElement$3 } from './loader.js';
  12. import { d as defineCustomElement$2 } from './scrim.js';
  13. /**
  14. * Traverses the slots of the open shadowroots and returns all children matching the query.
  15. * @param {ShadowRoot | HTMLElement} root
  16. * @param skipNode
  17. * @param isMatch
  18. * @param {number} maxDepth
  19. * @param {number} depth
  20. * @returns {HTMLElement[]}
  21. */
  22. function queryShadowRoot(root, skipNode, isMatch, maxDepth = 20, depth = 0) {
  23. let matches = [];
  24. // If the depth is above the max depth, abort the searching here.
  25. if (depth >= maxDepth) {
  26. return matches;
  27. }
  28. // Traverses a slot element
  29. const traverseSlot = ($slot) => {
  30. // Only check nodes that are of the type Node.ELEMENT_NODE
  31. // Read more here https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType
  32. const assignedNodes = $slot.assignedNodes().filter(node => node.nodeType === 1);
  33. if (assignedNodes.length > 0) {
  34. return queryShadowRoot(assignedNodes[0].parentElement, skipNode, isMatch, maxDepth, depth + 1);
  35. }
  36. return [];
  37. };
  38. // Go through each child and continue the traversing if necessary
  39. // Even though the typing says that children can't be undefined, Edge 15 sometimes gives an undefined value.
  40. // Therefore we fallback to an empty array if it is undefined.
  41. const children = Array.from(root.children || []);
  42. for (const $child of children) {
  43. // Check if the node and its descendants should be skipped
  44. if (skipNode($child)) {
  45. continue;
  46. }
  47. // If the child matches we always add it
  48. if (isMatch($child)) {
  49. matches.push($child);
  50. }
  51. if ($child.shadowRoot != null) {
  52. matches.push(...queryShadowRoot($child.shadowRoot, skipNode, isMatch, maxDepth, depth + 1));
  53. }
  54. else if ($child.tagName === "SLOT") {
  55. matches.push(...traverseSlot($child));
  56. }
  57. else {
  58. matches.push(...queryShadowRoot($child, skipNode, isMatch, maxDepth, depth + 1));
  59. }
  60. }
  61. return matches;
  62. }
  63. /**
  64. * Returns whether the element is hidden.
  65. * @param $elem
  66. */
  67. function isHidden($elem) {
  68. return $elem.hasAttribute("hidden")
  69. || ($elem.hasAttribute("aria-hidden") && $elem.getAttribute("aria-hidden") !== "false")
  70. // A quick and dirty way to check whether the element is hidden.
  71. // For a more fine-grained check we could use "window.getComputedStyle" but we don't because of bad performance.
  72. // If the element has visibility set to "hidden" or "collapse", display set to "none" or opacity set to "0" through CSS
  73. // we won't be able to catch it here. We accept it due to the huge performance benefits.
  74. || $elem.style.display === `none`
  75. || $elem.style.opacity === `0`
  76. || $elem.style.visibility === `hidden`
  77. || $elem.style.visibility === `collapse`;
  78. // If offsetParent is null we can assume that the element is hidden
  79. // https://stackoverflow.com/questions/306305/what-would-make-offsetparent-null
  80. //|| $elem.offsetParent == null;
  81. }
  82. /**
  83. * Returns whether the element is disabled.
  84. * @param $elem
  85. */
  86. function isDisabled($elem) {
  87. return $elem.hasAttribute("disabled")
  88. || ($elem.hasAttribute("aria-disabled") && $elem.getAttribute("aria-disabled") !== "false");
  89. }
  90. /**
  91. * Determines whether an element is focusable.
  92. * Read more here: https://stackoverflow.com/questions/1599660/which-html-elements-can-receive-focus/1600194#1600194
  93. * Or here: https://stackoverflow.com/questions/18261595/how-to-check-if-a-dom-element-is-focusable
  94. * @param $elem
  95. */
  96. function isFocusable($elem) {
  97. // Discard elements that are removed from the tab order.
  98. if ($elem.getAttribute("tabindex") === "-1" || isHidden($elem) || isDisabled($elem)) {
  99. return false;
  100. }
  101. return (
  102. // At this point we know that the element can have focus (eg. won't be -1) if the tabindex attribute exists
  103. $elem.hasAttribute("tabindex")
  104. // Anchor tags or area tags with a href set
  105. || ($elem instanceof HTMLAnchorElement || $elem instanceof HTMLAreaElement) && $elem.hasAttribute("href")
  106. // Form elements which are not disabled
  107. || ($elem instanceof HTMLButtonElement
  108. || $elem instanceof HTMLInputElement
  109. || $elem instanceof HTMLTextAreaElement
  110. || $elem instanceof HTMLSelectElement)
  111. // IFrames
  112. || $elem instanceof HTMLIFrameElement);
  113. }
  114. const CSS = {
  115. title: "title",
  116. header: "header",
  117. footer: "footer",
  118. scrim: "scrim",
  119. back: "back",
  120. close: "close",
  121. secondary: "secondary",
  122. primary: "primary",
  123. overflowHidden: "overflow-hidden"
  124. };
  125. const ICONS = {
  126. close: "x"
  127. };
  128. const SLOTS = {
  129. content: "content",
  130. header: "header",
  131. back: "back",
  132. secondary: "secondary",
  133. primary: "primary"
  134. };
  135. const TEXT = {
  136. close: "Close"
  137. };
  138. const modalCss = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{position:fixed;top:0px;right:0px;bottom:0px;left:0px;display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center;-ms-flex-pack:center;justify-content:center;overflow-y:hidden;color:var(--calcite-ui-text-2);opacity:0;visibility:hidden !important;-webkit-transition:visibility 0ms linear 300ms, opacity var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88);transition:visibility 0ms linear 300ms, opacity var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88);z-index:101}:host([scale=s]){--calcite-modal-padding:0.75rem;--calcite-modal-padding-large:1rem;--calcite-modal-title-text:var(--calcite-font-size-1);--calcite-modal-content-text:var(--calcite-font-size--1)}:host([scale=m]){--calcite-modal-padding:1rem;--calcite-modal-padding-large:1.25rem;--calcite-modal-title-text:var(--calcite-font-size-2);--calcite-modal-content-text:var(--calcite-font-size-0)}:host([scale=l]){--calcite-modal-padding:1.25rem;--calcite-modal-padding-large:1.5rem;--calcite-modal-title-text:var(--calcite-font-size-3);--calcite-modal-content-text:var(--calcite-font-size-1)}.scrim{--calcite-scrim-background:rgba(0, 0, 0, 0.75);position:fixed;top:0px;right:0px;bottom:0px;left:0px;display:-ms-flexbox;display:flex;overflow-y:hidden}.modal{pointer-events:none;float:none;margin:1.5rem;-webkit-box-sizing:border-box;box-sizing:border-box;display:-ms-flexbox;display:flex;width:100%;-ms-flex-direction:column;flex-direction:column;overflow:hidden;border-radius:0.25rem;background-color:var(--calcite-ui-foreground-1);opacity:0;--tw-shadow:0 2px 12px -4px rgba(0, 0, 0, 0.2), 0 2px 4px -2px rgba(0, 0, 0, 0.16);--tw-shadow-colored:0 2px 12px -4px var(--tw-shadow-color), 0 2px 4px -2px var(--tw-shadow-color);-webkit-box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);z-index:102;-webkit-overflow-scrolling:touch;visibility:hidden;-webkit-transition:visibility 0ms linear 300ms, opacity var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88), -webkit-transform var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88);transition:visibility 0ms linear 300ms, opacity var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88), -webkit-transform var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88);transition:transform var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88), visibility 0ms linear 300ms, opacity var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88);transition:transform var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88), visibility 0ms linear 300ms, opacity var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88), -webkit-transform var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88);-webkit-transform:translate3d(0, 20px, 0);transform:translate3d(0, 20px, 0)}:host([active]){opacity:1;visibility:visible !important;-webkit-transition-delay:0ms;transition-delay:0ms}:host([active]) .modal{pointer-events:auto;visibility:visible;opacity:1;-webkit-transform:translate3d(0, 0, 0);transform:translate3d(0, 0, 0);-webkit-transition:visibility 0ms linear, opacity var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88), max-width var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88), max-height var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88), -webkit-transform var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88);transition:visibility 0ms linear, opacity var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88), max-width var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88), max-height var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88), -webkit-transform var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88);transition:transform var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88), visibility 0ms linear, opacity var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88), max-width var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88), max-height var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88);transition:transform var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88), visibility 0ms linear, opacity var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88), max-width var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88), max-height var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88), -webkit-transform var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88);-webkit-transition-delay:0ms;transition-delay:0ms}.header{display:-ms-flexbox;display:flex;min-width:0px;max-width:100%;border-top-left-radius:0.25rem;border-top-right-radius:0.25rem;border-width:0px;border-bottom-width:1px;border-style:solid;border-color:var(--calcite-ui-border-3);background-color:var(--calcite-ui-foreground-1);-ms-flex:0 0 auto;flex:0 0 auto;z-index:2}.close{-ms-flex-order:2;order:2;margin:0px;cursor:pointer;-webkit-appearance:none;-moz-appearance:none;appearance:none;border-style:none;background-color:transparent;color:var(--calcite-ui-text-3);outline-offset:0;outline-color:transparent;-webkit-transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:var(--calcite-animation-timing);transition-duration:var(--calcite-animation-timing);-webkit-transition-timing-function:ease-in-out;transition-timing-function:ease-in-out;-webkit-transition-delay:0s;transition-delay:0s;border-start-end-radius:0.25rem;padding:var(--calcite-modal-padding);-ms-flex:0 0 auto;flex:0 0 auto}.close calcite-icon{pointer-events:none;vertical-align:-2px}.close:focus{outline:2px solid var(--calcite-ui-brand);outline-offset:-2px}.close:hover,.close:focus,.close:active{background-color:var(--calcite-ui-foreground-2);color:var(--calcite-ui-text-1)}.title{-ms-flex-order:1;order:1;display:-ms-flexbox;display:flex;min-width:0px;-ms-flex-align:center;align-items:center;-ms-flex:1 1 auto;flex:1 1 auto;padding:var(--calcite-modal-padding) var(--calcite-modal-padding-large)}slot[name=header]::slotted(*),*::slotted([slot=header]){margin:0px;font-weight:var(--calcite-font-weight-normal);color:var(--calcite-ui-text-1);font-size:var(--calcite-modal-title-text)}.content{position:relative;-webkit-box-sizing:border-box;box-sizing:border-box;display:block;height:100%;overflow:auto;background-color:var(--calcite-ui-foreground-1);padding:0px;max-height:calc(100vh - 12rem);z-index:1}.content--spaced{padding:var(--calcite-modal-padding) var(--calcite-modal-padding-large)}.content--no-footer{border-bottom-right-radius:0.25rem;border-bottom-left-radius:0.25rem}slot[name=content]::slotted(*),*::slotted([slot=content]){font-size:var(--calcite-modal-content-text)}:host([background-color=grey]) .content{background-color:var(--calcite-ui-background)}.footer{margin-top:auto;-webkit-box-sizing:border-box;box-sizing:border-box;display:-ms-flexbox;display:flex;width:100%;-ms-flex-pack:justify;justify-content:space-between;border-bottom-right-radius:0.25rem;border-bottom-left-radius:0.25rem;border-width:0px;border-top-width:1px;border-style:solid;border-color:var(--calcite-ui-border-3);background-color:var(--calcite-ui-foreground-1);-ms-flex:0 0 auto;flex:0 0 auto;padding:var(--calcite-modal-padding) var(--calcite-modal-padding-large);z-index:2}.footer--hide-back .back,.footer--hide-secondary .secondary{display:none}.back{display:block;-webkit-margin-end:auto;margin-inline-end:auto}.secondary{margin-left:0.25rem;margin-right:0.25rem;display:block}slot[name=primary]{display:block}:host([width=small]) .modal{width:auto}:host([width=s]) .modal{max-width:32rem}@media screen and (max-width: 35rem){:host([width=s]) .modal{margin:0px;height:100%;max-height:100%;width:100%;max-width:100%;border-radius:0px}:host([width=s]) .content{-ms-flex:1 1 auto;flex:1 1 auto;max-height:unset}:host([width=s][docked]){-ms-flex-align:end;align-items:flex-end}}:host([width=m]) .modal{max-width:48rem}@media screen and (max-width: 51rem){:host([width=m]) .modal{margin:0px;height:100%;max-height:100%;width:100%;max-width:100%;border-radius:0px}:host([width=m]) .content{-ms-flex:1 1 auto;flex:1 1 auto;max-height:unset}:host([width=m][docked]){-ms-flex-align:end;align-items:flex-end}}:host([width=l]) .modal{max-width:94rem}@media screen and (max-width: 97rem){:host([width=l]) .modal{margin:0px;height:100%;max-height:100%;width:100%;max-width:100%;border-radius:0px}:host([width=l]) .content{-ms-flex:1 1 auto;flex:1 1 auto;max-height:unset}:host([width=l][docked]){-ms-flex-align:end;align-items:flex-end}}:host([fullscreen]){background-color:transparent}:host([fullscreen]) .modal{margin:0px;height:100%;max-height:100%;width:100%;max-width:100%;-webkit-transform:translate3D(0, 20px, 0) scale(0.95);transform:translate3D(0, 20px, 0) scale(0.95)}:host([fullscreen]) .content{max-height:100%;-ms-flex:1 1 auto;flex:1 1 auto}:host([active][fullscreen]) .modal{-webkit-transform:translate3D(0, 0, 0) scale(1);transform:translate3D(0, 0, 0) scale(1)}:host([active][fullscreen]) .header{border-radius:0}:host([active][fullscreen]) .footer{border-radius:0}:host([docked]) .modal{height:auto}:host([docked]) .content{height:auto;-ms-flex:1 1 auto;flex:1 1 auto}@media screen and (max-width: 860px){:host([docked]) .modal{border-radius:var(--calcite-border-radius) var(--calcite-border-radius) 0 0}:host([docked]) .close{border-start-end-radius:var(--calcite-border-radius)}}:host([color=red]) .modal{border-color:var(--calcite-ui-danger)}:host([color=blue]) .modal{border-color:var(--calcite-ui-info)}:host([color=red]) .modal,:host([color=blue]) .modal{border-width:0px;border-top-width:4px;border-style:solid}:host([color=red]) .header,:host([color=blue]) .header{border-radius:0.25rem;border-bottom-right-radius:0px;border-bottom-left-radius:0px}@media screen and (max-width: 860px){slot[name=header]::slotted(*),*::slotted([slot=header]){font-size:var(--calcite-font-size-1)}.footer{position:-webkit-sticky;position:sticky;bottom:0px}}@media screen and (max-width: 480px){.footer{-ms-flex-direction:column;flex-direction:column}.back,.secondary{margin:0px;margin-bottom:0.25rem}}";
  139. const isFocusableExtended = (el) => {
  140. return isCalciteFocusable(el) || isFocusable(el);
  141. };
  142. const getFocusableElements = (el) => {
  143. return queryShadowRoot(el, isHidden, isFocusableExtended);
  144. };
  145. const Modal = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  146. constructor() {
  147. super();
  148. this.__registerHost();
  149. this.__attachShadow();
  150. this.calciteModalOpen = createEvent(this, "calciteModalOpen", 7);
  151. this.calciteModalClose = createEvent(this, "calciteModalClose", 7);
  152. //--------------------------------------------------------------------------
  153. //
  154. // Properties
  155. //
  156. //--------------------------------------------------------------------------
  157. /** Add the active attribute to open the modal */
  158. this.active = false;
  159. /** Optionally pass a function to run before close */
  160. this.beforeClose = () => Promise.resolve();
  161. /** Disables the display a close button within the Modal */
  162. this.disableCloseButton = false;
  163. /** Disables the closing of the Modal when clicked outside. */
  164. this.disableOutsideClose = false;
  165. /** Aria label for the close button */
  166. this.intlClose = TEXT.close;
  167. /** Flag to disable the default close on escape behavior */
  168. this.disableEscape = false;
  169. /** specify the scale of modal, defaults to m */
  170. this.scale = "m";
  171. /** Set the width of the modal. Can use stock sizes or pass a number (in pixels) */
  172. this.width = "m";
  173. /** Background color of modal content */
  174. this.backgroundColor = "white";
  175. /** Turn off spacing around the content area slot */
  176. this.noPadding = false;
  177. //--------------------------------------------------------------------------
  178. //
  179. // Variables
  180. //
  181. //--------------------------------------------------------------------------
  182. this.hasFooter = true;
  183. this.mutationObserver = createObserver("mutation", () => this.updateFooterVisibility());
  184. this.activeTransitionProp = "opacity";
  185. //--------------------------------------------------------------------------
  186. //
  187. // Private Methods
  188. //
  189. //--------------------------------------------------------------------------
  190. this.transitionEnd = (event) => {
  191. if (event.propertyName === this.activeTransitionProp) {
  192. this.active ? this.calciteModalOpen.emit() : this.calciteModalClose.emit();
  193. }
  194. };
  195. this.openEnd = () => {
  196. this.setFocus();
  197. this.el.removeEventListener("calciteModalOpen", this.openEnd);
  198. };
  199. this.handleOutsideClose = () => {
  200. if (this.disableOutsideClose) {
  201. return;
  202. }
  203. this.close();
  204. };
  205. /** Close the modal, first running the `beforeClose` method */
  206. this.close = () => {
  207. return this.beforeClose(this.el).then(() => {
  208. this.active = false;
  209. focusElement(this.previousActiveElement);
  210. this.removeOverflowHiddenClass();
  211. });
  212. };
  213. this.focusFirstElement = () => {
  214. focusElement(this.disableCloseButton ? getFocusableElements(this.el)[0] : this.closeButtonEl);
  215. };
  216. this.focusLastElement = () => {
  217. const focusableElements = getFocusableElements(this.el).filter((el) => !el.getAttribute("data-focus-fence"));
  218. if (focusableElements.length > 0) {
  219. focusElement(focusableElements[focusableElements.length - 1]);
  220. }
  221. else {
  222. focusElement(this.closeButtonEl);
  223. }
  224. };
  225. this.updateFooterVisibility = () => {
  226. this.hasFooter = !!getSlotted(this.el, [SLOTS.back, SLOTS.primary, SLOTS.secondary]);
  227. };
  228. }
  229. //--------------------------------------------------------------------------
  230. //
  231. // Lifecycle
  232. //
  233. //--------------------------------------------------------------------------
  234. componentWillLoad() {
  235. // when modal initially renders, if active was set we need to open as watcher doesn't fire
  236. if (this.active) {
  237. this.open();
  238. }
  239. }
  240. connectedCallback() {
  241. var _a;
  242. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true, subtree: true });
  243. this.updateFooterVisibility();
  244. connectConditionalSlotComponent(this);
  245. }
  246. disconnectedCallback() {
  247. var _a;
  248. this.removeOverflowHiddenClass();
  249. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  250. disconnectConditionalSlotComponent(this);
  251. }
  252. render() {
  253. 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: "modal", onTransitionEnd: this.transitionEnd }, 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: {
  254. content: true,
  255. "content--spaced": !this.noPadding,
  256. "content--no-footer": !this.hasFooter
  257. }, ref: (el) => (this.modalContent = el) }, h("slot", { name: SLOTS.content })), this.renderFooter(), h("div", { "data-focus-fence": true, onFocus: this.focusFirstElement, tabindex: "0" }))));
  258. }
  259. renderFooter() {
  260. 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;
  261. }
  262. renderCloseButton() {
  263. 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;
  264. }
  265. renderStyle() {
  266. const hasCustomWidth = !isNaN(parseInt(`${this.width}`));
  267. return hasCustomWidth ? (h("style", null, `
  268. .modal {
  269. max-width: ${this.width}px !important;
  270. }
  271. @media screen and (max-width: ${this.width}px) {
  272. .modal {
  273. height: 100% !important;
  274. max-height: 100% !important;
  275. width: 100% !important;
  276. max-width: 100% !important;
  277. margin: 0 !important;
  278. border-radius: 0 !important;
  279. }
  280. .content {
  281. flex: 1 1 auto !important;
  282. max-height: unset !important;
  283. }
  284. }
  285. `)) : null;
  286. }
  287. //--------------------------------------------------------------------------
  288. //
  289. // Event Listeners
  290. //
  291. //--------------------------------------------------------------------------
  292. handleEscape(e) {
  293. if (this.active && !this.disableEscape && e.key === "Escape") {
  294. this.close();
  295. }
  296. }
  297. //--------------------------------------------------------------------------
  298. //
  299. // Public Methods
  300. //
  301. //--------------------------------------------------------------------------
  302. /**
  303. * Focus first interactive element
  304. * @deprecated use `setFocus` instead.
  305. */
  306. async focusElement(el) {
  307. if (el) {
  308. el.focus();
  309. }
  310. return this.setFocus();
  311. }
  312. /**
  313. * Sets focus on the component.
  314. *
  315. * By default, will try to focus on any focusable content. If there is none, it will focus on the close button.
  316. * If you want to focus on the close button, you can use the `close-button` focus ID.
  317. */
  318. async setFocus(focusId) {
  319. const closeButton = this.closeButtonEl;
  320. return focusElement(focusId === "close-button" ? closeButton : getFocusableElements(this.el)[0] || closeButton);
  321. }
  322. /** Set the scroll top of the modal content */
  323. async scrollContent(top = 0, left = 0) {
  324. if (this.modalContent) {
  325. if (this.modalContent.scrollTo) {
  326. this.modalContent.scrollTo({ top, left, behavior: "smooth" });
  327. }
  328. else {
  329. this.modalContent.scrollTop = top;
  330. this.modalContent.scrollLeft = left;
  331. }
  332. }
  333. }
  334. async toggleModal(value, oldValue) {
  335. if (value !== oldValue) {
  336. if (value) {
  337. this.open();
  338. }
  339. else if (!value) {
  340. this.close();
  341. }
  342. }
  343. }
  344. /** Open the modal */
  345. open() {
  346. this.previousActiveElement = document.activeElement;
  347. this.el.addEventListener("calciteModalOpen", this.openEnd);
  348. this.active = true;
  349. const titleEl = getSlotted(this.el, SLOTS.header);
  350. const contentEl = getSlotted(this.el, SLOTS.content);
  351. this.titleId = ensureId(titleEl);
  352. this.contentId = ensureId(contentEl);
  353. document.documentElement.classList.add(CSS.overflowHidden);
  354. }
  355. removeOverflowHiddenClass() {
  356. document.documentElement.classList.remove(CSS.overflowHidden);
  357. }
  358. get el() { return this; }
  359. static get watchers() { return {
  360. "active": ["toggleModal"]
  361. }; }
  362. static get style() { return modalCss; }
  363. }, [1, "calcite-modal", {
  364. "active": [1540],
  365. "beforeClose": [16],
  366. "disableCloseButton": [4, "disable-close-button"],
  367. "disableOutsideClose": [4, "disable-outside-close"],
  368. "intlClose": [1, "intl-close"],
  369. "docked": [516],
  370. "firstFocus": [16],
  371. "disableEscape": [4, "disable-escape"],
  372. "scale": [513],
  373. "width": [520],
  374. "fullscreen": [516],
  375. "color": [513],
  376. "backgroundColor": [513, "background-color"],
  377. "noPadding": [4, "no-padding"],
  378. "hasFooter": [32],
  379. "focusElement": [64],
  380. "setFocus": [64],
  381. "scrollContent": [64]
  382. }, [[8, "keyup", "handleEscape"]]]);
  383. function defineCustomElement$1() {
  384. if (typeof customElements === "undefined") {
  385. return;
  386. }
  387. const components = ["calcite-modal", "calcite-icon", "calcite-loader", "calcite-scrim"];
  388. components.forEach(tagName => { switch (tagName) {
  389. case "calcite-modal":
  390. if (!customElements.get(tagName)) {
  391. customElements.define(tagName, Modal);
  392. }
  393. break;
  394. case "calcite-icon":
  395. if (!customElements.get(tagName)) {
  396. defineCustomElement$4();
  397. }
  398. break;
  399. case "calcite-loader":
  400. if (!customElements.get(tagName)) {
  401. defineCustomElement$3();
  402. }
  403. break;
  404. case "calcite-scrim":
  405. if (!customElements.get(tagName)) {
  406. defineCustomElement$2();
  407. }
  408. break;
  409. } });
  410. }
  411. defineCustomElement$1();
  412. const CalciteModal = Modal;
  413. const defineCustomElement = defineCustomElement$1;
  414. export { CalciteModal, defineCustomElement };