Skip to content

Grapheme-safe text: count characters the way people do - #4776

Merged
cixzhang merged 5 commits into
meta:mainfrom
AKnassa:rocky/issue-4759-grapheme-safety
Aug 16, 2026
Merged

cixzhang merged 5 commits into
meta:mainfrom
AKnassa:rocky/issue-4759-grapheme-safety

Conversation

@AKnassa

@AKnassa AKnassa commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

What this does

Makes character counting and text shortening treat an emoji, flag, or accented character as one character — the way people count — instead of the way computers store them.

Why

Reported in #4759: several spots indexed text by internal storage units. The TextArea character counter told you one emoji was "2 characters" (and announced the wrong count to screen readers), PowerSearch could slice an emoji in half when shortening a filter token (leaving a broken � character), and auto-generated Table headers couldn't capitalize some scripts.

Fixes #4759

What changed

  • One new shared utility measures and cuts text by user-perceived characters (characterCount, firstCharacter, truncateCharacters — also exported for consumers).
  • The TextArea counter, its over-limit warning, and its screen-reader announcements all use the human count now — and textareas without a limit skip the counting work entirely.
  • PowerSearch token shortening cuts between characters, never through one, keeping its exact previous lengths and ellipsis styles.
  • Table headers capitalize correctly for all scripts; Avatar's initials (fixed in fix(avatar): preserve grapheme clusters when generating initials #4750) now share the same utility instead of their own copy.

How to see it

In Storybook, open TextArea's Default story, set a maxLength, and type Hi 😀👍🇹🇷👨‍👩‍👧‍👦! — the counter reads 8, not 23. Verified live in Chrome; also fuzz-verified that plain-English behavior is byte-for-byte unchanged everywhere.

For reviewers

The two truncation helpers keep their exact thresholds (maxLength − 1 + … and the limit + 3 grace window + ...) — one shared function expresses both. The multi-item list gates (joined.length <= maxLength) deliberately stay code-unit-based: they never slice, so no corruption is possible, and changing them would alter when lists collapse to "N items". Two changesets ([fix] + [feat] for the new public utils, both patch per the 0.x rule). A same-family issue in useStreamingText (reveal ticks can transiently split emoji) surfaced during review and is filed as #4779.

A note on ownership

@nynexman4464 self-assigned this issue the same day they filed it. If you're already working on it, happy to defer to your fix or close this one — no hard feelings.

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 meta#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 meta#4759
@vercel

vercel Bot commented Aug 7, 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 16, 2026 4:04pm

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 7, 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 7, 2026
@github-actions

github-actions Bot commented Aug 7, 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 584 -
Complexity N/A Very High (71) -
PowerSearch (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 3667 -
Complexity N/A Very High (336) -
Table (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 1952 -
Complexity N/A Very High (113) -
TextArea (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 421 -
Complexity N/A Very High (66) -

Bundle Size Summary

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

Accessibility Audit

Status: 1 accessibility violation(s) found — 1 serious.

PowerSearch - 1 issue(s)
  • 🟠 serious: Ensure the contrast between foreground and background colors meets WCAG 2 AA minimum contrast ratio thresholds
    • Rule: color-contrast · Affects 2/25 stories · Learn more
    • WCAG: 1.4.3 (Level AA)

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

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 meta#4779.

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

Thanks for taking this on! I have some minor feedback on naming (I think grapheme is a little too technical) but otherwise LGTM

Comment thread packages/core/src/utils/grapheme.ts Resolved
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.
@vercel

vercel Bot commented Aug 10, 2026

Copy link
Copy Markdown

@AKnassa is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

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

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

great thanks for helping to fix!

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

Thanks I like this consolidation!

@github-actions github-actions Bot removed the needs:code-review High-risk change (new package/component/API) — needs human code review before merge label Aug 15, 2026
@cixzhang
cixzhang enabled auto-merge (squash) August 15, 2026 22:19
github-actions Bot added a commit that referenced this pull request Aug 15, 2026
@cixzhang
cixzhang merged commit 86a7f17 into meta:main Aug 16, 2026
19 checks passed
github-actions Bot added a commit that referenced this pull request Aug 16, 2026
@github-actions
github-actions Bot deleted the rocky/issue-4759-grapheme-safety branch August 17, 2026 06:50

This branch was successfully deployed

1 active deployment
Preview — 0f799945 Deployed Aug 16, 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.

i18n: grapheme-safe string manipulation (TextArea counter, PowerSearch truncation, capitalize)

3 participants