calcite-icon.entry.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 { a as getAssetPath, r as registerInstance, h, H as Host, g as getElement } from './index-1f9b54dc.js';
  7. import { a as getElementDir, t as toAriaBoolean } from './dom-8f0a9ff2.js';
  8. import { c as createObserver } from './observers-9f44e9b3.js';
  9. import './resources-9c476cb6.js';
  10. import './guid-9f15e57a.js';
  11. const CSS = {
  12. icon: "icon",
  13. flipRtl: "flip-rtl"
  14. };
  15. /**
  16. * Icon data cache.
  17. * Exported for testing purposes.
  18. *
  19. * @private
  20. */
  21. const iconCache = {};
  22. /**
  23. * Icon request cache.
  24. * Exported for testing purposes.
  25. *
  26. * @private
  27. */
  28. const requestCache = {};
  29. const scaleToPx = {
  30. s: 16,
  31. m: 24,
  32. l: 32
  33. };
  34. async function fetchIcon({ icon, scale }) {
  35. const size = scaleToPx[scale];
  36. const name = normalizeIconName(icon);
  37. const filled = name.charAt(name.length - 1) === "F";
  38. const iconName = filled ? name.substring(0, name.length - 1) : name;
  39. const id = `${iconName}${size}${filled ? "F" : ""}`;
  40. if (iconCache[id]) {
  41. return iconCache[id];
  42. }
  43. if (!requestCache[id]) {
  44. requestCache[id] = fetch(getAssetPath(`./assets/icon/${id}.json`))
  45. .then((resp) => resp.json())
  46. .catch(() => {
  47. console.error(`"${id}" is not a valid calcite-ui-icon name`);
  48. return "";
  49. });
  50. }
  51. const path = await requestCache[id];
  52. iconCache[id] = path;
  53. return path;
  54. }
  55. /**
  56. * Normalize the icon name to match the path data module exports.
  57. * Exported for testing purposes.
  58. *
  59. * @param name
  60. * @private
  61. */
  62. function normalizeIconName(name) {
  63. const numberLeadingName = !isNaN(Number(name.charAt(0)));
  64. const parts = name.split("-");
  65. if (parts.length === 1) {
  66. return numberLeadingName ? `i${name}` : name;
  67. }
  68. return parts
  69. .map((part, index) => {
  70. if (index === 0) {
  71. return numberLeadingName ? `i${part.toUpperCase()}` : part;
  72. }
  73. return part.charAt(0).toUpperCase() + part.slice(1);
  74. })
  75. .join("");
  76. }
  77. 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}";
  78. const Icon = class {
  79. constructor(hostRef) {
  80. registerInstance(this, hostRef);
  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 (!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 getElement(this); }
  160. static get watchers() { return {
  161. "icon": ["loadIconPathData"],
  162. "scale": ["loadIconPathData"]
  163. }; }
  164. };
  165. Icon.style = iconCss;
  166. export { Icon as calcite_icon };