icon.js 7.2 KB

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