Files
heroui-vue/apps/docs/components/link.md
T
maoyingjie 145a208687 docs: close documentation facts, structure and related-component mapping
HD-04 scope, against heroui ce90ea3 cn sources:

- stale/fictional content: input.md no longer claims TextField is
  unmigrated; color-field.md no longer claims ColorPicker is private;
  header.md drops the fictional Header.Root subcomponent and the stale
  'menus not migrated' wording (MenuSection label registration is now
  documented from HeaderRoot source); radio-group.md drops three copied
  CheckboxGroup wordings (variant override is Radio's, scalar emits,
  native single-value submit); input-otp.md drops the fictional
  data-complete state and lists the real missing upstream props
- toggle-button disabled row now matches the real CSS hooks
  (:disabled / [aria-disabled], with data-disabled as a marker only)
- part API / CSS classes / interactive states restored per pinned
  sources: button (full class lists + real variable-based global CSS),
  accordion (per-part tables + variant/state lists), card (per-part
  tables + variant list), switch (SwitchGroup snippet + Switch/
  SwitchGroup class lists + states), color-field Group (class list +
  states), input-otp (variant notes, global CSS, class/state lists);
  input-group gains the two upstream usage chapters
- RelatedComponents restored from the pinned componentRelationships
  (first-3, filtered against componentsMap): 64 pages rewritten,
  list-box/slider sections added, 7 empty-mapping pages' fabricated
  entries removed; RelatedShowcases left unmigrated with its real data
  inventory recorded (2 cases, showcase-site cards)
- defaults unified on implementation evidence: granularity (inferred
  day/minute) and visibleYears (full min-max span, no "or 20" fallback);
  HD-03 hand-offs resolved (trigger focus-visible noted for HD-06,
  width guards kept)
- new demos: header-collection-grouping (MenuSection/ListBoxSection
  label wiring), empty-state-collection (collection empty slot);
  empty-state page deduplicated with a boundary compatibility note
- ComponentPreview minHeight/isBgSolid restored per the 30 upstream
  marks; tabs-overflow max-w-full kept after measuring internal scroll
- tests: +3 header registration/demo cases, +1 empty-state collection
  case; full suite 110 files / 1630 tests, typecheck and build:docs
  pass
2026-10-28 22:32:10 +08:00

207 lines
6.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: Link 链接
description: 用于导航的样式化锚点组件,内置图标支持
component: link
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import { Link, LinkIcon } from "@heroui-vue/components";
</script>
```
<ComponentPreview name="link-basic" />
## 组件结构
导入 Link 与 LinkIcon 后,可按名称组合子部分。省略 LinkIcon 插槽内容时使用内置外部链接箭头图标。
```vue
<template>
<Link href="#">
Call to action
<LinkIcon />
</Link>
</template>
```
## 示例
### 图标位置
<ComponentPreview name="link-icon-placement" />
### 配合 Tailwind CSS 的文本装饰
Link 默认在悬浮时显示下划线。可使用 Tailwind CSS 的 text-decoration 工具类让下划线始终可见、完全移除,或自定义其颜色、样式、粗细与偏移。
<ComponentPreview name="link-underline-and-offset" />
**文本装饰线:**
- `underline` — 始终显示下划线
- `no-underline` — 移除下划线
- 默认 `Link` 样式 — 下划线在悬浮时显示
**文本装饰色:**
- `decoration-accent`、`decoration-muted` 等 — 使用主题色设置下划线颜色
- `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` 等 — 调整文本与下划线间距
可用的 BEM 类:
- 基础:`hv-link`
- 图标:`hv-link__icon`
### 自定义图标
<ComponentPreview name="link-custom-icon" />
### 渲染函数
Vue 使用 `asChild` 将链接属性合并到默认插槽中唯一的自定义元素,对应上游 render 函数示例,不将 render prop 原样搬入 Vue。自定义元素会获得链接角色与键盘激活(Enter 触发一次 `@click` 语义,可用 keydown `preventDefault` 取消);真实导航请使用 `a` 或路由组件。
<ComponentPreview name="link-render-function" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="link-custom-styles" />
### 全局 CSS
若要自定义组件类,可使用 `@layer components` 指令。
```css
@layer components {
.hv-link {
@apply font-semibold no-underline hover:underline;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
### CSS 类
Link 使用以下 CSS 类:
#### 基础类 [!toc]
- `.hv-link` — 链接基础样式
- `.hv-link__icon` — 链接图标样式
### 交互状态
组件同时支持 CSS 伪类与 data 属性,以获得更大灵活性:
- **焦点**:`:focus-visible` 或 `[data-focus-visible="true"]`
- **悬浮**:`:hover` 或 `[data-hovered="true"]`
- **按下**:`:active` 或 `[data-pressed="true"]`
- **禁用**:`:disabled` 或 `[aria-disabled="true"]`
## API 参考
### Link
| Prop | 类型 | 默认值 | 描述 |
| ------------ | ------------------- | ------- | ---------------------------------------------------------------------- |
| `href` | `string` | - | 锚点的目标 URL |
| `target` | `string` | — | 在何处打开链接文档;省略时使用浏览器同窗口默认行为,不输出 target 属性 |
| `rel` | `string` | - | 当前文档与链接文档的关系 |
| `download` | `boolean \| string` | - | 触发下载而非导航;`true` 渲染 `download`,`false`/未传省略属性 |
| `isDisabled` | `boolean` | `false` | 禁用指针与键盘交互;保留可聚焦与 `aria-disabled` |
| `asChild` | `boolean` | `false` | 合并到默认插槽唯一元素,并补充 `role="link"` 与 `tabindex="0"` |
| `autoFocus` | `boolean` | - | 挂载后聚焦真实 DOM 根元素(含 asChild) |
| `class` | `string` | - | 与默认样式合并的自定义类 |
默认插槽渲染链接内部内容。作用域插槽提供 `isDisabled`、`isHovered`、`isPressed`、`isFocused`、`isFocusVisible`。`@click` 等 DOM 事件通过 attrs 透传;`isDisabled` 时阻止激活(含 Vue 合并后的监听器数组)。React Aria 的 `onPress` / `PressEvent` 由原生 `click` 适配,不等价。
`asChild` 适用于 `<a>`、`RouterLink` 等已具备导航能力的元素,以及需要自定义外观的 `span`/`div`:非原生可激活元素会获得 `role="link"`、`tabindex="0"`,Enter 触发一次激活(与点击同一 `click` 路径)。自定义元素上的 `href` 不会由浏览器直接导航,请用 `a` / 路由组件作为 asChild 目标,或在 `@click` 中自行处理跳转。禁用时仍可聚焦以便读出 `aria-disabled`,但不会激活。
### LinkIcon
| Prop | 类型 | 描述 |
| ------- | -------- | ------------- |
| `class` | `string` | 附加的 CSS 类 |
默认插槽提供自定义图标元素;省略时使用内置箭头图标,并标记 `data-default-icon`。
### 与路由库配合使用
使用变体函数为框架专用链接添加样式:
```vue
<script setup lang="ts">
import { RouterLink } from "vue-router";
import { linkVariants } from "@heroui-vue/styles";
const slots = linkVariants();
</script>
<template>
<RouterLink :class="slots.base()" to="/about">
About Page
<span :class="slots.icon()" data-slot="link-icon" />
</RouterLink>
</template>
```
基础包不强依赖 Vue Router;可在应用层组合 `linkVariants` 或 `.hv-link` 类。
### 直接应用类
由于 HeroUI 使用 [BEM](https://getbem.com/) 类,你可以将 Link 样式直接应用到任意链接元素:
```vue
<script setup lang="ts">
import { RouterLink } from "vue-router";
</script>
<template>
<!-- 直接使用 Tailwind 工具类 -->
<RouterLink class="hv-link underline-offset-2" to="/about">
About Page
</RouterLink>
</template>
```
```vue
<template>
<!-- 或使用原生 <a> -->
<a
href="/about"
class="hv-link underline decoration-accent underline-offset-4"
>
About Page
<span class="hv-link__icon">
<!-- 图标内容 -->
</span>
</a>
</template>
```
## 相关组件
[Breadcrumbs 面包屑](./breadcrumbs)