diff --git a/packages/iconify/src/render/bg.ts b/packages/iconify/src/render/bg.ts index e678c5c..da4a1e1 100644 --- a/packages/iconify/src/render/bg.ts +++ b/packages/iconify/src/render/bg.ts @@ -41,6 +41,13 @@ for (const prefix in propsToAddTo) { } } +/** + * Fix size: add 'px' to numbers + */ +function fixSize(value: string): string { + return value + (value.match(/^[-0-9.]+$/) ? 'px' : ''); +} + /** * Render icon as inline SVG */ @@ -78,8 +85,8 @@ export function renderBackground( const url = svgToURL(html); const newStyles: Record = { '--svg': url, - 'width': renderAttribs.width, - 'height': renderAttribs.height, + 'width': fixSize(renderAttribs.width), + 'height': fixSize(renderAttribs.height), ...commonProps, ...(useMask ? monotoneProps : coloredProps), }; diff --git a/packages/iconify/tests/render-bg-test.ts b/packages/iconify/tests/render-bg-test.ts index d72fcea..39bfd83 100644 --- a/packages/iconify/tests/render-bg-test.ts +++ b/packages/iconify/tests/render-bg-test.ts @@ -167,4 +167,16 @@ describe('Testing rendering nodes as background', () => { expect(data.addedClasses).toEqual(['iconify--test']); expect(data.addedStyles).toEqual([...expectedBackgroundStyles]); }); + + it('Custom dimensions', async () => { + await testIcon( + '', + { + body: '', + width: 24, + height: 24, + }, + `` + ); + }); }); diff --git a/packages/react/src/render.ts b/packages/react/src/render.ts index cfa8f40..60fabf0 100644 --- a/packages/react/src/render.ts +++ b/packages/react/src/render.ts @@ -62,6 +62,13 @@ for (const prefix in propsToAddTo) { */ const inlineDefaults: FullIconCustomisations = { ...defaults, inline: true }; +/** + * Fix size: add 'px' to numbers + */ +function fixSize(value: string): string { + return value + (value.match(/^[-0-9.]+$/) ? 'px' : ''); +} + /** * Render icon */ @@ -216,8 +223,8 @@ export const render = ( componentProps.style = { ...style, '--svg': svgToURL(html), - 'width': renderAttribs.width, - 'height': renderAttribs.height, + 'width': fixSize(renderAttribs.width), + 'height': fixSize(renderAttribs.height), ...commonProps, ...(useMask ? monotoneProps : coloredProps), ...customStyle, diff --git a/packages/react/tests/iconify/10-style-mode.test.js b/packages/react/tests/iconify/10-style-mode.test.js index f51e424..45fb1ad 100644 --- a/packages/react/tests/iconify/10-style-mode.test.js +++ b/packages/react/tests/iconify/10-style-mode.test.js @@ -42,4 +42,40 @@ describe('Rendering as span', () => { children: null, }); }); + + test('custom dimensions', () => { + const component = renderer.create( + { + // Should be called only for icons loaded from API + throw new Error('onLoad called for object!'); + }} + /> + ); + const tree = component.toJSON(); + + expect(tree).toMatchObject({ + type: 'span', + props: { + style: { + '--svg': `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'%3E%3Cpath d='M4 19h16v2H4zm5-4h11v2H9zm-5-4h16v2H4zm0-8h16v2H4zm5 4h11v2H9z' fill='currentColor'/%3E%3C/svg%3E")`, + 'width': '32px', + 'height': '48px', + 'display': 'inline-block', + 'backgroundColor': 'currentColor', + 'webkitMaskImage': 'var(--svg)', + 'webkitMaskRepeat': 'no-repeat', + 'webkitMaskSize': '100% 100%', + 'maskImage': 'var(--svg)', + 'maskRepeat': 'no-repeat', + 'maskSize': '100% 100%', + }, + }, + children: null, + }); + }); }); diff --git a/packages/svelte/src/render.ts b/packages/svelte/src/render.ts index 231ebdb..a190e01 100644 --- a/packages/svelte/src/render.ts +++ b/packages/svelte/src/render.ts @@ -54,6 +54,13 @@ for (const prefix in propsToAddTo) { } } +/** + * Fix size: add 'px' to numbers + */ +function fixSize(value: string): string { + return value + (value.match(/^[-0-9.]+$/) ? 'px' : ''); +} + /** * Result */ @@ -217,8 +224,8 @@ export function render( const url = svgToURL(html); const styles: Record = { '--svg': url, - 'width': renderAttribs.width, - 'height': renderAttribs.height, + 'width': fixSize(renderAttribs.width), + 'height': fixSize(renderAttribs.height), ...commonProps, ...(useMask ? monotoneProps : coloredProps), }; diff --git a/packages/svelte/tests/iconify/10-style-mode.test.ts b/packages/svelte/tests/iconify/10-style-mode.test.ts index 91f932f..c716c7e 100644 --- a/packages/svelte/tests/iconify/10-style-mode.test.ts +++ b/packages/svelte/tests/iconify/10-style-mode.test.ts @@ -32,4 +32,28 @@ describe('Rendering as span', () => { "" ); }); + + test('custom dimensions', () => { + const component = render(Icon, { + icon: iconData, + mode: 'style', + width: '48', + height: 32, + onLoad: () => { + // Should be called only for icons loaded from API + throw new Error('onLoad called for object!'); + }, + }); + const node = component.container.querySelector( + 'span' + ) as HTMLSpanElement; + expect(node).not.toBeNull(); + expect(node.parentNode).not.toBeNull(); + const html = node.outerHTML; + + // Check HTML + expect(html).toBe( + "" + ); + }); }); diff --git a/packages/vue/src/render.ts b/packages/vue/src/render.ts index aead9b1..21c889a 100644 --- a/packages/vue/src/render.ts +++ b/packages/vue/src/render.ts @@ -83,6 +83,13 @@ const customisationAliases: Record = {}; */ type VStyle = Record; +/** + * Fix size: add 'px' to numbers + */ +function fixSize(value: string): string { + return value + (value.match(/^[-0-9.]+$/) ? 'px' : ''); +} + /** * Render icon */ @@ -232,8 +239,8 @@ export const render = ( componentProps.style = { ...style, '--svg': svgToURL(html), - 'width': renderAttribs.width, - 'height': renderAttribs.height, + 'width': fixSize(renderAttribs.width), + 'height': fixSize(renderAttribs.height), ...commonProps, ...(useMask ? monotoneProps : coloredProps), ...customStyle,