Dynamically loads an SVG source and inline <svg> element so you can manipulate the style of it
Dynamically loads an SVG source and inline <svg> element so you can manipulate the style of it
Vue component loads an SVG source dynamically and inline so you can manipulate the style of it with CSS or JS. It looks like basic so you markup will not be bloated with SVG content.
Loaded SVGs are cached so it will not make network request twice.
Check old version vue-inline-svg@2
npm install vue-inline-svgimport InlineSvg from 'vue-inline-svg';
// Your component
export default {
components: {
InlineSvg,
}
}import { createApp } from 'vue'
import InlineSvg from 'vue-inline-svg';
const app = createApp({/*...*/});
app.component('inline-svg', InlineSvg);Usually, registering components globally may be considered as bad practice, such it may pollute the main bundle, but since InlineSvg is a very small package and inlining icons may be needed throughout the whole app, registering globally may bring no to little downsides but improve DX
If you are referencing assets with relative path (not from public directory), e.g. "./assets/my.svg", "@/assets/my.svg", then @vitejs/plugin-vue or @vue/cli will not handle them automatically like they do for `` tag, so you will need to manually import them with your bundler or configure automatic handling.
@vitejs/plugin-vue…You might also like vite-plugin-require to enable require() in Vite.
srcType: string Required
Path to SVG file
Note on relative and aliased paths
titleType: string
Sets/overwrites the `` of the SVG
uniqueIdsType: boolean | string
Add suffix to all IDs in SVG to eliminate conflicts for multiple SVGs with the same source. If true - suffix is random string, if string - suffix is this string.
uniqueIdsBaseType: string
An URL to prefix each ID in case you use the `` tag and uniqueIds.
keepDuringLoadingType: boolean; Default: true
It makes vue-inline-svg to preserve old image visible, when new image is being loaded. Pass false to disable it and show nothing during loading.
transformSourceType: (svg: SVGElement) => SVGElement
Function to transform SVG content
This example create circle in svg:
Other SVG and HTML attributes will be passed to inlined ``. Except attributes with false or null value.
loadedCalled when SVG image is loaded and inlined. Inlined SVG element passed as argument into the listener’s callback function.
unloadedCalled when src prop was changed and another SVG start loading.
errorCalled when SVG failed to load. Error object passed as argument into the listener’s callback function.
Inlining SVGs directly into the DOM provides flexibility for styling and interaction. However, it can pose risks of XSS (Cross-Site Scripting) attacks. SVGs can contain JavaScript (`
## Alternative approaches
- [vite-svg-loader](https://www.npmjs.com/package/vite-svg-loader), webpack's [vue-svg-loader](https://github.com/visualfanatic/vue-svg-loader). These tools embed SVG content directly into the JS bundle during compilation. **Pros**: SVG is bundled, eliminating the need for separate HTTP requests. **Cons**: Increased bundle size can delay the execution of more critical JS tasks. Additionally, each icon will be parsed multiple times: first as JS and then as HTML when inserted into the document (JS parsing is resource-intensive). Further reading on the [cons of SVG-in-JS](https://kurtextrem.de/posts/svg-in-js#performance-deep-dive-why-svg-in-js-is-an-anti-pattern)
- The `` approach requires no JS, that is great, but it has several disadvantages:
- may be not very convenient, it requires referencing IDs, updating SVG `fill`'s with `currentColor`. May be hard to manipulate other things except the main color. While DX can be improved with [@svg-use](https://fotis.xyz/posts/introducing-svg-use/)
- doesn't support cross-origin resources
- doesn't support embedded ``, ``, ``, ``, ``, `` tags
- inlining with `vue-inline-svg`. As cons, it requires some JS, but very little, only for the component itself. Icons are loaded separately from the JS bundle, so they are not available instantly after JS is loaded, but it can also be seen as an advantage, as it leads to faster JS bundle load. Inlining offers maximum flexibility on par with SVG-in-JS. Overall, it has excellent DX: if the component will be [registered globally](#or-register-globally-in-the-vue-app) and automatic asset handling will be [configured](#configuring-automatic-handling-with-vitejsplugin-vue), then usage will be as easy as a simple
``
## Changelog
[CHANGELOG.md](https://github.com/shrpne/vue-inline-svg/blob/master/CHANGELOG.md)
## Contributing
[CONTRIBUTING.md](https://github.com/shrpne/vue-inline-svg/blob/master/CONTRIBUTING.md)
## License
MIT LicenseNo open issues yet, or sync has not completed.