icon.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 { getAssetPath, proxyCustomElement, HTMLElement, h, Host, Build } from '@stencil/core/internal/client';
  7. import { a as getElementDir, t as toAriaBoolean } from './dom.js';
  8. import { c as createObserver } from './observers.js';
  9. const CSS = {
  10. icon: "icon",
  11. flipRtl: "flip-rtl"
  12. };
  13. /**
  14. * Icon data cache.
  15. * Exported for testing purposes.
  16. * @private
  17. */
  18. const iconCache = {};
  19. /**
  20. * Icon request cache.
  21. * Exported for testing purposes.
  22. * @private
  23. */
  24. const requestCache = {};
  25. const scaleToPx = {
  26. s: 16,
  27. m: 24,
  28. l: 32
  29. };
  30. async function fetchIcon({ icon, scale }) {
  31. const size = scaleToPx[scale];
  32. const name = normalizeIconName(icon);
  33. const filled = name.charAt(name.length - 1) === "F";
  34. const iconName = filled ? name.substring(0, name.length - 1) : name;
  35. const id = `${iconName}${size}${filled ? "F" : ""}`;
  36. if (iconCache[id]) {
  37. return iconCache[id];
  38. }
  39. if (!requestCache[id]) {
  40. requestCache[id] = fetch(getAssetPath(`./assets/icon/${id}.json`))
  41. .then((resp) => resp.json())
  42. .catch(() => {
  43. console.error(`"${id}" is not a valid calcite-ui-icon name`);
  44. return "";
  45. });
  46. }
  47. const path = await requestCache[id];
  48. iconCache[id] = path;
  49. return path;
  50. }
  51. /**
  52. * Normalize the icon name to match the path data module exports.
  53. * Exported for testing purposes.
  54. * @private
  55. */
  56. function normalizeIconName(name) {
  57. const numberLeadingName = !isNaN(Number(name.charAt(0)));
  58. const parts = name.split("-");
  59. if (parts.length === 1) {
  60. return numberLeadingName ? `i${name}` : name;
  61. }
  62. return parts
  63. .map((part, index) => {
  64. if (index === 0) {
  65. return numberLeadingName ? `i${part.toUpperCase()}` : part;
  66. }
  67. return part.charAt(0).toUpperCase() + part.slice(1);
  68. })
  69. .join("");
  70. }
  71. const iconCss = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{display:-ms-inline-flexbox;display:inline-flex;color:var(--calcite-ui-icon-color)}:host([scale=s]){height:1rem;width:1rem;min-width:1rem;min-height:1rem}:host([scale=m]){height:1.5rem;width:1.5rem;min-width:1.5rem;min-height:1.5rem}:host([scale=l]){height:2rem;width:2rem;min-width:2rem;min-height:2rem}.flip-rtl{-webkit-transform:scaleX(-1);transform:scaleX(-1)}.svg{display:block}";
  72. const Icon = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  73. constructor() {
  74. super();
  75. this.__registerHost();
  76. this.__attachShadow();
  77. //--------------------------------------------------------------------------
  78. //
  79. // Properties
  80. //
  81. //--------------------------------------------------------------------------
  82. /**
  83. * 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/.
  84. */
  85. this.icon = null;
  86. /**
  87. * When true, the icon will be flipped when the element direction is 'rtl'.
  88. */
  89. this.flipRtl = false;
  90. /**
  91. * Icon scale.
  92. */
  93. this.scale = "m";
  94. this.visible = false;
  95. }
  96. //--------------------------------------------------------------------------
  97. //
  98. // Lifecycle
  99. //
  100. //--------------------------------------------------------------------------
  101. connectedCallback() {
  102. this.waitUntilVisible(() => {
  103. this.visible = true;
  104. this.loadIconPathData();
  105. });
  106. }
  107. disconnectedCallback() {
  108. var _a;
  109. (_a = this.intersectionObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  110. this.intersectionObserver = null;
  111. }
  112. async componentWillLoad() {
  113. this.loadIconPathData();
  114. }
  115. render() {
  116. const { el, flipRtl, pathData, scale, textLabel } = this;
  117. const dir = getElementDir(el);
  118. const size = scaleToPx[scale];
  119. const semantic = !!textLabel;
  120. const paths = [].concat(pathData || "");
  121. return (h(Host, { "aria-hidden": toAriaBoolean(!semantic), "aria-label": semantic ? textLabel : null, role: semantic ? "img" : null }, h("svg", { class: {
  122. [CSS.flipRtl]: dir === "rtl" && flipRtl,
  123. svg: true
  124. }, fill: "currentColor", height: "100%", viewBox: `0 0 ${size} ${size}`, width: "100%", xmlns: "http://www.w3.org/2000/svg" }, paths.map((path) => typeof path === "string" ? (h("path", { d: path })) : (h("path", { d: path.d, opacity: "opacity" in path ? path.opacity : 1 }))))));
  125. }
  126. //--------------------------------------------------------------------------
  127. //
  128. // Private Methods
  129. //
  130. //--------------------------------------------------------------------------
  131. async loadIconPathData() {
  132. const { icon, scale, visible } = this;
  133. if (!Build.isBrowser || !icon || !visible) {
  134. return;
  135. }
  136. this.pathData = await fetchIcon({ icon, scale });
  137. }
  138. waitUntilVisible(callback) {
  139. this.intersectionObserver = createObserver("intersection", (entries) => {
  140. entries.forEach((entry) => {
  141. if (entry.isIntersecting) {
  142. this.intersectionObserver.disconnect();
  143. this.intersectionObserver = null;
  144. callback();
  145. }
  146. });
  147. }, { rootMargin: "50px" });
  148. if (!this.intersectionObserver) {
  149. callback();
  150. return;
  151. }
  152. this.intersectionObserver.observe(this.el);
  153. }
  154. static get assetsDirs() { return ["assets"]; }
  155. get el() { return this; }
  156. static get watchers() { return {
  157. "icon": ["loadIconPathData"],
  158. "scale": ["loadIconPathData"]
  159. }; }
  160. static get style() { return iconCss; }
  161. }, [1, "calcite-icon", {
  162. "icon": [513],
  163. "flipRtl": [516, "flip-rtl"],
  164. "scale": [513],
  165. "textLabel": [1, "text-label"],
  166. "pathData": [32],
  167. "visible": [32]
  168. }]);
  169. function defineCustomElement() {
  170. if (typeof customElements === "undefined") {
  171. return;
  172. }
  173. const components = ["calcite-icon"];
  174. components.forEach(tagName => { switch (tagName) {
  175. case "calcite-icon":
  176. if (!customElements.get(tagName)) {
  177. customElements.define(tagName, Icon);
  178. }
  179. break;
  180. } });
  181. }
  182. defineCustomElement();
  183. export { Icon as I, defineCustomElement as d };