loader.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 { guid } from "../../utils/guid";
  8. export class Loader {
  9. constructor() {
  10. //--------------------------------------------------------------------------
  11. //
  12. // Properties
  13. //
  14. //--------------------------------------------------------------------------
  15. /** When `true`, the component is active. */
  16. this.active = false;
  17. /** When `true`, displays smaller and appears to the left of the text. */
  18. this.inline = false;
  19. /** Specifies the size of the component. */
  20. this.scale = "m";
  21. /** The component's value. Valid only for `"determinate"` indicators. Percent complete of 100. */
  22. this.value = 0;
  23. /** Text that displays under the component's indicator. */
  24. this.text = "";
  25. /**
  26. * Disables spacing around the component.
  27. *
  28. * @deprecated Use `--calcite-loader-padding` CSS variable instead.
  29. */
  30. this.noPadding = false;
  31. }
  32. //--------------------------------------------------------------------------
  33. //
  34. // Lifecycle
  35. //
  36. //--------------------------------------------------------------------------
  37. render() {
  38. const { el, inline, label, scale, text, type, value } = this;
  39. const id = el.id || guid();
  40. const radiusRatio = 0.45;
  41. const size = inline ? this.getInlineSize(scale) : this.getSize(scale);
  42. const radius = size * radiusRatio;
  43. const viewbox = `0 0 ${size} ${size}`;
  44. const isDeterminate = type === "determinate";
  45. const circumference = 2 * radius * Math.PI;
  46. const progress = (value / 100) * circumference;
  47. const remaining = circumference - progress;
  48. const valueNow = Math.floor(value);
  49. const hostAttributes = {
  50. "aria-valuenow": valueNow,
  51. "aria-valuemin": 0,
  52. "aria-valuemax": 100,
  53. complete: valueNow === 100
  54. };
  55. const svgAttributes = { r: radius, cx: size / 2, cy: size / 2 };
  56. const determinateStyle = { "stroke-dasharray": `${progress} ${remaining}` };
  57. return (h(Host, { "aria-label": label, id: id, role: "progressbar", ...(isDeterminate ? hostAttributes : {}) }, h("div", { class: "loader__svgs" }, h("svg", { class: "loader__svg loader__svg--1", viewBox: viewbox }, h("circle", { ...svgAttributes })), h("svg", { class: "loader__svg loader__svg--2", viewBox: viewbox }, h("circle", { ...svgAttributes })), h("svg", { class: "loader__svg loader__svg--3", viewBox: viewbox, ...(isDeterminate ? { style: determinateStyle } : {}) }, h("circle", { ...svgAttributes }))), text && h("div", { class: "loader__text" }, text), isDeterminate && h("div", { class: "loader__percentage" }, value)));
  58. }
  59. //--------------------------------------------------------------------------
  60. //
  61. // Private Methods
  62. //
  63. //--------------------------------------------------------------------------
  64. /**
  65. * Return the proper sizes based on the scale property
  66. *
  67. * @param scale
  68. */
  69. getSize(scale) {
  70. return {
  71. s: 32,
  72. m: 56,
  73. l: 80
  74. }[scale];
  75. }
  76. getInlineSize(scale) {
  77. return {
  78. s: 12,
  79. m: 16,
  80. l: 20
  81. }[scale];
  82. }
  83. static get is() { return "calcite-loader"; }
  84. static get encapsulation() { return "shadow"; }
  85. static get originalStyleUrls() {
  86. return {
  87. "$": ["loader.scss"]
  88. };
  89. }
  90. static get styleUrls() {
  91. return {
  92. "$": ["loader.css"]
  93. };
  94. }
  95. static get properties() {
  96. return {
  97. "active": {
  98. "type": "boolean",
  99. "mutable": false,
  100. "complexType": {
  101. "original": "boolean",
  102. "resolved": "boolean",
  103. "references": {}
  104. },
  105. "required": false,
  106. "optional": false,
  107. "docs": {
  108. "tags": [],
  109. "text": "When `true`, the component is active."
  110. },
  111. "attribute": "active",
  112. "reflect": true,
  113. "defaultValue": "false"
  114. },
  115. "inline": {
  116. "type": "boolean",
  117. "mutable": false,
  118. "complexType": {
  119. "original": "boolean",
  120. "resolved": "boolean",
  121. "references": {}
  122. },
  123. "required": false,
  124. "optional": false,
  125. "docs": {
  126. "tags": [],
  127. "text": "When `true`, displays smaller and appears to the left of the text."
  128. },
  129. "attribute": "inline",
  130. "reflect": true,
  131. "defaultValue": "false"
  132. },
  133. "label": {
  134. "type": "string",
  135. "mutable": false,
  136. "complexType": {
  137. "original": "string",
  138. "resolved": "string",
  139. "references": {}
  140. },
  141. "required": true,
  142. "optional": false,
  143. "docs": {
  144. "tags": [],
  145. "text": "Accessible name for the component."
  146. },
  147. "attribute": "label",
  148. "reflect": false
  149. },
  150. "scale": {
  151. "type": "string",
  152. "mutable": false,
  153. "complexType": {
  154. "original": "Scale",
  155. "resolved": "\"l\" | \"m\" | \"s\"",
  156. "references": {
  157. "Scale": {
  158. "location": "import",
  159. "path": "../interfaces"
  160. }
  161. }
  162. },
  163. "required": false,
  164. "optional": false,
  165. "docs": {
  166. "tags": [],
  167. "text": "Specifies the size of the component."
  168. },
  169. "attribute": "scale",
  170. "reflect": true,
  171. "defaultValue": "\"m\""
  172. },
  173. "type": {
  174. "type": "string",
  175. "mutable": false,
  176. "complexType": {
  177. "original": "\"indeterminate\" | \"determinate\"",
  178. "resolved": "\"determinate\" | \"indeterminate\"",
  179. "references": {}
  180. },
  181. "required": false,
  182. "optional": false,
  183. "docs": {
  184. "tags": [],
  185. "text": "Specifies the component type.\n\nUse `\"indeterminate\"` if finding actual progress value is impossible."
  186. },
  187. "attribute": "type",
  188. "reflect": true
  189. },
  190. "value": {
  191. "type": "number",
  192. "mutable": false,
  193. "complexType": {
  194. "original": "number",
  195. "resolved": "number",
  196. "references": {}
  197. },
  198. "required": false,
  199. "optional": false,
  200. "docs": {
  201. "tags": [],
  202. "text": "The component's value. Valid only for `\"determinate\"` indicators. Percent complete of 100."
  203. },
  204. "attribute": "value",
  205. "reflect": false,
  206. "defaultValue": "0"
  207. },
  208. "text": {
  209. "type": "string",
  210. "mutable": false,
  211. "complexType": {
  212. "original": "string",
  213. "resolved": "string",
  214. "references": {}
  215. },
  216. "required": false,
  217. "optional": true,
  218. "docs": {
  219. "tags": [],
  220. "text": "Text that displays under the component's indicator."
  221. },
  222. "attribute": "text",
  223. "reflect": false,
  224. "defaultValue": "\"\""
  225. },
  226. "noPadding": {
  227. "type": "boolean",
  228. "mutable": false,
  229. "complexType": {
  230. "original": "boolean",
  231. "resolved": "boolean",
  232. "references": {}
  233. },
  234. "required": false,
  235. "optional": false,
  236. "docs": {
  237. "tags": [{
  238. "name": "deprecated",
  239. "text": "Use `--calcite-loader-padding` CSS variable instead."
  240. }],
  241. "text": "Disables spacing around the component."
  242. },
  243. "attribute": "no-padding",
  244. "reflect": true,
  245. "defaultValue": "false"
  246. }
  247. };
  248. }
  249. static get elementRef() { return "el"; }
  250. }