项目作者: WandersonAlves

项目描述 :
:moon: Simple badge directive using shadow-dom (with fallback)
高级语言: JavaScript
项目地址: git://github.com/WandersonAlves/vue-shadow-badge.git
创建时间: 2018-06-19T20:39:07Z
项目社区:https://github.com/WandersonAlves/vue-shadow-badge

开源协议:MIT License

下载


vue-shadow-badge!

logo

GitHub stars
GitHub license

DEPRECATED

element.createShadowRoot is DEPRECATED, and using element.attachShadow don’t seens to work on the examples. Using fallback currently works (for anyone willing to study this directive). And Vue 3 is close to be released, so, this directive will not receive any update from me. Thanks!

Instalation

Use yarn add vue-shadow-badge or npm i --save vue-shadow-badge

Motivation

In my search for badges, i only found component based badges, for me, this isn’t the ideal, so a build a directive based badge that is extensible and highly configurable.

With shadow-dom is easy to create pseudo-elements in any element with a shadowRoot, just look at src/directives/Badge/utils/shadow-dom.js.

Sadly, shadow-dom isn’t supported in all browsers, so keep in mind this.

https://caniuse.com/#feat=shadowdom

But…

Now this directive have a fallback for browsers without shadow-dom. If you encounter something strange, please let me know it.


Usage

Simple import globally or locally in your vue app.

Locally:

  1. //SomeComponent.vue
  2. import Badge from 'vue-shadow-badge';
  3. export default {
  4. directives: {
  5. Badge
  6. }
  7. }

Global:

  1. //main.js (or another entry point)
  2. import Vue from 'vue';
  3. import App from './App.vue';
  4. import Badge from 'vue-shadow-badge';
  5. new Vue({
  6. render: h => h(App),
  7. directives: {
  8. Badge
  9. }
  10. }).$mount('#app')

Now you can use your new directive like this:

<h1 v-badge="badgeConfig">Examples</h1>

or this:

<button v-badge="3">Hi!</button>

or even this:

<span v-badge="{value: 54, right: '10px'}">


badgeConfig accept some values:

Prop Type Description
value number Sets the value on Badge
right string Set the right value for badge placement
left string Set the left value for badge placement
down string Set the down value for badge placement
up string Set the up value for badge placement
styles string Customize your badge! (see above)

Examples

  1. <template>
  2. <button-outline v-badge="{
  3. value: buttonCounter,
  4. right: '-25px',
  5. top: '-4px',
  6. styles: buttonBadgeStyle}" msg="Click Me!" @click.native="buttonCounter++">
  7. </button-outline>
  8. </template>
  9. <script>
  10. export default {
  11. data() {
  12. return {
  13. badgeConfig: {
  14. value: 2,
  15. right: '-20px',
  16. top: '-4px'
  17. },
  18. buttonBadgeStyle: `
  19. border: 2px solid #662a24;
  20. color: #662a24;
  21. background-color: transparent;
  22. font-size: 0.9em;
  23. `
  24. }
  25. }
  26. }
  27. }
  28. </script>

You can use styles prop to set the positioning of the badge too! Just use right|top|bottom|left: value

Contributing

This is a beginner friendly open source project. Submit your PR, let’s discuss some improvements.