calcite-icon.cjs.entry.js 7.6 KB

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