An open source design system that's fully customizable and agent ready
TypeScript
fix(Link): maxLines truncates inside a line narrower than the link (#9059)
Cindy Zhang2782f99035ea
diff --git a/.changeset/link-maxlines-truncates.md b/.changeset/link-maxlines-truncates.mdnew file mode 100644index 0000000000..488571ccf7--- /dev/null+++ b/.changeset/link-maxlines-truncates.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] Link: `maxLines` now truncates when the link sits in a line narrower than its text. The link, as an anchor or a button, stops at its container and its label draws an ellipsis, instead of growing to its whole text and being clipped with no ellipsis.++@cixzhangdiff --git a/apps/storybook/stories/Link.stories.tsx b/apps/storybook/stories/Link.stories.tsxindex 506f9c387d..846926cd9e 100644--- a/apps/storybook/stories/Link.stories.tsx+++ b/apps/storybook/stories/Link.stories.tsx@@ -135,6 +135,36 @@ export const InlineWithText: Story = {),};+/**+ * `maxLines={1}` inside a fixed-width line: the link, as an anchor or as a+ * button, stops at the line and draws an ellipsis.+ */+export const TruncatedInConstrainedLine: Story = {+ tags: ['!autodocs'],+ render: () => (+ <div style={{display: 'grid', gap: 12, width: 220}} data-truncation-host="">+ {(['anchor', 'button'] as const).map(form => (+ <p+ key={form}+ data-truncation-line={form}+ style={{+ margin: 0,+ overflow: 'hidden',+ whiteSpace: 'nowrap',+ textOverflow: 'ellipsis',+ }}>+ <Link+ href={form === 'anchor' ? '/meetings/weekly' : undefined}+ onClick={form === 'button' ? () => {} : undefined}+ maxLines={1}>+ Weekly design review with the product and research teams+ </Link>+ </p>+ ))}+ </div>+ ),+};+export const AllVariants: Story = {render: () => (<divdiff --git a/packages/core/src/Link/Link.spec.md b/packages/core/src/Link/Link.spec.mdindex dd0f25242f..3d58f62d00 100644--- a/packages/core/src/Link/Link.spec.md+++ b/packages/core/src/Link/Link.spec.md@@ -16,6 +16,7 @@ verified_by:packages/core/src/Link/__tests__/Link.a11y.test.tsx,packages/core/src/Link/__tests__/Link.a11y.chromium.spec.ts,packages/core/src/Link/__tests__/Link.navigation.a11y.chromium.spec.ts,+ packages/core/src/Link/__tests__/Link.truncation.a11y.chromium.spec.ts,]modules: []families: [family:navigation-destinations]@@ -107,7 +108,7 @@ Consumer migration instructions belong in consumer docs and release notes.| FR3 | An external Link that is not rendered as a button shows a trailing extra-small external-link icon that inherits the root's color, followed by visually hidden new-tab text: `newTabLabel`, else the localized default. | Current source and tests || FR4 | A destination reaches the link component only through the shared navigation rule; a rejected destination renders an inert anchor and never invokes a router. | `family:navigation-destinations` FR1–FR6; `spec:AST-005` || FR5 | A disabled Link stays focusable with `aria-disabled="true"`, never navigates, and never runs `onClick`. The disabled anchor form omits `href`, `target`, and `rel`, takes a Tab stop, and skips the custom link component. In every form, clicks are cancelled and stopped, Enter and Space are suppressed, other keys reach the caller's `onKeyDown`, and the Link paints at half opacity with no pointer events and no press paint. A focused Link that becomes disabled MUST keep focus and expose the change on that element (`spec:AST-085` FR4, FR5), and a caller `aria-disabled`, `role`, or `tabIndex` MUST NOT replace the disabled exposure (`architecture:public-component-api` INV5). Implementation gap: caller props spread after Link's own, so a caller `aria-disabled`, `role`, or `tabIndex` overrides them. Implementation gap: an enabled link renders through the safe link component and a disabled one as a plain anchor (or a `<button>` when it has `onClick`), so a focused link that becomes disabled (or re-enabled) is replaced and focus falls to the page. | `spec:AST-085` FR1, FR2, FR4, FR5; `family:navigation-destinations` FR7; current tests |-| FR6 | Children render inside Text with `type` (default `body`), `size`, `weight`, `color` (default `accent`), `display` (default `inline`), and `maxLines` (default `0`). The root inherits font family, size, weight, and line height and lays content out with a small gap; `isStandalone` sets the body size and leading on the root. A Link inside a Text with `maxLines` MUST truncate with that text. Implementation gap: the root is `inline-flex`, so an ancestor Text clamp does not truncate it. | Current source and tests |+| FR6 | Children render inside Text with `type` (default `body`), `size`, `weight`, `color` (default `accent`), `display` (default `inline`), and `maxLines` (default `0`). The root inherits font family, size, weight, and line height and lays content out with a small gap; with `maxLines` above 0 it stops at its container, so its Text truncates inside a constrained line; `isStandalone` sets the body size and leading on the root. A Link inside a Text with `maxLines` MUST truncate with that text. Implementation gap: the root is `inline-flex`, so an ancestor Text clamp does not truncate it. | Current source and tests || FR7 | The root paints the chosen color for the six built-in colors, which the underline and the external icon take; the label's color comes from Text (FR6). On hover-capable pointers an enabled Link underlines on hover, and for `primary`, `secondary`, and `accent` the root color mixes in the hover tint. `hasUnderline` underlines at rest. | Current source and tests || FR8 | Press feedback follows `design:user-states` DR1 and DR2 for Link's text surface: an enabled link or button form paints the pressed overlay, and a disabled Link does not. | `design:user-states` DR1, DR2; current tests || FR9 | `label`, when non-empty, becomes `aria-label` and replaces the content as the accessible name; a caller `aria-label` overrides it; otherwise the content is the name. | Current tests |@@ -191,7 +192,7 @@ carries no anatomy-theming map.| FR2, FR3, AR3 | `Link.test.tsx` external, target, rel, and new-tab cases; `Link.a11y.chromium.spec.ts` external state | External; explicit `_blank`; custom label | A missing safety token, icon, or new-tab text fails. Evidence gap: no test checks the accessible name of an external link with `label`. Evidence gap: no test sets `referrerPolicy` or asserts the external icon's size or color. | `audit:Link/accessibility` || FR4 | `Link.navigation.a11y.chromium.spec.ts` | Rejected and ordinary schemes; native, router, downloads | A rejected destination that navigates or reaches a router fails. | `audit:Link/behavior` || FR5, AR2 | `Link.test.tsx` disabled cases | Disabled anchor; disabled button form; with `onClick` | Navigation, a consumer `onClick`, a lost Tab stop, or a custom component on a disabled link fails. Evidence gap: no test disables a focused link or passes a caller `aria-disabled`, `role`, or `tabIndex`. Evidence gap: no test asserts that other keys reach the caller's `onKeyDown`, or the half-opacity, no-pointer-events paint. | `audit:Link/accessibility` |-| FR6 (type) | `Link.test.tsx` text-type cases | Default `body`; `type="inherit"` | A dropped Text `type` fails. Evidence gap: no test asserts that `size`, `weight`, `color`, `display`, or `maxLines` reach Text, or the `isStandalone` root size. | `audit:Link/visual` |+| FR6 (type) | `Link.test.tsx` text-type cases | Default `body`; `type="inherit"` | A dropped Text `type` fails. `Link.truncation.a11y.chromium.spec.ts`: an anchor and a button Link with `maxLines={1}` in a fixed-width line stop at the line and draw an ellipsis. Evidence gap: no test asserts that `size`, `weight`, `color`, or `display` reach Text, or the `isStandalone` root size. | `audit:Link/visual` || FR6, FR7 (paint) | Evidence gap: the color, underline, and standalone cases assert only that a link renders; hover paint is untested | Each color; `hasUnderline`; `isStandalone` | — | `audit:Link/visual` || FR8 | `Link.test.tsx` pressed-state suite | Link, button form, disabled | No press paint, or press paint on a disabled Link, fails. | `audit:Link/behavior` || FR9, FR11, AR4 | `Link.test.tsx` label, ref, and theme-class cases | With and without `label`; `data-color` | A missing or extra `aria-label`, target, `data-color`, or ref fails. The accessibility suites assert a passed `data-*` attribute and `aria-current` on the root, and the navigation suite asserts `download`. Evidence gap: no test passes both `label` and `aria-label`, or passes `className`, `style`, or `xstyle`. | `audit:Link/theming` |diff --git a/packages/core/src/Link/Link.tsx b/packages/core/src/Link/Link.tsxindex 963910ec09..3efed09e87 100644--- a/packages/core/src/Link/Link.tsx+++ b/packages/core/src/Link/Link.tsx@@ -76,6 +76,14 @@ const styles = stylex.create({transitionDuration: durationVars['--duration-fast'],transitionTimingFunction: easeVars['--ease-standard'],},+ // A truncating label needs a box that stops at its container: an+ // inline-flex root otherwise grows to its whole text, so its Text never+ // overflows and draws no ellipsis (`component:Link` FR6, `component:Text`+ // FR4).+ truncating: {+ maxWidth: '100%',+ minWidth: 0,+ },/*** Reset styles for rendering as a <button> when href is undefined.* Strips all native button chrome so it looks identical to a link.@@ -370,6 +378,7 @@ export function Link({hasUnderline && styles.hasUnderline,isStandalone && styles.standalone,isDisabled && styles.disabled,+ maxLines > 0 && styles.truncating,xstyle,),className,@@ -403,6 +412,7 @@ export function Link({hasUnderline && styles.hasUnderline,isStandalone && styles.standalone,styles.disabled,+ maxLines > 0 && styles.truncating,xstyle,),className,@@ -434,6 +444,7 @@ export function Link({hasUnderline && styles.hasUnderline,isStandalone && styles.standalone,isDisabled && styles.disabled,+ maxLines > 0 && styles.truncating,xstyle,),className,diff --git a/packages/core/src/Link/__tests__/Link.truncation.a11y.chromium.spec.ts b/packages/core/src/Link/__tests__/Link.truncation.a11y.chromium.spec.tsnew file mode 100644index 0000000000..7992e16c0d--- /dev/null+++ b/packages/core/src/Link/__tests__/Link.truncation.a11y.chromium.spec.ts@@ -0,0 +1,58 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.+// @pr-a11y scope ^(packages/core/src/Link/|packages/core/src/Text/|apps/storybook/stories/Link\.stories)++/**+ * Real-browser evidence for `component:Link` FR6 with `component:Text` FR4: a+ * Link with `maxLines={1}` in a fixed-width line, as an anchor and as a+ * button, stops at the line and its label overflows with an ellipsis.+ */++import {expect, test} from '@playwright/test';+import {+ DEFAULT_STORYBOOK_DIR,+ serveStorybook,+ type StaticServer,+} from '@astryxdesign/a11y-spec/storybook';++let storybook: StaticServer;++test.beforeAll(async () => {+ storybook = await serveStorybook(+ process.env.ASTRYX_STORYBOOK_DIR ?? DEFAULT_STORYBOOK_DIR,+ );+});++test.afterAll(async () => storybook?.close());++for (const form of ['anchor', 'button'] as const) {+ test(`a ${form} Link with maxLines={1} truncates inside a fixed-width line`, async ({+ page,+ }) => {+ await page.goto(+ `${storybook.origin}/iframe.html?id=core-link--truncated-in-constrained-line&viewMode=story`,+ );+ const line = page.locator(`[data-truncation-line="${form}"]`);+ await expect(line).toBeVisible();+ const measured = await line.evaluate(el => {+ const root = el.firstElementChild as HTMLElement;+ const label = root.firstElementChild as HTMLElement;+ const style = getComputedStyle(label);+ return {+ tag: root.tagName,+ line: el.getBoundingClientRect().width,+ root: root.getBoundingClientRect().width,+ labelClient: label.clientWidth,+ labelScroll: label.scrollWidth,+ textOverflow: style.textOverflow,+ };+ });+ expect(measured.tag, 'the Link renders its own element').toBe(+ form === 'anchor' ? 'A' : 'BUTTON',+ );+ // The root stops at the line instead of growing to its whole text.+ expect(measured.root).toBeLessThanOrEqual(measured.line + 0.5);+ // The label itself overflows and draws the ellipsis.+ expect(measured.labelScroll).toBeGreaterThan(measured.labelClient);+ expect(measured.textOverflow).toBe('ellipsis');+ });+}