An open source design system that's fully customizable and agent ready
TypeScript
fix(Badge): a long label no longer escapes its container (#5558)
freddymeta8530ece69dbf
diff --git a/.changeset/badge-clamp-long-label.md b/.changeset/badge-clamp-long-label.mdnew file mode 100644index 0000000000..c0d947f7c1--- /dev/null+++ b/.changeset/badge-clamp-long-label.md@@ -0,0 +1,45 @@+---+'@astryxdesign/core': patch+---++[fix] Badge: a long label no longer escapes its container.++`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.++```tsx+<div style={{width: 100}}>+ <Badge variant="pink" label="Awaiting security review" />+</div>+```++Measured in Chromium: that badge came out **163px** wide in a 100px column,+spilling 63px past it; in a fixed-layout table cell it painted 64px over the+text in the next cell. The badge now clamps to the width it is given and cuts+the label with an ellipsis.++A badge that already fits is untouched — same width, same height, same DOM.+Measured before and after, a badge with room to spare is 53px either way; only+the cases that were already overflowing change. `Badge` uses no hooks and+stays server-renderable.++The ellipsis sits on an 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.++So that a clipped tail is not simply lost, a string or number label is also+carried in the badge's `title` — the same shape `BaseTable` already uses for a+truncated header cell. That costs no measurement and no hook, so+`Badge` still renders the same on the server and stays usable in a server+component. A rich `label` is left alone rather than flattened to a guess.++Two gaps remain, both needing runtime measurement, and both tracked in #5585:+the `title` is set whether or not the label actually fits, and a native `title`+is a pointer affordance — it answers hover, not keyboard focus, and not touch+at all. The refinement is a tooltip shown only when the text is really cut,+reachable by hover and by focus, which makes `Badge` a client component and is+its own trade-off to weigh.++@freddymetadiff --git a/apps/storybook/stories/Badge.stories.tsx b/apps/storybook/stories/Badge.stories.tsxindex ada01cb53d..bc6bec88ae 100644--- a/apps/storybook/stories/Badge.stories.tsx+++ b/apps/storybook/stories/Badge.stories.tsx@@ -92,3 +92,67 @@ export const NonSemanticColors: Story = {</div>),};++// A badge is one line, so a label wider than the space it has is cut short+// with an ellipsis rather than escaping the container. The full text rides in+// the badge's `title`, so hovering a truncated one shows it — a pointer-only+// affordance for now; the focus- and touch-reachable tooltip needs runtime+// measurement and is tracked in #5585.+export const LongLabels: Story = {+ name: 'Long labels in tight space',+ render: () => (+ <div style={{display: 'flex', flexDirection: 'column', gap: 20}}>+ <div>+ <div style={{font: '600 12px system-ui', marginBottom: 6}}>+ In a 100px column+ </div>+ <div+ style={{+ width: 100,+ outline: '1px dashed var(--color-border)',+ padding: 4,+ }}>+ <Badge variant="pink" label="Awaiting security review" />+ </div>+ </div>+ <div>+ <div style={{font: '600 12px system-ui', marginBottom: 6}}>+ With an icon — the icon holds its place, the label gives way+ </div>+ <div+ style={{+ width: 140,+ outline: '1px dashed var(--color-border)',+ padding: 4,+ }}>+ <Badge+ variant="warning"+ icon={<span aria-hidden="true">⚠</span>}+ label="Awaiting security review"+ />+ </div>+ </div>+ <div>+ <div style={{font: '600 12px system-ui', marginBottom: 6}}>+ Room to spare — unchanged+ </div>+ <div+ style={{+ width: 300,+ outline: '1px dashed var(--color-border)',+ padding: 4,+ }}>+ <Badge variant="success" label="Active" />+ </div>+ </div>+ </div>+ ),+ parameters: {+ docs: {+ description: {+ story:+ 'A badge has a fixed height and never wraps, so a label wider than its container is truncated with an ellipsis rather than escaping the container. A badge that fits is untouched — the clamp only ever changes the case that would otherwise overflow.',+ },+ },+ },+};diff --git a/packages/core/src/Badge/Badge.doc.mjs b/packages/core/src/Badge/Badge.doc.mjsindex f5e4cbba1a..0376f5d145 100644--- a/packages/core/src/Badge/Badge.doc.mjs+++ b/packages/core/src/Badge/Badge.doc.mjs@@ -18,7 +18,8 @@ export const docs = {{name: 'label',type: 'ReactNode',- description: 'Badge text content.',+ description:+ 'Badge text content. A badge is one line, so a label wider than the space available is cut with an ellipsis rather than escaping its container; a string or number label is also set as the badge\'s title so the full text stays reachable on hover.',},{name: 'icon',@@ -132,7 +133,7 @@ export const docsDense = {},propDescriptions: {variant: 'visual style variant',- label: 'badge text content',+ label: 'badge text content; over-wide labels clip with an ellipsis, full text in title',icon: 'optional leading icon',},};diff --git a/packages/core/src/Badge/Badge.test.tsx b/packages/core/src/Badge/Badge.test.tsxindex 20fa2a042e..8e0b549000 100644--- a/packages/core/src/Badge/Badge.test.tsx+++ b/packages/core/src/Badge/Badge.test.tsx@@ -37,9 +37,7 @@ describe('Badge', () => {'yellow',] as const;- const {rerender} = render(- <Badge variant={colors[0]} label={colors[0]} />,- );+ const {rerender} = render(<Badge variant={colors[0]} label={colors[0]} />);expect(screen.getByText(colors[0])).toBeInTheDocument();for (const color of colors.slice(1)) {@@ -74,6 +72,37 @@ describe('Badge', () => {expect(screen.getByTestId('custom-badge')).toBeInTheDocument();});+ describe('full-label fallback', () => {+ it('carries the full text in title for a string label', () => {+ // The label is clipped when it does not fit, so the full text has to be+ // reachable. No measurement is involved, so this holds on the server too.+ const {container} = render(<Badge label="Awaiting security review" />);+ expect(container.firstElementChild).toHaveAttribute(+ 'title',+ 'Awaiting security review',+ );+ });++ it('carries a number label too', () => {+ const {container} = render(<Badge label={42} />);+ expect(container.firstElementChild).toHaveAttribute('title', '42');+ });++ it('sets no title for a rich label', () => {+ // Flattening a subtree to a string renders a guess — an icon or nested+ // element reads differently — so it is left alone rather than guessed at.+ const {container} = render(<Badge label={<strong>Rich</strong>} />);+ expect(container.firstElementChild).not.toHaveAttribute('title');+ });++ it('sets no title for an empty string label', () => {+ // Nothing to recover, so nothing to announce — the same guard+ // `BaseTable` applies to a truncated header cell.+ const {container} = render(<Badge label="" />);+ expect(container.firstElementChild).not.toHaveAttribute('title');+ });+ });+it('renders astryx-* class names for theme targeting', () => {const {container} = render(<Badge variant="success" label="Active" />);const root = container.firstElementChild!;diff --git a/packages/core/src/Badge/Badge.tsx b/packages/core/src/Badge/Badge.tsxindex 14fe58053f..50c9076903 100644--- a/packages/core/src/Badge/Badge.tsx+++ b/packages/core/src/Badge/Badge.tsx@@ -46,6 +46,24 @@ const styles = stylex.create({lineHeight: typeScaleVars['--text-supporting-leading'],fontWeight: fontWeightVars['--font-weight-medium'],whiteSpace: 'nowrap',+ // A badge is one line by construction — fixed height, `nowrap` — so a+ // label wider than the space available has to go somewhere. Without these+ // it went *outside* its container: `nowrap` with nothing to clip it+ // neither wraps nor truncates, it just escapes, and lands on whatever sits+ // beside it. `minWidth: 0` matters as much as the max: as a flex item the+ // automatic minimum size would otherwise hold the badge at its full text+ // width and push the clamp back out again.+ maxWidth: '100%',+ minWidth: 0,+ },+ // The ellipsis goes on the label 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. The label is a+ // flex item, so it needs its own `minWidth: 0` for the same reason as above.+ label: {+ overflow: 'hidden',+ textOverflow: 'ellipsis',+ minWidth: 0,},});@@ -163,6 +181,28 @@ export function Badge({ref,...props}: BadgeProps) {+ // Clipping a label makes its tail unrecoverable, so the full text has to+ // stay reachable somewhere. `title` is the half of that answer which costs+ // nothing: no measurement, no hook, so `Badge` renders the same on the+ // server and stays usable in a server component. It follows the shape+ // `BaseTable` already uses for a truncated header cell (BaseTable.tsx) —+ // string content only, and only when there is something to show.+ //+ // A rich label is left alone: it is a subtree whose text would have to be+ // flattened to a string, and flattening renders a guess — an icon, a+ // `<strong>`, a nested element all read differently.+ //+ // It is set whether or not the label actually fits, because knowing that+ // requires measuring. The refinement — a tooltip only when the text is+ // really cut, reachable by hover and by focus — needs that measurement and+ // a client component, so it is tracked in #5585.+ const labelTitle =+ typeof label === 'number'+ ? String(label)+ : typeof label === 'string' && label.length > 0+ ? label+ : undefined;+return (<spanref={ref}@@ -172,9 +212,10 @@ export function Badge({className,style,)}+ title={labelTitle}{...props}>{icon}- {label}+ <span {...stylex.props(styles.label)}>{label}</span></span>);}