UNPKG

ionicons

Version:

Premium icons for Ionic.

364 lines (363 loc) 12.3 kB
import { Build, Host, getAssetPath, getMode, h } from '@stencil/core'; import { getIconMap, getName, getSrc, isSrc, isValid } from './utils'; /** * @virtualProp {"ios" | "md"} mode - The mode determines which platform styles to use. */ export class Icon { constructor() { this.mode = getIonMode(this); this.isVisible = false; /** * If enabled, ion-icon will be loaded lazily when it's visible in the viewport. * Default, `false`. */ this.lazy = false; } connectedCallback() { // purposely do not return the promise here because loading // the svg file should not hold up loading the app // only load the svg if it's visible this.waitUntilVisible(this.el, '50px', () => { this.isVisible = true; this.loadIcon(); }); } disconnectedCallback() { if (this.io) { this.io.disconnect(); this.io = undefined; } } waitUntilVisible(el, rootMargin, cb) { if (Build.isBrowser && this.lazy && typeof window !== 'undefined' && window.IntersectionObserver) { const io = this.io = new window.IntersectionObserver((data) => { if (data[0].isIntersecting) { io.disconnect(); this.io = undefined; cb(); } }, { rootMargin }); io.observe(el); } else { // browser doesn't support IntersectionObserver // so just fallback to always show it cb(); } } loadIcon() { if (Build.isBrowser && this.isVisible) { const url = this.getUrl(); if (url) { getSvgContent(this.el.ownerDocument, url, 's-ion-icon') .then(svgContent => this.svgContent = svgContent); } else { console.error('icon was not resolved'); } } if (!this.ariaLabel) { const name = getName(this.getName(), this.mode, this.ios, this.md); // user did not provide a label // come up with the label based on the icon name if (name) { this.ariaLabel = name .replace('ios-', '') .replace('md-', '') .replace(/\-/g, ' '); } } } getName() { if (this.name !== undefined) { return this.name; } if (this.icon && !isSrc(this.icon)) { return this.icon; } return undefined; } getUrl() { let url = getSrc(this.src); if (url) { return url; } url = getName(this.getName(), this.mode, this.ios, this.md); if (url) { return getNamedUrl(url); } url = getSrc(this.icon); if (url) { return url; } return null; } render() { const mode = this.mode || 'md'; const flipRtl = this.flipRtl || (this.ariaLabel && this.ariaLabel.indexOf('arrow') > -1 && this.flipRtl !== false); return (h(Host, { role: "img", class: Object.assign({ [`${mode}`]: true }, createColorClasses(this.color), { [`icon-${this.size}`]: !!this.size, 'flip-rtl': !!flipRtl && this.el.ownerDocument.dir === 'rtl' }) }, ((Build.isBrowser && this.svgContent) ? h("div", { class: "icon-inner", innerHTML: this.svgContent }) : h("div", { class: "icon-inner" })))); } static get is() { return "ion-icon"; } static get encapsulation() { return "shadow"; } static get originalStyleUrls() { return { "$": ["icon.css"] }; } static get styleUrls() { return { "$": ["icon.css"] }; } static get assetsDirs() { return ["svg"]; } static get properties() { return { "color": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "The color to use for the background of the item." }, "attribute": "color", "reflect": false }, "ariaLabel": { "type": "string", "mutable": true, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Specifies the label to use for accessibility. Defaults to the icon name." }, "attribute": "aria-label", "reflect": true }, "ios": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Specifies which icon to use on `ios` mode." }, "attribute": "ios", "reflect": false }, "md": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Specifies which icon to use on `md` mode." }, "attribute": "md", "reflect": false }, "flipRtl": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Specifies whether the icon should horizontally flip when `dir` is `\"rtl\"`." }, "attribute": "flip-rtl", "reflect": false }, "name": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Specifies which icon to use from the built-in set of icons." }, "attribute": "name", "reflect": false }, "src": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Specifies the exact `src` of an SVG file to use." }, "attribute": "src", "reflect": false }, "icon": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "A combination of both `name` and `src`. If a `src` url is detected\nit will set the `src` property. Otherwise it assumes it's a built-in named\nSVG and set the `name` property." }, "attribute": "icon", "reflect": false }, "size": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "The size of the icon.\nAvailable options are: `\"small\"` and `\"large\"`." }, "attribute": "size", "reflect": false }, "lazy": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "If enabled, ion-icon will be loaded lazily when it's visible in the viewport.\nDefault, `false`." }, "attribute": "lazy", "reflect": false, "defaultValue": "false" } }; } static get states() { return { "svgContent": {}, "isVisible": {} }; } static get elementRef() { return "el"; } static get watchers() { return [{ "propName": "name", "methodName": "loadIcon" }, { "propName": "src", "methodName": "loadIcon" }, { "propName": "icon", "methodName": "loadIcon" }]; } } const getIonMode = (ref) => { return getMode(ref) || document.documentElement.getAttribute('mode') || 'md'; }; const getNamedUrl = (name) => { const url = getIconMap().get(name); if (url) { return url; } return getAssetPath(`svg/${name}.svg`); }; const requests = new Map(); const getSvgContent = (doc, url, scopedId) => { // see if we already have a request for this url let req = requests.get(url); if (!req) { // we don't already have a request req = fetch(url, { cache: 'force-cache' }).then(rsp => { if (isStatusValid(rsp.status)) { return rsp.text(); } return Promise.resolve(null); }).then(svgContent => validateContent(doc, svgContent, scopedId)); // cache for the same requests requests.set(url, req); } return req; }; const isStatusValid = (status) => { return status <= 299; }; const validateContent = (document, svgContent, scopeId) => { if (svgContent) { const frag = document.createDocumentFragment(); const div = document.createElement('div'); div.innerHTML = svgContent; frag.appendChild(div); // setup this way to ensure it works on our buddy IE for (let i = div.childNodes.length - 1; i >= 0; i--) { if (div.childNodes[i].nodeName.toLowerCase() !== 'svg') { div.removeChild(div.childNodes[i]); } } // must only have 1 root element const svgElm = div.firstElementChild; if (svgElm && svgElm.nodeName.toLowerCase() === 'svg') { if (scopeId) { svgElm.setAttribute('class', scopeId); } // root element must be an svg // lets double check we've got valid elements // do not allow scripts if (isValid(svgElm)) { return div.innerHTML; } } } return ''; }; const createColorClasses = (color) => { return (color) ? { 'ion-color': true, [`ion-color-${color}`]: true } : null; };