1- import { html , css , render , unsafeHTML , until , LitElement } from '@lion/core' ;
1+ import { html , css , LitElement } from '@lion/core' ;
22
3- const isDefinedPromise = action => typeof action === 'object' && Promise . resolve ( action ) === action ;
3+ const isPromise = action => typeof action === 'object' && Promise . resolve ( action ) === action ;
44
55/**
66 * Custom element for rendering SVG icons
@@ -9,6 +9,7 @@ const isDefinedPromise = action => typeof action === 'object' && Promise.resolve
99export class LionIcon extends LitElement {
1010 static get properties ( ) {
1111 return {
12+ // svg is a property to ensure the setter is called if the property is set before upgrading
1213 svg : {
1314 type : String ,
1415 } ,
@@ -59,13 +60,6 @@ export class LionIcon extends LitElement {
5960
6061 update ( changedProperties ) {
6162 super . update ( changedProperties ) ;
62- if ( changedProperties . has ( 'svg' ) ) {
63- if ( isDefinedPromise ( this . svg ) ) {
64- this . _setDynamicSvg ( ) ;
65- } else {
66- this . _setSvg ( ) ;
67- }
68- }
6963 if ( changedProperties . has ( 'ariaLabel' ) ) {
7064 this . _onLabelChanged ( changedProperties ) ;
7165 }
@@ -87,24 +81,25 @@ export class LionIcon extends LitElement {
8781 * On IE11, svgs without focusable false appear in the tab order
8882 * so make sure to have <svg focusable="false"> in svg files
8983 */
90- _setSvg ( ) {
91- this . innerHTML = this . svg ? this . svg : '' ;
84+ set svg ( svg ) {
85+ this . __svg = svg ;
86+ if ( svg === undefined ) {
87+ this . _renderSvg ( '' ) ;
88+ } else if ( isPromise ( svg ) ) {
89+ this . _renderSvg ( '' ) ; // show nothing before resolved
90+ svg . then ( resolvedSvg => {
91+ // render only if it is still the same and was not replaced after loading started
92+ if ( svg === this . __svg ) {
93+ this . _renderSvg ( resolvedSvg ) ;
94+ }
95+ } ) ;
96+ } else {
97+ this . _renderSvg ( svg ) ;
98+ }
9299 }
93100
94- // TODO: find a better way to render dynamic icons without the need for unsafeHTML
95- _setDynamicSvg ( ) {
96- const template = html `
97- ${ until (
98- this . svg . then ( _svg => {
99- // If the export was not made explicit, take the default
100- if ( typeof _svg !== 'string' ) {
101- return unsafeHTML ( _svg . default ) ;
102- }
103- return unsafeHTML ( _svg ) ;
104- } ) ,
105- ) }
106- ` ;
107- render ( template , this ) ;
101+ get svg ( ) {
102+ return this . __svg ;
108103 }
109104
110105 _onLabelChanged ( ) {
@@ -115,4 +110,9 @@ export class LionIcon extends LitElement {
115110 this . removeAttribute ( 'aria-label' ) ;
116111 }
117112 }
113+
114+ _renderSvg ( svgOrModule ) {
115+ const svg = svgOrModule && svgOrModule . default ? svgOrModule . default : svgOrModule ;
116+ this . innerHTML = svg ;
117+ }
118118}
0 commit comments