notice.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 { h } 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. /**
  12. * Notices are intended to be used to present users with important-but-not-crucial contextual tips or copy. Because
  13. * notices are displayed inline, a common use case is displaying them on page-load to present users with short hints or contextual copy.
  14. * They are optionally dismissible - useful for keeping track of whether or not a user has dismissed the notice. You can also choose not
  15. * to display a notice on page load and set the "active" attribute as needed to contextually provide inline messaging to users.
  16. */
  17. /**
  18. * @slot title - A slot for adding the title.
  19. * @slot message - A slot for adding the message.
  20. * @slot link - A slot for adding actions to take, such as: undo, try again, link to page, etc.
  21. * @slot actions-end - A slot for adding actions to the end of the component. It is recommended to use two or less actions.
  22. */
  23. export class Notice {
  24. constructor() {
  25. //--------------------------------------------------------------------------
  26. //
  27. // Properties
  28. //
  29. //---------------------------------------------------------------------------
  30. /**
  31. * When `true`, the component is active.
  32. *
  33. * @deprecated Use `open` instead.
  34. */
  35. this.active = false;
  36. /** When `true`, the component is visible. */
  37. this.open = false;
  38. /** The color for the component's top border and icon. */
  39. this.color = "blue";
  40. /**
  41. * When `true`, a close button is added to the component.
  42. *
  43. * @deprecated use `closable` instead.
  44. */
  45. this.dismissible = false;
  46. /** When `true`, a close button is added to the component. */
  47. this.closable = false;
  48. /**
  49. * Accessible name for the close button.
  50. *
  51. * @default "Close"
  52. */
  53. this.intlClose = TEXT.close;
  54. /** Specifies the size of the component. */
  55. this.scale = "m";
  56. /** Specifies the width of the component. */
  57. this.width = "auto";
  58. //--------------------------------------------------------------------------
  59. //
  60. // Private Methods
  61. //
  62. //--------------------------------------------------------------------------
  63. this.close = () => {
  64. this.open = false;
  65. this.calciteNoticeClose.emit();
  66. };
  67. }
  68. activeHandler(value) {
  69. this.open = value;
  70. }
  71. openHandler(value) {
  72. this.active = value;
  73. }
  74. handleDismissible(value) {
  75. this.closable = value;
  76. }
  77. handleClosable(value) {
  78. this.dismissible = value;
  79. }
  80. updateRequestedIcon() {
  81. this.requestedIcon = setRequestedIcon(StatusIcons, this.icon, this.color);
  82. }
  83. //--------------------------------------------------------------------------
  84. //
  85. // Lifecycle
  86. //
  87. //--------------------------------------------------------------------------
  88. connectedCallback() {
  89. connectConditionalSlotComponent(this);
  90. const isOpen = this.active || this.open;
  91. if (isOpen) {
  92. this.activeHandler(isOpen);
  93. this.openHandler(isOpen);
  94. }
  95. if (this.dismissible) {
  96. this.handleDismissible(this.dismissible);
  97. }
  98. if (this.closable) {
  99. this.handleClosable(this.closable);
  100. }
  101. }
  102. disconnectedCallback() {
  103. disconnectConditionalSlotComponent(this);
  104. }
  105. componentWillLoad() {
  106. this.requestedIcon = setRequestedIcon(StatusIcons, this.icon, this.color);
  107. }
  108. render() {
  109. const { el } = this;
  110. const closeButton = (h("button", { "aria-label": this.intlClose, class: CSS.close, onClick: this.close, ref: (el) => (this.closeButton = el) }, h("calcite-icon", { icon: "x", scale: this.scale === "l" ? "m" : "s" })));
  111. const hasActionEnd = getSlotted(el, SLOTS.actionsEnd);
  112. return (h("div", { class: CSS.container }, this.requestedIcon ? (h("div", { class: CSS.icon }, h("calcite-icon", { icon: this.requestedIcon, scale: this.scale === "l" ? "m" : "s" }))) : null, h("div", { class: CSS.content }, h("slot", { name: SLOTS.title }), h("slot", { name: SLOTS.message }), h("slot", { name: SLOTS.link })), hasActionEnd ? (h("div", { class: CSS.actionsEnd }, h("slot", { name: SLOTS.actionsEnd }))) : null, this.closable ? closeButton : null));
  113. }
  114. //--------------------------------------------------------------------------
  115. //
  116. // Public Methods
  117. //
  118. //--------------------------------------------------------------------------
  119. /** Sets focus on the component. */
  120. async setFocus() {
  121. const noticeLinkEl = this.el.querySelector("calcite-link");
  122. if (!this.closeButton && !noticeLinkEl) {
  123. return;
  124. }
  125. if (noticeLinkEl) {
  126. noticeLinkEl.setFocus();
  127. }
  128. else if (this.closeButton) {
  129. this.closeButton.focus();
  130. }
  131. }
  132. static get is() { return "calcite-notice"; }
  133. static get encapsulation() { return "shadow"; }
  134. static get originalStyleUrls() {
  135. return {
  136. "$": ["notice.scss"]
  137. };
  138. }
  139. static get styleUrls() {
  140. return {
  141. "$": ["notice.css"]
  142. };
  143. }
  144. static get properties() {
  145. return {
  146. "active": {
  147. "type": "boolean",
  148. "mutable": true,
  149. "complexType": {
  150. "original": "boolean",
  151. "resolved": "boolean",
  152. "references": {}
  153. },
  154. "required": false,
  155. "optional": false,
  156. "docs": {
  157. "tags": [{
  158. "name": "deprecated",
  159. "text": "Use `open` instead."
  160. }],
  161. "text": "When `true`, the component is active."
  162. },
  163. "attribute": "active",
  164. "reflect": true,
  165. "defaultValue": "false"
  166. },
  167. "open": {
  168. "type": "boolean",
  169. "mutable": true,
  170. "complexType": {
  171. "original": "boolean",
  172. "resolved": "boolean",
  173. "references": {}
  174. },
  175. "required": false,
  176. "optional": false,
  177. "docs": {
  178. "tags": [],
  179. "text": "When `true`, the component is visible."
  180. },
  181. "attribute": "open",
  182. "reflect": true,
  183. "defaultValue": "false"
  184. },
  185. "color": {
  186. "type": "string",
  187. "mutable": false,
  188. "complexType": {
  189. "original": "StatusColor",
  190. "resolved": "\"blue\" | \"green\" | \"red\" | \"yellow\"",
  191. "references": {
  192. "StatusColor": {
  193. "location": "import",
  194. "path": "../alert/interfaces"
  195. }
  196. }
  197. },
  198. "required": false,
  199. "optional": false,
  200. "docs": {
  201. "tags": [],
  202. "text": "The color for the component's top border and icon."
  203. },
  204. "attribute": "color",
  205. "reflect": true,
  206. "defaultValue": "\"blue\""
  207. },
  208. "dismissible": {
  209. "type": "boolean",
  210. "mutable": false,
  211. "complexType": {
  212. "original": "boolean",
  213. "resolved": "boolean",
  214. "references": {}
  215. },
  216. "required": false,
  217. "optional": true,
  218. "docs": {
  219. "tags": [{
  220. "name": "deprecated",
  221. "text": "use `closable` instead."
  222. }],
  223. "text": "When `true`, a close button is added to the component."
  224. },
  225. "attribute": "dismissible",
  226. "reflect": true,
  227. "defaultValue": "false"
  228. },
  229. "closable": {
  230. "type": "boolean",
  231. "mutable": false,
  232. "complexType": {
  233. "original": "boolean",
  234. "resolved": "boolean",
  235. "references": {}
  236. },
  237. "required": false,
  238. "optional": true,
  239. "docs": {
  240. "tags": [],
  241. "text": "When `true`, a close button is added to the component."
  242. },
  243. "attribute": "closable",
  244. "reflect": true,
  245. "defaultValue": "false"
  246. },
  247. "icon": {
  248. "type": "any",
  249. "mutable": false,
  250. "complexType": {
  251. "original": "string | boolean",
  252. "resolved": "boolean | string",
  253. "references": {}
  254. },
  255. "required": false,
  256. "optional": false,
  257. "docs": {
  258. "tags": [],
  259. "text": "When `true`, shows a default recommended icon. Alternatively, pass a Calcite UI Icon name to display a specific icon."
  260. },
  261. "attribute": "icon",
  262. "reflect": true
  263. },
  264. "intlClose": {
  265. "type": "string",
  266. "mutable": false,
  267. "complexType": {
  268. "original": "string",
  269. "resolved": "string",
  270. "references": {}
  271. },
  272. "required": false,
  273. "optional": false,
  274. "docs": {
  275. "tags": [{
  276. "name": "default",
  277. "text": "\"Close\""
  278. }],
  279. "text": "Accessible name for the close button."
  280. },
  281. "attribute": "intl-close",
  282. "reflect": false,
  283. "defaultValue": "TEXT.close"
  284. },
  285. "scale": {
  286. "type": "string",
  287. "mutable": false,
  288. "complexType": {
  289. "original": "Scale",
  290. "resolved": "\"l\" | \"m\" | \"s\"",
  291. "references": {
  292. "Scale": {
  293. "location": "import",
  294. "path": "../interfaces"
  295. }
  296. }
  297. },
  298. "required": false,
  299. "optional": false,
  300. "docs": {
  301. "tags": [],
  302. "text": "Specifies the size of the component."
  303. },
  304. "attribute": "scale",
  305. "reflect": true,
  306. "defaultValue": "\"m\""
  307. },
  308. "width": {
  309. "type": "string",
  310. "mutable": false,
  311. "complexType": {
  312. "original": "Width",
  313. "resolved": "\"auto\" | \"full\" | \"half\"",
  314. "references": {
  315. "Width": {
  316. "location": "import",
  317. "path": "../interfaces"
  318. }
  319. }
  320. },
  321. "required": false,
  322. "optional": false,
  323. "docs": {
  324. "tags": [],
  325. "text": "Specifies the width of the component."
  326. },
  327. "attribute": "width",
  328. "reflect": true,
  329. "defaultValue": "\"auto\""
  330. }
  331. };
  332. }
  333. static get events() {
  334. return [{
  335. "method": "calciteNoticeClose",
  336. "name": "calciteNoticeClose",
  337. "bubbles": true,
  338. "cancelable": false,
  339. "composed": true,
  340. "docs": {
  341. "tags": [],
  342. "text": "Fired when the component is closed."
  343. },
  344. "complexType": {
  345. "original": "void",
  346. "resolved": "void",
  347. "references": {}
  348. }
  349. }, {
  350. "method": "calciteNoticeOpen",
  351. "name": "calciteNoticeOpen",
  352. "bubbles": true,
  353. "cancelable": false,
  354. "composed": true,
  355. "docs": {
  356. "tags": [],
  357. "text": "Fired when the component is opened."
  358. },
  359. "complexType": {
  360. "original": "void",
  361. "resolved": "void",
  362. "references": {}
  363. }
  364. }];
  365. }
  366. static get methods() {
  367. return {
  368. "setFocus": {
  369. "complexType": {
  370. "signature": "() => Promise<void>",
  371. "parameters": [],
  372. "references": {
  373. "Promise": {
  374. "location": "global"
  375. }
  376. },
  377. "return": "Promise<void>"
  378. },
  379. "docs": {
  380. "text": "Sets focus on the component.",
  381. "tags": []
  382. }
  383. }
  384. };
  385. }
  386. static get elementRef() { return "el"; }
  387. static get watchers() {
  388. return [{
  389. "propName": "active",
  390. "methodName": "activeHandler"
  391. }, {
  392. "propName": "open",
  393. "methodName": "openHandler"
  394. }, {
  395. "propName": "dismissible",
  396. "methodName": "handleDismissible"
  397. }, {
  398. "propName": "closable",
  399. "methodName": "handleClosable"
  400. }, {
  401. "propName": "icon",
  402. "methodName": "updateRequestedIcon"
  403. }, {
  404. "propName": "color",
  405. "methodName": "updateRequestedIcon"
  406. }];
  407. }
  408. }