Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(docsite): unify underline + hover treatment for inline prose links (#4929)

Matt Andrycf0c3ea564d89
diff --git a/apps/docsite/src/__tests__/prose-link.test.ts b/apps/docsite/src/__tests__/prose-link.test.ts
new file mode 100644
index 0000000000..ce49cb2356
--- /dev/null
+++ b/apps/docsite/src/__tests__/prose-link.test.ts
@@ -0,0 +1,146 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file Unit tests for the shared prose-link treatment.
+ *
+ * Every inline link in docs prose — authored links, linkified code chips,
+ * prop-description links — must carry the styles from proseLink.ts. These
+ * tests pin the wiring by inspecting the element trees the renderers return;
+ * the components involved are hook-free, so no DOM is needed.
+ *
+ * StyleX is mocked because this suite runs without the StyleX compiler:
+ * create() passes raw style objects through and props() records which ones it
+ * was handed. That keeps the assertions about *which* partials reach *which*
+ * elements exact, which is the contract worth pinning here — the CSS itself
+ * is the compiler's job.
+ *
+ * Run: pnpm -F @astryxdesign/docsite test
+ */
+
+import {describe, it, expect, vi} from 'vitest';
+import {isValidElement, type ReactElement, type ReactNode} from 'react';
+import * as stylex from '@stylexjs/stylex';
+
+vi.mock('@stylexjs/stylex', async importOriginal => {
+ const actual = await importOriginal<Record<string, unknown>>();
+ return {
+ ...actual,
+ create: <T>(styles: T) => styles,
+ props: (...styleList: unknown[]) => ({
+ className: JSON.stringify(styleList.filter(Boolean)),
+ }),
+ };
+});
+
+import {Link} from '@astryxdesign/core/Link';
+import {Markdown} from '@astryxdesign/core/Markdown';
+import {proseLinkStyles} from '../components/proseLink';
+import {InlineCode} from '../components/InlineCode';
+import {MarkdownText} from '../components/MarkdownText';
+import {renderInlineMarkdown} from '../components/docs/inlineMarkdown';
+
+/**
+ * Depth-first search for the first matching element. Walks props.children
+ * without invoking function components, so only elements the renderer under
+ * test returned directly are reachable.
+ */
+function findElement(
+ node: ReactNode,
+ match: (el: ReactElement) => boolean,
+): ReactElement | null {
+ if (Array.isArray(node)) {
+ for (const child of node) {
+ const found = findElement(child, match);
+ if (found != null) {
+ return found;
+ }
+ }
+ return null;
+ }
+ if (!isValidElement(node)) {
+ return null;
+ }
+ if (match(node)) {
+ return node;
+ }
+ const {children} = node.props as {children?: ReactNode};
+ return children == null ? null : findElement(children, match);
+}
+
+describe('linkified code chips', () => {
+ it('wrap a resolvable span in Link with the shared treatment', () => {
+ const el = InlineCode({children: 'astryx docs tokens'});
+ expect(el.type).toBe(Link);
+
+ const props = el.props as React.ComponentProps<typeof Link>;
+ expect(props.href).toBe('/docs/tokens');
+ expect(props.hasUnderline).toBe(true);
+ expect(props.xstyle).toContain(proseLinkStyles.underline);
+ expect(props.xstyle).toContain(proseLinkStyles.color);
+ expect(props.xstyle).toContain(proseLinkStyles.chipOffset);
+ });
+
+ it('leave an unrecognized span as a plain chip', () => {
+ const el = InlineCode({children: 'not a command'});
+ expect(el.type).not.toBe(Link);
+ expect((el.props as {children: string}).children).toBe('not a command');
+ });
+});
+
+describe('authored docs links', () => {
+ it('render anchors with the shared treatment', () => {
+ const anchor = findElement(
+ renderInlineMarkdown('see [the guide](/docs/styling)'),
+ el => el.type === 'a',
+ );
+ expect(anchor).not.toBeNull();
+ expect(anchor!.props).toMatchObject({
+ href: '/docs/styling',
+ ...stylex.props(
+ proseLinkStyles.underline,
+ proseLinkStyles.color,
+ proseLinkStyles.focusRing,
+ ),
+ });
+ });
+
+ it('give chip-labeled links the chip underline offset', () => {
+ const anchor = findElement(
+ renderInlineMarkdown('use [`Table`](/components/Table)'),
+ el => el.type === 'a',
+ );
+ expect(anchor).not.toBeNull();
+ expect(anchor!.props).toMatchObject(
+ stylex.props(
+ proseLinkStyles.underline,
+ proseLinkStyles.color,
+ proseLinkStyles.focusRing,
+ proseLinkStyles.chipOffset,
+ ),
+ );
+ });
+});
+
+describe('prop-description links', () => {
+ it('route through Link with the shared treatment', () => {
+ const markdown = findElement(
+ MarkdownText({children: 'see [theming](/docs/theme)'}),
+ el => el.type === Markdown,
+ );
+ expect(markdown).not.toBeNull();
+
+ const {components} = markdown!.props as {
+ components: {
+ link: (props: {href: string; children: ReactNode}) => ReactElement;
+ };
+ };
+ const linkEl = components.link({href: '/docs/theme', children: 'theming'});
+ expect(linkEl.type).toBe(Link);
+
+ const props = linkEl.props as React.ComponentProps<typeof Link>;
+ expect(props.href).toBe('/docs/theme');
+ expect(props.hasUnderline).toBe(true);
+ expect(props.xstyle).toContain(proseLinkStyles.underline);
+ expect(props.xstyle).toContain(proseLinkStyles.color);
+ });
+});
diff --git a/apps/docsite/src/__tests__/type-definitions.test.ts b/apps/docsite/src/__tests__/type-definitions.test.ts
index 97c081e105..b11a624baa 100644
--- a/apps/docsite/src/__tests__/type-definitions.test.ts
+++ b/apps/docsite/src/__tests__/type-definitions.test.ts
@@ -195,7 +195,7 @@ describe('type definition trigger', () => {
it('renders the type name itself as the definition trigger', () => {
expect(source).toContain('splitTypeRefSegments');
- expect(source).toMatch(/<button type="button"[^>]*>\s*\{def\.name\}/);
+ expect(source).toMatch(/<button\s+type="button"[^>]*>\s*\{def\.name\}/);
});
it('no longer renders a separate "View {Type}" button', () => {
diff --git a/apps/docsite/src/components/InlineCode.tsx b/apps/docsite/src/components/InlineCode.tsx
index 60957c39e7..1cbb3cc35d 100644
--- a/apps/docsite/src/components/InlineCode.tsx
+++ b/apps/docsite/src/components/InlineCode.tsx
@@ -5,8 +5,8 @@
/**
* @file InlineCode.tsx
* @input The text content of an inline code span.
- * @output A `Code` chip, or a link when a linkifier recognizes the span as a
- * reference to a page this site serves.
+ * @output A `Code` chip, or a link-wrapped chip when a linkifier recognizes
+ * the span as a reference to a page this site serves.
* @position Renders every inline code span on the docs site — /docs pages go
* through inlineMarkdown.tsx, component pages through MarkdownText.tsx's
* `inlineCode` override.
@@ -15,16 +15,11 @@
* snippet — render as an ordinary code chip.
*/
-import * as stylex from '@stylexjs/stylex';
+import type {StyleXStyles} from '@stylexjs/stylex';
import {Code} from '@astryxdesign/core/CodeBlock';
import {Link} from '@astryxdesign/core/Link';
import {linkifyCode} from './codeLinkifiers';
-
-const styles = stylex.create({
- // Only ever seen on hover, where Link draws its underline. Nudged clear of
- // the descenders in commands like `astryx docs typography`.
- link: {textUnderlineOffset: '0.2em'},
-});
+import {proseLinkStyles} from './proseLink';
export function InlineCode({children}: {children: string}) {
const href = linkifyCode(children);
@@ -32,13 +27,25 @@ export function InlineCode({children}: {children: string}) {
return <Code>{children}</Code>;
}
- // The chip is unchanged at rest — same background, padding, and colour as
- // any other inline code. Link contributes the hit target and the pointer
- // cursor, and reveals an underline on hover. `type="inherit"` keeps the
- // surrounding text's metrics; Code keeps its own colour, so wrapping it
- // changes nothing visually.
+ // Linked chips carry the standard prose-link treatment (proseLink.ts): an
+ // accent underline at rest, matching the chip-labeled links renderLink
+ // already draws for bare component names. The anchor's color paints the
+ // underline while Code keeps its own text color, so the chip itself is
+ // unchanged. `type="inherit"` keeps the surrounding text's metrics.
return (
- <Link href={href} type="inherit" xstyle={styles.link}>
+ <Link
+ href={href}
+ type="inherit"
+ hasUnderline
+ xstyle={
+ // Cast because the partials guard their hover states behind
+ // @media (hover: hover), a nesting StyleXStyles cannot express.
+ [
+ proseLinkStyles.underline,
+ proseLinkStyles.color,
+ proseLinkStyles.chipOffset,
+ ] as unknown as StyleXStyles
+ }>
<Code>{children}</Code>
</Link>
);
diff --git a/apps/docsite/src/components/MarkdownText.tsx b/apps/docsite/src/components/MarkdownText.tsx
index 0f463c5737..3a46d2aef3 100644
--- a/apps/docsite/src/components/MarkdownText.tsx
+++ b/apps/docsite/src/components/MarkdownText.tsx
@@ -2,19 +2,61 @@
'use client';
-import type {ComponentProps} from 'react';
+/**
+ * @file MarkdownText.tsx
+ * @input Markdown-authored prose from component docs (prop descriptions,
+ * dos and don'ts, theming notes).
+ * @output Text-wrapped paragraphs whose code spans run the site's linkifiers
+ * and whose links carry the shared prose-link treatment from proseLink.ts.
+ * @position Prose renderer for component detail pages, over core Markdown.
+ */
+
+import type {ComponentProps, ReactNode} from 'react';
+import type {StyleXStyles} from '@stylexjs/stylex';
import {Markdown} from '@astryxdesign/core/Markdown';
import type {MarkdownComponents} from '@astryxdesign/core/Markdown';
import {VStack} from '@astryxdesign/core/Layout';
+import {Link} from '@astryxdesign/core/Link';
import {Text} from '@astryxdesign/core/Text';
import {InlineCode} from './InlineCode';
+import {proseLinkStyles} from './proseLink';
type TextProps = ComponentProps<typeof Text>;
+// Authored [label](href) links, restyled from core Markdown's plain underline
+// to the docsite's prose-link treatment (proseLink.ts). External links open in
+// a new tab like renderLink's do; internal ones keep router navigation through
+// core Link's useLinkComponent.
+function MarkdownLink({href, children}: {href: string; children: ReactNode}) {
+ const isExternal = /^https?:\/\//.test(href);
+ return (
+ <Link
+ href={href}
+ type="inherit"
+ hasUnderline
+ target={isExternal ? '_blank' : undefined}
+ rel={isExternal ? 'noreferrer' : undefined}
+ xstyle={
+ // Cast because the partials guard their hover states behind
+ // @media (hover: hover), a nesting StyleXStyles cannot express.
+ [
+ proseLinkStyles.underline,
+ proseLinkStyles.color,
+ ] as unknown as StyleXStyles
+ }>
+ {children}
+ </Link>
+ );
+}
+
// Component-page prose (prop descriptions, dos and don'ts, theming notes)
// cites doc topics the same way /docs prose does, e.g. "Icon name — see
-// `astryx docs icons`". Route those spans through the same linkifiers.
-const MARKDOWN_COMPONENTS: MarkdownComponents = {inlineCode: InlineCode};
+// `astryx docs icons`". Route those spans through the same linkifiers, and
+// links through the same treatment.
+const MARKDOWN_COMPONENTS: MarkdownComponents = {
+ inlineCode: InlineCode,
+ link: MarkdownLink,
+};
interface MarkdownTextProps {
children: string;
diff --git a/apps/docsite/src/components/component-detail/TypeRefText.tsx b/apps/docsite/src/components/component-detail/TypeRefText.tsx
index 52a614a3dc..3e6072ee4f 100644
--- a/apps/docsite/src/components/component-detail/TypeRefText.tsx
+++ b/apps/docsite/src/components/component-detail/TypeRefText.tsx
@@ -17,11 +17,13 @@ import {Popover} from '@astryxdesign/core/Popover';
import {splitTypeRefSegments} from './parsePropType';
import type {TypeDefinition} from '../../generated/componentRegistry';
import {CodeExampleBlock} from '../CodeExampleBlock';
+import {proseLinkStyles} from '../proseLink';
const styles = stylex.create({
- // Inline link treatment for type-name definition triggers, mirroring the
- // authored-docs link pattern (docs/inlineMarkdown.tsx) on a button element
- // so the trigger stays keyboard-focusable with a visible focus ring.
+ // Strips the native button chrome from type-name definition triggers; the
+ // link treatment itself comes from proseLink.ts, shared with the rest of
+ // the docs prose. A button rather than an anchor so the trigger stays
+ // keyboard-focusable without pretending to navigate.
typeRefTrigger: {
backgroundColor: 'transparent',
borderStyle: 'none',
@@ -33,22 +35,6 @@ const styles = stylex.create({
fontSize: 'inherit',
lineHeight: 'inherit',
cursor: 'pointer',
- color: 'var(--color-text-accent)',
- textDecorationLine: 'underline',
- textDecorationThickness: '1px',
- textUnderlineOffset: '0.16em',
- transition: 'color 120ms ease, text-decoration-color 120ms ease',
- ':hover': {
- '@media (hover: hover)': {
- color: 'var(--color-accent)',
- textDecorationThickness: '2px',
- },
- },
- ':focus-visible': {
- borderRadius: 'var(--radius-sm)',
- outline: '2px solid var(--color-accent)',
- outlineOffset: 2,
- },
},
});
@@ -80,7 +66,14 @@ function TypeDefinitionTrigger({def}: {def: TypeDefinition}) {
</Card>
</VStack>
}>
- <button type="button" {...stylex.props(styles.typeRefTrigger)}>
+ <button
+ type="button"
+ {...stylex.props(
+ styles.typeRefTrigger,
+ proseLinkStyles.underline,
+ proseLinkStyles.color,
+ proseLinkStyles.focusRing,
+ )}>
{def.name}
</button>
</Popover>
diff --git a/apps/docsite/src/components/docs/inlineMarkdown.tsx b/apps/docsite/src/components/docs/inlineMarkdown.tsx
index 8f1f10f75d..8cb311882a 100644
--- a/apps/docsite/src/components/docs/inlineMarkdown.tsx
+++ b/apps/docsite/src/components/docs/inlineMarkdown.tsx
@@ -2,35 +2,23 @@
'use client';
+/**
+ * @file inlineMarkdown.tsx
+ * @input Authored docs prose containing the inline markdown subset.
+ * @output React nodes: plain text, code chips via InlineCode, and links
+ * carrying the shared prose-link treatment from proseLink.ts.
+ * @position Inline renderer for /docs page prose, lists, and table cells.
+ */
+
import {Fragment, type ReactNode} from 'react';
import * as stylex from '@stylexjs/stylex';
import {Code} from '@astryxdesign/core/CodeBlock';
import {InlineCode} from '../InlineCode';
+import {proseLinkStyles} from '../proseLink';
import {tokenizeInline, type InlineToken} from './inlineTokens';
type LinkToken = Extract<InlineToken, {type: 'link'}>;
-const styles = stylex.create({
- link: {
- color: 'var(--color-text-accent)',
- textDecorationLine: 'underline',
- textDecorationThickness: '1px',
- textUnderlineOffset: '0.16em',
- transition: 'color 120ms ease, text-decoration-color 120ms ease',
- ':hover': {
- '@media (hover: hover)': {
- color: 'var(--color-accent)',
- textDecorationThickness: '2px',
- },
- },
- ':focus-visible': {
- borderRadius: 'var(--radius-sm)',
- outline: '2px solid var(--color-accent)',
- outlineOffset: 2,
- },
- },
-});
-
function renderLink(token: LinkToken): ReactNode {
const isExternal = /^https?:\/\//.test(token.href);
return (
@@ -38,7 +26,12 @@ function renderLink(token: LinkToken): ReactNode {
href={token.href}
rel={isExternal ? 'noreferrer' : undefined}
target={isExternal ? '_blank' : undefined}
- {...stylex.props(styles.link)}>
+ {...stylex.props(
+ proseLinkStyles.underline,
+ proseLinkStyles.color,
+ proseLinkStyles.focusRing,
+ token.isCodeLabel && proseLinkStyles.chipOffset,
+ )}>
{token.isCodeLabel ? (
// color="inherit" keeps the link's accent color on the code span;
// Code's default `primary` would read as body text inside the anchor.
diff --git a/apps/docsite/src/components/proseLink.ts b/apps/docsite/src/components/proseLink.ts
new file mode 100644
index 0000000000..cb9c36d40a
--- /dev/null
+++ b/apps/docsite/src/components/proseLink.ts
@@ -0,0 +1,54 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file proseLink.ts
+ * @input none
+ * @output Shared StyleX partials for the docsite's inline prose-link treatment.
+ * @position Single source of truth for how an inline link looks in docs prose.
+ * Consumed by docs/inlineMarkdown.tsx, InlineCode.tsx, MarkdownText.tsx, and
+ * component-detail/TypeRefText.tsx. Change link styling here, not at the
+ * call sites.
+ *
+ * The treatment: an always-visible 1px accent underline that thickens to 2px
+ * on hover, with the color stepping from --color-text-accent to --color-accent.
+ * The underline is the non-color affordance WCAG 1.4.1 asks for, so it is not
+ * optional at rest. Split into partials because the consumers differ: raw
+ * anchors and buttons take all three, while core Link brings its own focus
+ * ring and only layers the decoration and color on top via xstyle.
+ */
+
+import * as stylex from '@stylexjs/stylex';
+import {colorVars, radiusVars} from '@astryxdesign/core/theme/tokens.stylex';
+
+export const proseLinkStyles = stylex.create({
+ // Decoration only — safe to layer onto core Link via xstyle.
+ underline: {
+ textDecorationLine: 'underline',
+ textDecorationThickness: {
+ default: '1px',
+ ':hover': {'@media (hover: hover)': '2px'},
+ },
+ textUnderlineOffset: '0.16em',
+ transition: 'color 120ms ease, text-decoration-color 120ms ease',
+ },
+ // Rest and hover color ramp. On core Link this replaces its color-mix hover
+ // tint, so chips and text links shift through the same colors.
+ color: {
+ color: {
+ default: colorVars['--color-text-accent'],
+ ':hover': {'@media (hover: hover)': colorVars['--color-accent']},
+ },
+ },
+ // For raw <a>/<button> only. Core Link draws its own ring through
+ // focusOutlineProps — never apply this partial to it.
+ focusRing: {
+ ':focus-visible': {
+ borderRadius: radiusVars['--radius-inner'],
+ outline: `2px solid ${colorVars['--color-accent']}`,
+ outlineOffset: 2,
+ },
+ },
+ // Chip labels only: nudges the underline clear of descenders in commands
+ // like `astryx docs typography`, which sit lower inside a padded chip.
+ chipOffset: {textUnderlineOffset: '0.2em'},
+});