Skip to content

fix: define avatar identity and fallback semantics - #5058

Open
oleksandrzavarzin-callstack wants to merge 21 commits into
callstack:mainfrom
oleksandrzavarzin-callstack:fix/define-avatar-identity-and-fallback-semantics
Open

oleksandrzavarzin-callstack wants to merge 21 commits into
callstack:mainfrom
oleksandrzavarzin-callstack:fix/define-avatar-identity-and-fallback-semantics

Conversation

@oleksandrzavarzin-callstack

@oleksandrzavarzin-callstack oleksandrzavarzin-callstack commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Avatar has no MD3 spec, so this PR defines how an avatar is named for screen readers and what it shows when its image fails. Colors come from #5016.

  • Avatar.Image gets alt (or aria-label/accessibilityLabel), fallback for a failed image, and sourceKey to retry a function source.
  • An unnamed avatar is decorative. A named one is one role="img" stop, read with its row inside a pressable.
  • Avatar.Text shows its label as given.

Related issue

#4990. Stacked on #5016: review only the last 4 commits until it merges.

Screenshots / Videos

image image image

Test plan

On the Avatar screen with a screen reader, the missing image shows "JD" and is read once, as "Jane Doe".

Comment thread src/components/Avatar/AvatarText.tsx Resolved
Comment thread src/components/Avatar/AvatarImage.tsx Resolved
Comment thread src/utils/splitAccessibilityProps.ts Resolved
Comment thread src/utils/splitAccessibilityProps.ts Resolved
Comment thread src/utils/splitAccessibilityProps.ts Resolved
@JKobrynski

Copy link
Copy Markdown
Collaborator

@oleksandrzavarzin-callstack let me know when this is ready for another review!

@oleksandrzavarzin-callstack

oleksandrzavarzin-callstack commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor Author

@oleksandrzavarzin-callstack let me know when this is ready for another review!

@JKobrynski It's ready for the another review. Let me know if you have any other suggestions

Comment thread src/components/Avatar/AvatarImage.tsx Resolved
Comment thread src/components/Avatar/AvatarImage.tsx Resolved
@oleksandrzavarzin-callstack

oleksandrzavarzin-callstack commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor Author

@JKobrynski Thanks for the review! I've pushed the fix to your comments.
Please review again.

Comment thread src/components/Avatar/utils.ts Resolved
@oleksandrzavarzin-callstack oleksandrzavarzin-callstack changed the title Fix/define avatar identity and fallback semantics fix: define avatar identity and fallback semantics Sep 4, 2026
@JKobrynski

Copy link
Copy Markdown
Collaborator

@oleksandrzavarzin-callstack lint is failing

@oleksandrzavarzin-callstack

oleksandrzavarzin-callstack commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor Author

@oleksandrzavarzin-callstack lint is failing

@JKobrynski The lint should be fixed now

@JKobrynski JKobrynski left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! @satya164

@satya164
satya164 requested a balanced review from Copilot September 15, 2026 20:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@satya164

Copy link
Copy Markdown
Contributor

Review by Claude

Findings from the example app with extra Avatar cases added to AvatarExample.tsx.

iOS

  1. Avatar.Text cuts the label to two characters. Steps: render <Avatar.Text label="+12" /> and <Avatar.Text label="ABC" />. What happens: the avatars show "+1" and "AB", while VoiceOver still reads "+12" and "ABC". What should happen: the avatar shows the label it gets, as on main. (via Claude, verify if it makes sense)
  2. Text on a custom PlatformColor background gets less contrast in dark theme. Steps: turn on the dark theme and render <Avatar.Text label="PC" style={{ backgroundColor: PlatformColor('systemBlue') }} />. What happens: the text uses onSurface (#E6E0E9) on #0A84FF, about 2.8:1. On main it is white, about 3.65:1. What should happen: the text color does not get worse than on main. (via Claude, verify if it makes sense)

iOS, dark theme: "+12" shows as "+1", "ABC" as "AB", and "PC" on systemBlue has low contrast

Android

  1. Same label cut as on iOS. +12 shows "+1" and ABC shows "AB". (via Claude, verify if it makes sense)
  2. Same dark theme contrast drop with PlatformColor('@android:color/holo_blue_dark'). The text uses the light onSurface color on #0099CC, about 2.5:1. White on main is about 3.3:1. (via Claude, verify if it makes sense)
  3. An Avatar.Text inside a pressable List.Item is now a separate TalkBack stop. Steps: render <List.Item title="Pressable item" onPress={() => {}} left={(props) => <Avatar.Text style={props.style} label="PI" size={40} />} /> and dump the tree. What happens: the row has content-desc="Pressable item" and the avatar is its own focusable node with content-desc="PI". What should happen: one stop for the row, as on main, where the avatar is not focusable. (via Claude, verify if it makes sense)
  4. An Avatar.Image that shows fallback has two TalkBack stops. Steps: render <Avatar.Image alt="Broken alt" source={{ uri: 'https://invalid.invalid/a.png' }} fallback={({ size }) => <Avatar.Text size={size} label="BA" />} />. What happens: the host is focusable with "Broken alt", and the Avatar.Text inside it is also focusable with "BA". What should happen: one stop that reads "Broken alt". (via Claude, verify if it makes sense)

Android, dark theme: "+12" shows as "+1", "ABC" as "AB", and "PC" on holo_blue_dark has low contrast

Web

  1. Same label cut. +12 shows "+1" and ABC shows "AB". (via Claude, verify if it makes sense)
  2. The Avatar.Text name sits on an element with no role. Steps: render <Avatar.Text label="JD" /> and inspect it. What happens: the output is <div aria-label="JD"> with an aria-hidden text child, and Chrome exposes it as a generic node. ARIA does not allow a name on a generic element, so screen readers can skip it, and the visible text is hidden. The same applies to the Avatar.Image host when fallback shows. What should happen: the avatar is announced, for example with role="img" on the labelled host. (via Claude, verify if it makes sense)

Web: "+12" shows as "+1" and "ABC" as "AB"

@satya164 satya164 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review by Claude

  • example/src/Examples/AvatarExample.tsx has no case for alt, fallback or a function source, so the new API is not shown or testable in the example app. CONTRIBUTING asks for example usage of component changes. (via Claude, verify if it makes sense)

Comment thread src/components/Avatar/AvatarText.tsx Resolved
Comment thread src/components/Avatar/AvatarText.tsx Resolved
Comment thread src/components/Avatar/AvatarImage.tsx Resolved
Comment thread src/components/Avatar/AvatarImage.tsx Resolved
Comment thread src/components/Avatar/utils.ts Resolved
Comment thread src/components/Avatar/utils.ts Outdated
};
}

return { background, textColor: contentColorFor(theme, background) };

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a dynamic color that is not a theme role, contentColorFor returns onSurface, which depends on the theme mode and not on the background. In dark theme that is a light color on any background. PlatformColor('systemBlue') (#0A84FF in dark mode) gets #E6E0E9 text at about 2.8:1, while main gives white at about 3.65:1. PlatformColor('systemYellow') gets about 1.1:1. So the PR description's "PlatformColor stays legible" holds only in light theme.

iOS, dark theme: "PC" on systemBlue with onSurface text

The lightness of an opaque color is not known in JS, so any automatic pick is a guess. Keep the contentColorFor match for theme roles, keep white for other values so the result is no worse than main, and say in the style docs that color must be set for other dynamic colors. (via Claude, verify if it makes sense)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your measurements are correct, but white is also a guess.
I brought it back, and still the only reliable fix is that the caller sets color. So I've also added these changes:

  • A dynamic background that is a theme color still pairs with its on- color.
  • For any other dynamic background, Avatar warns once in development env and asks for color.
  • The style docs of Avatar.Text and Avatar.Icon say that color must be set for these backgrounds.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR no longer handles text color. After rebasing onto #5016, colors come
from there: primaryContainer / onPrimaryContainer by default, plus the
backgroundColor prop. Following your review on #5016, a custom background
needs a custom color, with no automatic pick, so the contentColorFor
branch and the dev warning are gone.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: color is typed string, so a PlatformColor background can't get a
PlatformColor text color yet. I raised widening it to ColorValue on #5016:
#5016 (comment)

@Doberjohn Doberjohn added this to the 6.0.0 milestone Oct 30, 2026
@Doberjohn Doberjohn modified the milestones: 6.0.0, 6.0.0-beta.0 Nov 12, 2026
@Doberjohn Doberjohn added the needs pickup Work that has been started but needs someone to pick it up and continue. label Nov 17, 2026
@Doberjohn Doberjohn removed the needs pickup Work that has been started but needs someone to pick it up and continue. label Nov 20, 2026
Add `alt`, `fallback` and `sourceKey` to `Avatar.Image`. A failed image
shows `fallback`, and a changed `sourceKey` retries a function source.

An avatar without a label is not its own focus stop. A labelled avatar
gets `role="img"`, and a described host hides its fallback content.

`Avatar.Text` shows at most two graphemes.

Rebuilt on top of callstack#5016, which owns the avatar colors, so the
automatic contrast handling from earlier revisions is dropped.

Co-authored-by: Norbert Kamieński <norbert.kamienski@callstack.com>
Cutting the label to two graphemes changed what main renders: "+12"
showed as "+1". Callers choose the initials, so render the label as is.
On Android, accessible makes a node its own TalkBack stop, so a labelled
avatar in a pressable row, such as List.Item, was announced apart from
the row. Android reads the label without it, so set accessible only on
iOS and web, and label Android images with aria-label instead of alt,
which Image turns into accessible.
In a row whose title is the person's name, alt makes screen readers read
the name twice.
@Stoneski93
Stoneski93 force-pushed the fix/define-avatar-identity-and-fallback-semantics branch from bd7d35c to a1e2c9b Compare November 23, 2026 10:22
@github-actions

Copy link
Copy Markdown

Found potential problems with the pull request:

  • The description is too long. Please keep it under 1000 characters.

import type { ThemeProp } from '../../theme/types';

const defaultSize = 64;
export type AvatarImageSourceProps = {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AvatarImageSourceProps is exported here but not from src/index.tsx, which exports only AvatarImageProps. Anyone typing a function source can't import it, and the tests retype the shape inline (Avatar.test.tsx:536, :565, :614). Export it next to AvatarImageProps.

* an element without a role. `role` wins over `accessibilityRole`, so a
* custom `accessibilityRole` is kept.
*/
export const getAvatarRole = (accessibilityRole?: AccessibilityRole) =>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getAvatarRole has one caller, getLabelledAvatarProps below. Inline it there.

Comment on lines +79 to +80
const hasCustomLabel =
rest.accessibilityLabel !== undefined || rest['aria-label'] !== undefined;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same check as hasLabel in AvatarIcon.tsx:63, under another name. A small hasCustomLabel(props) helper in utils.ts would keep the two in sync.

Comment on lines 224 to 228
{
width: size,
height: size,
borderRadius: size / 2,
borderRadius: cornerFull,
},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use there imageStyle?

<Avatar.Image
testID="avatar-image"
source={{ uri: 'avatar.png' }}
fallback={({ size }) => <Avatar.Text size={size} label="JD" />}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fallback={({ size }) => <Avatar.Text size={size} label="JD" />} repeats about 15 times. Hoist it to one const fallback at the top of the file, as the sourceKey test already does.

style,
onError,
}: {
style: object;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: object is too broad. Use AvatarImageSourceProps (once exported) for the whole argument.

return source.uri;
}

return source;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For an array source this returns the array itself, so the key is the array's identity. With an inline source={[{ uri: 'a@1x.png', width: 40 }, { uri: 'a@2x.png', width: 80 }]}, the fallback shows after the error, but any later parent re-render passes a new array. The key no longer matches, hasError turns false, and the Image mounts again: it requests the same URL, shows an empty circle until it fails, then the fallback comes back. A { uri } source keeps the fallback in the same case ("keeps the fallback when the source object identity changes").

Key arrays by their URIs:

+  if (Array.isArray(source)) {
+    return source.map((item) => item.uri).join('|');
+  }
+
   return source;

and add the array case to "keeps the fallback when the source object identity changes".

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants