In this episode, we're diving into dealing with SVG icons in our project. First up, we bring in a set of SVG icons (from Iconic), and we chat about why having a reusable approach is way better than importing each icon one-by-one everywhere you need it. Tracking down imports for dozens of icons can get pretty messy!
We're going to make life easier by building a single SVG component that can take the name of any icon and magically render it wherever we need. Plus, we'll allow passing in style props (like width, height, color, etc.), so you can tweak the icons however you like.
We set up Vite SVG Loader and register it as a plugin, discussing some config options (like disabling SVGO if you need to keep the icon's viewBox for styling). Then, we create the core SVG.vue
component, which receives the icon name as a prop, and uses Vue's defineAsyncComponent
to dynamically import the right SVG file based on the name. The key trick is using Vue's <component :is="icon" />
for rendering whatever SVG gets pulled in.
Once this reusable SVG component is in place, we wire it up in our Markdown toolbar — adding icons like bold, code, and link, all without massive import statements. We just specify the icon's name as a prop to the SVG component, and it takes care of the rest! We also whip up some quick styles to set the size of the icons.
Finally, we show how you can plop this SVG component anywhere else in your app — for example, on the "close" button in the create discussion form, making your UI look that much nicer with consistent, simple icon handling. Overall, this episode is all about making SVGs in your project organized, scalable, and nice to work with!