-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
VBadge.ts
198 lines (177 loc) Β· 4.74 KB
/
VBadge.ts
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Styles
import './VBadge.sass'
// Components
import VIcon from '../VIcon/VIcon'
// Mixins
import Colorable from '../../mixins/colorable'
import Themeable from '../../mixins/themeable'
import Toggleable from '../../mixins/toggleable'
import Transitionable from '../../mixins/transitionable'
import { factory as PositionableFactory } from '../../mixins/positionable'
// Utilities
import mixins from '../../util/mixins'
import {
convertToUnit,
getSlot,
} from '../../util/helpers'
// Types
import { VNode } from 'vue'
export default mixins(
Colorable,
PositionableFactory(['left', 'bottom']),
Themeable,
Toggleable,
Transitionable,
/* @vue/component */
).extend({
name: 'v-badge',
props: {
avatar: Boolean,
bordered: Boolean,
color: {
type: String,
default: 'primary',
},
content: { required: false },
dot: Boolean,
label: {
type: String,
default: '$vuetify.badge',
},
icon: String,
inline: Boolean,
offsetX: [Number, String],
offsetY: [Number, String],
overlap: Boolean,
tile: Boolean,
transition: {
type: String,
default: 'scale-rotate-transition',
},
value: { default: true },
},
computed: {
classes (): object {
return {
'v-badge--avatar': this.avatar,
'v-badge--bordered': this.bordered,
'v-badge--bottom': this.bottom,
'v-badge--dot': this.dot,
'v-badge--icon': this.icon != null,
'v-badge--inline': this.inline,
'v-badge--left': this.left,
'v-badge--overlap': this.overlap,
'v-badge--tile': this.tile,
...this.themeClasses,
}
},
computedBottom (): string {
return this.bottom ? 'auto' : this.computedYOffset
},
computedLeft (): string {
if (this.isRtl) {
return this.left ? this.computedXOffset : 'auto'
}
return this.left ? 'auto' : this.computedXOffset
},
computedRight (): string {
if (this.isRtl) {
return this.left ? 'auto' : this.computedXOffset
}
return !this.left ? 'auto' : this.computedXOffset
},
computedTop (): string {
return this.bottom ? this.computedYOffset : 'auto'
},
computedXOffset (): string {
return this.calcPosition(this.offsetX)
},
computedYOffset (): string {
return this.calcPosition(this.offsetY)
},
isRtl (): boolean {
return this.$vuetify.rtl
},
// Default fallback if offsetX
// or offsetY are undefined.
offset (): number {
if (this.overlap) return this.dot ? 8 : 12
return this.dot ? 2 : 4
},
styles (): object {
if (this.inline) return {}
return {
bottom: this.computedBottom,
left: this.computedLeft,
right: this.computedRight,
top: this.computedTop,
}
},
},
methods: {
calcPosition (offset: string | number): string {
return `calc(100% - ${convertToUnit(offset || this.offset)})`
},
genBadge () {
const lang = this.$vuetify.lang
const label = this.$attrs['aria-label'] || lang.t(this.label)
const data = this.setBackgroundColor(this.color, {
staticClass: 'v-badge__badge',
style: this.styles,
attrs: {
'aria-atomic': this.$attrs['aria-atomic'] || 'true',
'aria-label': label,
'aria-live': this.$attrs['aria-live'] || 'polite',
title: this.$attrs.title,
role: this.$attrs.role || 'status',
},
directives: [{
name: 'show',
value: this.isActive,
}],
})
const badge = this.$createElement('span', data, [this.genBadgeContent()])
if (!this.transition) return badge
return this.$createElement('transition', {
props: {
name: this.transition,
origin: this.origin,
mode: this.mode,
},
}, [badge])
},
genBadgeContent () {
// Dot prop shows no content
if (this.dot) return undefined
const slot = getSlot(this, 'badge')
if (slot) return slot
if (this.content) return String(this.content)
if (this.icon) return this.$createElement(VIcon, this.icon)
return undefined
},
genBadgeWrapper () {
return this.$createElement('span', {
staticClass: 'v-badge__wrapper',
}, [this.genBadge()])
},
},
render (h): VNode {
const badge = [this.genBadgeWrapper()]
const children = [getSlot(this)]
const {
'aria-atomic': _x,
'aria-label': _y,
'aria-live': _z,
role,
title,
...attrs
} = this.$attrs
if (this.inline && this.left) children.unshift(badge)
else children.push(badge)
return h('span', {
staticClass: 'v-badge',
attrs,
class: this.classes,
}, children)
},
})