I've a experimented with a few different ways of handling SVG icons in Svelte. Here's a brief thread about the workflow that I'm enjoying lately.

↓↓↓
I looked at using <img> tags, directly inlining the SVG, and passing the SVG as a component. Of these three, the last is the one that seems to result in the most maintainable code
I originally used a <slot>, and passed the icon component into it. This works, but not when generating the components dynamically: code sample showing a butto...code sample showing a card ...
What I've come to prefer is passing the icon component as a prop, and using <svelte:component> to render it, which works in both static and dynamic situations.
I start by optimizing the SVG file directly into a Svelte component using svgo: code sample showing termina...
I then make any edits, such as changing hard-coded colors to `currentColor` so the icon can inherit its color. It's also important to hard-code a starting width and height (a viewbox alone won't work). If the `xmlns` attribute is not present, you may need to add this as well: Image
Then we display the icon with <svelte:component>, rather than <slot>. When we use that component, we just import the relevant icon and pass it as a prop instead of a tag: code sample showing use of ...code sample showing how our...
I really like this pattern. It allows you to dynamically generate components that include SVGs, for example by using an {#each} block on an array of objects. Interested in other approaches if anyone wants to share them.

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Dusty Pomerleau

Dusty Pomerleau Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(