@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
188 lines (182 loc) • 8.33 kB
JavaScript
/*!
* All material copyright ESRI, All Rights Reserved, unless otherwise specified.
* See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
*/
import { getAssetPath, proxyCustomElement, HTMLElement, h, Host, Build } from '@stencil/core/internal/client';
import { a as getElementDir } from './dom.js';
import { c as createObserver } from './observers.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/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 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}";
const Icon = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
constructor() {
super();
this.__registerHost();
this.__attachShadow();
//--------------------------------------------------------------------------
//
// 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() {
var _a;
(_a = this.intersectionObserver) === null || _a === void 0 ? void 0 : _a.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 (!Build.isBrowser || !icon || !visible) {
return;
}
this.pathData = await fetchIcon({ icon, scale });
}
waitUntilVisible(callback) {
this.intersectionObserver = createObserver("intersection", (entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
this.intersectionObserver.disconnect();
this.intersectionObserver = null;
callback();
}
});
}, { rootMargin: "50px" });
if (!this.intersectionObserver) {
callback();
return;
}
this.intersectionObserver.observe(this.el);
}
static get assetsDirs() { return ["assets"]; }
get el() { return this; }
static get watchers() { return {
"icon": ["loadIconPathData"],
"scale": ["loadIconPathData"]
}; }
static get style() { return iconCss; }
}, [1, "calcite-icon", {
"icon": [513],
"flipRtl": [516, "flip-rtl"],
"scale": [513],
"textLabel": [1, "text-label"],
"pathData": [32],
"visible": [32]
}]);
function defineCustomElement() {
if (typeof customElements === "undefined") {
return;
}
const components = ["calcite-icon"];
components.forEach(tagName => { switch (tagName) {
case "calcite-icon":
if (!customElements.get(tagName)) {
customElements.define(tagName, Icon);
}
break;
} });
}
defineCustomElement();
export { Icon as I, defineCustomElement as d };