Skip to content

fix(Badge): a long label no longer escapes its container - #5558

Merged
cixzhang merged 8 commits into
mainfrom
fix/badge-truncate
Aug 27, 2026
Merged

cixzhang merged 8 commits into
mainfrom
fix/badge-truncate

Conversation

@freddymeta

Copy link
Copy Markdown
Contributor

Summary

Badge sets white-space: nowrap with nothing to clip it — the one pairing that neither wraps nor truncates. A label wider than the space available doesn't ellipsize, it renders outside the badge's container, over whatever sits beside it.

<div style={{width: 100}}>
  <Badge variant="pink" label="Awaiting security review" />
</div>
  whiteSpace: 'nowrap',
+ maxWidth: '100%',
+ minWidth: 0,

plus overflow: hidden; text-overflow: ellipsis; min-width: 0 on a new inner label span.

Rubric B10 — "long text truncates or wraps with no layout breakage" — severity FIX.

Measured in Chromium

main this PR
100px column 163px wide, 63px outside it 100px, 0 spill
140px column, with icon 175px, 35px outside 140px, 0 spill
fixed-layout table cell 163px, 64px over the next cell's text 98px, 0 spill
300px column, label="Active" 53px 53px — unchanged

That last row is the one that matters for risk: a badge with room to spare is identical before and after. The clamp only ever changes a case that was already overflowing.

Why the inner span

text-overflow needs a block container. The badge root is inline-flex, and taking it off that to get one would cost the icon its vertical centring — so the ellipsis goes on the label instead, and with an icon the icon holds its place while the label gives way.

minWidth: 0 is not decoration. As a flex item the badge's automatic minimum size is its max-content width, which holds it at full text width and undoes maxWidth: 100% entirely; the label span needs the same for the same reason.

What is deliberately not here

A tooltip carrying the full text. Only-when-truncated needs a scrollWidth > offsetWidth measurement, which means hooks — and Badge is currently one of the ~24 files in packages/core/src with no 'use client'. [[RSC Compatibility]] names Badge specifically among the components that should become server-renderable, so quietly making it a client component inside a defect fix is the wrong trade to make silently. It is a separate PR with its own case to argue, and this fix stands without it.

Test plan

  • pnpm build, verify-exports, check:repo, lint:strict, core + storybook + CLI-strict typechecks — clean. Badge: 8/8 existing tests pass unchanged.
  • pnpm a11y:audit -- --components Badge: 0 new violations. (It also reports 2 color-contrast baseline entries as resolved — I checked, those report identically on main, so they are not this PR's doing.)
  • New story: Long labels in tight space — the 100px case, the icon case, and a badge with room to spare side by side.
  • No unit test for the clamp: jsdom has no layout, so the only thing it could assert is the declaration itself — the shape of test that makes a feature look covered while it is inert. The Chromium numbers above are the evidence.

Where this came from

An engineer asked whether Badge had a max width, having hit exactly this in a hover card. The workaround in the wild is per-call-site:

<Badge className="min-w-0 max-w-full"
       label={<span className="min-w-0 truncate" title={label}>{label}</span>} />

Structurally that is this fix, which is a decent signal it belongs in the component — one wrapper knows to do it, the next person putting a Badge in a table cell won't.

@freddymeta

`Badge` set `white-space: nowrap` with nothing to clip it — the one pairing
that neither wraps nor truncates. A label wider than the space available
rendered outside the badge's container and over whatever sat beside it.

Measured in Chromium, `label="Awaiting security review"` in a 100px column:
163px wide, 63px of it outside the column. In a fixed-layout table cell it
painted 64px over the text in the next cell.

`maxWidth: 100%` clamps it and `minWidth: 0` lets it actually shrink — as a
flex item the automatic minimum size would otherwise hold the badge at its
full text width and undo the clamp. The ellipsis goes on a new inner label
span rather than the badge itself, because `text-overflow` needs a block
container and taking the root off `inline-flex` to get one would cost the icon
its centring; with an icon, the icon holds its place and the label gives way.

A badge that already fits is untouched: measured before and after, one with
room to spare is 53px either way. Only the cases that were already overflowing
change.

No hooks, so `Badge` stays one of the few core components that renders on the
server. A tooltip carrying the full text needs runtime measurement and would
make it a client component — that is a separate change with its own trade-off,
and this fix stands without it.

No unit test for the clamp itself: jsdom has no layout, so the only thing it
could assert is the declaration. The Chromium measurements are the evidence.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 26, 2026
@vercel

vercel Bot commented Aug 26, 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 27, 2026 6:30am

Request Review

@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 needs:design-review Affects visuals — Design should review labels Aug 26, 2026
A worktree-local symlink to a sibling install got committed with the change,
and `pnpm install --frozen-lockfile` then fails on it — which is why every
job on this PR went red before reaching any build step. Nothing to do with
the change itself.
@github-actions

github-actions Bot commented Aug 26, 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

Badge (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 189 -
Complexity N/A Low (4) -

Bundle Size Summary

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

Accessibility Audit

Status: No accessibility violations detected.

Visual Regression

70 added · 0 removed. View the report

To accept these exact frames: /accept-visual 33045929788/1 <reason>

Added — After
Added — After visual regression frame

Added — After
Added — After visual regression frame

Added — After
Added — After visual regression frame


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

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

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

The containment behavior looks right: the badge stays within its available width, the icon remains visible, and labels that fit are unchanged.

However, truncating the label hides meaningful status text with no way for sighted users to recover it. The new story even says to hover for the full text, but Badge adds neither a tooltip nor a title, so hovering currently reveals nothing.

Please add an accessible way to expose the complete label when it is truncated—ideally a tooltip available on hover and keyboard focus, with an explicit touch-device alternative. A native title would be a minimum fallback, but silent truncation should not ship on its own.

@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, this fixes the spill. I agree with Ruby’s hold though: the status is now clipped, but the rendered badge has no title, tooltip, or other path for a sighted user to recover the full label. Can you add a server-safe full-label fallback for string/number labels and keep the richer only-when-truncated tooltip as its own PR if it needs measurement?

[Reviewed by Robohands]

Truncation without a way back to the full text is what Ruby and cixzhang
held the PR on: the label is clipped, and the tail is simply gone.

A string or number label is now also set as the badge's `title`. That is
the half of the answer that costs nothing — no measurement, no hook — so
`Badge` renders identically on the server and stays usable in a server
component. It follows the shape `BaseTable` already uses for a truncated
header cell: string content only, and only when there is something to
show. A rich label is left alone, because flattening a subtree to a
string renders a guess.

Two gaps stay open, both needing runtime measurement: the `title` is set
whether or not the label actually fits, and a native `title` answers
hover but not keyboard focus and not touch. The measured, only-when-cut
tooltip is a client component and belongs in its own PR, as cixzhang
asked.

The `LongLabels` story previously promised the full text on hover while
nothing supplied it — Ruby's catch. Its comment is now accurate about
both what works and what does not.
The changeset, the story and the component comment all said the
focus/touch-reachable tooltip was "tracked separately" without saying
where. It is #5585.
@freddymeta

Copy link
Copy Markdown
Contributor Author

Both holds addressed, in the shape @cixzhang scoped: a server-safe fallback here, the measured tooltip as its own thing.

What's in now (Badge.tsx) — a string or number label is also set as the badge's title:

const labelTitle =
  typeof label === 'number'
    ? String(label)
    : typeof label === 'string' && label.length > 0
      ? label
      : undefined;

That's the same shape BaseTable already uses for a truncated header cell — string content only, non-empty guard — so it's a pattern the codebase already carries rather than a new one. It costs no measurement and no hook, so Badge renders identically on the server and stays usable in a server component. A rich ReactNode label is left alone: flattening a subtree to a string renders a guess, since an icon, a <strong> and a nested element all read differently.

Four tests cover it: string → title, number → title, rich label → no title, empty string → no title. (I wrote a fifth for "a caller's own title wins" and deleted it — BaseProps deliberately Omits title, so it's unreachable through the public API.)

@rubyycheung — your catch on the story was right, and it was worse than a missing tooltip. The LongLabels comment said "Hover a truncated one for the full text" while nothing supplied it. It now describes what actually happens, including what doesn't.

What this does not close, and I'd rather say it plainly than let title look like the whole answer:

  • the title is set whether or not the label actually fits — knowing that means measuring;
  • a native title is pointer-only. Not keyboard focus, and on touch not at all. That's the part of your review this PR doesn't reach.

Both are in #5585, along with the reason they're not here: doing it properly needs scrollWidth > clientWidth (or a ResizeObserver) plus a focusable badge, which makes Badge a client component and turns a non-interactive element into a focusable one. That's a behaviour change worth its own discussion, not a rider on a containment 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, this covers the server-safe string/number fallback I asked for. I’d take this and leave focus/touch/rich-label recovery to #5585.

[Reviewed by Robohands]

@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 27, 2026
@freddymeta

Copy link
Copy Markdown
Contributor Author

@rubyycheung re-requesting — your hold was the one this head answers. The full text now rides in the badge's title for string and number labels, which is the minimum fallback you named.

Being straight about what it does not do, since you asked for more than the minimum: title is pointer-only (no keyboard focus, nothing on touch), and it is set whether or not the label actually fits. Both need runtime measurement and make Badge a client component, so cixzhang asked for them as a separate PR — that is #5585, which also covers the focusable-badge question your "keyboard focus" ask implies.

The story comment you caught was the worse half of this and is fixed: it no longer promises something nothing supplied.

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

Re-approving after the mechanical main refresh; the reviewed change is unchanged.\n\n[Reviewed by Robohands]

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

Re-approving after the mechanical main refresh; the reviewed change is unchanged.

[Reviewed by Robohands]

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

Re-approving after the mechanical main refresh; the reviewed change is unchanged.

[Reviewed by Robohands]

@cixzhang

Copy link
Copy Markdown
Contributor

/accept-visual 33045929788/1 These 70 frames add Badge coverage; no existing pixels changed, and sensor-backed review verified layout containment plus the intended full-label fallback.

@github-actions

Copy link
Copy Markdown
Contributor

Visual acceptance refused: only a repository maintainer may accept stable visual changes.

@freddymeta

Copy link
Copy Markdown
Contributor Author

/accept-visual

@github-actions

Copy link
Copy Markdown
Contributor

Visual acceptance refused: copy the exact /accept-visual <run>/<attempt> <reason> command from the report.

@freddymeta

Copy link
Copy Markdown
Contributor Author

/accept-visual 33045929788/1 These 70 frames add Badge coverage; no existing pixels changed, and sensor-backed review verified layout containment plus the intended full-label fallback.

@github-actions

Copy link
Copy Markdown
Contributor

Visual acceptance refused: only a repository maintainer may accept stable visual changes.

@cixzhang

Copy link
Copy Markdown
Contributor

/accept-visual 33045929788/1 These 70 frames add Badge coverage; no existing pixels changed, and sensor-backed review verified layout containment plus the intended full-label fallback.

github-actions Bot added a commit that referenced this pull request Aug 27, 2026
@github-actions github-actions Bot added the visual-approved Current visual bundle has an explicit acceptance record label Aug 27, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Visual changes accepted for 251e77c203c6. A new push invalidates this approval; after merge, the reviewed AFTER frames must reproduce before the baseline is promoted.

@cixzhang
cixzhang merged commit 8530ece into main Aug 27, 2026
25 of 27 checks passed
@github-actions
github-actions Bot deleted the fix/badge-truncate branch August 27, 2026 09:11

This branch was successfully deployed

1 active deployment
Preview — 251e77c2 Deployed Aug 27, 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) needs:design-review Affects visuals — Design should review visual-approved Current visual bundle has an explicit acceptance record

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants