diff --git a/src/components/components.d.ts b/src/components/components.d.ts new file mode 100644 index 00000000..732c6316 --- /dev/null +++ b/src/components/components.d.ts @@ -0,0 +1,40 @@ +/** + * This is an autogenerated file created by the Stencil build process. + * It contains typing information for all components that exist in this project + * and imports for stencil collections that might be configured in your stencil.config.js file + */ + + +import { + Icon as IonIcon +} from './icon/icon'; + +declare global { + interface HTMLIonIconElement extends IonIcon, HTMLElement { + } + var HTMLIonIconElement: { + prototype: HTMLIonIconElement; + new (): HTMLIonIconElement; + }; + interface HTMLElementTagNameMap { + "ion-icon": HTMLIonIconElement; + } + interface ElementTagNameMap { + "ion-icon": HTMLIonIconElement; + } + namespace JSX { + interface IntrinsicElements { + "ion-icon": JSXElements.IonIconAttributes; + } + } + namespace JSXElements { + export interface IonIconAttributes extends HTMLAttributes { + + ariaLabel?: string, + name?: string, + ios?: string, + md?: string + } + } +} + diff --git a/src/components/icon/icon.tsx b/src/components/icon/icon.tsx new file mode 100755 index 00000000..467f979c --- /dev/null +++ b/src/components/icon/icon.tsx @@ -0,0 +1,211 @@ +import { Component, Prop, State } from '@stencil/core'; + + +@Component({ + tag: 'ion-icon', + host: { + theme: 'icon' + }, + assetsDir: 'svg' +}) +export class Icon { + @Prop({ context: 'mode' }) 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]|-|\d/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('-outline', '') + .replace(/\-/g, ' '); + } + } + + return attrs; + } + + + render() { + if (this.isServer) { + return