JGuang

链接

Link · Astryx

交互预览
npm install @astryxdesign/core@0.7.1 @astryxdesign/theme-neutral@0.7.1 @stylexjs/stylex@0.19.0

链接 · 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,不把示例状态名称当成组件属性。

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 isStandalone when the link appears outside of inline text, so it receives proper base font sizing.
  • Only set label when the link content is not descriptive text (e.g. an icon-only link). For text links, the visible text is already the accessible name; adding label overrides 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 label on text links; aria-label prevents assistive technology from reading the actual link content.

主题 API

{
  "targets": [
    {
      "className": "astryx-link",
      "visualProps": [
        "color"
      ]
    }
  ]
}

带有变体、外部链接支持和多态渲染的样式化锚点链接。

属性类型默认值说明
asLinkComponentType用于替代 <a> 渲染的自定义组件
labelstring无障碍标签(aria-label)。仅在子内容不是描述性文本时使用(如纯图标链接)。文本链接请省略此项。
hrefstring链接目标 URL
hasUnderlinebooleanfalse始终显示下划线
isDisabledbooleanfalse禁用链接
isExternalLinkbooleanfalse在新标签页中打开,带外部图标和安全 rel 标记
newTabLabelstring'(opens in new tab)'向屏幕阅读器announce外部链接将在新标签页打开的文本。可覆盖以本地化。
targetstring链接文档的打开位置。target="_blank" 会自动添加 noopener noreferrer。
relstring链接关系标记。target="_blank" 会自动合并 noopener noreferrer。
downloadstring | booleanCauses the browser to download the linked URL. A string specifies the suggested filename.
referrerPolicyHTMLAttributeReferrerPolicyReferrer policy for the link.
onClickMouseEventHandler点击事件处理器
tooltipstring悬停时显示的工具提示文本
isStandalonebooleanfalse应用基础字体大小
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.
maxLinesnumber0Maximum lines before truncation.
children *ReactNode链接内容

为子树中所有 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.

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 *LinkComponentTypeLink 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 *ReactNodeContent to render with the link provider.