apps/docs/content/docs/cn/react/components/(navigation)/link.mdx
import { Link } from '@heroui/react';
<ComponentPreview name="link-basic" />
导入 Link 组件后,可通过点语法访问所有子部分。
import { Link } from '@heroui/react';
export default () => (
<Link href="#">
Call to action
<Link.Icon />
</Link>
);
<ComponentPreview name="link-custom-icon" />
<ComponentPreview name="link-icon-placement" />
Link 默认在悬浮时显示下划线。可使用 Tailwind CSS 的 text-decoration 工具类让下划线始终可见、完全移除,或自定义其颜色、样式、粗细与偏移。
<ComponentPreview name="link-underline-and-offset" />
文本装饰线:
underline — 始终显示下划线no-underline — 移除下划线Link 样式 — 下划线在悬浮时显示文本装饰色:
decoration-primary、decoration-secondary 等 — 使用主题色设置下划线颜色decoration-muted/50 — 使用透明度修饰符实现半透明下划线文本装饰样式:
decoration-solid — 实线(默认)decoration-double — 双线decoration-dotted — 点线decoration-dashed — 虚线decoration-wavy — 波浪线文本装饰粗细:
decoration-1、decoration-2、decoration-4 等 — 控制下划线粗细下划线偏移:
underline-offset-1、underline-offset-2、underline-offset-4 等 — 调整文本与下划线间距更多说明见 Tailwind CSS 文档:
可用的 BEM 类:
linklink__icon<ComponentPreview name="link-custom-render-function" />
import { Link } from '@heroui/react';
function CustomLink() {
return (
<Link
href="#"
className="text-lg font-bold text-accent hover:text-accent/80"
>
Custom styled link
</Link>
);
}
要自定义 Link 的组件类,可使用 @layer components 指令。
了解更多。
@layer components {
.link {
@apply font-semibold;
}
}
HeroUI 遵循 BEM 方法论,确保组件变体与状态可复用且易于自定义。
Link 使用以下 CSS 类(查看源码样式):
.link — 链接基础样式.link__icon — 链接图标样式组件同时支持 CSS 伪类与 data 属性,以获得更大灵活性:
:focus-visible 或 [data-focus-visible="true"]:hover 或 [data-hovered="true"]:active 或 [data-pressed="true"]:disabled 或 [aria-disabled="true"]| Prop | 类型 | 默认值 | 描述 |
|---|---|---|---|
href | string | - | 锚点的目标 URL |
target | string | "_self" | 在何处打开链接文档 |
rel | string | - | 当前文档与链接文档的关系 |
download | boolean | string | - | 触发下载而非导航 |
isDisabled | boolean | false | 禁用指针与键盘交互 |
className | string | - | 与默认样式合并的自定义类 |
children | React.ReactNode | - | 渲染在链接内部的内容 |
onPress | (e: PressEvent) => void | - | 链接被激活时触发 |
autoFocus | boolean | - | 元素挂载时是否应获得焦点 |
render | DOMRenderFunction<keyof React.JSX.IntrinsicElements, LinkRenderProps> | - | 使用自定义渲染函数覆盖默认 DOM 元素。 |
| Prop | 类型 | 默认值 | 描述 |
|---|---|---|---|
children | React.ReactNode | - | 自定义图标元素;省略时使用内置箭头图标 |
className | string | - | 附加的 CSS 类 |
使用变体函数为框架专用链接(例如 Next.js)添加样式:
import { Link } from '@heroui/react';
import { linkVariants } from '@heroui/styles';
import NextLink from 'next/link';
export default function Demo() {
const slots = linkVariants();
return (
<NextLink className={slots.base()} href="/about">
About Page
<Link.Icon className={slots.icon()} />
</NextLink>
);
}
由于 HeroUI 使用 BEM 类,你可以将 Link 样式直接应用到任意链接元素:
import NextLink from 'next/link';
// 直接使用 Tailwind 工具类
export default function Demo() {
return (
<NextLink href="/about" className="link underline-offset-2">
About Page
</NextLink>
);
}
// 或使用原生 <a>
export default function NativeLink() {
return (
<a href="/about" className="link underline decoration-primary underline-offset-4">
About Page
<Link.Icon className="link__icon" />
</a>
);
}