链接 · Link
链接用于操作场景。下面的官方示例展示原生用法与状态,属性名保持上游 API。
安装与前置条件
npm install @astryxdesign/core@0.7.1 @astryxdesign/theme-neutral@0.7.1 @stylexjs/stylex@0.19.0
需要 React 19+,按顺序导入基础 CSS 和主题,并设置 Theme Provider。参见开始使用。
示例与使用方式
- 链接 · 综合展示 1:首先体验这个示例,再查看代码理解组件的组合方式。
- 链接 · 交互示例 2:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
- 链接 · 交互示例 3:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
- 链接 · 自定义 4:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
- 链接 · 交互示例 5:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
API 与上游详细参考
优先使用 0.7.1 官方中文说明;上游尚未提供中文的条目保留英文。属性名、类型及默认值保持原生 API,不把示例状态名称当成组件属性。
Link
A styled anchor for inline and standalone text navigation. Supports external links, underline variants, tooltips, and custom link components for router integration. Use it for navigating between pages or to external URLs.
使用建议
- Write descriptive, concise link text that clearly communicates the destination.
- Set
isStandalonewhen the link appears outside of inline text, so it receives proper base font sizing. - Only set
labelwhen the link content is not descriptive text (e.g. an icon-only link). For text links, the visible text is already the accessible name; addinglabeloverrides it for screen readers, which is harmful. - 避免:Use Link for actions that do not navigate; use a Button instead.
- 避免:Use generic text like "click here" or "read more"; describe the destination.
- 避免:Set
labelon text links;aria-labelprevents assistive technology from reading the actual link content.
主题 API
{
"targets": [
{
"className": "astryx-link",
"visualProps": [
"color"
]
}
]
}
Link
带有变体、外部链接支持和多态渲染的样式化锚点链接。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| as | LinkComponentType | 用于替代 <a> 渲染的自定义组件 | |
| label | string | 无障碍标签(aria-label)。仅在子内容不是描述性文本时使用(如纯图标链接)。文本链接请省略此项。 | |
| href | string | 链接目标 URL | |
| hasUnderline | boolean | false | 始终显示下划线 |
| isDisabled | boolean | false | 禁用链接 |
| isExternalLink | boolean | false | 在新标签页中打开,带外部图标和安全 rel 标记 |
| newTabLabel | string | '(opens in new tab)' | 向屏幕阅读器announce外部链接将在新标签页打开的文本。可覆盖以本地化。 |
| target | string | 链接文档的打开位置。target="_blank" 会自动添加 noopener noreferrer。 | |
| rel | string | 链接关系标记。target="_blank" 会自动合并 noopener noreferrer。 | |
| download | string | boolean | Causes the browser to download the linked URL. A string specifies the suggested filename. | |
| referrerPolicy | HTMLAttributeReferrerPolicy | Referrer policy for the link. | |
| onClick | MouseEventHandler | 点击事件处理器 | |
| tooltip | string | 悬停时显示的工具提示文本 | |
| isStandalone | boolean | false | 应用基础字体大小 |
| size | '4xs' | '3xs' | '2xs' | 'xsm' | 'sm' | 'base' | 'lg' | 'xl' | '2xl' | '3xl' | '4xl' | Explicit font size override forwarded to Text. | |
| weight | 'normal' | 'medium' | 'semibold' | 'bold' | Font weight override forwarded to Text. | |
| color | 'primary' | 'secondary' | 'disabled' | 'placeholder' | 'accent' | 'inherit' | 'accent' | Text color forwarded to Text. |
| display | 'inline' | 'block' | 'inline' | Display type for Text. |
| maxLines | number | 0 | Maximum lines before truncation. |
| children * | ReactNode | 链接内容 |
Link Provider
为子树中所有 Astryx 链接组件设置默认链接组件的 Provider。
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| component * | LinkComponentType | 用于所有链接元素的组件 | |
| children * | ReactNode | 子树 |
无障碍
- Link text: Link text must have at least 4.5:1 contrast with the background behind it. For Pointer down, measure against the pressed overlay the link paints behind its text. — 4.5:1
组件结构
- Label: The visible text of the link.
- Right icon: Icon placed after the label to indicate an action affordance.
- Left icon: Icon placed before the label to represent meaning.
Link Provider
Wraps your app to replace the default <a> tag with a framework-specific link component (e.g. Next.js Link) for client-side routing across all Astryx components.
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| component * | LinkComponentType | Link component to use for all link elements in the subtree (e.g. Next.js Link). It receives accepted href and to values under the shared navigation rule described on the Link href prop. Supported structured destinations, including their protocol, are checked without changing object identity. If either supplied destination is rejected, Astryx renders inert content without invoking this component; it does not pass undefined or fall back to the other destination. | |
| children * | ReactNode | Content to render with the link provider. |