Skip to content

fix(avatar): preserve grapheme clusters when generating initials - #4750

Merged
cixzhang merged 7 commits into
meta:mainfrom
alex-js-ltd:fix/avatar-initials-surrogate-pairs
Aug 6, 2026
Merged

cixzhang merged 7 commits into
meta:mainfrom
alex-js-ltd:fix/avatar-initials-surrogate-pairs

Conversation

@alex-js-ltd

Copy link
Copy Markdown
Contributor

Fixes #4749

getInitials() used .charAt(0), which indexes UTF-16 code units rather than user-perceived grapheme clusters. As a result, names beginning with multi-code-unit characters—such as emoji, flag or ZWJ sequences, and decomposed letters with combining marks—could produce broken or truncated initials.

Changes

  • Updated getInitials() to extract the first grapheme using Intl.Segmenter with granularity: 'grapheme'.
  • Added a code-point-safe fallback for environments where Intl.Segmenter is unavailable. The fallback prevents surrogate pairs from being split, although full multi-code-point grapheme preservation requires Intl.Segmenter.
  • Added regression tests covering an emoji surrogate pair and a multi-code-point flag emoji.
  • Added a changeset.

Test plan

  • pnpm -F @astryxdesign/core test — Avatar suite passes (45/45), including the two new tests. Both fail against the previous .charAt(0) implementation and pass with this fix.
  • pnpm check:changesets

alex-js-ltd and others added 4 commits August 6, 2026 10:58
getInitials() uses String.charAt(0), which indexes by UTF-16 code unit
rather than codepoint. Names starting with a character outside the
Basic Multilingual Plane (e.g. emoji) get split mid-surrogate-pair,
rendering as a replacement character instead of the initial. This
test documents the failure; the fix follows in a subsequent commit.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@vercel

vercel Bot commented Aug 6, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview Aug 6, 2026 1:17pm

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 6, 2026
@github-actions github-actions Bot added community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge labels Aug 6, 2026
github-actions Bot added a commit that referenced this pull request Aug 6, 2026
@github-actions

github-actions Bot commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

Modified Components

Avatar (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 613 -
Complexity N/A Very High (76) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.7KB 1.2KB

Accessibility Audit

Status: No accessibility violations detected.


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

github-actions Bot added a commit that referenced this pull request Aug 6, 2026
github-actions Bot added a commit that referenced this pull request Aug 6, 2026

@cixzhang cixzhang 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.

Thank you for improving the i18n here!

@cixzhang
cixzhang enabled auto-merge (squash) August 6, 2026 16:11
@cixzhang cixzhang removed the needs:code-review High-risk change (new package/component/API) — needs human code review before merge label Aug 6, 2026
@cixzhang
cixzhang merged commit 8c64f99 into meta:main Aug 6, 2026
21 checks passed
@github-actions
github-actions Bot deleted the fix/avatar-initials-surrogate-pairs branch August 10, 2026 07:05
cixzhang added a commit that referenced this pull request Aug 12, 2026
The approval event is one-shot: if review-clear's run dies, the gate stays
pending forever and only a human noticing recovers it. During the 2026-08-06
GitHub Actions incident the workflow failed in 'Set up job' (Failed to resolve
action download info: Service Unavailable) before any script ran, and #4750 sat
blocked on an approval it already had.

Add a 15-minute clear-only sweep to review-clear.yml, sharing the exact same
clearOne() as the event path so recovery can never drift from the live path. It
considers an open PR unresolved when the needs:code-review label is present OR
the review-required status is pending, and clears only when an entitled
ENGOWNER has approved -- it can remove a gate an approval already satisfied,
never create one, so it is safe unattended.

Also add workflow_dispatch for an immediate sweep, and document that a human
cannot hand-clear this gate: branch protection requires the review-required
context from the GitHub Actions app, so a status posted with a personal token
greens the rollup without satisfying the requirement.
cixzhang added a commit that referenced this pull request Aug 12, 2026
The approval event is one-shot: if review-clear's run dies, the gate stays
pending forever and only a human noticing recovers it. During the 2026-08-06
GitHub Actions incident the workflow failed in 'Set up job' (Failed to resolve
action download info: Service Unavailable) before any script ran, and #4750 sat
blocked on an approval it already had.

Add a 15-minute clear-only sweep to review-clear.yml, sharing the exact same
clearOne() as the event path so recovery can never drift from the live path. It
considers an open PR unresolved when the needs:code-review label is present OR
the review-required status is pending, and clears only when an entitled
ENGOWNER has approved -- it can remove a gate an approval already satisfied,
never create one, so it is safe unattended.

Also add workflow_dispatch for an immediate sweep, and document that a human
cannot hand-clear this gate: branch protection requires the review-required
context from the GitHub Actions app, so a status posted with a personal token
greens the rollup without satisfying the requirement.
cixzhang added a commit that referenced this pull request Aug 12, 2026
The approval event is one-shot: if review-clear's run dies, the gate stays
pending forever and only a human noticing recovers it. During the 2026-08-06
GitHub Actions incident the workflow failed in 'Set up job' (Failed to resolve
action download info: Service Unavailable) before any script ran, and #4750 sat
blocked on an approval it already had.

Add a 15-minute clear-only sweep to review-clear.yml, sharing the exact same
clearOne() as the event path so recovery can never drift from the live path. It
considers an open PR unresolved when the needs:code-review label is present OR
the review-required status is pending, and clears only when an entitled
ENGOWNER has approved -- it can remove a gate an approval already satisfied,
never create one, so it is safe unattended.

Also add workflow_dispatch for an immediate sweep, and document that a human
cannot hand-clear this gate: branch protection requires the review-required
context from the GitHub Actions app, so a status posted with a personal token
greens the rollup without satisfying the requirement.
cixzhang added a commit that referenced this pull request Aug 16, 2026
* fix(core): count strings by graphemes, not code units

Several user-visible string operations indexed by UTF-16 code units,
miscounting or corrupting emoji, flags, ZWJ sequences, and combining
marks:

- TextArea's character counter (display, over-limit state, aria-invalid,
  warning icon, and live-region announcements) showed an emoji as 2+
  characters. All limit math now counts user-perceived characters, and
  is skipped entirely when no maxLength is set.
- PowerSearch's two truncation helpers could split a surrogate pair or
  drop a combining mark at the cut point. Both now cut on grapheme
  boundaries with their exact previous thresholds and ellipses.
- Table's auto-generated column headers failed to capitalize
  astral-plane letters (charAt(0) grabbed half a surrogate pair).

Adds shared utils/grapheme.ts (graphemeLength, firstGrapheme,
truncateGraphemes) built on a reused Intl.Segmenter with a code-point
fallback — extracted from Avatar's merged #4750 fix, which now consumes
the shared util. For pure-ASCII input every refactored site is
byte-identical to the previous behavior at all reachable parameters.

Fixes #4759

* perf(core): take the first grapheme without segmenting the whole string

Also memoize TextArea's counter length, harden the combining-mark test
fixture against NFC normalization, and point maxLength consumers at
graphemeLength for validation parity. Streaming reveal follow-up filed
as #4779.

* refactor(core): name the character utilities in plain English

Review feedback on the new public API: `grapheme` is Unicode jargon and
reads as too technical for consumers.

  graphemeLength     -> characterCount
  firstGrapheme      -> firstCharacter
  truncateGraphemes  -> truncateCharacters

Renames the file, the internal helpers and the doc prose to match, so the
word only survives where it is the literal `Intl.Segmenter` granularity
value. No behavior change: every function body is unchanged apart from
the identifiers.

---------

Co-authored-by: Cindy Zhang <[email protected]>

This branch was successfully deployed

1 active deployment
Preview — d5301ebc Deployed Aug 6, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. community Authored by a community contributor (not on the eng/design team)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avatar: getInitials splits emoji/grapheme clusters when generating initials

2 participants