Skip to content

Commit

Permalink
feat: add hover-fill prop (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
E1mir authored Mar 30, 2021
1 parent 3b02bc5 commit 24ed866
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ Then we add the file path inside the plugins key in `nuxt.config.js`, and set `m
| `width` | Width of icon | `string` | - | - |
| `height` | Height of icon | `string` | - | - |
| `fill` | Fill color of icon | `string` | HEX or color name | - |
| `hover-fill` | Fill color on hover| `string` | HEX or color name | - |
| `icon-style` | Icon style | `string` | line / monochrome | line |

## Events
Expand Down
32 changes: 29 additions & 3 deletions src/components/Unicon.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
:width="width"
:height="height"
:viewBox.camel="viewBox"
:fill="fill"
:fill="localFill"
v-bind="$attrs"
@click="$emit('click')"
@mouseover="onHover"
@mouseout="onLeave"
v-html="icon"
/>
</div>
Expand Down Expand Up @@ -41,6 +43,10 @@ export default {
type: String,
default: 'inherit'
},
hoverFill: {
type: String,
default: null
},
viewBox: {
type: String,
default: '0 0 24 24'
Expand All @@ -57,6 +63,12 @@ export default {
}
},
data () {
return {
localFill: this.fill
}
},
computed: {
icon () {
const icon = this.$options.lib.find(
Expand All @@ -66,18 +78,32 @@ export default {
if (icon) {
return icon.path
} else {
console.error(`Name '${ this.name }' of the icon is not correct`)
console.error(`Name '${this.name}' of the icon is not correct`)
return undefined
}
}
},
methods: {
onHover () {
if (this.hoverFill) {
this.localFill = this.hoverFill
}
},
onLeave () {
if (this.hoverFill) {
this.localFill = this.fill
}
}
}
}
</script>

<style>
.unicon {
display: inline-block;
fill: var(--uni-color-primary);
}
.unicon svg {
transition: 0.2s all;
}
.uim-primary {
opacity: 1;
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/Unicon.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,23 @@ describe('Unicon', () => {
svg.trigger('click')
expect(wrapper.emitted().click).toBeTruthy()
})
it('hover fill is working', () => {
const wrapper = shallowMount(Unicon, {
propsData: {
...baseProps,
fill: 'blue',
hoverFill: 'red'
}
})
expect(wrapper.exists()).toBe(true)
expect(wrapper.props().fill).toBe('blue')
expect(wrapper.props().hoverFill).toBe('red')

const svg = wrapper.find('svg')
svg.trigger('mouseover')
expect(svg.attributes().fill).toBe('red')

svg.trigger('mouseout')
expect(svg.attributes().fill).toBe('blue')
})
})

0 comments on commit 24ed866

Please sign in to comment.