action.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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, Host, Method, Prop, h, forceUpdate } from "@stencil/core";
  7. import { CSS, TEXT } from "./resources";
  8. import { createObserver } from "../../utils/observers";
  9. import { updateHostInteraction } from "../../utils/interactive";
  10. import { toAriaBoolean } from "../../utils/dom";
  11. /**
  12. * @slot - A slot for adding a `calcite-icon`.
  13. */
  14. export class Action {
  15. constructor() {
  16. // --------------------------------------------------------------------------
  17. //
  18. // Properties
  19. //
  20. // --------------------------------------------------------------------------
  21. /**
  22. * Indicates whether the action is highlighted.
  23. */
  24. this.active = false;
  25. /** Specify the appearance style of the action, defaults to solid. */
  26. this.appearance = "solid";
  27. /**
  28. * Compact mode is used internally by components to reduce side padding, e.g. calcite-block-section.
  29. */
  30. this.compact = false;
  31. /**
  32. * When true, disabled prevents interaction. This state shows items with lower opacity/grayed.
  33. */
  34. this.disabled = false;
  35. /**
  36. * Indicates unread changes.
  37. */
  38. this.indicator = false;
  39. /** string to override English loading text
  40. * @default "Loading"
  41. */
  42. this.intlLoading = TEXT.loading;
  43. /**
  44. * When true, content is waiting to be loaded. This state shows a busy indicator.
  45. */
  46. this.loading = false;
  47. /**
  48. * Specifies the size of the action.
  49. */
  50. this.scale = "m";
  51. /**
  52. * Indicates whether the text is displayed.
  53. */
  54. this.textEnabled = false;
  55. this.mutationObserver = createObserver("mutation", () => forceUpdate(this));
  56. // --------------------------------------------------------------------------
  57. //
  58. // Private Methods
  59. //
  60. // --------------------------------------------------------------------------
  61. this.calciteActionClickHandler = () => {
  62. if (!this.disabled) {
  63. this.calciteActionClick.emit();
  64. }
  65. };
  66. }
  67. // --------------------------------------------------------------------------
  68. //
  69. // Lifecycle
  70. //
  71. // --------------------------------------------------------------------------
  72. connectedCallback() {
  73. var _a;
  74. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true, subtree: true });
  75. }
  76. disconnectedCallback() {
  77. var _a;
  78. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  79. }
  80. componentDidRender() {
  81. updateHostInteraction(this);
  82. }
  83. // --------------------------------------------------------------------------
  84. //
  85. // Methods
  86. //
  87. // --------------------------------------------------------------------------
  88. /** Sets focus on the component. */
  89. async setFocus() {
  90. this.buttonEl.focus();
  91. }
  92. // --------------------------------------------------------------------------
  93. //
  94. // Render Methods
  95. //
  96. // --------------------------------------------------------------------------
  97. renderTextContainer() {
  98. const { text, textEnabled } = this;
  99. const textContainerClasses = {
  100. [CSS.textContainer]: true,
  101. [CSS.textContainerVisible]: textEnabled
  102. };
  103. return text ? (h("div", { class: textContainerClasses, key: "text-container" }, text)) : null;
  104. }
  105. renderIconContainer() {
  106. var _a;
  107. const { loading, icon, scale, el, intlLoading } = this;
  108. const iconScale = scale === "l" ? "m" : "s";
  109. const loaderScale = scale === "l" ? "l" : "m";
  110. const calciteLoaderNode = loading ? (h("calcite-loader", { active: true, inline: true, label: intlLoading, scale: loaderScale })) : null;
  111. const calciteIconNode = icon ? h("calcite-icon", { icon: icon, scale: iconScale }) : null;
  112. const iconNode = calciteLoaderNode || calciteIconNode;
  113. const hasIconToDisplay = iconNode || ((_a = el.children) === null || _a === void 0 ? void 0 : _a.length);
  114. const slotContainerNode = (h("div", { class: {
  115. [CSS.slotContainer]: true,
  116. [CSS.slotContainerHidden]: loading
  117. } },
  118. h("slot", null)));
  119. return hasIconToDisplay ? (h("div", { "aria-hidden": "true", class: CSS.iconContainer, key: "icon-container" },
  120. iconNode,
  121. slotContainerNode)) : null;
  122. }
  123. render() {
  124. const { compact, disabled, loading, textEnabled, label, text } = this;
  125. const ariaLabel = label || text;
  126. const buttonClasses = {
  127. [CSS.button]: true,
  128. [CSS.buttonTextVisible]: textEnabled,
  129. [CSS.buttonCompact]: compact
  130. };
  131. return (h(Host, { onClick: this.calciteActionClickHandler },
  132. h("button", { "aria-busy": toAriaBoolean(loading), "aria-disabled": toAriaBoolean(disabled), "aria-label": ariaLabel, class: buttonClasses, disabled: disabled, ref: (buttonEl) => (this.buttonEl = buttonEl) },
  133. this.renderIconContainer(),
  134. this.renderTextContainer())));
  135. }
  136. static get is() { return "calcite-action"; }
  137. static get encapsulation() { return "shadow"; }
  138. static get originalStyleUrls() { return {
  139. "$": ["action.scss"]
  140. }; }
  141. static get styleUrls() { return {
  142. "$": ["action.css"]
  143. }; }
  144. static get properties() { return {
  145. "active": {
  146. "type": "boolean",
  147. "mutable": false,
  148. "complexType": {
  149. "original": "boolean",
  150. "resolved": "boolean",
  151. "references": {}
  152. },
  153. "required": false,
  154. "optional": false,
  155. "docs": {
  156. "tags": [],
  157. "text": "Indicates whether the action is highlighted."
  158. },
  159. "attribute": "active",
  160. "reflect": true,
  161. "defaultValue": "false"
  162. },
  163. "alignment": {
  164. "type": "string",
  165. "mutable": false,
  166. "complexType": {
  167. "original": "Alignment",
  168. "resolved": "\"center\" | \"end\" | \"start\"",
  169. "references": {
  170. "Alignment": {
  171. "location": "import",
  172. "path": "../interfaces"
  173. }
  174. }
  175. },
  176. "required": false,
  177. "optional": true,
  178. "docs": {
  179. "tags": [],
  180. "text": "Optionally specify the horizontal alignment of button elements with text content."
  181. },
  182. "attribute": "alignment",
  183. "reflect": true
  184. },
  185. "appearance": {
  186. "type": "string",
  187. "mutable": false,
  188. "complexType": {
  189. "original": "Extract<\"solid\" | \"clear\", Appearance>",
  190. "resolved": "\"clear\" | \"solid\"",
  191. "references": {
  192. "Extract": {
  193. "location": "global"
  194. },
  195. "Appearance": {
  196. "location": "import",
  197. "path": "../interfaces"
  198. }
  199. }
  200. },
  201. "required": false,
  202. "optional": false,
  203. "docs": {
  204. "tags": [],
  205. "text": "Specify the appearance style of the action, defaults to solid."
  206. },
  207. "attribute": "appearance",
  208. "reflect": true,
  209. "defaultValue": "\"solid\""
  210. },
  211. "compact": {
  212. "type": "boolean",
  213. "mutable": false,
  214. "complexType": {
  215. "original": "boolean",
  216. "resolved": "boolean",
  217. "references": {}
  218. },
  219. "required": false,
  220. "optional": false,
  221. "docs": {
  222. "tags": [],
  223. "text": "Compact mode is used internally by components to reduce side padding, e.g. calcite-block-section."
  224. },
  225. "attribute": "compact",
  226. "reflect": true,
  227. "defaultValue": "false"
  228. },
  229. "disabled": {
  230. "type": "boolean",
  231. "mutable": false,
  232. "complexType": {
  233. "original": "boolean",
  234. "resolved": "boolean",
  235. "references": {}
  236. },
  237. "required": false,
  238. "optional": false,
  239. "docs": {
  240. "tags": [],
  241. "text": "When true, disabled prevents interaction. This state shows items with lower opacity/grayed."
  242. },
  243. "attribute": "disabled",
  244. "reflect": true,
  245. "defaultValue": "false"
  246. },
  247. "icon": {
  248. "type": "string",
  249. "mutable": false,
  250. "complexType": {
  251. "original": "string",
  252. "resolved": "string",
  253. "references": {}
  254. },
  255. "required": false,
  256. "optional": true,
  257. "docs": {
  258. "tags": [],
  259. "text": "The name of the icon to display. The value of this property must match the icon name from https://esri.github.io/calcite-ui-icons/."
  260. },
  261. "attribute": "icon",
  262. "reflect": false
  263. },
  264. "indicator": {
  265. "type": "boolean",
  266. "mutable": false,
  267. "complexType": {
  268. "original": "boolean",
  269. "resolved": "boolean",
  270. "references": {}
  271. },
  272. "required": false,
  273. "optional": false,
  274. "docs": {
  275. "tags": [],
  276. "text": "Indicates unread changes."
  277. },
  278. "attribute": "indicator",
  279. "reflect": true,
  280. "defaultValue": "false"
  281. },
  282. "intlLoading": {
  283. "type": "string",
  284. "mutable": false,
  285. "complexType": {
  286. "original": "string",
  287. "resolved": "string",
  288. "references": {}
  289. },
  290. "required": false,
  291. "optional": true,
  292. "docs": {
  293. "tags": [{
  294. "name": "default",
  295. "text": "\"Loading\""
  296. }],
  297. "text": "string to override English loading text"
  298. },
  299. "attribute": "intl-loading",
  300. "reflect": false,
  301. "defaultValue": "TEXT.loading"
  302. },
  303. "label": {
  304. "type": "string",
  305. "mutable": false,
  306. "complexType": {
  307. "original": "string",
  308. "resolved": "string",
  309. "references": {}
  310. },
  311. "required": false,
  312. "optional": true,
  313. "docs": {
  314. "tags": [],
  315. "text": "The label of the action. If no label is provided, the label inherits what's provided for the `text` prop."
  316. },
  317. "attribute": "label",
  318. "reflect": false
  319. },
  320. "loading": {
  321. "type": "boolean",
  322. "mutable": false,
  323. "complexType": {
  324. "original": "boolean",
  325. "resolved": "boolean",
  326. "references": {}
  327. },
  328. "required": false,
  329. "optional": false,
  330. "docs": {
  331. "tags": [],
  332. "text": "When true, content is waiting to be loaded. This state shows a busy indicator."
  333. },
  334. "attribute": "loading",
  335. "reflect": true,
  336. "defaultValue": "false"
  337. },
  338. "scale": {
  339. "type": "string",
  340. "mutable": false,
  341. "complexType": {
  342. "original": "Scale",
  343. "resolved": "\"l\" | \"m\" | \"s\"",
  344. "references": {
  345. "Scale": {
  346. "location": "import",
  347. "path": "../interfaces"
  348. }
  349. }
  350. },
  351. "required": false,
  352. "optional": false,
  353. "docs": {
  354. "tags": [],
  355. "text": "Specifies the size of the action."
  356. },
  357. "attribute": "scale",
  358. "reflect": true,
  359. "defaultValue": "\"m\""
  360. },
  361. "text": {
  362. "type": "string",
  363. "mutable": false,
  364. "complexType": {
  365. "original": "string",
  366. "resolved": "string",
  367. "references": {}
  368. },
  369. "required": true,
  370. "optional": false,
  371. "docs": {
  372. "tags": [],
  373. "text": "Text that accompanies the action icon."
  374. },
  375. "attribute": "text",
  376. "reflect": false
  377. },
  378. "textEnabled": {
  379. "type": "boolean",
  380. "mutable": false,
  381. "complexType": {
  382. "original": "boolean",
  383. "resolved": "boolean",
  384. "references": {}
  385. },
  386. "required": false,
  387. "optional": false,
  388. "docs": {
  389. "tags": [],
  390. "text": "Indicates whether the text is displayed."
  391. },
  392. "attribute": "text-enabled",
  393. "reflect": true,
  394. "defaultValue": "false"
  395. }
  396. }; }
  397. static get events() { return [{
  398. "method": "calciteActionClick",
  399. "name": "calciteActionClick",
  400. "bubbles": true,
  401. "cancelable": true,
  402. "composed": true,
  403. "docs": {
  404. "tags": [{
  405. "name": "deprecated",
  406. "text": "use onClick instead."
  407. }],
  408. "text": "Emitted when the action has been clicked."
  409. },
  410. "complexType": {
  411. "original": "any",
  412. "resolved": "any",
  413. "references": {}
  414. }
  415. }]; }
  416. static get methods() { return {
  417. "setFocus": {
  418. "complexType": {
  419. "signature": "() => Promise<void>",
  420. "parameters": [],
  421. "references": {
  422. "Promise": {
  423. "location": "global"
  424. }
  425. },
  426. "return": "Promise<void>"
  427. },
  428. "docs": {
  429. "text": "Sets focus on the component.",
  430. "tags": []
  431. }
  432. }
  433. }; }
  434. static get elementRef() { return "el"; }
  435. }