feat(icon): add ion-icon as custom element

This commit is contained in:
Adam Bradley
2017-09-17 16:10:45 -05:00
parent 0bf7d6b783
commit db177ab21d
6 changed files with 4897 additions and 11 deletions
+3 -2
View File
@@ -1,4 +1,5 @@
_site
.sass-cache
*.scssc
*.swp
*.swp
node_modules
dist
+4618
View File
File diff suppressed because it is too large Load Diff
Regular → Executable
+33 -9
View File
@@ -1,9 +1,36 @@
{
"name": "ionicons",
"version": "4.0.0-0",
"description": "Premium icons for Ionic.",
"version": "3.0.0-alpha.3",
"files": ["dist"],
"keywords": ["ionicons", "ionic", "icon", "svg", "font", "mobile"],
"files": [
"dist/"
],
"browser": "dist/ionicons.js",
"main": "dist/collection/index.js",
"types": "dist/collection/index.d.ts",
"scripts": {
"build": "stencil build --dev && python scripts/generate.py && node scripts/svj.js",
"deploy": "np",
"prepublishOnly": "npm run build",
"serve": "stencil-dev-server",
"start": "npm run dev"
},
"devDependencies": {
"@ionic/jpml": "0.0.3",
"@stencil/core": "next",
"np": "2.16.0"
},
"keywords": [
"ionicons",
"ionic",
"icon",
"svg",
"font",
"mobile",
"web component",
"component",
"custom element"
],
"homepage": "http://ionicons.com/",
"author": {
"name": "Ben Sperry",
@@ -20,10 +47,7 @@
"url": "https://github.com/driftyco/ionicons.git"
},
"bugs": {
"url" : "https://github.com/driftyco/ionicons/issues"
"url": "https://github.com/driftyco/ionicons/issues"
},
"license": "MIT",
"engines" : {
"node" : ">=0.10.3"
}
}
"license": "MIT"
}
+212
View File
@@ -0,0 +1,212 @@
import { Component, Prop, State } from '@stencil/core';
@Component({
tag: 'ion-icon',
host: {
theme: 'icon'
},
assetsDir: 'svj'
})
export class Icon {
mode: string;
/**
* @input {string} Specifies the label to use for accessibility. Defaults to the icon name.
*/
@Prop() ariaLabel: string = '';
/**
* @input {string} Specifies which icon to use. The appropriate icon will be used based on the mode.
* For more information, see [Ionicons](/docs/ionicons/).
*/
@Prop() name: string = '';
/**
* @input {string} Specifies which icon to use on `ios` mode.
*/
@Prop() ios: string = '';
/**
* @input {string} Specifies which icon to use on `md` mode.
*/
@Prop() md: string = '';
@Prop({ context: 'isServer'}) private isServer: boolean;
@State() private svgContent: string = null;
private get iconName() {
// if no name was passed set iconName to null
if (!this.name) {
return null;
}
let iconName = this.name.toLowerCase();
// default to "md" if somehow the mode wasn't set
const mode = this.mode || 'md';
if (!(/^md-|^ios-|^logo-/.test(iconName))) {
// this does not have one of the defaults
// so lets auto add in the mode prefix for them
iconName = mode + '-' + iconName;
} else if (this.ios && mode === 'ios') {
// if an icon was passed in using the ios or md attributes
// set the iconName to whatever was passed in
// when we're also on that mode
// basically, use the ios attribute when you're on ios
iconName = this.ios;
} else if (this.md && mode === 'md') {
// use the md attribute when you're in md mode
// and the md attribute has been set
iconName = this.md;
}
// only allow alpha characters and dash
const invalidChars = iconName.replace(/[a-z]|-/g, '');
if (invalidChars !== '') {
console.error(`invalid characters in ion-icon name: ${invalidChars}`);
return null;
}
return iconName;
}
hostData() {
const attrs: {[attrName: string]: string} = {
'role': 'img'
};
if (this.ariaLabel) {
// user provided label
attrs['aria-label'] = this.ariaLabel;
} else {
// come up with the label based on the icon name
const iconName = this.iconName;
if (iconName) {
attrs['aria-label'] = iconName
.replace('ios-', '')
.replace('md-', '')
.replace(/\-/g, ' ');
}
}
return {
attrs
};
}
render() {
if (this.isServer) {
return <div class="icon-inner">{/* ssr */}</div>;
}
const iconName = this.iconName;
if (!iconName) {
// we don't have good data
return <div class="icon-inner">{/* invalid svg */}</div>;
}
const svgContent = svgContents[iconName];
if (svgContent === this.svgContent) {
// we've already loaded up this svg at one point
// and the svg content we've loaded and assigned checks out
// render this svg!!
return <div class="icon-inner" innerHTML={svgContent}></div>;
}
// haven't loaded this svg yet
// start the request
loadSvgContent(iconName, loadedSvgContent => {
// we're finished loading the svg content!
// set to this.svgContent so we do another render
this.svgContent = loadedSvgContent;
});
// actively requesting the svg, so let's just render a div for now
return <div class="icon-inner">{/* loading svg */}</div>;
}
}
function loadSvgContent(iconName: string, callback: {(loadedSvgContent: string): void}) {
// static since all IonIcons use this same function and pointing at global/shared data
// passed in callback will have instance info
// add to the list of callbacks to fiure when this url is finished loading
(loadCallbacks[iconName] = loadCallbacks[iconName] || []).push(callback);
if (activeRequests[iconName]) {
// already requesting this icon, don't bother kicking off another
return;
}
// add this icons to our list of active requests
activeRequests[iconName] = true;
// kick off the request for the external svg file
// create a script element to add to the document.head
var scriptElm = document.createElement('script');
scriptElm.charset = 'utf-8';
scriptElm.async = true;
scriptElm.src = `${publicPath}svj/${iconName}.svj`;
// create a fallback timeout if something goes wrong
var tmrId = setTimeout(onScriptComplete, 120000);
function onScriptComplete() {
clearTimeout(tmrId);
scriptElm.onerror = scriptElm.onload = null;
scriptElm.parentNode.removeChild(scriptElm);
// remove from our list of active requests
delete activeRequests[iconName];
}
// add script completed listener to this script element
scriptElm.onerror = scriptElm.onload = onScriptComplete;
// inject a script tag in the head
// kick off the actual request
document.head.appendChild(scriptElm);
}
const activeRequests: {[iconName: string]: boolean} = {};
const loadCallbacks: {[iconName: string]: {(loadedSvgContent: string): void}[]} = [] as any;
const svgContents: {[iconName: string]: string} = {};
// add a jsonp handler to the window
// as svg jsonp files are requested
// once they load they'll call this method
(window as any).loadIonicon = function loadIonicon(svgContent: string, iconName: string) {
// awesome, we've finished loading the svg file
// remove this url from the active requests
delete activeRequests[iconName];
svgContents[iconName] = svgContent
// find any callbacks waiting on this icon
const svgLoadCallbacks = loadCallbacks[iconName];
if (svgLoadCallbacks) {
// loop through all the callbacks that are waiting on the svg content
svgLoadCallbacks.forEach(cb => {
// fire off this callback which was provided by an instance
cb(svgContent);
});
delete loadCallbacks[iconName];
}
};
+5
View File
@@ -0,0 +1,5 @@
exports.config = {
namespace: 'ionicons',
generateDistribution: true,
generateWWW: false
};
Executable
+26
View File
@@ -0,0 +1,26 @@
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"allowUnreachableCode": false,
"declaration": false,
"experimentalDecorators": true,
"lib": [
"dom",
"es2015"
],
"moduleResolution": "node",
"module": "es2015",
"target": "es2015",
"noUnusedLocals": true,
"noUnusedParameters": true,
"jsx": "react",
"jsxFactory": "h"
},
"include": [
"src",
"types/jsx.d.ts"
],
"exclude": [
"node_modules"
]
}