Repository navigation
fix: define avatar identity and fallback semantics - #5058
oleksandrzavarzin-callstack wants to merge 21 commits into
Conversation
|
@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 |
|
@JKobrynski Thanks for the review! I've pushed the fix to your comments. |
|
@oleksandrzavarzin-callstack lint is failing |
@JKobrynski The lint should be fixed now |
|
Review by Claude Findings from the example app with extra iOS
Android
Web
|
satya164
left a comment
There was a problem hiding this comment.
Review by Claude
example/src/Examples/AvatarExample.tsxhas no case foralt,fallbackor a functionsource, 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)
| }; | ||
| } | ||
|
|
||
| return { background, textColor: contentColorFor(theme, background) }; |
There was a problem hiding this comment.
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.
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)
There was a problem hiding this comment.
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
styledocs ofAvatar.TextandAvatar.Iconsay thatcolormust be set for these backgrounds.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
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.
bd7d35c to
a1e2c9b
Compare
|
Found potential problems with the pull request:
|
| import type { ThemeProp } from '../../theme/types'; | ||
|
|
||
| const defaultSize = 64; | ||
| export type AvatarImageSourceProps = { |
There was a problem hiding this comment.
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) => |
There was a problem hiding this comment.
getAvatarRole has one caller, getLabelledAvatarProps below. Inline it there.
| const hasCustomLabel = | ||
| rest.accessibilityLabel !== undefined || rest['aria-label'] !== undefined; |
There was a problem hiding this comment.
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.
| { | ||
| width: size, | ||
| height: size, | ||
| borderRadius: size / 2, | ||
| borderRadius: cornerFull, | ||
| }, |
There was a problem hiding this comment.
Can we use there imageStyle?
| <Avatar.Image | ||
| testID="avatar-image" | ||
| source={{ uri: 'avatar.png' }} | ||
| fallback={({ size }) => <Avatar.Text size={size} label="JD" />} |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
style: object is too broad. Use AvatarImageSourceProps (once exported) for the whole argument.
| return source.uri; | ||
| } | ||
|
|
||
| return source; |
There was a problem hiding this comment.
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".




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.Imagegetsalt(oraria-label/accessibilityLabel),fallbackfor a failed image, andsourceKeyto retry a functionsource.role="img"stop, read with its row inside a pressable.Avatar.Textshows its label as given.Related issue
#4990. Stacked on #5016: review only the last 4 commits until it merges.
Screenshots / Videos
Test plan
On the Avatar screen with a screen reader, the missing image shows "JD" and is read once, as "Jane Doe".