This repository has been archived by the owner on Nov 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Button.vue
129 lines (127 loc) · 2.64 KB
/
Button.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<template>
<a
v-if="href"
:class="classes"
:href="href"
v-bind="$attrs"
role="button"
class="mdc-button"
v-on="$listeners"
>
<slot name="icon" />
<span class="mdc-button__label"><slot /></span>
<slot name="trailingIcon" />
</a>
<button
v-else
:class="classes"
v-bind="$attrs"
class="mdc-button"
v-on="$listeners"
>
<slot name="icon" />
<span class="mdc-button__label"><slot /></span>
<slot name="trailingIcon" />
</button>
</template>
<script>
import { MDCRipple } from '@material/ripple'
import { baseComponentMixin, themeClassMixin } from '../base'
export default {
mixins: [baseComponentMixin, themeClassMixin],
props: {
raised: {
type: Boolean,
default: false
},
unelevated: {
type: Boolean,
default: false
},
outlined: {
type: Boolean,
default: false
},
dense: {
type: Boolean,
default: false
},
href: {
type: String,
default: ''
},
ripple: {
type: Boolean,
default: true
}
},
data () {
return {
mdcRipple: undefined,
slotObserver: undefined
}
},
computed: {
classes () {
return {
'mdc-button--raised': this.raised,
'mdc-button--unelevated': this.unelevated,
'mdc-button--outlined': this.outlined,
'mdc-button--dense': this.dense
}
}
},
watch: {
classes () {
this.reInstantiateRipple()
},
ripple () {
this.reInstantiateRipple()
}
},
mounted () {
this.updateSlot()
this.slotObserver = new MutationObserver(() => this.updateSlot())
this.slotObserver.observe(this.$el, {
childList: true,
subtree: true
})
if (this.ripple) this.mdcRipple = MDCRipple.attachTo(this.$el)
},
beforeDestroy () {
this.slotObserver.disconnect()
if (this.mdcRipple) {
this.mdcRipple.destroy()
}
},
methods: {
updateSlot () {
if (this.$slots.icon) {
this.$slots.icon.map(n => {
n.elm.classList.add('mdc-button__icon')
n.elm.setAttribute('aria-hidden', 'true')
})
}
if (this.$slots.trailingIcon) {
this.$slots.trailingIcon.map(n => {
n.elm.classList.add('mdc-button__icon')
n.elm.setAttribute('aria-hidden', 'true')
})
}
},
reInstantiateRipple () {
if (this.ripple) {
if (this.mdcRipple) {
this.mdcRipple.destroy()
}
MDCRipple.attachTo(this.$el)
} else {
if (this.mdcRipple) {
this.mdcRipple.destroy()
}
this.mdcRipple = undefined
}
}
}
}
</script>