Skip to content

Instantly share code, notes, and snippets.

@achhunna
Last active April 18, 2021 08:56
Show Gist options
  • Save achhunna/cae74645f3ef1e7787c07bcdee163d18 to your computer and use it in GitHub Desktop.
Save achhunna/cae74645f3ef1e7787c07bcdee163d18 to your computer and use it in GitHub Desktop.
Import svg inline and modify element to create icon
<script>
function recursivelyRemoveFill(el) {
if (!el) {
return;
}
el.removeAttribute('fill');
[].forEach.call(el.children, child => {
recursivelyRemoveFill(child);
});
}
export default {
name: 'svg-icon',
props: {
icon: {
type: String,
default: null
},
hasFill: {
type: Boolean,
default: false
},
growByHeight: {
type: Boolean,
default: true
},
},
mounted() {
if (this.$el.firstElementChild.nodeName === 'svg') {
const svgElement = this.$el.firstElementChild;
// use `viewBox` attribute to get the svg's inherent width and height
const viewBox = svgElement.getAttribute('viewBox').split(' ').map(n => Number(n));
const widthToHeight = (viewBox[2] / viewBox[3]).toFixed(2);
if (this.hasFill) {
// recursively remove all fill attribute of element and its nested children
recursivelyRemoveFill(svgElement);
}
// set width and height relative to font size
// if growByHeight is true, height set to 1em else width set to 1em and remaining is calculated based on widthToHeight ratio
if (this.growByHeight) {
svgElement.setAttribute('height', '1em');
svgElement.setAttribute('width', `${widthToHeight}em`);
} else {
svgElement.setAttribute('width', '1em');
svgElement.setAttribute('height', `${1 / widthToHeight}em`);
}
svgElement.classList.add('svg-class');
}
}
}
</script>
<template>
<div v-html="require(`!html-loader!../assets/svg/${icon}.svg`)" class="svg-container"></div>
</template>
<style lang="scss" scoped>
.svg-container {
display: inline-flex;
}
</style>
@WvanDam
Copy link

WvanDam commented Sep 15, 2020

Again thanks for this gist, it's been quite helpful.

In order to allow icons to change I made a small adjustment. I put the contents of the mounted function inside a method function called setupSvg and added watchers on the props:

  watch: {
    icon() {
      this.$nextTick(this.setupSvg)
    },
    hasFill() {
      this.$nextTick(this.setupSvg)
    },
    growByHeight() {
      this.$nextTick(this.setupSvg)
    }
  },
  mounted() {
    this.setupSvg()
  },
  methods: {
    setupSvg() {
      // code from original mounted() function
    }
  }

I've only used it to change the icon prop, but I couldn't see why it wouldn't work for the other two, so I added watchers on those as well.

@bviala
Copy link

bviala commented Sep 20, 2020

Hello! Thank you for your post, it was really helpful.

.svg-class {
    vertical-align: middle;
}

This doesn't work for me. Any rule added in this block simply doesn't appear in devtools inspector. It seems to be because of the scoping. If put in an non scoped style block it works.

Also, in my component I added

.svg-container {
    fill: currentColor;
}

This way, the SVG Icon component behaves like a font icon and inherits current color, and you don't have to define fill in the parent:

<p style="color: red">
  <SvgIcon icon="foo" />
  bar
</p>

@achhunna
Copy link
Author

Hello! Thank you for your post, it was really helpful.

.svg-class {
    vertical-align: middle;
}

This doesn't work for me. Any rule added in this block simply doesn't appear in devtools inspector. It seems to be because of the scoping. If put in an non scoped style block it works.

Also, in my component I added

.svg-container {
    fill: currentColor;
}

This way, the SVG Icon component behaves like a font icon and inherits current color, and you don't have to define fill in the parent:

<p style="color: red">
  <SvgIcon icon="foo" />
  bar
</p>

Thanks for the feedback. I have removed the vertical-align: middle style since there isn't a .svg-class but the fill should inherit without needing to add currentColor value.

@achhunna
Copy link
Author

Again thanks for this gist, it's been quite helpful.

In order to allow icons to change I made a small adjustment. I put the contents of the mounted function inside a method function called setupSvg and added watchers on the props:

  watch: {
    icon() {
      this.$nextTick(this.setupSvg)
    },
    hasFill() {
      this.$nextTick(this.setupSvg)
    },
    growByHeight() {
      this.$nextTick(this.setupSvg)
    }
  },
  mounted() {
    this.setupSvg()
  },
  methods: {
    setupSvg() {
      // code from original mounted() function
    }
  }

I've only used it to change the icon prop, but I couldn't see why it wouldn't work for the other two, so I added watchers on those as well.

Great suggestion, when would we want to watch changes to these props?

@WvanDam
Copy link

WvanDam commented Sep 28, 2020

I use the icon prop to change toggle states, and changes in the category indicator icon of items that can be sorted in categories by the user.

The other props may change depending on the icon change. However, the one time I might have had to use it previously I just ended up fixing the inconsistent icon instead. So yeah, you could have just the watcher on the icon prop.

@achhunna
Copy link
Author

I use the icon prop to change toggle states, and changes in the category indicator icon of items that can be sorted in categories by the user.

The other props may change depending on the icon change. However, the one time I might have had to use it previously I just ended up fixing the inconsistent icon instead. So yeah, you could have just the watcher on the icon prop.

That's a cool use case. For the scope of the article, I will skip adding it since I just want to highlight basic functionality. Thanks for your feedback and input though! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment