@@ -11,9 +11,12 @@ import { BIconPersonFill } from '../../icons/icons'
1111
1212// --- Constants ---
1313const NAME = 'BAvatar'
14+ const CLASS_NAME = 'b-avatar'
1415
1516const RX_NUMBER = / ^ [ 0 - 9 ] * \. ? [ 0 - 9 ] + $ /
1617
18+ const FONT_SIZE_SCALE = 0.4
19+
1720const 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
0 commit comments