diff --git a/src/components/components.d.ts b/src/components/components.d.ts index 055382b3..be295923 100644 --- a/src/components/components.d.ts +++ b/src/components/components.d.ts @@ -32,10 +32,11 @@ declare global { * Specifies the label to use for accessibility. Defaults to the icon name. */ 'ariaLabel': string; - /** - * The color to use from your Sass `$colors` map. Default options are: `"primary"`, `"secondary"`, `"danger"`, `"light"`, and `"dark"`. For more information, see [Theming your App](/docs/theming/theming-your-app). - */ 'color': string; + /** + * A combination of both `name` and `src`. If a `src` url is detected it will set the `src` property. Otherwise it assumes it's a built-in named SVG and set the `name` property. + */ + 'icon': string; /** * Specifies which icon to use on `ios` mode. */ @@ -45,13 +46,17 @@ declare global { */ 'md': string; /** - * Specifies which icon to use. The appropriate icon will be used based on the mode. For more information, see [Ionicons](/docs/ionicons/). + * Specifies which icon to use from the built-in set of icons. */ 'name': string; /** * The size of the icon. Available options are: `"small"` and `"large"`. */ 'size': string; + /** + * Specifies the exact `src` of an SVG file to use. + */ + 'src': string; } } @@ -78,10 +83,11 @@ declare global { * Specifies the label to use for accessibility. Defaults to the icon name. */ 'ariaLabel'?: string; - /** - * The color to use from your Sass `$colors` map. Default options are: `"primary"`, `"secondary"`, `"danger"`, `"light"`, and `"dark"`. For more information, see [Theming your App](/docs/theming/theming-your-app). - */ 'color'?: string; + /** + * A combination of both `name` and `src`. If a `src` url is detected it will set the `src` property. Otherwise it assumes it's a built-in named SVG and set the `name` property. + */ + 'icon'?: string; /** * Specifies which icon to use on `ios` mode. */ @@ -91,15 +97,21 @@ declare global { */ 'md'?: string; /** - * Specifies which icon to use. The appropriate icon will be used based on the mode. For more information, see [Ionicons](/docs/ionicons/). + * Specifies which icon to use from the built-in set of icons. */ 'name'?: string; /** * The size of the icon. Available options are: `"small"` and `"large"`. */ 'size'?: string; + /** + * Specifies the exact `src` of an SVG file to use. + */ + 'src'?: string; } } } declare global { namespace JSX { interface StencilJSX {} } } + +export declare function defineCustomElements(window: any): void; \ No newline at end of file diff --git a/src/components/icon/icon.tsx b/src/components/icon/icon.tsx index ecdee031..2e99f551 100755 --- a/src/components/icon/icon.tsx +++ b/src/components/icon/icon.tsx @@ -1,4 +1,4 @@ -import { Component, Prop, State } from '@stencil/core'; +import { Component, Element, Prop, State, Watch } from '@stencil/core'; @Component({ @@ -10,19 +10,17 @@ import { Component, Prop, State } from '@stencil/core'; styleUrl: 'icon.css' }) export class Icon { + @Element() el: HTMLElement; + @State() private svgContent: string = null; + @State() private isVisible: boolean; @Prop({ context: 'isServer'}) private isServer: boolean; - - @Prop({ context: 'publicPath'}) private publicPath: string; - + @Prop({ context: 'resourcesUrl'}) private resourcesUrl: string; @Prop({ context: 'mode' }) mode: string; + @Prop({ context: 'document' }) doc: Document; + @Prop({ context: 'window' }) win: any; - /** - * The color to use from your Sass `$colors` map. - * Default options are: `"primary"`, `"secondary"`, `"danger"`, `"light"`, and `"dark"`. - * For more information, see [Theming your App](/docs/theming/theming-your-app). - */ @Prop() color: string; /** @@ -41,11 +39,22 @@ export class Icon { @Prop() md = ''; /** - * Specifies which icon to use. The appropriate icon will be used based on the mode. - * For more information, see [Ionicons](/docs/ionicons/). + * Specifies which icon to use from the built-in set of icons. */ @Prop() name = ''; + /** + * Specifies the exact `src` of an SVG file to use. + */ + @Prop() src = ''; + + /** + * A combination of both `name` and `src`. If a `src` url is detected + * it will set the `src` property. Otherwise it assumes it's a built-in named + * SVG and set the `name` property. + */ + @Prop() icon = ''; + /** * The size of the icon. * Available options are: `"small"` and `"large"`. @@ -53,54 +62,96 @@ export class Icon { @Prop() size: string; - private get iconName() { - let iconName = this.name.toLowerCase(); - - // default to "md" if somehow the mode wasn't set - const mode = this.mode || 'md'; - - // if an icon was passed in using the ios or md attributes - // set the iconName to whatever was passed in - if (this.ios && mode === 'ios') { - iconName = this.ios.toLowerCase(); - } else if (this.md && mode === 'md') { - iconName = this.md.toLowerCase(); - // this does not have one of the defaults - // so lets auto add in the mode prefix for them - } else if (iconName && !(/^md-|^ios-|^logo-/.test(iconName))) { - iconName = mode + '-' + iconName; - } - - // 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; + componentWillLoad() { + // 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(); + }); } + waitUntilVisible(el: HTMLElement, rootMargin: string, cb: Function) { + if (this.win.IntersectionObserver) { + const io = new this.win.IntersectionObserver(data => { + if (data[0].isIntersecting) { + io.disconnect(); + cb(); + } + }, { rootMargin }); + + io.observe(el); + + } else { + // browser doesn't support IntersectionObserver + // so just fallback to always show it + cb(); + } + } + + + @Watch('name') + @Watch('src') + @Watch('icon') + loadIcon() { + if (!this.isServer && this.isVisible) { + const url = this.getUrl(); + + if (url) { + getSvgContent(url).then(svgContent => { + this.svgContent = svgContent; + }); + } + } + } + + + + getUrl() { + let url = getSrc(this.src); + if (url) { + return url; + } + + url = getName(this.name, this.mode, this.ios, this.md); + if (url) { + return this.getNamedUrl(url); + } + + url = getSrc(this.icon); + if (url) { + return url; + } + + url = getName(this.icon, this.mode, this.ios, this.md); + if (url) { + return this.getNamedUrl(url); + } + + return null; + } + + getNamedUrl(name: string) { + return `${this.resourcesUrl}svg/${name}.svg`; + } + hostData() { const attrs: {[attrName: string]: string} = { 'role': 'img' }; - if (this.ariaLabel) { - // user provided label - attrs['aria-label'] = this.ariaLabel; - - } else { + if (!this.ariaLabel) { + const name = getName(this.name, this.mode, this.ios, this.md); + // user did not provide a label // 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, ' '); + if (name) { + attrs['aria-label'] = this.name + .replace('ios-', '') + .replace('md-', '') + .replace('-outline', '') + .replace(/\-/g, ' '); } } @@ -121,103 +172,137 @@ export class Icon { return