Skip to content

Inconsistent viewBox attribute in <svg/> elements #249

@delilahw

Description

@delilahw

What version of astro-icon are you using?

v1.1.2

Astro Info

Astro                    v4.16.10
Node                     v18.20.3
System                   Linux (x64)
Package Manager          unknown
Output                   static
Adapter                  none
Integrations             astro-icon

If this issue only occurs in one browser, which browser is a problem?

No response


Describe the Bug

Hi! Thank you all for your work on this library, it's been super helpful in optimising icons and bringing them to my Astro site. 😇

In #246, the viewBox attribute was moved from <svg/> to <symbol/>. However, for non-inline icons, it appears that this only takes effect on the first occurrence of <Icon/>. Subsequent instances of <Icon/> retain their viewBox attribute, which is inconsistent behaviour.

That is,

{/* Put both of these `<Icon/>` instances, one after another, in the same Astro file: */}

{/* This renders `<svg/>` _without_ a `viewBox` attribute */}
<Icon name="square"/>

{/* This renders `<svg/>` _with_ a `viewBox` attribute */}
<Icon name="square"/>

I suspect the cause to be: includeSymbol: bool determines whether to remove the viewBox attribute. According to the below assignment, it will be false in subsequent usages of <Icon/> because i > 0.

const includeSymbol = !inline && i === 0;

const { viewBox } = normalizedProps;
if (includeSymbol) {
delete normalizedProps.viewBox;
}
---
<svg {...normalizedProps} data-icon={name}>


What's the expected result?

The behaviour should be consistent. A consumer should be able to specify the same markup (<Icon name="square"/>) and expect them to render <svg/> that behaves the same — it should either omit the viewBox attribute across all instances, or include them across all instances.

Another suggestion is to add some sort of attribute/config to let users decide the viewBox behaviour.


Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-u6hdxa?file=src%2Fpages%2Findex.astro

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions