Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shrpne/vue-inline-svg
Dynamically loads an SVG source and inline <svg> element so you can manipulate the style of it
https://github.com/shrpne/vue-inline-svg
dynamic inline load svg
Last synced: 3 months ago
JSON representation
Dynamically loads an SVG source and inline <svg> element so you can manipulate the style of it
- Host: GitHub
- URL: https://github.com/shrpne/vue-inline-svg
- Owner: shrpne
- License: mit
- Created: 2018-03-15T14:51:26.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-06-10T23:49:43.000Z (5 months ago)
- Last Synced: 2024-07-18T15:49:45.411Z (4 months ago)
- Topics: dynamic, inline, load, svg
- Language: JavaScript
- Homepage:
- Size: 509 KB
- Stars: 173
- Watchers: 2
- Forks: 21
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- awesome-vue-3 - vue-inline-svg - Vue component loads an SVG source dynamically and inline `<svg>` so you can manipulate the style of it with CSS or JS. Works for Vue 2 and 3. (Packages)
README
# Vue Inline SVG
[![NPM Package](https://img.shields.io/npm/v/vue-inline-svg.svg?style=flat-square)](https://www.npmjs.org/package/vue-inline-svg)
[![Minified Size](https://img.shields.io/bundlephobia/min/vue-inline-svg.svg?style=flat-square)](https://bundlephobia.com/result?p=vue-inline-svg)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square)](https://github.com/shrpne/vue-inline-svg/blob/master/LICENSE)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.### Using Vue v2?
Check old version [vue-inline-svg@2](https://github.com/shrpne/vue-inline-svg/tree/v2?tab=readme-ov-file#vue-inline-svg)[ci-img]: https://travis-ci.org/shrpne/vue-inline-svg.svg
[ci]: https://travis-ci.org/shrpne/vue-inline-svg- [Install](#install)
- [NPM](#npm)
- [CDN](#cdn)
- [Usage](#usage)
- [props](#props)
- [src](#--src)
- [title](#--title)
- [keepDuringLoading](#--keepduringloading)
- [transformSource](#--transformsource)
- [SVG attributes](#svg-attributes)
- [events](#events)
- [loaded](#--loaded)
- [unloaded](#--unloaded)
- [error](#--error)
- [Comparison](#comparison)
- [Changelog](#changelog)
- [Contributing](#contributing)
- [License](#license)## Install
### NPM
```bash
npm install vue-inline-svg
```Register locally in your component
```js
import InlineSvg from 'vue-inline-svg';// Your component
export default {
components: {
InlineSvg,
}
}
```Or register globally in the Vue app
```js
import { createApp } from 'vue'
import InlineSvg from 'vue-inline-svg';const app = createApp({/*...*/});
app.component('inline-svg', InlineSvg);
```### CDN
```html
const app = Vue.createApp({/*...*/});
app.component('inline-svg', VueInlineSvg);```
## Usage
```html
```
[Example](https://github.com/shrpne/vue-inline-svg/blob/master/demo/index.html)### props
#### - `src`
Path to SVG file```html
```
Note: if you use vue-loader assets or vue-cli, then paths like '../assets/my.svg' will not be handled by file-loader automatically like vue-cli do for `` tag, so you will need to use it with `require`:
```html```
Learn more:
- https://vue-loader.vuejs.org/guide/asset-url.html#transform-rules
- https://cli.vuejs.org/guide/html-and-static-assets.html#static-assets-handling#### - `title`
Sets/overwrites the `` of the SVG```html
```
#### - `keepDuringLoading`
`true` by default. 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.```html
```
#### - `transformSource`
Function to transform SVG sourceThis example create circle in svg:
```htmlconst transform = (svg) => {
let point = document.createElementNS("http://www.w3.org/2000/svg", 'circle');
point.setAttributeNS(null, 'cx', '20');
point.setAttributeNS(null, 'cy', '20');
point.setAttributeNS(null, 'r', '10');
point.setAttributeNS(null, 'fill', 'red');
svg.appendChild(point);
return svg;
}
// For cleaner syntax you could use https://github.com/svgdotjs/svg.js```
### SVG attributes
Other SVG and HTML attributes will be passed to inlined ``. Except attributes with `false` or `null` value.
```html```
### events
#### - `loaded`
Called when SVG image is loaded and inlined.
Inlined SVG element passed as argument into the listener’s callback function.
```html```
#### - `unloaded`
Called when `src` prop was changed and another SVG start loading.
```html```
#### - `error`
Called when SVG failed to load.
Error object passed as argument into the listener’s callback function.
```html```
## Comparison
- This module: [![Minified Size](https://img.shields.io/bundlephobia/min/vue-inline-svg.svg?style=flat-square)](https://bundlephobia.com/result?p=vue-inline-svg)
- [vue-simple-svg](https://github.com/seiyable/vue-simple-svg): [![Minified Size](https://img.shields.io/bundlephobia/min/vue-simple-svg.svg?style=flat-square)](https://bundlephobia.com/result?p=vue-simple-svg), does not cache network requests, has wrapper around svg, attrs passed to `` are limited, converts `` tag into `style=""` attr
- [svg-loader](https://github.com/visualfanatic/vue-svg-loader) uses different approach, it inlines SVG during compilation. It has pros that SVG is prerendered and no http request needed. But also it has cons that markup size grows, especially if you have same image repeated several times. (Discussed in [#11](https://github.com/shrpne/vue-inline-svg/issues/11))## 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 License