calcite-icon.entry.js 7.6 KB

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