Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(core): Avatar initials paint in primary text on the neutral tint (#10205)

Cindy Zhang20f7d2d3b9b3
diff --git a/.changeset/avatar-initials-contrast.md b/.changeset/avatar-initials-contrast.md
new file mode 100644
index 0000000000..64117e7f18
--- /dev/null
+++ b/.changeset/avatar-initials-contrast.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `Avatar` initials and the default person icon paint in primary text on the neutral tint, a pair at 4.5:1 or more in light and dark mode; secondary text fell below 4.5:1 in dark mode. Initials read darker in light mode and lighter in dark mode.
+
+@cixzhang
diff --git a/docs/specs/components/Avatar/Avatar.spec.md b/docs/specs/components/Avatar/Avatar.spec.md
index fdd30fbe3d..451f182362 100644
--- a/docs/specs/components/Avatar/Avatar.spec.md
+++ b/docs/specs/components/Avatar/Avatar.spec.md
@@ -122,17 +122,17 @@ Consumer migration instructions belong in consumer docs and release notes.
## Behavioral and layout contract
-| ID | Invariant | Basis |
-| --- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------ |
-| FR1 | The root is a relative, non-shrinking inline-flex box of the resolved size with the shape's radius; it carries the `avatar` target, which reflects `size` and `shape`. A clipped content area fills it with the same radius and suppresses text selection. | Current source and tests |
-| FR2 | The content area shows `src` while it has not failed, then `fallbackSrc` while it has not failed, then initials when the name yields any, then a default person icon at 60% of the box. Each image renders with empty `alt` and `object-fit: cover`. A failure is tracked per source, so a changed `src` or `fallbackSrc` is tried again. | Current source and tests |
-| FR3 | Initials are the first letter, digit, or emoji of the first and last words of the trimmed `name`, uppercased; leading punctuation in a word is skipped, words with none are dropped, one qualifying word yields one initial, and grapheme clusters are never split. A whitespace-only name counts as absent. Initials and the default icon paint on the `avatar-fallback` target (reflecting `size`) with `--color-neutral`, secondary text, body family, medium weight, and initials at 40% of the box. In forced colors, a lone Avatar's initials and default icon take the surrounding color (`currentColor`): the forced foreground by default, and the system color of a control that paints one around it (GrayText, HighlightText); inside an AvatarGroup they keep the forced foreground on the group's ring. | Current source and tests |
-| FR4 | A truthy `status` renders in an absolutely positioned slot outside the clipped content area. On a circle it centers on the circle's edge at the bottom inline-end 45° point; on other shapes it sits at the bottom inline-end corner, offset outward by a quarter of its size. Placement mirrors in right-to-left documents. | Current source and tests |
-| FR5 | The resolved name is `alt`, else `name` (blank strings count as absent), composed with the status label as the translated "{name}, {status}" when both exist; a status label alone also names the avatar. The status label comes from a string `label` on the passed element, read on every render, and from a label an AvatarStatusDot reports at any wrapper depth, which wins and updates the root before paint without a second render. | Current source and tests |
-| FR6 | `href` renders the shared link component (or `as`) with `target` and `rel`; otherwise `onClick` renders a `button type="button"`; otherwise the root is a `div`. Link and button roots carry the `data-avatar-item` marker and the shared focus-visible outline; `onClick` attaches to the link or button. In development, an interactive avatar whose only name is a status label, or that has no name and no caller `aria-label`/`aria-labelledby`, logs a warning. | Current source and tests |
-| FR7 | The tooltip shows the trimmed custom string, else the meaningful `name`, placed above, on hover and keyboard focus; `tooltip={false}` or a blank result shows none. A static avatar with a tooltip and outside a group gets `tabIndex={0}` so keyboard users can reveal it. A custom string is added to `aria-describedby` after any caller value; the default name tooltip is not, because it repeats the name. Implementation gap: a grouped link or button avatar that is not the group's tab stop stops opening its tooltip on focus after a re-render that re-attaches its root element: a new `status` element (an inline `status` prop creates one on every parent render), a changed `name`, `alt`, locale, caller ARIA, or caller `ref`, or a tooltip that turns on after mount (Current-state impact). | Current source and tests |
-| FR8 | Inside an AvatarGroup in the same layer (`spec:AST-038` FR7, FR8), the group's size and shape win over the avatar's own props; the root gains a 2px surface-colored ring outside its box and the group's negative inline-start overlap; a static avatar adds no tooltip tab stop, and link and button roots keep the `data-avatar-item` marker so they join the group's roving set. An avatar in a hover card, popover, or other layer opened from a group keeps its own size and shape, gets no ring or overlap, and keeps its tooltip tab stop; a link or button avatar there still carries the marker, so the group's roving focus, which selects marked elements in its DOM subtree, still includes it. | Current source and tests |
-| FR9 | `ref` reaches the root of every variant. Supported DOM, ARIA, data, and event props reach the root after the derived ARIA, so a caller `aria-label`, `role`, or `aria-hidden` wins (`architecture:public-component-api` INV5); `data-testid` reaches the root; `xstyle`, `className`, and `style` merge in that order after component styles. Implementation gap: on the static root the component sets `tabIndex` after caller props, so a caller `tabIndex` is replaced (Current-state impact). | Current source and tests |
+| ID | Invariant | Basis |
+| --- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------ |
+| FR1 | The root is a relative, non-shrinking inline-flex box of the resolved size with the shape's radius; it carries the `avatar` target, which reflects `size` and `shape`. A clipped content area fills it with the same radius and suppresses text selection. | Current source and tests |
+| FR2 | The content area shows `src` while it has not failed, then `fallbackSrc` while it has not failed, then initials when the name yields any, then a default person icon at 60% of the box. Each image renders with empty `alt` and `object-fit: cover`. A failure is tracked per source, so a changed `src` or `fallbackSrc` is tried again. | Current source and tests |
+| FR3 | Initials are the first letter, digit, or emoji of the first and last words of the trimmed `name`, uppercased; leading punctuation in a word is skipped, words with none are dropped, one qualifying word yields one initial, and grapheme clusters are never split. A whitespace-only name counts as absent. Initials and the default icon paint on the `avatar-fallback` target (reflecting `size`) with `--color-neutral` and primary text, a pair at 4.5:1 or more in light and dark mode, body family, medium weight, and initials at 40% of the box. In forced colors, a lone Avatar's initials and default icon take the surrounding color (`currentColor`): the forced foreground by default, and the system color of a control that paints one around it (GrayText, HighlightText); inside an AvatarGroup they keep the forced foreground on the group's ring. | Current source and tests |
+| FR4 | A truthy `status` renders in an absolutely positioned slot outside the clipped content area. On a circle it centers on the circle's edge at the bottom inline-end 45° point; on other shapes it sits at the bottom inline-end corner, offset outward by a quarter of its size. Placement mirrors in right-to-left documents. | Current source and tests |
+| FR5 | The resolved name is `alt`, else `name` (blank strings count as absent), composed with the status label as the translated "{name}, {status}" when both exist; a status label alone also names the avatar. The status label comes from a string `label` on the passed element, read on every render, and from a label an AvatarStatusDot reports at any wrapper depth, which wins and updates the root before paint without a second render. | Current source and tests |
+| FR6 | `href` renders the shared link component (or `as`) with `target` and `rel`; otherwise `onClick` renders a `button type="button"`; otherwise the root is a `div`. Link and button roots carry the `data-avatar-item` marker and the shared focus-visible outline; `onClick` attaches to the link or button. In development, an interactive avatar whose only name is a status label, or that has no name and no caller `aria-label`/`aria-labelledby`, logs a warning. | Current source and tests |
+| FR7 | The tooltip shows the trimmed custom string, else the meaningful `name`, placed above, on hover and keyboard focus; `tooltip={false}` or a blank result shows none. A static avatar with a tooltip and outside a group gets `tabIndex={0}` so keyboard users can reveal it. A custom string is added to `aria-describedby` after any caller value; the default name tooltip is not, because it repeats the name. Implementation gap: a grouped link or button avatar that is not the group's tab stop stops opening its tooltip on focus after a re-render that re-attaches its root element: a new `status` element (an inline `status` prop creates one on every parent render), a changed `name`, `alt`, locale, caller ARIA, or caller `ref`, or a tooltip that turns on after mount (Current-state impact). | Current source and tests |
+| FR8 | Inside an AvatarGroup in the same layer (`spec:AST-038` FR7, FR8), the group's size and shape win over the avatar's own props; the root gains a 2px surface-colored ring outside its box and the group's negative inline-start overlap; a static avatar adds no tooltip tab stop, and link and button roots keep the `data-avatar-item` marker so they join the group's roving set. An avatar in a hover card, popover, or other layer opened from a group keeps its own size and shape, gets no ring or overlap, and keeps its tooltip tab stop; a link or button avatar there still carries the marker, so the group's roving focus, which selects marked elements in its DOM subtree, still includes it. | Current source and tests |
+| FR9 | `ref` reaches the root of every variant. Supported DOM, ARIA, data, and event props reach the root after the derived ARIA, so a caller `aria-label`, `role`, or `aria-hidden` wins (`architecture:public-component-api` INV5); `data-testid` reaches the root; `xstyle`, `className`, and `style` merge in that order after component styles. Implementation gap: on the static root the component sets `tabIndex` after caller props, so a caller `tabIndex` is replaced (Current-state impact). | Current source and tests |
### Allowed variation
diff --git a/packages/core/src/Avatar/Avatar.test.tsx b/packages/core/src/Avatar/Avatar.test.tsx
index 0a7a17d49b..21ecabd29a 100644
--- a/packages/core/src/Avatar/Avatar.test.tsx
+++ b/packages/core/src/Avatar/Avatar.test.tsx
@@ -4,6 +4,8 @@ import {Profiler, useState} from 'react';
import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
import {act, render, screen, fireEvent} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
+import * as stylex from '@stylexjs/stylex';
+import {colorVars} from '../theme/tokens.stylex';
import {Avatar} from './Avatar';
import {AvatarStatusDot} from './AvatarStatusDot';
import {AvatarGroup} from '../AvatarGroup';
@@ -67,6 +69,52 @@ describe('Avatar', () => {
expect(icon?.querySelector('svg')).not.toBeNull();
});
+ it('paints initials in primary text on the neutral tint, a pair that holds 4.5:1 in dark mode', () => {
+ // Secondary text on the neutral tint falls below 4.5:1 in dark mode.
+ const probe = stylex.create({
+ tint: {backgroundColor: colorVars['--color-neutral']},
+ ink: {
+ color: {
+ default: colorVars['--color-text-primary'],
+ '@media (forced-colors: active)': 'currentColor',
+ },
+ },
+ });
+ render(<Avatar name="Ada Lovelace" />);
+ const initials = screen.getByText('AL');
+ const classesOf = (style: stylex.StyleXStyles) =>
+ (stylex.props(style).className ?? '')
+ .split(' ')
+ // Dev mode prepends a per-file `File__style.key` debug class that
+ // differs between the probe and the component.
+ .filter(cls => cls !== '' && !cls.includes('__'));
+ for (const cls of [...classesOf(probe.tint), ...classesOf(probe.ink)]) {
+ expect(initials.className).toContain(cls);
+ }
+ });
+
+ it('paints grouped initials in primary text too', () => {
+ // A grouped face skips the lone-avatar colour rule, so the fallback's own
+ // colour is the one it shows.
+ const probe = stylex.create({
+ ink: {color: colorVars['--color-text-primary']},
+ });
+ render(
+ <AvatarGroup>
+ <Avatar name="Ada Lovelace" />
+ <Avatar name="Grace Hopper" />
+ </AvatarGroup>,
+ );
+ const initials = screen.getByText('AL');
+ const classes = (stylex.props(probe.ink).className ?? '')
+ .split(' ')
+ .filter(cls => cls !== '' && !cls.includes('__'));
+ expect(classes.length).toBeGreaterThan(0);
+ for (const cls of classes) {
+ expect(initials.className).toContain(cls);
+ }
+ });
+
it('puts the avatar box on the element that carries the theme target', () => {
// T7: `.astryx-avatar` documents a `size` visual prop, so the width and
// height that prop selects on must live on the targeted element — a theme
diff --git a/packages/core/src/Avatar/Avatar.tsx b/packages/core/src/Avatar/Avatar.tsx
index 482278f9c8..d07dc9c1ba 100644
--- a/packages/core/src/Avatar/Avatar.tsx
+++ b/packages/core/src/Avatar/Avatar.tsx
@@ -194,8 +194,10 @@ const styles = stylex.create({
// `.astryx-avatar-fallback` class target (font size through its size
// variant, `.astryx-avatar-fallback.<size>`), so the defaults here are
// plain values with no internal-var seam. See Avatar.doc.mjs theming.
+ // Primary text: secondary text on the neutral tint falls below 4.5:1 in
+ // dark mode.
backgroundColor: colorVars['--color-neutral'],
- color: colorVars['--color-text-secondary'],
+ color: colorVars['--color-text-primary'],
fontFamily: typographyVars['--font-family-body'],
fontWeight: fontWeightVars['--font-weight-medium'],
textTransform: 'uppercase',
@@ -209,7 +211,7 @@ const styles = stylex.create({
// only CanvasText is sure to read on it.
loneFallback: {
color: {
- default: colorVars['--color-text-secondary'],
+ default: colorVars['--color-text-primary'],
'@media (forced-colors: active)': 'currentColor',
},
},