calcite-alert.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 { s as setRequestedIcon, t as toAriaBoolean, b as getSlotted } from './dom.js';
  8. import { S as StatusIcons } from './interfaces.js';
  9. import { c as connectOpenCloseComponent, d as disconnectOpenCloseComponent } from './openCloseComponent.js';
  10. import { c as connectLocalized, d as disconnectLocalized, n as numberStringFormatter } from './locale.js';
  11. import { d as defineCustomElement$3 } from './chip.js';
  12. import { d as defineCustomElement$2 } from './icon.js';
  13. const TEXT = {
  14. intlClose: "Close"
  15. };
  16. const DURATIONS = {
  17. slow: 14000,
  18. medium: 10000,
  19. fast: 6000
  20. };
  21. const SLOTS = {
  22. title: "title",
  23. message: "message",
  24. link: "link"
  25. };
  26. const alertCss = "@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([scale=s]){--calcite-alert-width:40em;--calcite-alert-spacing-token-small:0.5rem;--calcite-alert-spacing-token-large:0.75rem}:host([scale=s]) slot[name=title]::slotted(*),:host([scale=s]) *::slotted([slot=title]){font-size:var(--calcite-font-size--1);line-height:1.375}:host([scale=s]) slot[name=message]::slotted(*),:host([scale=s]) *::slotted([slot=message]){font-size:var(--calcite-font-size--2);line-height:1.375}:host([scale=s]) slot[name=link]::slotted(*),:host([scale=s]) *::slotted([slot=link]){font-size:var(--calcite-font-size--2);line-height:1.375}:host([scale=s]) .alert-queue-count{margin-inline:0.5rem}:host([scale=s]) .container{--calcite-alert-min-height:3.5rem}:host([scale=s]) .alert-close{padding:0.5rem}:host([scale=m]){--calcite-alert-width:50em;--calcite-alert-spacing-token-small:0.75rem;--calcite-alert-spacing-token-large:1rem}:host([scale=m]) slot[name=title]::slotted(*),:host([scale=m]) *::slotted([slot=title]){font-size:var(--calcite-font-size-0);line-height:1.375}:host([scale=m]) slot[name=message]::slotted(*),:host([scale=m]) *::slotted([slot=message]){font-size:var(--calcite-font-size--1);line-height:1.375}:host([scale=m]) slot[name=link]::slotted(*),:host([scale=m]) *::slotted([slot=link]){font-size:var(--calcite-font-size--1);line-height:1.375}:host([scale=m]) .alert-queue-count{margin-inline:0.75rem}:host([scale=m]) .container{--calcite-alert-min-height:4.1875rem}:host([scale=l]){--calcite-alert-width:60em;--calcite-alert-spacing-token-small:1rem;--calcite-alert-spacing-token-large:1.25rem}:host([scale=l]) slot[name=title]::slotted(*),:host([scale=l]) *::slotted([slot=title]){margin-block-end:0.25rem;font-size:var(--calcite-font-size-1);line-height:1.375}:host([scale=l]) slot[name=message]::slotted(*),:host([scale=l]) *::slotted([slot=message]){font-size:var(--calcite-font-size-0);line-height:1.375}:host([scale=l]) slot[name=link]::slotted(*),:host([scale=l]) *::slotted([slot=link]){font-size:var(--calcite-font-size-0);line-height:1.375}:host([scale=l]) .alert-queue-count{margin-inline:1rem}:host([scale=l]) .container{--calcite-alert-min-height:5.625rem}:host{--calcite-alert-edge-distance:2rem;display:block}:host .container{pointer-events:none;position:fixed;z-index:500;margin-block:0px;margin-inline:auto;display:flex;align-items:center;justify-content:center;background-color:var(--calcite-ui-foreground-1);opacity:0;--tw-shadow:0 6px 20px -4px rgba(0, 0, 0, 0.1), 0 4px 12px -2px rgba(0, 0, 0, 0.08);--tw-shadow-colored:0 6px 20px -4px var(--tw-shadow-color), 0 4px 12px -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);border-radius:var(--calcite-border-radius);border-block-start:0px solid transparent;border-inline:1px solid var(--calcite-ui-border-3);border-block-end:1px solid var(--calcite-ui-border-3);min-block-size:var(--calcite-alert-min-height);inline-size:var(--calcite-alert-width);max-inline-size:calc(100% - (var(--calcite-alert-edge-distance) * 2 + 2px));max-block-size:0;transition:var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88), opacity var(--calcite-internal-animation-timing-slow) cubic-bezier(0.215, 0.44, 0.42, 0.88), all var(--calcite-animation-timing) ease-in-out}:host .container.bottom,:host .container.top{inset-inline-end:0;inset-inline-start:0}:host .container[class*=bottom]{transform:translate3d(0, var(--calcite-alert-edge-distance), 0);inset-block-end:var(--calcite-alert-edge-distance)}:host .container[class*=top]{transform:translate3d(0, calc(-1 * var(--calcite-alert-edge-distance)), 0);inset-block-start:var(--calcite-alert-edge-distance)}:host .container[class*=-start]{inset-inline-start:var(--calcite-alert-edge-distance);inset-inline-end:auto}:host .container[class*=-end]{inset-inline-end:var(--calcite-alert-edge-distance);inset-inline-start:auto}:host([calcite-hydrated-hidden]){visibility:hidden !important;pointer-events:none}.container{display:flex;inline-size:100%;align-items:center;justify-content:center}.alert-close{outline-color:transparent}.alert-close:focus{outline:2px solid var(--calcite-ui-brand);outline-offset:-2px}:host([open]) .container:not(.queued){max-block-size:100%;border-block-start-width:2px;opacity:1;pointer-events:initial}:host([open]) .container:not(.queued)[class*=bottom]{transform:translate3d(0, calc(-1 * var(--calcite-alert-edge-distance)), inherit)}:host([open]) .container:not(.queued)[class*=top]{transform:translate3d(0, var(--calcite-alert-edge-distance), inherit)}slot[name=title]::slotted(*),*::slotted([slot=title]){font-size:var(--calcite-font-size-0);line-height:1.375;font-weight:var(--calcite-font-weight-medium);color:var(--calcite-ui-text-1)}slot[name=message]::slotted(*),*::slotted([slot=message]){margin:0px;display:inline;font-size:var(--calcite-font-size--1);line-height:1.375;font-weight:var(--calcite-font-weight-normal);color:var(--calcite-ui-text-2);margin-inline-end:0.5rem}slot[name=link]::slotted(*),*::slotted([slot=link]){display:inline-flex;color:var(--calcite-ui-text-link)}.alert-content{transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s;padding-inline:var(--calcite-alert-spacing-token-large);flex:0 0 auto;overflow-wrap:break-word;background-color:var(--calcite-ui-foreground-1);flex:1 1 auto;min-inline-size:0;padding-block:var(--calcite-alert-spacing-token-small);padding-inline:0 var(--calcite-alert-spacing-token-small);border-end-start-radius:var(--calcite-border-radius);border-end-end-radius:var(--calcite-border-radius)}.alert-content:first-of-type:not(:only-child){padding-inline-start:var(--calcite-alert-spacing-token-large)}.alert-content:only-child{padding:var(--calcite-alert-spacing-token-small)}.alert-icon{transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s;padding-block:var(--calcite-alert-spacing-token-small);padding-inline:var(--calcite-alert-spacing-token-large);flex:0 0 auto;display:flex;align-items:center;align-self:stretch;background-color:var(--calcite-ui-foreground-1);padding-block:0px}.alert-close{transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s;padding-block:var(--calcite-alert-spacing-token-small);padding-inline:var(--calcite-alert-spacing-token-large);flex:0 0 auto;cursor:pointer;align-self:stretch;overflow:hidden;border-style:none;background-color:var(--calcite-ui-foreground-1);padding-block:0px;color:var(--calcite-ui-text-3);outline:2px solid transparent;outline-offset:2px;border-end-end-radius:var(--calcite-border-radius)}.alert-close:hover,.alert-close:focus{background-color:var(--calcite-ui-foreground-2);color:var(--calcite-ui-text-1)}.alert-close:open{background-color:var(--calcite-ui-foreground-3)}.alert-queue-count{visibility:hidden;display:flex;cursor:default;align-items:center;justify-content:space-around;align-self:stretch;overflow:hidden;background-color:var(--calcite-ui-foreground-1);text-align:center;font-weight:var(--calcite-font-weight-medium);color:var(--calcite-ui-text-2);opacity:0;transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s;border-inline:0px solid transparent;border-start-end-radius:0}.alert-queue-count.active{visibility:visible;opacity:1}:host([auto-dismiss])>.alert-queue-count{border-inline-end:0px solid transparent}.alert-dismiss-progress{position:absolute;display:block;inline-size:100%;overflow:hidden;inset-inline:0;inset-block-start:-2px;block-size:2px;border-radius:var(--calcite-border-radius) var(--calcite-border-radius) 0 0}.alert-dismiss-progress:after{position:absolute;inset-block-start:0px;display:block;block-size:2px;content:\"\";background-color:var(--calcite-alert-dismiss-progress-background);inset-inline-end:0}:host([color=blue]) .container{border-block-start-color:var(--calcite-ui-info)}:host([color=blue]) .container .alert-icon{color:var(--calcite-ui-info)}:host([color=red]) .container{border-block-start-color:var(--calcite-ui-danger)}:host([color=red]) .container .alert-icon{color:var(--calcite-ui-danger)}:host([color=yellow]) .container{border-block-start-color:var(--calcite-ui-warning)}:host([color=yellow]) .container .alert-icon{color:var(--calcite-ui-warning)}:host([color=green]) .container{border-block-start-color:var(--calcite-ui-success)}:host([color=green]) .container .alert-icon{color:var(--calcite-ui-success)}:host([auto-dismiss-duration=fast]) .alert-dismiss-progress:after{animation:dismissProgress 6000ms ease-out}:host([auto-dismiss-duration=medium]) .alert-dismiss-progress:after{animation:dismissProgress 10000ms ease-out}:host([auto-dismiss-duration=slow]) .alert-dismiss-progress:after{animation:dismissProgress 14000ms ease-out}@keyframes dismissProgress{0%{inline-size:0px;opacity:0.75}100%{inline-size:100%;opacity:1}}";
  27. const Alert = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  28. constructor() {
  29. super();
  30. this.__registerHost();
  31. this.__attachShadow();
  32. this.calciteAlertBeforeClose = createEvent(this, "calciteAlertBeforeClose", 6);
  33. this.calciteAlertClose = createEvent(this, "calciteAlertClose", 6);
  34. this.calciteAlertBeforeOpen = createEvent(this, "calciteAlertBeforeOpen", 6);
  35. this.calciteAlertOpen = createEvent(this, "calciteAlertOpen", 6);
  36. this.calciteInternalAlertSync = createEvent(this, "calciteInternalAlertSync", 6);
  37. this.calciteInternalAlertRegister = createEvent(this, "calciteInternalAlertRegister", 6);
  38. //--------------------------------------------------------------------------
  39. //
  40. // Properties
  41. //
  42. //---------------------------------------------------------------------------
  43. /**
  44. * When `true`, displays and positions the component.
  45. *
  46. * @deprecated use `open` instead.
  47. */
  48. this.active = false;
  49. /** When `true`, displays and positions the component. */
  50. this.open = false;
  51. /** When `true`, the component closes automatically (recommended for passive, non-blocking alerts). */
  52. this.autoDismiss = false;
  53. /** Specifies the duration before the component automatically closes (only use with `autoDismiss`). */
  54. this.autoDismissDuration = this.autoDismiss ? "medium" : null;
  55. /** Specifies the color for the component (will apply to top border and icon). */
  56. this.color = "blue";
  57. /**
  58. * Specifies the text label for the close button.
  59. *
  60. * @default "Close"
  61. */
  62. this.intlClose = TEXT.intlClose;
  63. /** Specifies the placement of the component */
  64. this.placement = "bottom";
  65. /** Specifies the size of the component. */
  66. this.scale = "m";
  67. //--------------------------------------------------------------------------
  68. //
  69. // Private State/Props
  70. //
  71. //--------------------------------------------------------------------------
  72. this.effectiveLocale = "";
  73. /** the list of queued alerts */
  74. this.queue = [];
  75. /** the count of queued alerts */
  76. this.queueLength = 0;
  77. /** is the alert queued */
  78. this.queued = false;
  79. this.autoDismissTimeoutId = null;
  80. this.trackTimer = Date.now();
  81. this.openTransitionProp = "opacity";
  82. //--------------------------------------------------------------------------
  83. //
  84. // Private Methods
  85. //
  86. //--------------------------------------------------------------------------
  87. this.setTransitionEl = (el) => {
  88. this.transitionEl = el;
  89. connectOpenCloseComponent(this);
  90. };
  91. /** close and emit calciteInternalAlertSync event with the updated queue payload */
  92. this.closeAlert = () => {
  93. this.autoDismissTimeoutId = null;
  94. this.queued = false;
  95. this.open = false;
  96. this.queue = this.queue.filter((el) => el !== this.el);
  97. this.determineActiveAlert();
  98. this.calciteInternalAlertSync.emit({ queue: this.queue });
  99. };
  100. }
  101. activeHandler(value) {
  102. this.open = value;
  103. }
  104. openHandler(value) {
  105. if (this.open && !this.queued) {
  106. this.calciteInternalAlertRegister.emit();
  107. this.active = value;
  108. }
  109. if (!this.open) {
  110. this.queue = this.queue.filter((el) => el !== this.el);
  111. this.calciteInternalAlertSync.emit({ queue: this.queue });
  112. this.active = false;
  113. }
  114. }
  115. updateRequestedIcon() {
  116. this.requestedIcon = setRequestedIcon(StatusIcons, this.icon, this.color);
  117. }
  118. updateDuration() {
  119. if (this.autoDismiss && this.autoDismissTimeoutId) {
  120. window.clearTimeout(this.autoDismissTimeoutId);
  121. this.autoDismissTimeoutId = window.setTimeout(() => this.closeAlert(), DURATIONS[this.autoDismissDuration] - (Date.now() - this.trackTimer));
  122. }
  123. }
  124. //--------------------------------------------------------------------------
  125. //
  126. // Lifecycle
  127. //
  128. //--------------------------------------------------------------------------
  129. connectedCallback() {
  130. connectLocalized(this);
  131. const open = this.open || this.active;
  132. if (open && !this.queued) {
  133. this.activeHandler(open);
  134. this.openHandler(open);
  135. this.calciteInternalAlertRegister.emit();
  136. }
  137. connectOpenCloseComponent(this);
  138. }
  139. componentWillLoad() {
  140. this.requestedIcon = setRequestedIcon(StatusIcons, this.icon, this.color);
  141. }
  142. disconnectedCallback() {
  143. window.clearTimeout(this.autoDismissTimeoutId);
  144. disconnectOpenCloseComponent(this);
  145. disconnectLocalized(this);
  146. }
  147. render() {
  148. const closeButton = (h("button", { "aria-label": this.intlClose, class: "alert-close", onClick: this.closeAlert, ref: (el) => (this.closeButton = el), type: "button" }, h("calcite-icon", { icon: "x", scale: this.scale === "l" ? "m" : "s" })));
  149. numberStringFormatter.numberFormatOptions = {
  150. locale: this.effectiveLocale,
  151. numberingSystem: this.numberingSystem,
  152. signDisplay: "always"
  153. };
  154. const queueNumber = this.queueLength > 2 ? this.queueLength - 1 : 1;
  155. const queueText = numberStringFormatter.numberFormatter.format(queueNumber);
  156. const queueCount = (h("div", { class: `${this.queueLength > 1 ? "active " : ""}alert-queue-count` }, h("calcite-chip", { scale: this.scale, value: queueText }, queueText)));
  157. const { active, autoDismiss, label, placement, queued, requestedIcon } = this;
  158. const role = autoDismiss ? "alert" : "alertdialog";
  159. const hidden = !active;
  160. return (h(Host, { "aria-hidden": toAriaBoolean(hidden), "aria-label": label, "calcite-hydrated-hidden": hidden, role: role }, h("div", { class: {
  161. container: true,
  162. queued,
  163. [placement]: true
  164. }, ref: this.setTransitionEl }, requestedIcon ? (h("div", { class: "alert-icon" }, h("calcite-icon", { icon: requestedIcon, scale: this.scale === "l" ? "m" : "s" }))) : null, h("div", { class: "alert-content" }, h("slot", { name: SLOTS.title }), h("slot", { name: SLOTS.message }), h("slot", { name: SLOTS.link })), queueCount, !autoDismiss ? closeButton : null, active && !queued && autoDismiss ? h("div", { class: "alert-dismiss-progress" }) : null)));
  165. }
  166. // when an alert is opened or closed, update queue and determine active alert
  167. alertSync(event) {
  168. if (this.queue !== event.detail.queue) {
  169. this.queue = event.detail.queue;
  170. }
  171. this.queueLength = this.queue.length;
  172. this.determineActiveAlert();
  173. event.stopPropagation();
  174. }
  175. // when an alert is first registered, trigger a queue sync
  176. alertRegister() {
  177. if (this.open && !this.queue.includes(this.el)) {
  178. this.queued = true;
  179. this.queue.push(this.el);
  180. }
  181. this.calciteInternalAlertSync.emit({ queue: this.queue });
  182. this.determineActiveAlert();
  183. }
  184. //--------------------------------------------------------------------------
  185. //
  186. // Public Methods
  187. //
  188. //--------------------------------------------------------------------------
  189. /** Sets focus on the component. */
  190. async setFocus() {
  191. const alertLinkEl = getSlotted(this.el, { selector: "calcite-link" });
  192. if (!this.closeButton && !alertLinkEl) {
  193. return;
  194. }
  195. else if (alertLinkEl) {
  196. alertLinkEl.setFocus();
  197. }
  198. else if (this.closeButton) {
  199. this.closeButton.focus();
  200. }
  201. }
  202. /** determine which alert is active */
  203. determineActiveAlert() {
  204. var _a;
  205. if (((_a = this.queue) === null || _a === void 0 ? void 0 : _a[0]) === this.el) {
  206. this.openAlert();
  207. if (this.autoDismiss && !this.autoDismissTimeoutId) {
  208. this.trackTimer = Date.now();
  209. this.autoDismissTimeoutId = window.setTimeout(() => this.closeAlert(), DURATIONS[this.autoDismissDuration]);
  210. }
  211. }
  212. else {
  213. return;
  214. }
  215. }
  216. onBeforeOpen() {
  217. this.calciteAlertBeforeOpen.emit();
  218. }
  219. onOpen() {
  220. this.calciteAlertOpen.emit();
  221. }
  222. onBeforeClose() {
  223. this.calciteAlertBeforeClose.emit();
  224. }
  225. onClose() {
  226. this.calciteAlertClose.emit();
  227. }
  228. /** remove queued class after animation completes */
  229. openAlert() {
  230. window.clearTimeout(this.queueTimeout);
  231. this.queueTimeout = window.setTimeout(() => (this.queued = false), 300);
  232. }
  233. get el() { return this; }
  234. static get watchers() { return {
  235. "active": ["activeHandler"],
  236. "open": ["openHandler"],
  237. "icon": ["updateRequestedIcon"],
  238. "color": ["updateRequestedIcon"],
  239. "autoDismissDuration": ["updateDuration"]
  240. }; }
  241. static get style() { return alertCss; }
  242. }, [1, "calcite-alert", {
  243. "active": [1540],
  244. "open": [1540],
  245. "autoDismiss": [516, "auto-dismiss"],
  246. "autoDismissDuration": [513, "auto-dismiss-duration"],
  247. "color": [513],
  248. "icon": [520],
  249. "intlClose": [1, "intl-close"],
  250. "label": [1],
  251. "numberingSystem": [513, "numbering-system"],
  252. "placement": [513],
  253. "scale": [513],
  254. "effectiveLocale": [32],
  255. "queue": [32],
  256. "queueLength": [32],
  257. "queued": [32],
  258. "requestedIcon": [32],
  259. "setFocus": [64]
  260. }, [[8, "calciteInternalAlertSync", "alertSync"], [8, "calciteInternalAlertRegister", "alertRegister"]]]);
  261. function defineCustomElement$1() {
  262. if (typeof customElements === "undefined") {
  263. return;
  264. }
  265. const components = ["calcite-alert", "calcite-chip", "calcite-icon"];
  266. components.forEach(tagName => { switch (tagName) {
  267. case "calcite-alert":
  268. if (!customElements.get(tagName)) {
  269. customElements.define(tagName, Alert);
  270. }
  271. break;
  272. case "calcite-chip":
  273. if (!customElements.get(tagName)) {
  274. defineCustomElement$3();
  275. }
  276. break;
  277. case "calcite-icon":
  278. if (!customElements.get(tagName)) {
  279. defineCustomElement$2();
  280. }
  281. break;
  282. } });
  283. }
  284. defineCustomElement$1();
  285. const CalciteAlert = Alert;
  286. const defineCustomElement = defineCustomElement$1;
  287. export { CalciteAlert, defineCustomElement };