@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
163 lines (157 loc) • 6.56 kB
JavaScript
import { e as getAssetPath, r as registerInstance, h, H as Host, g as getElement } from './index-aa8afca4.js';
import { g as getElementDir } from './dom-466af3c7.js';
import './guid-09142681.js';
const CSS = {
icon: "icon",
flipRtl: "flip-rtl"
};
/**
* Icon data cache.
* Exported for testing purposes.
* @private
*/
const iconCache = {};
/**
* Icon request cache.
* Exported for testing purposes.
* @private
*/
const requestCache = {};
const scaleToPx = {
s: 16,
m: 24,
l: 32
};
async function fetchIcon({ icon, scale }) {
const size = scaleToPx[scale];
const name = normalizeIconName(icon);
const filled = name.charAt(name.length - 1) === "F";
const iconName = filled ? name.substring(0, name.length - 1) : name;
const id = `${iconName}${size}${filled ? "F" : ""}`;
if (iconCache[id]) {
return iconCache[id];
}
if (!requestCache[id]) {
requestCache[id] = fetch(getAssetPath(`./assets/calcite-icon/${id}.json`))
.then((resp) => resp.json())
.catch(() => {
console.error(`"${id}" is not a valid calcite-ui-icon name`);
return "";
});
}
const path = await requestCache[id];
iconCache[id] = path;
return path;
}
/**
* Normalize the icon name to match the path data module exports.
* Exported for testing purposes.
* @private
*/
function normalizeIconName(name) {
const numberLeadingName = !isNaN(Number(name.charAt(0)));
const parts = name.split("-");
if (parts.length === 1) {
return numberLeadingName ? `i${name}` : name;
}
return parts
.map((part, index) => {
if (index === 0) {
return numberLeadingName ? `i${part.toUpperCase()}` : part;
}
return part.charAt(0).toUpperCase() + part.slice(1);
})
.join("");
}
const calciteIconCss = "@-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-popper-transition:150ms ease-in-out}:host([hidden]){display:none}:host{display:-ms-inline-flexbox;display:inline-flex}:host([scale=s]){width:1rem;height:1rem;min-width:1rem;min-height:1rem}:host([scale=m]){width:1.5rem;height:1.5rem;min-width:1.5rem;min-height:1.5rem}:host([scale=l]){width:2rem;height:2rem;min-width:2rem;min-height:2rem}.flip-rtl{-webkit-transform:scaleX(-1);transform:scaleX(-1)}.svg{display:block}";
const CalciteIcon = class {
constructor(hostRef) {
registerInstance(this, hostRef);
//--------------------------------------------------------------------------
//
// Properties
//
//--------------------------------------------------------------------------
/**
* 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/.
*/
this.icon = null;
/**
* When true, the icon will be flipped when the element direction is 'rtl'.
*/
this.flipRtl = false;
/**
* Icon scale.
*/
this.scale = "m";
this.visible = false;
}
//--------------------------------------------------------------------------
//
// Lifecycle
//
//--------------------------------------------------------------------------
connectedCallback() {
this.waitUntilVisible(() => {
this.visible = true;
this.loadIconPathData();
});
}
disconnectedCallback() {
if (this.intersectionObserver) {
this.intersectionObserver.disconnect();
this.intersectionObserver = null;
}
}
async componentWillLoad() {
this.loadIconPathData();
}
render() {
const { el, flipRtl, pathData, scale, textLabel } = this;
const dir = getElementDir(el);
const size = scaleToPx[scale];
const semantic = !!textLabel;
const paths = [].concat(pathData || "");
return (h(Host, { "aria-hidden": (!semantic).toString(), "aria-label": semantic ? textLabel : null, role: semantic ? "img" : null }, h("svg", { class: {
[CSS.flipRtl]: dir === "rtl" && flipRtl,
svg: true
}, 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 }))))));
}
//--------------------------------------------------------------------------
//
// Private Methods
//
//--------------------------------------------------------------------------
async loadIconPathData() {
const { icon, scale, visible } = this;
if (!icon || !visible) {
return;
}
this.pathData = await fetchIcon({ icon, scale });
}
waitUntilVisible(callback) {
if (typeof window === "undefined" ||
!window.IntersectionObserver) {
callback();
return;
}
this.intersectionObserver = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
this.intersectionObserver.disconnect();
this.intersectionObserver = null;
callback();
}
});
}, { rootMargin: "50px" });
this.intersectionObserver.observe(this.el);
}
static get assetsDirs() { return ["assets"]; }
get el() { return getElement(this); }
static get watchers() { return {
"icon": ["loadIconPathData"],
"scale": ["loadIconPathData"]
}; }
};
CalciteIcon.style = calciteIconCss;
export { CalciteIcon as calcite_icon };