notice.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.82
  5. */
  6. import { Component, Element, Event, h, Method, Prop, Watch } from "@stencil/core";
  7. import { CSS, SLOTS, TEXT } from "./resources";
  8. import { StatusIcons } from "../alert/interfaces";
  9. import { getSlotted, setRequestedIcon } from "../../utils/dom";
  10. import { connectConditionalSlotComponent, disconnectConditionalSlotComponent } from "../../utils/conditionalSlot";
  11. /** Notices are intended to be used to present users with important-but-not-crucial contextual tips or copy. Because
  12. * notices are displayed inline, a common use case is displaying them on page-load to present users with short hints or contextual copy.
  13. * They are optionally dismissible - useful for keeping track of whether or not a user has dismissed the notice. You can also choose not
  14. * to display a notice on page load and set the "active" attribute as needed to contextually provide inline messaging to users.
  15. */
  16. /**
  17. * @slot title - Title of the notice (optional)
  18. * @slot message - Main text of the notice
  19. * @slot link - Optional action to take from the notice (undo, try again, link to page, etc.)
  20. * @slot actions-end - Allows adding a `calcite-action` at the end of the notice. It is recommended to use 2 or less actions.
  21. */
  22. export class Notice {
  23. constructor() {
  24. //--------------------------------------------------------------------------
  25. //
  26. // Properties
  27. //
  28. //---------------------------------------------------------------------------
  29. /** Is the notice currently active or not */
  30. this.active = false;
  31. /** Color for the notice (will apply to top border and icon) */
  32. this.color = "blue";
  33. /** Optionally show a button the user can click to dismiss the notice */
  34. this.dismissible = false;
  35. /** String for the close button.
  36. * @default "Close"
  37. */
  38. this.intlClose = TEXT.close;
  39. /** specify the scale of the notice, defaults to m */
  40. this.scale = "m";
  41. /** specify the width of the notice, defaults to auto */
  42. this.width = "auto";
  43. //--------------------------------------------------------------------------
  44. //
  45. // Private Methods
  46. //
  47. //--------------------------------------------------------------------------
  48. this.close = () => {
  49. this.active = false;
  50. this.calciteNoticeClose.emit();
  51. };
  52. }
  53. updateRequestedIcon() {
  54. this.requestedIcon = setRequestedIcon(StatusIcons, this.icon, this.color);
  55. }
  56. //--------------------------------------------------------------------------
  57. //
  58. // Lifecycle
  59. //
  60. //--------------------------------------------------------------------------
  61. connectedCallback() {
  62. connectConditionalSlotComponent(this);
  63. }
  64. disconnectedCallback() {
  65. disconnectConditionalSlotComponent(this);
  66. }
  67. componentWillLoad() {
  68. this.requestedIcon = setRequestedIcon(StatusIcons, this.icon, this.color);
  69. }
  70. render() {
  71. const { el } = this;
  72. const closeButton = (h("button", { "aria-label": this.intlClose, class: CSS.close, onClick: this.close, ref: (el) => (this.closeButton = el) },
  73. h("calcite-icon", { icon: "x", scale: this.scale === "l" ? "m" : "s" })));
  74. const hasActionEnd = getSlotted(el, SLOTS.actionsEnd);
  75. return (h("div", { class: CSS.container },
  76. this.requestedIcon ? (h("div", { class: CSS.icon },
  77. h("calcite-icon", { icon: this.requestedIcon, scale: this.scale === "l" ? "m" : "s" }))) : null,
  78. h("div", { class: CSS.content },
  79. h("slot", { name: SLOTS.title }),
  80. h("slot", { name: SLOTS.message }),
  81. h("slot", { name: SLOTS.link })),
  82. hasActionEnd ? (h("div", { class: CSS.actionsEnd },
  83. h("slot", { name: SLOTS.actionsEnd }))) : null,
  84. this.dismissible ? closeButton : null));
  85. }
  86. //--------------------------------------------------------------------------
  87. //
  88. // Public Methods
  89. //
  90. //--------------------------------------------------------------------------
  91. /** Sets focus on the component. */
  92. async setFocus() {
  93. const noticeLinkEl = this.el.querySelector("calcite-link");
  94. if (!this.closeButton && !noticeLinkEl) {
  95. return;
  96. }
  97. if (noticeLinkEl) {
  98. noticeLinkEl.setFocus();
  99. }
  100. else if (this.closeButton) {
  101. this.closeButton.focus();
  102. }
  103. }
  104. static get is() { return "calcite-notice"; }
  105. static get encapsulation() { return "shadow"; }
  106. static get originalStyleUrls() { return {
  107. "$": ["notice.scss"]
  108. }; }
  109. static get styleUrls() { return {
  110. "$": ["notice.css"]
  111. }; }
  112. static get properties() { return {
  113. "active": {
  114. "type": "boolean",
  115. "mutable": true,
  116. "complexType": {
  117. "original": "boolean",
  118. "resolved": "boolean",
  119. "references": {}
  120. },
  121. "required": false,
  122. "optional": false,
  123. "docs": {
  124. "tags": [],
  125. "text": "Is the notice currently active or not"
  126. },
  127. "attribute": "active",
  128. "reflect": true,
  129. "defaultValue": "false"
  130. },
  131. "color": {
  132. "type": "string",
  133. "mutable": false,
  134. "complexType": {
  135. "original": "StatusColor",
  136. "resolved": "\"blue\" | \"green\" | \"red\" | \"yellow\"",
  137. "references": {
  138. "StatusColor": {
  139. "location": "import",
  140. "path": "../alert/interfaces"
  141. }
  142. }
  143. },
  144. "required": false,
  145. "optional": false,
  146. "docs": {
  147. "tags": [],
  148. "text": "Color for the notice (will apply to top border and icon)"
  149. },
  150. "attribute": "color",
  151. "reflect": true,
  152. "defaultValue": "\"blue\""
  153. },
  154. "dismissible": {
  155. "type": "boolean",
  156. "mutable": false,
  157. "complexType": {
  158. "original": "boolean",
  159. "resolved": "boolean",
  160. "references": {}
  161. },
  162. "required": false,
  163. "optional": false,
  164. "docs": {
  165. "tags": [],
  166. "text": "Optionally show a button the user can click to dismiss the notice"
  167. },
  168. "attribute": "dismissible",
  169. "reflect": true,
  170. "defaultValue": "false"
  171. },
  172. "icon": {
  173. "type": "any",
  174. "mutable": false,
  175. "complexType": {
  176. "original": "string | boolean",
  177. "resolved": "boolean | string",
  178. "references": {}
  179. },
  180. "required": false,
  181. "optional": false,
  182. "docs": {
  183. "tags": [],
  184. "text": "when used as a boolean set to true, show a default recommended icon. You can\nalso pass a calcite-ui-icon name to this prop to display a requested icon"
  185. },
  186. "attribute": "icon",
  187. "reflect": true
  188. },
  189. "intlClose": {
  190. "type": "string",
  191. "mutable": false,
  192. "complexType": {
  193. "original": "string",
  194. "resolved": "string",
  195. "references": {}
  196. },
  197. "required": false,
  198. "optional": false,
  199. "docs": {
  200. "tags": [{
  201. "name": "default",
  202. "text": "\"Close\""
  203. }],
  204. "text": "String for the close button."
  205. },
  206. "attribute": "intl-close",
  207. "reflect": false,
  208. "defaultValue": "TEXT.close"
  209. },
  210. "scale": {
  211. "type": "string",
  212. "mutable": false,
  213. "complexType": {
  214. "original": "Scale",
  215. "resolved": "\"l\" | \"m\" | \"s\"",
  216. "references": {
  217. "Scale": {
  218. "location": "import",
  219. "path": "../interfaces"
  220. }
  221. }
  222. },
  223. "required": false,
  224. "optional": false,
  225. "docs": {
  226. "tags": [],
  227. "text": "specify the scale of the notice, defaults to m"
  228. },
  229. "attribute": "scale",
  230. "reflect": true,
  231. "defaultValue": "\"m\""
  232. },
  233. "width": {
  234. "type": "string",
  235. "mutable": false,
  236. "complexType": {
  237. "original": "Width",
  238. "resolved": "\"auto\" | \"full\" | \"half\"",
  239. "references": {
  240. "Width": {
  241. "location": "import",
  242. "path": "../interfaces"
  243. }
  244. }
  245. },
  246. "required": false,
  247. "optional": false,
  248. "docs": {
  249. "tags": [],
  250. "text": "specify the width of the notice, defaults to auto"
  251. },
  252. "attribute": "width",
  253. "reflect": true,
  254. "defaultValue": "\"auto\""
  255. }
  256. }; }
  257. static get events() { return [{
  258. "method": "calciteNoticeClose",
  259. "name": "calciteNoticeClose",
  260. "bubbles": true,
  261. "cancelable": true,
  262. "composed": true,
  263. "docs": {
  264. "tags": [],
  265. "text": "Fired when an notice is closed"
  266. },
  267. "complexType": {
  268. "original": "any",
  269. "resolved": "any",
  270. "references": {}
  271. }
  272. }, {
  273. "method": "calciteNoticeOpen",
  274. "name": "calciteNoticeOpen",
  275. "bubbles": true,
  276. "cancelable": true,
  277. "composed": true,
  278. "docs": {
  279. "tags": [],
  280. "text": "Fired when an Notice is opened"
  281. },
  282. "complexType": {
  283. "original": "any",
  284. "resolved": "any",
  285. "references": {}
  286. }
  287. }]; }
  288. static get methods() { return {
  289. "setFocus": {
  290. "complexType": {
  291. "signature": "() => Promise<void>",
  292. "parameters": [],
  293. "references": {
  294. "Promise": {
  295. "location": "global"
  296. }
  297. },
  298. "return": "Promise<void>"
  299. },
  300. "docs": {
  301. "text": "Sets focus on the component.",
  302. "tags": []
  303. }
  304. }
  305. }; }
  306. static get elementRef() { return "el"; }
  307. static get watchers() { return [{
  308. "propName": "icon",
  309. "methodName": "updateRequestedIcon"
  310. }, {
  311. "propName": "color",
  312. "methodName": "updateRequestedIcon"
  313. }]; }
  314. }