Skip to content

Commit 39d5371

Browse files
authored
fallback to initials if the image is not loaded (#40) (#64)
1 parent 73a14f6 commit 39d5371

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

documentation/_getting-started.pug

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,18 @@ a(href='https://github.com/eliep/vue-avatar')
257257
initials="AS"
258258
:size="100">
259259
</avatar>
260+
tr
261+
td
262+
avatar(username='Luke Skywalker', src='./static/luke-skywalker.png', :size='100')
263+
td
264+
pre
265+
code.language-html.
266+
<!-- fallback to initials if image doesn't load -->
267+
<avatar
268+
src="./static/luke-skywalker.png"
269+
username="Luke Skywalker"
270+
:size="100">
271+
</avatar>
260272

261273
h2 Default color
262274
p

src/Avatar.vue

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<template>
22
<div class="vue-avatar--wrapper" :style="[style, customStyle]" aria-hidden="true">
3+
<!-- this img is not displayed; it is used to detect failure-to-load of div background image -->
4+
<img v-if="this.isImage" style="display: none" :src="this.src" @error="onImgError"></img>
35
<span v-show="!this.isImage">{{ userInitial }}</span>
46
</div>
57
</template>
@@ -49,7 +51,8 @@ export default {
4951
'#F44336', '#FF4081', '#9C27B0', '#673AB7',
5052
'#3F51B5', '#2196F3', '#03A9F4', '#00BCD4', '#009688',
5153
'#4CAF50', '#8BC34A', '#CDDC39', /* '#FFEB3B' , */ '#FFC107',
52-
'#FF9800', '#FF5722', '#795548', '#9E9E9E', '#607D8B']
54+
'#FF9800', '#FF5722', '#795548', '#9E9E9E', '#607D8B'],
55+
imgError: false
5356
}
5457
},
5558
@@ -73,7 +76,7 @@ export default {
7376
},
7477
7578
isImage () {
76-
return Boolean(this.src)
79+
return !this.imgError && Boolean(this.src)
7780
},
7881
7982
style () {
@@ -136,6 +139,10 @@ export default {
136139
return initials
137140
},
138141
142+
onImgError (evt) {
143+
this.imgError = true
144+
},
145+
139146
randomBackgroundColor (seed, colors) {
140147
return colors[seed % (colors.length)]
141148
},

test/unit/specs/Avatar.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,18 @@ describe('Avatar.vue', function () {
5151

5252
var backgroundImage = wrapper.element.style.backgroundImage
5353
expect(backgroundImage).to.contain('path/to/img')
54+
expect(wrapper.element.querySelector('.vue-avatar--wrapper > span').textContent).not.to.contain('HF')
55+
})
56+
57+
it('should render initials if the \'src\' does not load', function () {
58+
var username = 'Hubert-Félix'
59+
60+
const wrapper = mount(Avatar, { propsData: {
61+
username: username,
62+
src: 'path/to/img'
63+
} })
64+
wrapper.setData({ imgError: true })
65+
66+
expect(wrapper.element.querySelector('.vue-avatar--wrapper > span').textContent).to.contain('HF')
5467
})
5568
})

0 commit comments

Comments
 (0)