jonathantneal / svg4everybody

Use external SVG spritemaps today
https://jonneal.dev/svg4everybody/
Other
3.29k stars 353 forks source link

JS Frameworks #177

Closed timbomckay closed 1 year ago

timbomckay commented 6 years ago

Does this work with a JS Framework such as React or Vue? Would the function needed to be called any time there's a change to the DOM?

DmitriyKurto commented 6 years ago

@timbomckay, work with Vue (tested in IE11)

Ericky14 commented 6 years ago

@DmitriyKurto How and where do you call the function? Please explain, I'm not being able to use it.

DmitriyKurto commented 6 years ago

@Ericky14 I show you the part of code from my main.js

import Vue from 'vue' import App from './App.vue' import svg4everybody from 'svg4everybody'

Vue.component('app', App) svg4everybody() new Vue().$mount('#app')

timbomckay commented 6 years ago

That doesn't look like svg4everybody() is running in the vue instance, only when the main.js is loaded. When Vue changes the DOM with new content that has an SVG in the new content does this script need to run again?

@Ericky14 here's my Vue SPA solution: icon.vue

I need to update some documentation on it and change out some styling. If the user agent is IE it'll fetch the svg sprite using axios and place in the body with display:none and adjusts the <use href path accordingly via the computed property. THEME_URI is a variable I defined in my webpack configuration, just update the let url = variable to match the path to your sprite.

Add as global component to the Vue instance:

import icon from './elements/icon.vue'

Vue.component('icon', icon)

new Vue({
  el: '#main',
  render: (h) => h(Main),
});

Add icon in vue template:

<icon href="facebook"></icon>
Ericky14 commented 6 years ago

Thanks for the replies.

yellow1912 commented 4 years ago

My current solution is to re-run svg4everybody everytime I update the content. @jonathantneal I would like to limit the performance penalty by giving it a container to run (instead of running inside the whole document). I'm thinking of passing an optional container element to the options. Would that be okie?

yellow1912 commented 4 years ago

Also, a minor refractor to run the svg4everybody on a specific element would be nice as well. I will do something about it and post some sample code here.

wmcmurray commented 4 years ago

I believe it can't work in a dynamic environment since the list of <use> tags never get updated after being initially fetched here :

https://github.com/jonathantneal/svg4everybody/blob/8bd711049ba3faefb8285ca70c63a67efc35636c/lib/svg4everybody.js#L135-L136

I would personally refetch those tags inside the loop already available (using a modulo to reduce the rate at which it refreshes them). This way no breaking/API changes would be introduced.