calcite-modal.entry.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 { r as registerInstance, c as createEvent, h, H as Host, g as getElement } from './index-8ece2564.js';
  7. import { f as focusElement, g as getSlotted, e as ensureId, i as isCalciteFocusable } from './dom-da697a3f.js';
  8. import { c as createObserver } from './observers-b198f831.js';
  9. import { c as connectConditionalSlotComponent, d as disconnectConditionalSlotComponent } from './conditionalSlot-203be745.js';
  10. import './guid-b4461004.js';
  11. /**
  12. * Traverses the slots of the open shadowroots and returns all children matching the query.
  13. * @param {ShadowRoot | HTMLElement} root
  14. * @param skipNode
  15. * @param isMatch
  16. * @param {number} maxDepth
  17. * @param {number} depth
  18. * @returns {HTMLElement[]}
  19. */
  20. function queryShadowRoot(root, skipNode, isMatch, maxDepth = 20, depth = 0) {
  21. let matches = [];
  22. // If the depth is above the max depth, abort the searching here.
  23. if (depth >= maxDepth) {
  24. return matches;
  25. }
  26. // Traverses a slot element
  27. const traverseSlot = ($slot) => {
  28. // Only check nodes that are of the type Node.ELEMENT_NODE
  29. // Read more here https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType
  30. const assignedNodes = $slot.assignedNodes().filter(node => node.nodeType === 1);
  31. if (assignedNodes.length > 0) {
  32. return queryShadowRoot(assignedNodes[0].parentElement, skipNode, isMatch, maxDepth, depth + 1);
  33. }
  34. return [];
  35. };
  36. // Go through each child and continue the traversing if necessary
  37. // Even though the typing says that children can't be undefined, Edge 15 sometimes gives an undefined value.
  38. // Therefore we fallback to an empty array if it is undefined.
  39. const children = Array.from(root.children || []);
  40. for (const $child of children) {
  41. // Check if the node and its descendants should be skipped
  42. if (skipNode($child)) {
  43. continue;
  44. }
  45. // If the child matches we always add it
  46. if (isMatch($child)) {
  47. matches.push($child);
  48. }
  49. if ($child.shadowRoot != null) {
  50. matches.push(...queryShadowRoot($child.shadowRoot, skipNode, isMatch, maxDepth, depth + 1));
  51. }
  52. else if ($child.tagName === "SLOT") {
  53. matches.push(...traverseSlot($child));
  54. }
  55. else {
  56. matches.push(...queryShadowRoot($child, skipNode, isMatch, maxDepth, depth + 1));
  57. }
  58. }
  59. return matches;
  60. }
  61. /**
  62. * Returns whether the element is hidden.
  63. * @param $elem
  64. */
  65. function isHidden($elem) {
  66. return $elem.hasAttribute("hidden")
  67. || ($elem.hasAttribute("aria-hidden") && $elem.getAttribute("aria-hidden") !== "false")
  68. // A quick and dirty way to check whether the element is hidden.
  69. // For a more fine-grained check we could use "window.getComputedStyle" but we don't because of bad performance.
  70. // If the element has visibility set to "hidden" or "collapse", display set to "none" or opacity set to "0" through CSS
  71. // we won't be able to catch it here. We accept it due to the huge performance benefits.
  72. || $elem.style.display === `none`
  73. || $elem.style.opacity === `0`
  74. || $elem.style.visibility === `hidden`
  75. || $elem.style.visibility === `collapse`;
  76. // If offsetParent is null we can assume that the element is hidden
  77. // https://stackoverflow.com/questions/306305/what-would-make-offsetparent-null
  78. //|| $elem.offsetParent == null;
  79. }
  80. /**
  81. * Returns whether the element is disabled.
  82. * @param $elem
  83. */
  84. function isDisabled($elem) {
  85. return $elem.hasAttribute("disabled")
  86. || ($elem.hasAttribute("aria-disabled") && $elem.getAttribute("aria-disabled") !== "false");
  87. }
  88. /**
  89. * Determines whether an element is focusable.
  90. * Read more here: https://stackoverflow.com/questions/1599660/which-html-elements-can-receive-focus/1600194#1600194
  91. * Or here: https://stackoverflow.com/questions/18261595/how-to-check-if-a-dom-element-is-focusable
  92. * @param $elem
  93. */
  94. function isFocusable($elem) {
  95. // Discard elements that are removed from the tab order.
  96. if ($elem.getAttribute("tabindex") === "-1" || isHidden($elem) || isDisabled($elem)) {
  97. return false;
  98. }
  99. return (
  100. // At this point we know that the element can have focus (eg. won't be -1) if the tabindex attribute exists
  101. $elem.hasAttribute("tabindex")
  102. // Anchor tags or area tags with a href set
  103. || ($elem instanceof HTMLAnchorElement || $elem instanceof HTMLAreaElement) && $elem.hasAttribute("href")
  104. // Form elements which are not disabled
  105. || ($elem instanceof HTMLButtonElement
  106. || $elem instanceof HTMLInputElement
  107. || $elem instanceof HTMLTextAreaElement
  108. || $elem instanceof HTMLSelectElement)
  109. // IFrames
  110. || $elem instanceof HTMLIFrameElement);
  111. }
  112. const CSS = {
  113. title: "title",
  114. header: "header",
  115. footer: "footer",
  116. scrim: "scrim",
  117. back: "back",
  118. close: "close",
  119. secondary: "secondary",
  120. primary: "primary",
  121. overflowHidden: "overflow-hidden"
  122. };
  123. const ICONS = {
  124. close: "x"
  125. };
  126. const SLOTS = {
  127. content: "content",
  128. header: "header",
  129. back: "back",
  130. secondary: "secondary",
  131. primary: "primary"
  132. };
  133. const TEXT = {
  134. close: "Close"
  135. };
  136. 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}}";
  137. const isFocusableExtended = (el) => {
  138. return isCalciteFocusable(el) || isFocusable(el);
  139. };
  140. const getFocusableElements = (el) => {
  141. return queryShadowRoot(el, isHidden, isFocusableExtended);
  142. };
  143. const Modal = class {
  144. constructor(hostRef) {
  145. registerInstance(this, hostRef);
  146. this.calciteModalOpen = createEvent(this, "calciteModalOpen", 7);
  147. this.calciteModalClose = createEvent(this, "calciteModalClose", 7);
  148. //--------------------------------------------------------------------------
  149. //
  150. // Properties
  151. //
  152. //--------------------------------------------------------------------------
  153. /** Add the active attribute to open the modal */
  154. this.active = false;
  155. /** Optionally pass a function to run before close */
  156. this.beforeClose = () => Promise.resolve();
  157. /** Disables the display a close button within the Modal */
  158. this.disableCloseButton = false;
  159. /** Disables the closing of the Modal when clicked outside. */
  160. this.disableOutsideClose = false;
  161. /** Aria label for the close button */
  162. this.intlClose = TEXT.close;
  163. /** Flag to disable the default close on escape behavior */
  164. this.disableEscape = false;
  165. /** specify the scale of modal, defaults to m */
  166. this.scale = "m";
  167. /** Set the width of the modal. Can use stock sizes or pass a number (in pixels) */
  168. this.width = "m";
  169. /** Background color of modal content */
  170. this.backgroundColor = "white";
  171. /** Turn off spacing around the content area slot */
  172. this.noPadding = false;
  173. //--------------------------------------------------------------------------
  174. //
  175. // Variables
  176. //
  177. //--------------------------------------------------------------------------
  178. this.hasFooter = true;
  179. this.mutationObserver = createObserver("mutation", () => this.updateFooterVisibility());
  180. this.activeTransitionProp = "opacity";
  181. //--------------------------------------------------------------------------
  182. //
  183. // Private Methods
  184. //
  185. //--------------------------------------------------------------------------
  186. this.transitionEnd = (event) => {
  187. if (event.propertyName === this.activeTransitionProp) {
  188. this.active ? this.calciteModalOpen.emit() : this.calciteModalClose.emit();
  189. }
  190. };
  191. this.openEnd = () => {
  192. this.setFocus();
  193. this.el.removeEventListener("calciteModalOpen", this.openEnd);
  194. };
  195. this.handleOutsideClose = () => {
  196. if (this.disableOutsideClose) {
  197. return;
  198. }
  199. this.close();
  200. };
  201. /** Close the modal, first running the `beforeClose` method */
  202. this.close = () => {
  203. return this.beforeClose(this.el).then(() => {
  204. this.active = false;
  205. focusElement(this.previousActiveElement);
  206. this.removeOverflowHiddenClass();
  207. });
  208. };
  209. this.focusFirstElement = () => {
  210. focusElement(this.disableCloseButton ? getFocusableElements(this.el)[0] : this.closeButtonEl);
  211. };
  212. this.focusLastElement = () => {
  213. const focusableElements = getFocusableElements(this.el).filter((el) => !el.getAttribute("data-focus-fence"));
  214. if (focusableElements.length > 0) {
  215. focusElement(focusableElements[focusableElements.length - 1]);
  216. }
  217. else {
  218. focusElement(this.closeButtonEl);
  219. }
  220. };
  221. this.updateFooterVisibility = () => {
  222. this.hasFooter = !!getSlotted(this.el, [SLOTS.back, SLOTS.primary, SLOTS.secondary]);
  223. };
  224. }
  225. //--------------------------------------------------------------------------
  226. //
  227. // Lifecycle
  228. //
  229. //--------------------------------------------------------------------------
  230. componentWillLoad() {
  231. // when modal initially renders, if active was set we need to open as watcher doesn't fire
  232. if (this.active) {
  233. this.open();
  234. }
  235. }
  236. connectedCallback() {
  237. var _a;
  238. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true, subtree: true });
  239. this.updateFooterVisibility();
  240. connectConditionalSlotComponent(this);
  241. }
  242. disconnectedCallback() {
  243. var _a;
  244. this.removeOverflowHiddenClass();
  245. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  246. disconnectConditionalSlotComponent(this);
  247. }
  248. render() {
  249. 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: {
  250. content: true,
  251. "content--spaced": !this.noPadding,
  252. "content--no-footer": !this.hasFooter
  253. }, ref: (el) => (this.modalContent = el) }, h("slot", { name: SLOTS.content })), this.renderFooter(), h("div", { "data-focus-fence": true, onFocus: this.focusFirstElement, tabindex: "0" }))));
  254. }
  255. renderFooter() {
  256. 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;
  257. }
  258. renderCloseButton() {
  259. 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;
  260. }
  261. renderStyle() {
  262. const hasCustomWidth = !isNaN(parseInt(`${this.width}`));
  263. return hasCustomWidth ? (h("style", null, `
  264. .modal {
  265. max-width: ${this.width}px !important;
  266. }
  267. @media screen and (max-width: ${this.width}px) {
  268. .modal {
  269. height: 100% !important;
  270. max-height: 100% !important;
  271. width: 100% !important;
  272. max-width: 100% !important;
  273. margin: 0 !important;
  274. border-radius: 0 !important;
  275. }
  276. .content {
  277. flex: 1 1 auto !important;
  278. max-height: unset !important;
  279. }
  280. }
  281. `)) : null;
  282. }
  283. //--------------------------------------------------------------------------
  284. //
  285. // Event Listeners
  286. //
  287. //--------------------------------------------------------------------------
  288. handleEscape(e) {
  289. if (this.active && !this.disableEscape && e.key === "Escape") {
  290. this.close();
  291. }
  292. }
  293. //--------------------------------------------------------------------------
  294. //
  295. // Public Methods
  296. //
  297. //--------------------------------------------------------------------------
  298. /**
  299. * Focus first interactive element
  300. * @deprecated use `setFocus` instead.
  301. */
  302. async focusElement(el) {
  303. if (el) {
  304. el.focus();
  305. }
  306. return this.setFocus();
  307. }
  308. /**
  309. * Sets focus on the component.
  310. *
  311. * By default, will try to focus on any focusable content. If there is none, it will focus on the close button.
  312. * If you want to focus on the close button, you can use the `close-button` focus ID.
  313. */
  314. async setFocus(focusId) {
  315. const closeButton = this.closeButtonEl;
  316. return focusElement(focusId === "close-button" ? closeButton : getFocusableElements(this.el)[0] || closeButton);
  317. }
  318. /** Set the scroll top of the modal content */
  319. async scrollContent(top = 0, left = 0) {
  320. if (this.modalContent) {
  321. if (this.modalContent.scrollTo) {
  322. this.modalContent.scrollTo({ top, left, behavior: "smooth" });
  323. }
  324. else {
  325. this.modalContent.scrollTop = top;
  326. this.modalContent.scrollLeft = left;
  327. }
  328. }
  329. }
  330. async toggleModal(value, oldValue) {
  331. if (value !== oldValue) {
  332. if (value) {
  333. this.open();
  334. }
  335. else if (!value) {
  336. this.close();
  337. }
  338. }
  339. }
  340. /** Open the modal */
  341. open() {
  342. this.previousActiveElement = document.activeElement;
  343. this.el.addEventListener("calciteModalOpen", this.openEnd);
  344. this.active = true;
  345. const titleEl = getSlotted(this.el, SLOTS.header);
  346. const contentEl = getSlotted(this.el, SLOTS.content);
  347. this.titleId = ensureId(titleEl);
  348. this.contentId = ensureId(contentEl);
  349. document.documentElement.classList.add(CSS.overflowHidden);
  350. }
  351. removeOverflowHiddenClass() {
  352. document.documentElement.classList.remove(CSS.overflowHidden);
  353. }
  354. get el() { return getElement(this); }
  355. static get watchers() { return {
  356. "active": ["toggleModal"]
  357. }; }
  358. };
  359. Modal.style = modalCss;
  360. export { Modal as calcite_modal };