@@ -22,12 +22,6 @@ export default function avatar(user: User | null, attrs: ComponentAttrs = {}): M
2222 const hasTitle : boolean | string = attrs . title === 'undefined' || attrs . title ;
2323 if ( ! hasTitle ) delete attrs . title ;
2424
25- // If the `alt` attribute is set to null or false, we don't want to give the
26- // avatar an alt description. If it hasn't been set, we can default it later
27- // to the user's display name for accessibility.
28- const hasAlt : boolean | string = attrs . alt === 'undefined' || attrs . alt ;
29- if ( ! hasAlt ) delete attrs . alt ;
30-
3125 // If a user has been passed, then we will set up an avatar using their
3226 // uploaded image, or the first letter of their username if they haven't
3327 // uploaded one.
@@ -37,25 +31,27 @@ export default function avatar(user: User | null, attrs: ComponentAttrs = {}): M
3731
3832 if ( hasTitle ) attrs . title = attrs . title || username ;
3933
40- // If alt has not been explicitly set, default it to the username
41- // so screen readers have meaningful context.
42- if ( attrs . alt === undefined ) {
43- attrs . alt = username ;
44- }
34+ // Alt text logic:
35+ // If the `alt` attribute is set to null or false, we don't want to give the
36+ // avatar an alt description. If it hasn't been provided, we'll default it to
37+ // the user's display name *when rendering an <img>* so screen readers have context.
38+ const hasAlt : boolean | string = attrs . alt === 'undefined' || attrs . alt ;
39+ if ( ! hasAlt ) delete attrs . alt ;
4540
4641 if ( avatarUrl ) {
42+ // Default alt to username unless explicitly overridden.
43+ if ( attrs . alt === undefined ) {
44+ attrs . alt = username ;
45+ }
46+
4747 return < img { ...attrs } src = { avatarUrl } /> ;
4848 }
4949
5050 content = username . charAt ( 0 ) . toUpperCase ( ) ;
5151 attrs . style = { '--avatar-bg' : user . color ( ) } ;
52- } else {
53- // If there is no user, and no alt provided, set alt to an empty string
54- // so the avatar is treated as decorative.
55- if ( attrs . alt === undefined ) {
56- attrs . alt = '' ;
57- }
5852 }
5953
54+ // Note: We intentionally do NOT set `alt` when rendering the fallback <span>,
55+ // as `alt` is only valid for certain elements (e.g., <img>, <area>, <input type="image">).
6056 return < span { ...attrs } > { content } </ span > ;
6157}
0 commit comments