calcite-modal.entry.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. import { r as registerInstance, c as createEvent, h, H as Host, g as getElement } from './index-1f9b54dc.js';
  7. import { f as focusElement, g as getSlotted, h as ensureId, j as isCalciteFocusable } from './dom-8f0a9ff2.js';
  8. import { c as createObserver } from './observers-9f44e9b3.js';
  9. import { c as connectConditionalSlotComponent, d as disconnectConditionalSlotComponent } from './conditionalSlot-a3e8c63a.js';
  10. import { c as connectOpenCloseComponent, d as disconnectOpenCloseComponent } from './openCloseComponent-ba7d452b.js';
  11. import './resources-9c476cb6.js';
  12. import './guid-9f15e57a.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. modal: "modal",
  116. modalOpen: "modal--open",
  117. title: "title",
  118. header: "header",
  119. footer: "footer",
  120. scrim: "scrim",
  121. back: "back",
  122. close: "close",
  123. secondary: "secondary",
  124. primary: "primary",
  125. overflowHidden: "overflow-hidden",
  126. // these classes help apply the animation in phases to only set transform on open/close
  127. // this helps avoid a positioning issue for any floating-ui-owning children
  128. openingIdle: "modal--opening-idle",
  129. openingActive: "modal--opening-active",
  130. closingIdle: "modal--closing-idle",
  131. closingActive: "modal--closing-active"
  132. };
  133. const ICONS = {
  134. close: "x"
  135. };
  136. const SLOTS = {
  137. content: "content",
  138. header: "header",
  139. back: "back",
  140. secondary: "secondary",
  141. primary: "primary"
  142. };
  143. const TEXT = {
  144. close: "Close"
  145. };
  146. 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}}";
  147. const isFocusableExtended = (el) => {
  148. return isCalciteFocusable(el) || isFocusable(el);
  149. };
  150. const getFocusableElements = (el) => {
  151. return queryShadowRoot(el, isHidden, isFocusableExtended);
  152. };
  153. const Modal = class {
  154. constructor(hostRef) {
  155. registerInstance(this, hostRef);
  156. this.calciteModalBeforeClose = createEvent(this, "calciteModalBeforeClose", 6);
  157. this.calciteModalClose = createEvent(this, "calciteModalClose", 6);
  158. this.calciteModalBeforeOpen = createEvent(this, "calciteModalBeforeOpen", 6);
  159. this.calciteModalOpen = createEvent(this, "calciteModalOpen", 6);
  160. //--------------------------------------------------------------------------
  161. //
  162. // Properties
  163. //
  164. //--------------------------------------------------------------------------
  165. /**
  166. * When `true`, the component is active.
  167. *
  168. * @deprecated use `open` instead.
  169. */
  170. this.active = false;
  171. /** When `true`, displays and positions the component. */
  172. this.open = false;
  173. /** Passes a function to run before the component closes. */
  174. this.beforeClose = () => Promise.resolve();
  175. /** When `true`, disables the component's close button. */
  176. this.disableCloseButton = false;
  177. /** When `true`, disables the closing of the component when clicked outside. */
  178. this.disableOutsideClose = false;
  179. /** Accessible name for the component's close button. */
  180. this.intlClose = TEXT.close;
  181. /** When `true`, disables the default close on escape behavior. */
  182. this.disableEscape = false;
  183. /** Specifies the size of the component. */
  184. this.scale = "m";
  185. /** Specifies the width of the component. Can use scale sizes or pass a number (displays in pixels). */
  186. this.width = "m";
  187. /** Sets the background color of the component's content. */
  188. this.backgroundColor = "white";
  189. /**
  190. * When `true`, disables spacing to the content area slot.
  191. *
  192. * @deprecated Use `--calcite-modal-padding` CSS variable instead.
  193. */
  194. this.noPadding = false;
  195. //--------------------------------------------------------------------------
  196. //
  197. // Variables
  198. //
  199. //--------------------------------------------------------------------------
  200. this.hasFooter = true;
  201. /**
  202. * We use internal variable to make sure initially open modal can transition from closed state when rendered
  203. *
  204. * @private
  205. */
  206. this.isOpen = false;
  207. this.mutationObserver = createObserver("mutation", () => this.updateFooterVisibility());
  208. this.openTransitionProp = "opacity";
  209. //--------------------------------------------------------------------------
  210. //
  211. // Private Methods
  212. //
  213. //--------------------------------------------------------------------------
  214. this.setTransitionEl = (el) => {
  215. this.transitionEl = el;
  216. connectOpenCloseComponent(this);
  217. };
  218. this.openEnd = () => {
  219. this.setFocus();
  220. this.el.removeEventListener("calciteModalOpen", this.openEnd);
  221. };
  222. this.handleOutsideClose = () => {
  223. if (this.disableOutsideClose) {
  224. return;
  225. }
  226. this.close();
  227. };
  228. /** Close the modal, first running the `beforeClose` method */
  229. this.close = () => {
  230. return this.beforeClose(this.el).then(() => {
  231. this.open = false;
  232. this.isOpen = false;
  233. focusElement(this.previousActiveElement);
  234. this.removeOverflowHiddenClass();
  235. });
  236. };
  237. this.focusFirstElement = () => {
  238. focusElement(this.disableCloseButton ? getFocusableElements(this.el)[0] : this.closeButtonEl);
  239. };
  240. this.focusLastElement = () => {
  241. const focusableElements = getFocusableElements(this.el).filter((el) => !el.getAttribute("data-focus-fence"));
  242. if (focusableElements.length > 0) {
  243. focusElement(focusableElements[focusableElements.length - 1]);
  244. }
  245. else {
  246. focusElement(this.closeButtonEl);
  247. }
  248. };
  249. this.updateFooterVisibility = () => {
  250. this.hasFooter = !!getSlotted(this.el, [SLOTS.back, SLOTS.primary, SLOTS.secondary]);
  251. };
  252. }
  253. //--------------------------------------------------------------------------
  254. //
  255. // Lifecycle
  256. //
  257. //--------------------------------------------------------------------------
  258. componentWillLoad() {
  259. // when modal initially renders, if active was set we need to open as watcher doesn't fire
  260. if (this.open) {
  261. requestAnimationFrame(() => this.openModal());
  262. }
  263. }
  264. connectedCallback() {
  265. var _a;
  266. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true, subtree: true });
  267. this.updateFooterVisibility();
  268. connectConditionalSlotComponent(this);
  269. connectOpenCloseComponent(this);
  270. if (this.open) {
  271. this.active = this.open;
  272. }
  273. if (this.active) {
  274. this.activeHandler(this.active);
  275. }
  276. }
  277. disconnectedCallback() {
  278. var _a;
  279. this.removeOverflowHiddenClass();
  280. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  281. disconnectConditionalSlotComponent(this);
  282. disconnectOpenCloseComponent(this);
  283. }
  284. render() {
  285. 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: {
  286. [CSS.modal]: true,
  287. [CSS.modalOpen]: this.isOpen
  288. }, ref: this.setTransitionEl }, h("div", { "data-focus-fence": true, onFocus: this.focusLastElement, tabindex: "0" }), h("div", { class: CSS.header }, this.renderCloseButton(), h("header", { class: CSS.title }, h("slot", { name: CSS.header }))), h("div", { class: {
  289. content: true,
  290. "content--spaced": !this.noPadding,
  291. "content--no-footer": !this.hasFooter
  292. }, ref: (el) => (this.modalContent = el) }, h("slot", { name: SLOTS.content })), this.renderFooter(), h("div", { "data-focus-fence": true, onFocus: this.focusFirstElement, tabindex: "0" }))));
  293. }
  294. renderFooter() {
  295. 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;
  296. }
  297. renderCloseButton() {
  298. 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;
  299. }
  300. renderStyle() {
  301. const hasCustomWidth = !isNaN(parseInt(`${this.width}`));
  302. return hasCustomWidth ? (h("style", null, `
  303. .${CSS.modal} {
  304. max-width: ${this.width}px !important;
  305. }
  306. @media screen and (max-width: ${this.width}px) {
  307. .${CSS.modal} {
  308. height: 100% !important;
  309. max-height: 100% !important;
  310. width: 100% !important;
  311. max-width: 100% !important;
  312. margin: 0 !important;
  313. border-radius: 0 !important;
  314. }
  315. .content {
  316. flex: 1 1 auto !important;
  317. max-height: unset !important;
  318. }
  319. }
  320. `)) : null;
  321. }
  322. //--------------------------------------------------------------------------
  323. //
  324. // Event Listeners
  325. //
  326. //--------------------------------------------------------------------------
  327. handleEscape(event) {
  328. if (this.open && !this.disableEscape && event.key === "Escape" && !event.defaultPrevented) {
  329. this.close();
  330. event.preventDefault();
  331. }
  332. }
  333. //--------------------------------------------------------------------------
  334. //
  335. // Public Methods
  336. //
  337. //--------------------------------------------------------------------------
  338. /**
  339. * Focus the first interactive element.
  340. *
  341. * @param el
  342. * @deprecated use `setFocus` instead.
  343. */
  344. async focusElement(el) {
  345. if (el) {
  346. el.focus();
  347. }
  348. return this.setFocus();
  349. }
  350. /**
  351. * Sets focus on the component.
  352. *
  353. * By default, tries to focus on focusable content. If there is none, it will focus on the close button.
  354. * To focus on the close button, use the `close-button` focus ID.
  355. *
  356. * @param focusId
  357. */
  358. async setFocus(focusId) {
  359. const closeButton = this.closeButtonEl;
  360. return focusElement(focusId === "close-button" ? closeButton : getFocusableElements(this.el)[0] || closeButton);
  361. }
  362. /**
  363. * Sets the scroll top of the component's content.
  364. *
  365. * @param top
  366. * @param left
  367. */
  368. async scrollContent(top = 0, left = 0) {
  369. if (this.modalContent) {
  370. if (this.modalContent.scrollTo) {
  371. this.modalContent.scrollTo({ top, left, behavior: "smooth" });
  372. }
  373. else {
  374. this.modalContent.scrollTop = top;
  375. this.modalContent.scrollLeft = left;
  376. }
  377. }
  378. }
  379. onBeforeOpen() {
  380. this.transitionEl.classList.add(CSS.openingActive);
  381. this.calciteModalBeforeOpen.emit();
  382. }
  383. onOpen() {
  384. this.transitionEl.classList.remove(CSS.openingIdle, CSS.openingActive);
  385. this.calciteModalOpen.emit();
  386. }
  387. onBeforeClose() {
  388. this.transitionEl.classList.add(CSS.closingActive);
  389. this.calciteModalBeforeClose.emit();
  390. }
  391. onClose() {
  392. this.transitionEl.classList.remove(CSS.closingIdle, CSS.closingActive);
  393. this.calciteModalClose.emit();
  394. }
  395. activeHandler(value) {
  396. this.open = value;
  397. }
  398. async toggleModal(value) {
  399. var _a, _b;
  400. this.active = value;
  401. if (value) {
  402. (_a = this.transitionEl) === null || _a === void 0 ? void 0 : _a.classList.add(CSS.openingIdle);
  403. this.openModal();
  404. }
  405. else {
  406. (_b = this.transitionEl) === null || _b === void 0 ? void 0 : _b.classList.add(CSS.closingIdle);
  407. this.close();
  408. }
  409. }
  410. /** Open the modal */
  411. openModal() {
  412. this.previousActiveElement = document.activeElement;
  413. this.el.addEventListener("calciteModalOpen", this.openEnd);
  414. this.open = true;
  415. this.isOpen = true;
  416. const titleEl = getSlotted(this.el, SLOTS.header);
  417. const contentEl = getSlotted(this.el, SLOTS.content);
  418. this.titleId = ensureId(titleEl);
  419. this.contentId = ensureId(contentEl);
  420. document.documentElement.classList.add(CSS.overflowHidden);
  421. }
  422. removeOverflowHiddenClass() {
  423. document.documentElement.classList.remove(CSS.overflowHidden);
  424. }
  425. get el() { return getElement(this); }
  426. static get watchers() { return {
  427. "active": ["activeHandler"],
  428. "open": ["toggleModal"]
  429. }; }
  430. };
  431. Modal.style = modalCss;
  432. export { Modal as calcite_modal };