Skip to content

Commit d1474f2

Browse files
feat(b-avatar): add alt prop for adding alt attribute to image and icon avatars (closes #4990) (#4991)
* feat(b-avatar): add `img-alt` prop for adding alt attribute to image avatars (closes #4990) * Update package.json * Update avatar.js * Update package.json * Update avatar.js * Update avatar.js * Update avatar.js Co-authored-by: Jacob Müller <jacob.mueller.elz@gmail.com>
1 parent 627eaca commit d1474f2

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

src/components/avatar/avatar.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ import { BIconPersonFill } from '../../icons/icons'
1111

1212
// --- Constants ---
1313
const NAME = 'BAvatar'
14+
const CLASS_NAME = 'b-avatar'
1415

1516
const RX_NUMBER = /^[0-9]*\.?[0-9]+$/
1617

18+
const FONT_SIZE_SCALE = 0.4
19+
1720
const DEFAULT_SIZES = {
1821
sm: '1.5em',
1922
md: '2.5em',
@@ -81,6 +84,10 @@ const props = {
8184
type: String
8285
// default: null
8386
},
87+
alt: {
88+
type: String,
89+
default: 'avatar'
90+
},
8491
variant: {
8592
type: String,
8693
default: () => getComponentConfig(NAME, 'variant')
@@ -130,48 +137,46 @@ export const BAvatar = /*#__PURE__*/ Vue.extend({
130137
functional: true,
131138
props,
132139
render(h, { props, data, children }) {
133-
const isButton = props.button
140+
const { variant, disabled, square, icon, src, text, button: isButton, buttonType: type } = props
134141
const isBLink = !isButton && (props.href || props.to)
135142
const tag = isButton ? BButton : isBLink ? BLink : 'span'
136-
const variant = props.variant
137-
const disabled = props.disabled
138-
const type = props.buttonType
139-
const square = props.square
140143
const rounded = square ? false : props.rounded === '' ? true : props.rounded || 'circle'
141144
const size = computeSize(props.size)
145+
const alt = props.alt || null
146+
const ariaLabel = props.ariaLabel || null
142147

143148
let $content = null
144149
if (children) {
145150
// Default slot overrides props
146151
$content = children
147-
} else if (props.icon) {
152+
} else if (icon) {
148153
$content = h(BIcon, {
149-
props: { icon: props.icon },
150-
attrs: { 'aria-hidden': 'true' }
154+
props: { icon },
155+
attrs: { 'aria-hidden': 'true', alt }
151156
})
152-
} else if (props.src) {
153-
$content = h('img', { attrs: { src: props.src } })
154-
} else if (props.text) {
155-
const fontSize = size ? `calc(${size} * 0.4)` : null
156-
$content = h('span', { style: { fontSize } }, props.text)
157+
} else if (src) {
158+
$content = h('img', { attrs: { src, alt } })
159+
} else if (text) {
160+
const fontSize = size ? `calc(${size} * ${FONT_SIZE_SCALE})` : null
161+
$content = h('span', { style: { fontSize } }, text)
157162
} else {
158-
$content = h(BIconPersonFill, { attrs: { 'aria-hidden': 'true' } })
163+
$content = h(BIconPersonFill, { attrs: { 'aria-hidden': 'true', alt } })
159164
}
160165

161166
const componentData = {
162-
staticClass: 'b-avatar',
167+
staticClass: CLASS_NAME,
163168
class: {
164169
// We use badge/button styles for theme variants
165-
[`${isButton ? 'btn' : 'badge'}-${props.variant}`]: !!props.variant,
166-
// Rounding / Square
170+
[`${isButton ? 'btn' : 'badge'}-${variant}`]: !!variant,
171+
// Rounding/Square
167172
rounded: rounded === true,
168173
'rounded-0': square,
169174
[`rounded-${rounded}`]: rounded && rounded !== true,
170175
// Other classes
171176
disabled
172177
},
173178
style: { width: size, height: size },
174-
attrs: { 'aria-label': props.ariaLabel || null },
179+
attrs: { 'aria-label': ariaLabel },
175180
props: isButton ? { variant, disabled, type } : isBLink ? pluckProps(linkProps, props) : {}
176181
}
177182

src/components/avatar/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
"prop": "icon",
2323
"description": "Icon name to use for the avatar. Must be all lowercase. Defaults to `person-fill` if `text` or `src` props not provided"
2424
},
25+
{
26+
"prop": "alt",
27+
"version": "2.9.0",
28+
"description": "Value to place in the 'alt' attribute for image and icon avatars"
29+
},
2530
{
2631
"prop": "size",
2732
"description": "Size of the avatar. Refer to the documentation for details"

0 commit comments

Comments
 (0)