207 lines
6.9 KiB
Markdown
207 lines
6.9 KiB
Markdown
---
|
||
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` 中自行处理跳转。禁用时移除根元素的 `href`、`target`、`download`,包括 asChild 子元素自带的对应属性;`tabindex="-1"` 排除 Tab 顺序,仍可通过暴露的 `focus()` 程序化聚焦。重新启用后恢复属性,保留用户处理器和子元素 ref。自定义路由组件应把合并后的属性透传到真实根元素,不应自行覆盖禁用态的导航属性。
|
||
|
||
### 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)
|