calcite-modal.cjs.entry.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. 'use strict';
  7. Object.defineProperty(exports, '__esModule', { value: true });
  8. const index = require('./index-5c65e149.js');
  9. const dom = require('./dom-9ac0341c.js');
  10. const observers = require('./observers-d9fdf006.js');
  11. const conditionalSlot = require('./conditionalSlot-ba5cd797.js');
  12. require('./guid-8b6d6cb4.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 dom.isCalciteFocusable(el) || isFocusable(el);
  141. };
  142. const getFocusableElements = (el) => {
  143. return queryShadowRoot(el, isHidden, isFocusableExtended);
  144. };
  145. const Modal = class {
  146. constructor(hostRef) {
  147. index.registerInstance(this, hostRef);
  148. this.calciteModalOpen = index.createEvent(this, "calciteModalOpen", 7);
  149. this.calciteModalClose = index.createEvent(this, "calciteModalClose", 7);
  150. //--------------------------------------------------------------------------
  151. //
  152. // Properties
  153. //
  154. //--------------------------------------------------------------------------
  155. /** Add the active attribute to open the modal */
  156. this.active = false;
  157. /** Optionally pass a function to run before close */
  158. this.beforeClose = () => Promise.resolve();
  159. /** Disables the display a close button within the Modal */
  160. this.disableCloseButton = false;
  161. /** Disables the closing of the Modal when clicked outside. */
  162. this.disableOutsideClose = false;
  163. /** Aria label for the close button */
  164. this.intlClose = TEXT.close;
  165. /** Flag to disable the default close on escape behavior */
  166. this.disableEscape = false;
  167. /** specify the scale of modal, defaults to m */
  168. this.scale = "m";
  169. /** Set the width of the modal. Can use stock sizes or pass a number (in pixels) */
  170. this.width = "m";
  171. /** Background color of modal content */
  172. this.backgroundColor = "white";
  173. /** Turn off spacing around the content area slot */
  174. this.noPadding = false;
  175. //--------------------------------------------------------------------------
  176. //
  177. // Variables
  178. //
  179. //--------------------------------------------------------------------------
  180. this.hasFooter = true;
  181. this.mutationObserver = observers.createObserver("mutation", () => this.updateFooterVisibility());
  182. this.activeTransitionProp = "opacity";
  183. //--------------------------------------------------------------------------
  184. //
  185. // Private Methods
  186. //
  187. //--------------------------------------------------------------------------
  188. this.transitionEnd = (event) => {
  189. if (event.propertyName === this.activeTransitionProp) {
  190. this.active ? this.calciteModalOpen.emit() : this.calciteModalClose.emit();
  191. }
  192. };
  193. this.openEnd = () => {
  194. this.setFocus();
  195. this.el.removeEventListener("calciteModalOpen", this.openEnd);
  196. };
  197. this.handleOutsideClose = () => {
  198. if (this.disableOutsideClose) {
  199. return;
  200. }
  201. this.close();
  202. };
  203. /** Close the modal, first running the `beforeClose` method */
  204. this.close = () => {
  205. return this.beforeClose(this.el).then(() => {
  206. this.active = false;
  207. dom.focusElement(this.previousActiveElement);
  208. this.removeOverflowHiddenClass();
  209. });
  210. };
  211. this.focusFirstElement = () => {
  212. dom.focusElement(this.disableCloseButton ? getFocusableElements(this.el)[0] : this.closeButtonEl);
  213. };
  214. this.focusLastElement = () => {
  215. const focusableElements = getFocusableElements(this.el).filter((el) => !el.getAttribute("data-focus-fence"));
  216. if (focusableElements.length > 0) {
  217. dom.focusElement(focusableElements[focusableElements.length - 1]);
  218. }
  219. else {
  220. dom.focusElement(this.closeButtonEl);
  221. }
  222. };
  223. this.updateFooterVisibility = () => {
  224. this.hasFooter = !!dom.getSlotted(this.el, [SLOTS.back, SLOTS.primary, SLOTS.secondary]);
  225. };
  226. }
  227. //--------------------------------------------------------------------------
  228. //
  229. // Lifecycle
  230. //
  231. //--------------------------------------------------------------------------
  232. componentWillLoad() {
  233. // when modal initially renders, if active was set we need to open as watcher doesn't fire
  234. if (this.active) {
  235. this.open();
  236. }
  237. }
  238. connectedCallback() {
  239. var _a;
  240. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true, subtree: true });
  241. this.updateFooterVisibility();
  242. conditionalSlot.connectConditionalSlotComponent(this);
  243. }
  244. disconnectedCallback() {
  245. var _a;
  246. this.removeOverflowHiddenClass();
  247. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  248. conditionalSlot.disconnectConditionalSlotComponent(this);
  249. }
  250. render() {
  251. return (index.h(index.Host, { "aria-describedby": this.contentId, "aria-labelledby": this.titleId, "aria-modal": "true", role: "dialog" }, index.h("calcite-scrim", { class: CSS.scrim, onClick: this.handleOutsideClose }), this.renderStyle(), index.h("div", { class: "modal", onTransitionEnd: this.transitionEnd }, index.h("div", { "data-focus-fence": true, onFocus: this.focusLastElement, tabindex: "0" }), index.h("div", { class: CSS.header }, this.renderCloseButton(), index.h("header", { class: CSS.title }, index.h("slot", { name: CSS.header }))), index.h("div", { class: {
  252. content: true,
  253. "content--spaced": !this.noPadding,
  254. "content--no-footer": !this.hasFooter
  255. }, ref: (el) => (this.modalContent = el) }, index.h("slot", { name: SLOTS.content })), this.renderFooter(), index.h("div", { "data-focus-fence": true, onFocus: this.focusFirstElement, tabindex: "0" }))));
  256. }
  257. renderFooter() {
  258. return this.hasFooter ? (index.h("div", { class: CSS.footer, key: "footer" }, index.h("span", { class: CSS.back }, index.h("slot", { name: SLOTS.back })), index.h("span", { class: CSS.secondary }, index.h("slot", { name: SLOTS.secondary })), index.h("span", { class: CSS.primary }, index.h("slot", { name: SLOTS.primary })))) : null;
  259. }
  260. renderCloseButton() {
  261. return !this.disableCloseButton ? (index.h("button", { "aria-label": this.intlClose, class: CSS.close, key: "button", onClick: this.close, ref: (el) => (this.closeButtonEl = el), title: this.intlClose }, index.h("calcite-icon", { icon: ICONS.close, scale: this.scale === "s" ? "s" : this.scale === "m" ? "m" : this.scale === "l" ? "l" : null }))) : null;
  262. }
  263. renderStyle() {
  264. const hasCustomWidth = !isNaN(parseInt(`${this.width}`));
  265. return hasCustomWidth ? (index.h("style", null, `
  266. .modal {
  267. max-width: ${this.width}px !important;
  268. }
  269. @media screen and (max-width: ${this.width}px) {
  270. .modal {
  271. height: 100% !important;
  272. max-height: 100% !important;
  273. width: 100% !important;
  274. max-width: 100% !important;
  275. margin: 0 !important;
  276. border-radius: 0 !important;
  277. }
  278. .content {
  279. flex: 1 1 auto !important;
  280. max-height: unset !important;
  281. }
  282. }
  283. `)) : null;
  284. }
  285. //--------------------------------------------------------------------------
  286. //
  287. // Event Listeners
  288. //
  289. //--------------------------------------------------------------------------
  290. handleEscape(e) {
  291. if (this.active && !this.disableEscape && e.key === "Escape") {
  292. this.close();
  293. }
  294. }
  295. //--------------------------------------------------------------------------
  296. //
  297. // Public Methods
  298. //
  299. //--------------------------------------------------------------------------
  300. /**
  301. * Focus first interactive element
  302. * @deprecated use `setFocus` instead.
  303. */
  304. async focusElement(el) {
  305. if (el) {
  306. el.focus();
  307. }
  308. return this.setFocus();
  309. }
  310. /**
  311. * Sets focus on the component.
  312. *
  313. * By default, will try to focus on any focusable content. If there is none, it will focus on the close button.
  314. * If you want to focus on the close button, you can use the `close-button` focus ID.
  315. */
  316. async setFocus(focusId) {
  317. const closeButton = this.closeButtonEl;
  318. return dom.focusElement(focusId === "close-button" ? closeButton : getFocusableElements(this.el)[0] || closeButton);
  319. }
  320. /** Set the scroll top of the modal content */
  321. async scrollContent(top = 0, left = 0) {
  322. if (this.modalContent) {
  323. if (this.modalContent.scrollTo) {
  324. this.modalContent.scrollTo({ top, left, behavior: "smooth" });
  325. }
  326. else {
  327. this.modalContent.scrollTop = top;
  328. this.modalContent.scrollLeft = left;
  329. }
  330. }
  331. }
  332. async toggleModal(value, oldValue) {
  333. if (value !== oldValue) {
  334. if (value) {
  335. this.open();
  336. }
  337. else if (!value) {
  338. this.close();
  339. }
  340. }
  341. }
  342. /** Open the modal */
  343. open() {
  344. this.previousActiveElement = document.activeElement;
  345. this.el.addEventListener("calciteModalOpen", this.openEnd);
  346. this.active = true;
  347. const titleEl = dom.getSlotted(this.el, SLOTS.header);
  348. const contentEl = dom.getSlotted(this.el, SLOTS.content);
  349. this.titleId = dom.ensureId(titleEl);
  350. this.contentId = dom.ensureId(contentEl);
  351. document.documentElement.classList.add(CSS.overflowHidden);
  352. }
  353. removeOverflowHiddenClass() {
  354. document.documentElement.classList.remove(CSS.overflowHidden);
  355. }
  356. get el() { return index.getElement(this); }
  357. static get watchers() { return {
  358. "active": ["toggleModal"]
  359. }; }
  360. };
  361. Modal.style = modalCss;
  362. exports.calcite_modal = Modal;