calcite-modal.cjs.entry.js 26 KB

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