card.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 { getSlotted, toAriaBoolean } from "../../utils/dom";
  8. import { CSS, SLOTS, TEXT } from "./resources";
  9. import { connectConditionalSlotComponent, disconnectConditionalSlotComponent } from "../../utils/conditionalSlot";
  10. /**
  11. * Cards do not include a grid or bounding container
  12. * - cards will expand to fit the width of their container
  13. */
  14. /**
  15. * @slot - A slot for adding subheader/description content.
  16. * @slot thumbnail - A slot for adding a thumbnail to the component.
  17. * @slot title - A slot for adding a title.
  18. * @slot subtitle - A slot for adding a subtitle or short summary.
  19. * @slot footer-leading - A slot for adding a leading footer.
  20. * @slot footer-trailing - A slot for adding a trailing footer.
  21. */
  22. export class Card {
  23. constructor() {
  24. //--------------------------------------------------------------------------
  25. //
  26. // Public Properties
  27. //
  28. //--------------------------------------------------------------------------
  29. /** When `true`, a busy indicator is displayed. */
  30. this.loading = false;
  31. /** When `true`, the component is selected. */
  32. this.selected = false;
  33. /** When `true`, the component is selectable. */
  34. this.selectable = false;
  35. /**
  36. * Accessible name when the component is loading.
  37. *
  38. * @default "Loading"
  39. */
  40. this.intlLoading = TEXT.loading;
  41. /**
  42. * When `selectable` is `true`, the accessible name for the component's checkbox for selection.
  43. *
  44. * @default "Select"
  45. */
  46. this.intlSelect = TEXT.select;
  47. /**
  48. * When `selectable` is `true`, the accessible name for the component's checkbox for deselection.
  49. *
  50. * @default "Deselect"
  51. */
  52. this.intlDeselect = TEXT.deselect;
  53. /** Sets the placement of the thumbnail defined in the `thumbnail` slot. */
  54. this.thumbnailPosition = "block-start";
  55. //--------------------------------------------------------------------------
  56. //
  57. // Private State/Props
  58. //
  59. //--------------------------------------------------------------------------
  60. //--------------------------------------------------------------------------
  61. //
  62. // Private Methods
  63. //
  64. //--------------------------------------------------------------------------
  65. this.cardSelectClick = () => {
  66. this.selectCard();
  67. };
  68. this.cardSelectKeyDown = (event) => {
  69. switch (event.key) {
  70. case " ":
  71. case "Enter":
  72. this.selectCard();
  73. event.preventDefault();
  74. break;
  75. }
  76. };
  77. }
  78. // --------------------------------------------------------------------------
  79. //
  80. // Lifecycle
  81. //
  82. // --------------------------------------------------------------------------
  83. connectedCallback() {
  84. connectConditionalSlotComponent(this);
  85. }
  86. disonnectedCallback() {
  87. disconnectConditionalSlotComponent(this);
  88. }
  89. render() {
  90. const thumbnailInline = this.thumbnailPosition.startsWith("inline");
  91. const thumbnailStart = this.thumbnailPosition.endsWith("start");
  92. return (h("div", { class: { "calcite-card-container": true, inline: thumbnailInline } }, this.loading ? (h("div", { class: "calcite-card-loader-container" }, h("calcite-loader", { active: true, label: this.intlLoading }))) : null, thumbnailStart && this.renderThumbnail(), h("section", { "aria-busy": toAriaBoolean(this.loading), class: { [CSS.container]: true } }, this.selectable ? this.renderCheckbox() : null, this.renderHeader(), h("div", { class: "card-content" }, h("slot", null)), this.renderFooter()), !thumbnailStart && this.renderThumbnail()));
  93. }
  94. selectCard() {
  95. this.selected = !this.selected;
  96. this.calciteCardSelect.emit();
  97. }
  98. renderThumbnail() {
  99. return getSlotted(this.el, SLOTS.thumbnail) ? (h("section", { class: CSS.thumbnailWrapper }, h("slot", { name: SLOTS.thumbnail }))) : null;
  100. }
  101. renderCheckbox() {
  102. const checkboxLabel = this.selected ? this.intlDeselect : this.intlSelect;
  103. return (h("calcite-label", { class: CSS.checkboxWrapper, onClick: this.cardSelectClick, onKeyDown: this.cardSelectKeyDown }, h("calcite-checkbox", { checked: this.selected, label: checkboxLabel })));
  104. }
  105. renderHeader() {
  106. const { el } = this;
  107. const title = getSlotted(el, SLOTS.title);
  108. const subtitle = getSlotted(el, SLOTS.subtitle);
  109. const hasHeader = title || subtitle;
  110. return hasHeader ? (h("header", { class: CSS.header }, h("slot", { name: SLOTS.title }), h("slot", { name: SLOTS.subtitle }))) : null;
  111. }
  112. renderFooter() {
  113. const { el } = this;
  114. const leadingFooter = getSlotted(el, SLOTS.footerLeading);
  115. const trailingFooter = getSlotted(el, SLOTS.footerTrailing);
  116. const hasFooter = leadingFooter || trailingFooter;
  117. return hasFooter ? (h("footer", { class: CSS.footer }, h("slot", { name: SLOTS.footerLeading }), h("slot", { name: SLOTS.footerTrailing }))) : null;
  118. }
  119. static get is() { return "calcite-card"; }
  120. static get encapsulation() { return "shadow"; }
  121. static get originalStyleUrls() {
  122. return {
  123. "$": ["card.scss"]
  124. };
  125. }
  126. static get styleUrls() {
  127. return {
  128. "$": ["card.css"]
  129. };
  130. }
  131. static get properties() {
  132. return {
  133. "loading": {
  134. "type": "boolean",
  135. "mutable": false,
  136. "complexType": {
  137. "original": "boolean",
  138. "resolved": "boolean",
  139. "references": {}
  140. },
  141. "required": false,
  142. "optional": false,
  143. "docs": {
  144. "tags": [],
  145. "text": "When `true`, a busy indicator is displayed."
  146. },
  147. "attribute": "loading",
  148. "reflect": true,
  149. "defaultValue": "false"
  150. },
  151. "selected": {
  152. "type": "boolean",
  153. "mutable": true,
  154. "complexType": {
  155. "original": "boolean",
  156. "resolved": "boolean",
  157. "references": {}
  158. },
  159. "required": false,
  160. "optional": false,
  161. "docs": {
  162. "tags": [],
  163. "text": "When `true`, the component is selected."
  164. },
  165. "attribute": "selected",
  166. "reflect": true,
  167. "defaultValue": "false"
  168. },
  169. "selectable": {
  170. "type": "boolean",
  171. "mutable": false,
  172. "complexType": {
  173. "original": "boolean",
  174. "resolved": "boolean",
  175. "references": {}
  176. },
  177. "required": false,
  178. "optional": false,
  179. "docs": {
  180. "tags": [],
  181. "text": "When `true`, the component is selectable."
  182. },
  183. "attribute": "selectable",
  184. "reflect": true,
  185. "defaultValue": "false"
  186. },
  187. "intlLoading": {
  188. "type": "string",
  189. "mutable": false,
  190. "complexType": {
  191. "original": "string",
  192. "resolved": "string",
  193. "references": {}
  194. },
  195. "required": false,
  196. "optional": true,
  197. "docs": {
  198. "tags": [{
  199. "name": "default",
  200. "text": "\"Loading\""
  201. }],
  202. "text": "Accessible name when the component is loading."
  203. },
  204. "attribute": "intl-loading",
  205. "reflect": false,
  206. "defaultValue": "TEXT.loading"
  207. },
  208. "intlSelect": {
  209. "type": "string",
  210. "mutable": false,
  211. "complexType": {
  212. "original": "string",
  213. "resolved": "string",
  214. "references": {}
  215. },
  216. "required": false,
  217. "optional": false,
  218. "docs": {
  219. "tags": [{
  220. "name": "default",
  221. "text": "\"Select\""
  222. }],
  223. "text": "When `selectable` is `true`, the accessible name for the component's checkbox for selection."
  224. },
  225. "attribute": "intl-select",
  226. "reflect": false,
  227. "defaultValue": "TEXT.select"
  228. },
  229. "intlDeselect": {
  230. "type": "string",
  231. "mutable": false,
  232. "complexType": {
  233. "original": "string",
  234. "resolved": "string",
  235. "references": {}
  236. },
  237. "required": false,
  238. "optional": false,
  239. "docs": {
  240. "tags": [{
  241. "name": "default",
  242. "text": "\"Deselect\""
  243. }],
  244. "text": "When `selectable` is `true`, the accessible name for the component's checkbox for deselection."
  245. },
  246. "attribute": "intl-deselect",
  247. "reflect": false,
  248. "defaultValue": "TEXT.deselect"
  249. },
  250. "thumbnailPosition": {
  251. "type": "string",
  252. "mutable": false,
  253. "complexType": {
  254. "original": "LogicalFlowPosition",
  255. "resolved": "\"block-end\" | \"block-start\" | \"inline-end\" | \"inline-start\"",
  256. "references": {
  257. "LogicalFlowPosition": {
  258. "location": "import",
  259. "path": "../interfaces"
  260. }
  261. }
  262. },
  263. "required": false,
  264. "optional": false,
  265. "docs": {
  266. "tags": [],
  267. "text": "Sets the placement of the thumbnail defined in the `thumbnail` slot."
  268. },
  269. "attribute": "thumbnail-position",
  270. "reflect": true,
  271. "defaultValue": "\"block-start\""
  272. }
  273. };
  274. }
  275. static get events() {
  276. return [{
  277. "method": "calciteCardSelect",
  278. "name": "calciteCardSelect",
  279. "bubbles": true,
  280. "cancelable": false,
  281. "composed": true,
  282. "docs": {
  283. "tags": [],
  284. "text": "Fires when `selectable` is `true` and the component is selected."
  285. },
  286. "complexType": {
  287. "original": "void",
  288. "resolved": "void",
  289. "references": {}
  290. }
  291. }];
  292. }
  293. static get elementRef() { return "el"; }
  294. }