calcite-icon.cjs.entry.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. 'use strict';
  7. Object.defineProperty(exports, '__esModule', { value: true });
  8. const index = require('./index-a0010f96.js');
  9. const dom = require('./dom-2ec8c9ed.js');
  10. const observers = require('./observers-5706326b.js');
  11. require('./resources-b5a5f8a7.js');
  12. require('./guid-f4f03a7a.js');
  13. const CSS = {
  14. icon: "icon",
  15. flipRtl: "flip-rtl"
  16. };
  17. /**
  18. * Icon data cache.
  19. * Exported for testing purposes.
  20. *
  21. * @private
  22. */
  23. const iconCache = {};
  24. /**
  25. * Icon request cache.
  26. * Exported for testing purposes.
  27. *
  28. * @private
  29. */
  30. const requestCache = {};
  31. const scaleToPx = {
  32. s: 16,
  33. m: 24,
  34. l: 32
  35. };
  36. async function fetchIcon({ icon, scale }) {
  37. const size = scaleToPx[scale];
  38. const name = normalizeIconName(icon);
  39. const filled = name.charAt(name.length - 1) === "F";
  40. const iconName = filled ? name.substring(0, name.length - 1) : name;
  41. const id = `${iconName}${size}${filled ? "F" : ""}`;
  42. if (iconCache[id]) {
  43. return iconCache[id];
  44. }
  45. if (!requestCache[id]) {
  46. requestCache[id] = fetch(index.getAssetPath(`./assets/icon/${id}.json`))
  47. .then((resp) => resp.json())
  48. .catch(() => {
  49. console.error(`"${id}" is not a valid calcite-ui-icon name`);
  50. return "";
  51. });
  52. }
  53. const path = await requestCache[id];
  54. iconCache[id] = path;
  55. return path;
  56. }
  57. /**
  58. * Normalize the icon name to match the path data module exports.
  59. * Exported for testing purposes.
  60. *
  61. * @param name
  62. * @private
  63. */
  64. function normalizeIconName(name) {
  65. const numberLeadingName = !isNaN(Number(name.charAt(0)));
  66. const parts = name.split("-");
  67. if (parts.length === 1) {
  68. return numberLeadingName ? `i${name}` : name;
  69. }
  70. return parts
  71. .map((part, index) => {
  72. if (index === 0) {
  73. return numberLeadingName ? `i${part.toUpperCase()}` : part;
  74. }
  75. return part.charAt(0).toUpperCase() + part.slice(1);
  76. })
  77. .join("");
  78. }
  79. 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}";
  80. const Icon = class {
  81. constructor(hostRef) {
  82. index.registerInstance(this, hostRef);
  83. //--------------------------------------------------------------------------
  84. //
  85. // Properties
  86. //
  87. //--------------------------------------------------------------------------
  88. /**
  89. * 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/.
  90. */
  91. this.icon = null;
  92. /**
  93. * When true, the icon will be flipped when the element direction is 'rtl'.
  94. */
  95. this.flipRtl = false;
  96. /**
  97. * Icon scale.
  98. */
  99. this.scale = "m";
  100. this.visible = false;
  101. }
  102. //--------------------------------------------------------------------------
  103. //
  104. // Lifecycle
  105. //
  106. //--------------------------------------------------------------------------
  107. connectedCallback() {
  108. this.waitUntilVisible(() => {
  109. this.visible = true;
  110. this.loadIconPathData();
  111. });
  112. }
  113. disconnectedCallback() {
  114. var _a;
  115. (_a = this.intersectionObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  116. this.intersectionObserver = null;
  117. }
  118. async componentWillLoad() {
  119. this.loadIconPathData();
  120. }
  121. render() {
  122. const { el, flipRtl, pathData, scale, textLabel } = this;
  123. const dir = dom.getElementDir(el);
  124. const size = scaleToPx[scale];
  125. const semantic = !!textLabel;
  126. const paths = [].concat(pathData || "");
  127. return (index.h(index.Host, { "aria-hidden": dom.toAriaBoolean(!semantic), "aria-label": semantic ? textLabel : null, role: semantic ? "img" : null }, index.h("svg", { class: {
  128. [CSS.flipRtl]: dir === "rtl" && flipRtl,
  129. svg: true
  130. }, fill: "currentColor", height: "100%", viewBox: `0 0 ${size} ${size}`, width: "100%", xmlns: "http://www.w3.org/2000/svg" }, paths.map((path) => typeof path === "string" ? (index.h("path", { d: path })) : (index.h("path", { d: path.d, opacity: "opacity" in path ? path.opacity : 1 }))))));
  131. }
  132. //--------------------------------------------------------------------------
  133. //
  134. // Private Methods
  135. //
  136. //--------------------------------------------------------------------------
  137. async loadIconPathData() {
  138. const { icon, scale, visible } = this;
  139. if (!icon || !visible) {
  140. return;
  141. }
  142. this.pathData = await fetchIcon({ icon, scale });
  143. }
  144. waitUntilVisible(callback) {
  145. this.intersectionObserver = observers.createObserver("intersection", (entries) => {
  146. entries.forEach((entry) => {
  147. if (entry.isIntersecting) {
  148. this.intersectionObserver.disconnect();
  149. this.intersectionObserver = null;
  150. callback();
  151. }
  152. });
  153. }, { rootMargin: "50px" });
  154. if (!this.intersectionObserver) {
  155. callback();
  156. return;
  157. }
  158. this.intersectionObserver.observe(this.el);
  159. }
  160. static get assetsDirs() { return ["assets"]; }
  161. get el() { return index.getElement(this); }
  162. static get watchers() { return {
  163. "icon": ["loadIconPathData"],
  164. "scale": ["loadIconPathData"]
  165. }; }
  166. };
  167. Icon.style = iconCss;
  168. exports.calcite_icon = Icon;