Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vue.use() options #35

Closed
davestewart opened this issue May 29, 2020 · 4 comments
Closed

Vue.use() options #35

davestewart opened this issue May 29, 2020 · 4 comments

Comments

@davestewart
Copy link

davestewart commented May 29, 2020

Would be good to be able to set defaults:

Vue.use(unicons, {
  width: 20,
  height: 20,
  fill: '#FF0000',
})
@davestewart
Copy link
Author

davestewart commented May 29, 2020

Additionally, you could consider something like this to reduce duplication / potential errors in choosing and adding icons:

// icons.js
export {
  uniCheck,
  uniCheckCircle,
  uniExclamationCircle,
  uniTimes,
  uniTimesCircle
} from 'vue-unicons/src/icons'
// plugin.js
import Unicon from 'vue-unicons'
import * as icons from './icons'

Vue.use(Unicon, {
  width: 20,
  height: 20,
  fill: '#FF0000',
  icons,
})

This would be backwards compatible with Unicon.add()

@hmaesta
Copy link

hmaesta commented Jun 2, 2020

I support this ideia.

Btw, would be awesome to define fill: 'currentColor' as default

@davestewart
Copy link
Author

davestewart commented Jun 2, 2020

Funny you should mention that!

I've just written this which doesn't require any setup or imports, camel-casing or prefixes, and will work with any SVG icons.

Prerequisites:

  • run npm install raw-loader --save-dev
  • edit the path a folder of SVG assets (works with webpack aliases)
<script>
export default {
  functional: true,

  props: {
    icon: {
      type: String,
      required: true
    },

    size: {
      type: [Number, String],
      default: 24
    },

    color: {
      type: String,
      default: 'currentColor'
    }
  },

  render (h, context) {
    // input
    const { props, data, listeners } = context
    const { icon, size, color } = props

    // svg
    const svg = require(`!raw-loader!assets/icons/${icon}.svg`).default
    const html = svg
      .replace(/fill="#.+?"/g, `fill="${color}"`)
      .replace(/<svg.+?>|<\/svg>/g, '')

    // options
    // TODO parse attributes from original SVG
    const attrs = {
      xmlns: 'http://www.w3.org/2000/svg',
      width: size,
      height: size,
      viewBox: '0 0 24 24',
      class: 'uiIcon',
      'aria-hidden': true,
    }

    const domProps = {
      innerHTML: html
    }

    // shape
    return h('svg', { ...data, attrs, domProps, listeners })
  }
}
</script>

<style>
.uiIcon {
  pointer-events: bounding-box;
}
</style>
<UiIcon icon="blah" />

antonreshetov added a commit that referenced this issue Jul 5, 2020
@antonreshetov
Copy link
Owner

Closed because it was added in #37

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants