avatar.js
7.11 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import { extend } from '../../vue'
import { NAME_AVATAR } from '../../constants/components'
import { EVENT_NAME_CLICK, EVENT_NAME_IMG_ERROR } from '../../constants/events'
import {
PROP_TYPE_BOOLEAN,
PROP_TYPE_BOOLEAN_STRING,
PROP_TYPE_NUMBER_STRING,
PROP_TYPE_STRING
} from '../../constants/props'
import { SLOT_NAME_BADGE } from '../../constants/slots'
import { isNumber, isNumeric, isString } from '../../utils/inspect'
import { toFloat } from '../../utils/number'
import { omit, sortKeys } from '../../utils/object'
import { makeProp, makePropsConfigurable, pluckProps } from '../../utils/props'
import { isLink } from '../../utils/router'
import { normalizeSlotMixin } from '../../mixins/normalize-slot'
import { BIcon } from '../../icons/icon'
import { BIconPersonFill } from '../../icons/icons'
import { BButton } from '../button/button'
import { BLink, props as BLinkProps } from '../link/link'
// --- Constants ---
const CLASS_NAME = 'b-avatar'
const SIZES = ['sm', null, 'lg']
const FONT_SIZE_SCALE = 0.4
const BADGE_FONT_SIZE_SCALE = FONT_SIZE_SCALE * 0.7
// --- Helper methods ---
export const computeSize = value => {
// Parse to number when value is a float-like string
value = isString(value) && isNumeric(value) ? toFloat(value, 0) : value
// Convert all numbers to pixel values
return isNumber(value) ? `${value}px` : value || null
}
// --- Props ---
const linkProps = omit(BLinkProps, ['active', 'event', 'routerTag'])
export const props = makePropsConfigurable(
sortKeys({
...linkProps,
alt: makeProp(PROP_TYPE_STRING, 'avatar'),
ariaLabel: makeProp(PROP_TYPE_STRING),
badge: makeProp(PROP_TYPE_BOOLEAN_STRING, false),
badgeLeft: makeProp(PROP_TYPE_BOOLEAN, false),
badgeOffset: makeProp(PROP_TYPE_STRING),
badgeTop: makeProp(PROP_TYPE_BOOLEAN, false),
badgeVariant: makeProp(PROP_TYPE_STRING, 'primary'),
button: makeProp(PROP_TYPE_BOOLEAN, false),
buttonType: makeProp(PROP_TYPE_STRING, 'button'),
icon: makeProp(PROP_TYPE_STRING),
rounded: makeProp(PROP_TYPE_BOOLEAN_STRING, false),
size: makeProp(PROP_TYPE_NUMBER_STRING),
square: makeProp(PROP_TYPE_BOOLEAN, false),
src: makeProp(PROP_TYPE_STRING),
text: makeProp(PROP_TYPE_STRING),
variant: makeProp(PROP_TYPE_STRING, 'secondary')
}),
NAME_AVATAR
)
// --- Main component ---
// @vue/component
export const BAvatar = /*#__PURE__*/ extend({
name: NAME_AVATAR,
mixins: [normalizeSlotMixin],
inject: {
getBvAvatarGroup: { default: () => () => null }
},
props,
data() {
return {
localSrc: this.src || null
}
},
computed: {
bvAvatarGroup() {
return this.getBvAvatarGroup()
},
computedSize() {
// Always use the avatar group size
const { bvAvatarGroup } = this
return computeSize(bvAvatarGroup ? bvAvatarGroup.size : this.size)
},
computedVariant() {
const { bvAvatarGroup } = this
return bvAvatarGroup && bvAvatarGroup.variant ? bvAvatarGroup.variant : this.variant
},
computedRounded() {
const { bvAvatarGroup } = this
const square = bvAvatarGroup && bvAvatarGroup.square ? true : this.square
const rounded = bvAvatarGroup && bvAvatarGroup.rounded ? bvAvatarGroup.rounded : this.rounded
return square ? '0' : rounded === '' ? true : rounded || 'circle'
},
fontStyle() {
const { computedSize: size } = this
const fontSize = SIZES.indexOf(size) === -1 ? `calc(${size} * ${FONT_SIZE_SCALE})` : null
return fontSize ? { fontSize } : {}
},
marginStyle() {
const { computedSize: size, bvAvatarGroup } = this
const overlapScale = bvAvatarGroup ? bvAvatarGroup.overlapScale : 0
const value = size && overlapScale ? `calc(${size} * -${overlapScale})` : null
return value ? { marginLeft: value, marginRight: value } : {}
},
badgeStyle() {
const { computedSize: size, badgeTop, badgeLeft, badgeOffset } = this
const offset = badgeOffset || '0px'
return {
fontSize: SIZES.indexOf(size) === -1 ? `calc(${size} * ${BADGE_FONT_SIZE_SCALE} )` : null,
top: badgeTop ? offset : null,
bottom: badgeTop ? null : offset,
left: badgeLeft ? offset : null,
right: badgeLeft ? null : offset
}
}
},
watch: {
src(newValue, oldValue) {
if (newValue !== oldValue) {
this.localSrc = newValue || null
}
}
},
methods: {
onImgError(event) {
this.localSrc = null
this.$emit(EVENT_NAME_IMG_ERROR, event)
},
onClick(event) {
this.$emit(EVENT_NAME_CLICK, event)
}
},
render(h) {
const {
computedVariant: variant,
disabled,
computedRounded: rounded,
icon,
localSrc: src,
text,
fontStyle,
marginStyle,
computedSize: size,
button,
buttonType: type,
badge,
badgeVariant,
badgeStyle
} = this
const link = !button && isLink(this)
const tag = button ? BButton : link ? BLink : 'span'
const alt = this.alt
const ariaLabel = this.ariaLabel || null
let $content = null
if (this.hasNormalizedSlot()) {
// Default slot overrides props
$content = h('span', { staticClass: 'b-avatar-custom' }, [this.normalizeSlot()])
} else if (src) {
$content = h('img', {
style: variant ? {} : { width: '100%', height: '100%' },
attrs: { src, alt },
on: { error: this.onImgError }
})
$content = h('span', { staticClass: 'b-avatar-img' }, [$content])
} else if (icon) {
$content = h(BIcon, {
props: { icon },
attrs: { 'aria-hidden': 'true', alt }
})
} else if (text) {
$content = h(
'span',
{
staticClass: 'b-avatar-text',
style: fontStyle
},
[h('span', text)]
)
} else {
// Fallback default avatar content
$content = h(BIconPersonFill, { attrs: { 'aria-hidden': 'true', alt } })
}
let $badge = h()
const hasBadgeSlot = this.hasNormalizedSlot(SLOT_NAME_BADGE)
if (badge || badge === '' || hasBadgeSlot) {
const badgeText = badge === true ? '' : badge
$badge = h(
'span',
{
staticClass: 'b-avatar-badge',
class: { [`badge-${badgeVariant}`]: badgeVariant },
style: badgeStyle
},
[hasBadgeSlot ? this.normalizeSlot(SLOT_NAME_BADGE) : badgeText]
)
}
const componentData = {
staticClass: CLASS_NAME,
class: {
// Apply size class
[`${CLASS_NAME}-${size}`]: size && SIZES.indexOf(size) !== -1,
// We use badge styles for theme variants when not rendering `BButton`
[`badge-${variant}`]: !button && variant,
// Rounding/Square
rounded: rounded === true,
[`rounded-${rounded}`]: rounded && rounded !== true,
// Other classes
disabled
},
style: { ...marginStyle, width: size, height: size },
attrs: { 'aria-label': ariaLabel || null },
props: button ? { variant, disabled, type } : link ? pluckProps(linkProps, this) : {},
on: button || link ? { click: this.onClick } : {}
}
return h(tag, componentData, [$content, $badge])
}
})