block.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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, Host } from "@stencil/core";
  7. import { CSS, HEADING_LEVEL, ICONS, SLOTS, TEXT } from "./resources";
  8. import { getSlotted, toAriaBoolean } from "../../utils/dom";
  9. import { Heading } from "../functional/Heading";
  10. import { connectConditionalSlotComponent, disconnectConditionalSlotComponent } from "../../utils/conditionalSlot";
  11. import { updateHostInteraction } from "../../utils/interactive";
  12. import { guid } from "../../utils/guid";
  13. /**
  14. * @slot - A slot for adding content to the component.
  15. * @slot icon - A slot for adding a leading header icon with `calcite-icon`.
  16. * @slot control - A slot for adding a single HTML input element in a header.
  17. * @slot header-menu-actions - A slot for adding an overflow menu with `calcite-action`s inside a dropdown.
  18. */
  19. export class Block {
  20. constructor() {
  21. // --------------------------------------------------------------------------
  22. //
  23. // Properties
  24. //
  25. // --------------------------------------------------------------------------
  26. /**
  27. * When `true`, the component is collapsible.
  28. */
  29. this.collapsible = false;
  30. /**
  31. * When `true`, interaction is prevented and the component is displayed with lower opacity.
  32. */
  33. this.disabled = false;
  34. /**
  35. * When `true`, displays a drag handle in the header.
  36. */
  37. this.dragHandle = false;
  38. /**
  39. * Accessible name for the component's collapse button.
  40. *
  41. * @default "Collapse"
  42. */
  43. this.intlCollapse = TEXT.collapse;
  44. /**
  45. * Accessible name for the component's expand button.
  46. *
  47. * @default "Expand"
  48. */
  49. this.intlExpand = TEXT.expand;
  50. /**
  51. * Accessible name when the component is loading.
  52. *
  53. * @default "Loading"
  54. */
  55. this.intlLoading = TEXT.loading;
  56. /**
  57. * Accessible name for the component's options button.
  58. *
  59. * @default "Options"
  60. */
  61. this.intlOptions = TEXT.options;
  62. /**
  63. * When `true`, a busy indicator is displayed.
  64. */
  65. this.loading = false;
  66. /**
  67. * When `true`, expands the component and its contents.
  68. */
  69. this.open = false;
  70. /**
  71. * When `true`, removes padding for the slotted content.
  72. *
  73. * @deprecated Use `--calcite-block-padding` CSS variable instead.
  74. */
  75. this.disablePadding = false;
  76. this.guid = guid();
  77. // --------------------------------------------------------------------------
  78. //
  79. // Private Methods
  80. //
  81. // --------------------------------------------------------------------------
  82. this.onHeaderClick = () => {
  83. this.open = !this.open;
  84. this.calciteBlockToggle.emit();
  85. };
  86. }
  87. //--------------------------------------------------------------------------
  88. //
  89. // Lifecycle
  90. //
  91. //--------------------------------------------------------------------------
  92. componentDidRender() {
  93. updateHostInteraction(this);
  94. }
  95. // --------------------------------------------------------------------------
  96. //
  97. // Lifecycle
  98. //
  99. // --------------------------------------------------------------------------
  100. connectedCallback() {
  101. connectConditionalSlotComponent(this);
  102. }
  103. disconnectedCallback() {
  104. disconnectConditionalSlotComponent(this);
  105. }
  106. // --------------------------------------------------------------------------
  107. //
  108. // Render Methods
  109. //
  110. // --------------------------------------------------------------------------
  111. renderScrim() {
  112. const { loading } = this;
  113. const defaultSlot = h("slot", null);
  114. return [loading ? h("calcite-scrim", { loading: loading }) : null, defaultSlot];
  115. }
  116. renderIcon() {
  117. const { el, status } = this;
  118. const showingLoadingStatus = this.loading && !this.open;
  119. const statusIcon = showingLoadingStatus ? ICONS.refresh : ICONS[status];
  120. const hasIcon = getSlotted(el, SLOTS.icon) || statusIcon;
  121. const iconEl = !statusIcon ? (h("slot", { key: "icon-slot", name: SLOTS.icon })) : (h("calcite-icon", { class: {
  122. [CSS.statusIcon]: true,
  123. [CSS.valid]: status == "valid",
  124. [CSS.invalid]: status == "invalid",
  125. [CSS.loading]: showingLoadingStatus
  126. }, icon: statusIcon, scale: "m" }));
  127. return hasIcon ? h("div", { class: CSS.icon }, iconEl) : null;
  128. }
  129. renderTitle() {
  130. const { heading, headingLevel, summary, description } = this;
  131. return heading || summary || description ? (h("div", { class: CSS.title }, h(Heading, { class: CSS.heading, level: headingLevel || HEADING_LEVEL }, heading), summary || description ? (h("div", { class: CSS.description }, summary || description)) : null)) : null;
  132. }
  133. render() {
  134. const { collapsible, el, intlCollapse, intlExpand, loading, open, intlLoading } = this;
  135. const toggleLabel = open ? intlCollapse || TEXT.collapse : intlExpand || TEXT.expand;
  136. const headerContent = (h("header", { class: CSS.header }, this.renderIcon(), this.renderTitle()));
  137. const hasControl = !!getSlotted(el, SLOTS.control);
  138. const hasMenuActions = !!getSlotted(el, SLOTS.headerMenuActions);
  139. const collapseIcon = open ? ICONS.opened : ICONS.closed;
  140. const { guid } = this;
  141. const regionId = `${guid}-region`;
  142. const buttonId = `${guid}-button`;
  143. const headerNode = (h("div", { class: CSS.headerContainer }, this.dragHandle ? h("calcite-handle", null) : null, collapsible ? (h("button", { "aria-controls": regionId, "aria-expanded": collapsible ? toAriaBoolean(open) : null, "aria-label": toggleLabel, class: CSS.toggle, id: buttonId, onClick: this.onHeaderClick, title: toggleLabel }, headerContent, !hasControl && !hasMenuActions ? (h("calcite-icon", { "aria-hidden": "true", class: CSS.toggleIcon, icon: collapseIcon, scale: "s" })) : null)) : (headerContent), loading ? (h("calcite-loader", { inline: true, "is-active": true, label: intlLoading })) : hasControl ? (h("div", { class: CSS.controlContainer }, h("slot", { name: SLOTS.control }))) : null, hasMenuActions ? (h("calcite-action-menu", { label: this.intlOptions || TEXT.options }, h("slot", { name: SLOTS.headerMenuActions }))) : null));
  144. return (h(Host, null, h("article", { "aria-busy": toAriaBoolean(loading), class: {
  145. [CSS.container]: true
  146. } }, headerNode, h("section", { "aria-expanded": toAriaBoolean(open), "aria-labelledby": buttonId, class: {
  147. [CSS.content]: true,
  148. [CSS.contentSpaced]: !this.disablePadding
  149. }, hidden: !open, id: regionId }, this.renderScrim()))));
  150. }
  151. static get is() { return "calcite-block"; }
  152. static get encapsulation() { return "shadow"; }
  153. static get originalStyleUrls() {
  154. return {
  155. "$": ["block.scss"]
  156. };
  157. }
  158. static get styleUrls() {
  159. return {
  160. "$": ["block.css"]
  161. };
  162. }
  163. static get properties() {
  164. return {
  165. "collapsible": {
  166. "type": "boolean",
  167. "mutable": false,
  168. "complexType": {
  169. "original": "boolean",
  170. "resolved": "boolean",
  171. "references": {}
  172. },
  173. "required": false,
  174. "optional": false,
  175. "docs": {
  176. "tags": [],
  177. "text": "When `true`, the component is collapsible."
  178. },
  179. "attribute": "collapsible",
  180. "reflect": true,
  181. "defaultValue": "false"
  182. },
  183. "disabled": {
  184. "type": "boolean",
  185. "mutable": false,
  186. "complexType": {
  187. "original": "boolean",
  188. "resolved": "boolean",
  189. "references": {}
  190. },
  191. "required": false,
  192. "optional": false,
  193. "docs": {
  194. "tags": [],
  195. "text": "When `true`, interaction is prevented and the component is displayed with lower opacity."
  196. },
  197. "attribute": "disabled",
  198. "reflect": true,
  199. "defaultValue": "false"
  200. },
  201. "dragHandle": {
  202. "type": "boolean",
  203. "mutable": false,
  204. "complexType": {
  205. "original": "boolean",
  206. "resolved": "boolean",
  207. "references": {}
  208. },
  209. "required": false,
  210. "optional": false,
  211. "docs": {
  212. "tags": [],
  213. "text": "When `true`, displays a drag handle in the header."
  214. },
  215. "attribute": "drag-handle",
  216. "reflect": true,
  217. "defaultValue": "false"
  218. },
  219. "heading": {
  220. "type": "string",
  221. "mutable": false,
  222. "complexType": {
  223. "original": "string",
  224. "resolved": "string",
  225. "references": {}
  226. },
  227. "required": true,
  228. "optional": false,
  229. "docs": {
  230. "tags": [],
  231. "text": "The component header text."
  232. },
  233. "attribute": "heading",
  234. "reflect": false
  235. },
  236. "headingLevel": {
  237. "type": "number",
  238. "mutable": false,
  239. "complexType": {
  240. "original": "HeadingLevel",
  241. "resolved": "1 | 2 | 3 | 4 | 5 | 6",
  242. "references": {
  243. "HeadingLevel": {
  244. "location": "import",
  245. "path": "../functional/Heading"
  246. }
  247. }
  248. },
  249. "required": false,
  250. "optional": false,
  251. "docs": {
  252. "tags": [],
  253. "text": "Specifies the number at which section headings should start."
  254. },
  255. "attribute": "heading-level",
  256. "reflect": true
  257. },
  258. "intlCollapse": {
  259. "type": "string",
  260. "mutable": false,
  261. "complexType": {
  262. "original": "string",
  263. "resolved": "string",
  264. "references": {}
  265. },
  266. "required": false,
  267. "optional": true,
  268. "docs": {
  269. "tags": [{
  270. "name": "default",
  271. "text": "\"Collapse\""
  272. }],
  273. "text": "Accessible name for the component's collapse button."
  274. },
  275. "attribute": "intl-collapse",
  276. "reflect": false,
  277. "defaultValue": "TEXT.collapse"
  278. },
  279. "intlExpand": {
  280. "type": "string",
  281. "mutable": false,
  282. "complexType": {
  283. "original": "string",
  284. "resolved": "string",
  285. "references": {}
  286. },
  287. "required": false,
  288. "optional": true,
  289. "docs": {
  290. "tags": [{
  291. "name": "default",
  292. "text": "\"Expand\""
  293. }],
  294. "text": "Accessible name for the component's expand button."
  295. },
  296. "attribute": "intl-expand",
  297. "reflect": false,
  298. "defaultValue": "TEXT.expand"
  299. },
  300. "intlLoading": {
  301. "type": "string",
  302. "mutable": false,
  303. "complexType": {
  304. "original": "string",
  305. "resolved": "string",
  306. "references": {}
  307. },
  308. "required": false,
  309. "optional": true,
  310. "docs": {
  311. "tags": [{
  312. "name": "default",
  313. "text": "\"Loading\""
  314. }],
  315. "text": "Accessible name when the component is loading."
  316. },
  317. "attribute": "intl-loading",
  318. "reflect": false,
  319. "defaultValue": "TEXT.loading"
  320. },
  321. "intlOptions": {
  322. "type": "string",
  323. "mutable": false,
  324. "complexType": {
  325. "original": "string",
  326. "resolved": "string",
  327. "references": {}
  328. },
  329. "required": false,
  330. "optional": true,
  331. "docs": {
  332. "tags": [{
  333. "name": "default",
  334. "text": "\"Options\""
  335. }],
  336. "text": "Accessible name for the component's options button."
  337. },
  338. "attribute": "intl-options",
  339. "reflect": false,
  340. "defaultValue": "TEXT.options"
  341. },
  342. "loading": {
  343. "type": "boolean",
  344. "mutable": false,
  345. "complexType": {
  346. "original": "boolean",
  347. "resolved": "boolean",
  348. "references": {}
  349. },
  350. "required": false,
  351. "optional": false,
  352. "docs": {
  353. "tags": [],
  354. "text": "When `true`, a busy indicator is displayed."
  355. },
  356. "attribute": "loading",
  357. "reflect": true,
  358. "defaultValue": "false"
  359. },
  360. "open": {
  361. "type": "boolean",
  362. "mutable": true,
  363. "complexType": {
  364. "original": "boolean",
  365. "resolved": "boolean",
  366. "references": {}
  367. },
  368. "required": false,
  369. "optional": false,
  370. "docs": {
  371. "tags": [],
  372. "text": "When `true`, expands the component and its contents."
  373. },
  374. "attribute": "open",
  375. "reflect": true,
  376. "defaultValue": "false"
  377. },
  378. "status": {
  379. "type": "string",
  380. "mutable": false,
  381. "complexType": {
  382. "original": "Status",
  383. "resolved": "\"idle\" | \"invalid\" | \"valid\"",
  384. "references": {
  385. "Status": {
  386. "location": "import",
  387. "path": "../interfaces"
  388. }
  389. }
  390. },
  391. "required": false,
  392. "optional": true,
  393. "docs": {
  394. "tags": [],
  395. "text": "Displays a status-related indicator icon."
  396. },
  397. "attribute": "status",
  398. "reflect": true
  399. },
  400. "summary": {
  401. "type": "string",
  402. "mutable": false,
  403. "complexType": {
  404. "original": "string",
  405. "resolved": "string",
  406. "references": {}
  407. },
  408. "required": false,
  409. "optional": false,
  410. "docs": {
  411. "tags": [{
  412. "name": "deprecated",
  413. "text": "use `description` instead"
  414. }],
  415. "text": "A description for the component, which displays below the heading."
  416. },
  417. "attribute": "summary",
  418. "reflect": false
  419. },
  420. "description": {
  421. "type": "string",
  422. "mutable": false,
  423. "complexType": {
  424. "original": "string",
  425. "resolved": "string",
  426. "references": {}
  427. },
  428. "required": false,
  429. "optional": false,
  430. "docs": {
  431. "tags": [],
  432. "text": "A description for the component, which displays below the heading."
  433. },
  434. "attribute": "description",
  435. "reflect": false
  436. },
  437. "disablePadding": {
  438. "type": "boolean",
  439. "mutable": false,
  440. "complexType": {
  441. "original": "boolean",
  442. "resolved": "boolean",
  443. "references": {}
  444. },
  445. "required": false,
  446. "optional": false,
  447. "docs": {
  448. "tags": [{
  449. "name": "deprecated",
  450. "text": "Use `--calcite-block-padding` CSS variable instead."
  451. }],
  452. "text": "When `true`, removes padding for the slotted content."
  453. },
  454. "attribute": "disable-padding",
  455. "reflect": true,
  456. "defaultValue": "false"
  457. }
  458. };
  459. }
  460. static get events() {
  461. return [{
  462. "method": "calciteBlockToggle",
  463. "name": "calciteBlockToggle",
  464. "bubbles": true,
  465. "cancelable": false,
  466. "composed": true,
  467. "docs": {
  468. "tags": [],
  469. "text": "Emits when the component's header is clicked."
  470. },
  471. "complexType": {
  472. "original": "void",
  473. "resolved": "void",
  474. "references": {}
  475. }
  476. }];
  477. }
  478. static get elementRef() { return "el"; }
  479. }