calcite-modal.js 28 KB

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