feat: add Surface, Link and Spinner components

This commit is contained in:
maoyingjie
2026-10-21 13:04:06 +08:00
parent 9d40dc0ecb
commit 630963c652
52 changed files with 2683 additions and 162 deletions
+206
View File
@@ -0,0 +1,206 @@
---
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` | `"_self"` | 在何处打开链接文档 |
| `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>
```
## 相关组件
[Button 按钮](./button) · [Card 卡片](./card)
+94
View File
@@ -0,0 +1,94 @@
---
title: Spinner 加载指示器
description: 用于展示等待或处理中状态的加载指示组件
component: spinner
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import { Spinner } from "@heroui-vue/components";
</script>
```
<ComponentPreview name="spinner-basic" />
## 示例
### 颜色
<ComponentPreview name="spinner-colors" />
### 尺寸
<ComponentPreview name="spinner-sizes" />
### 速度
为单个 Spinner 添加动画工具类即可调整其旋转速度。请同时加上 `motion-reduce:animate-none`,以便在用户开启“减少动态效果”时保持静止。
<ComponentPreview name="spinner-speed" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="spinner-custom-styles" />
### 全局 CSS
若要自定义组件类,可使用 `@layer components` 指令。
```css
@layer components {
.hv-spinner {
@apply animate-spin;
}
.hv-spinner--accent {
color: var(--accent);
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
### CSS 类
Spinner 使用以下 CSS 类:
#### 基础类与尺寸类 [!toc]
- `.hv-spinner` - 基础样式与默认尺寸
- `.hv-spinner--sm` - 小尺寸变体
- `.hv-spinner--md` - 中等尺寸变体(默认)
- `.hv-spinner--lg` - 大尺寸变体
- `.hv-spinner--xl` - 特大尺寸变体
#### 颜色类 [!toc]
- `.hv-spinner--current` - 继承当前文本颜色
- `.hv-spinner--accent` - 强调色变体
- `.hv-spinner--danger` - 危险色变体
- `.hv-spinner--success` - 成功色变体
- `.hv-spinner--warning` - 警告色变体
## API 参考
### Spinner
| Prop | 类型 | 默认值 | 描述 |
| ------- | ------------------------------------------------------------- | ---------- | ------------------ |
| `size` | `"sm" \| "md" \| "lg" \| "xl"` | `"md"` | Spinner 的尺寸 |
| `color` | `"current" \| "accent" \| "success" \| "warning" \| "danger"` | `"accent"` | Spinner 的颜色变体 |
| `class` | `string` | - | 额外的 CSS 类名 |
根节点默认 `role="status"` 与 `aria-label="Loading"`,可通过 attrs 覆盖。内部 SVG 标记 `aria-hidden`,使用 `currentColor` 渐变;嵌入 Button 时保持所选尺寸(如 `size="sm"` 为 16px),不被 Button 的普通 SVG 规则覆盖。
## 相关组件
[Button 按钮](./button) · [Input 输入框](./input)
+107
View File
@@ -0,0 +1,107 @@
---
title: Surface 表面
description: 提供表面级样式与子组件上下文的容器组件
component: surface
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import { Surface } from "@heroui-vue/components";
</script>
```
<ComponentPreview name="surface-basic" />
## 示例
### 变体
Surface 提供描述 prominence 层级的语义变体:
- **`default`** - 标准表面外观(bg-surface)
- **`secondary`** - 中等 prominence(bg-surface-secondary)
- **`tertiary`** - 较高 prominence(bg-surface-tertiary)
- **`transparent`** - 透明背景,适用于遮罩层和自定义背景
<ComponentPreview name="surface-variants" />
### 搭配表单
在 Surface 内使用表单组件时,请使用 `variant="secondary"` 以应用适合表面背景的低强调变体。
<ComponentPreview name="surface-with-form-components" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="surface-custom-styles" />
### 全局 CSS
若要自定义组件类,可使用 `@layer components` 指令。
```css
@layer components {
.hv-surface {
@apply rounded-2xl border border-border;
}
.hv-surface--secondary {
@apply bg-gradient-to-br from-blue-50 to-purple-50;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
### CSS 类
Surface 组件使用以下 CSS 类:
#### 基础类 [!toc]
- `.hv-surface` - Surface 根容器
#### 变体类 [!toc]
- `.hv-surface--default` - 默认 Surface 变体(bg-surface)
- `.hv-surface--secondary` - Secondary Surface 变体(bg-surface-secondary)
- `.hv-surface--tertiary` - Tertiary Surface 变体(bg-surface-tertiary)
- `.hv-surface--transparent` - 透明 Surface 变体
## API 参考
### Surface
| Prop | 类型 | 默认值 | 描述 |
| --------- | --------------------------------------------------------- | ----------- | -------------------- |
| `variant` | `"transparent" \| "default" \| "secondary" \| "tertiary"` | `"default"` | Surface 的视觉变体。 |
| `class` | `string` | - | 额外的 CSS 类。 |
默认插槽渲染 Surface 内容。其他属性与 DOM 事件透传到根 `div`。
## Context API
### surfaceContextKey
子组件可通过 Surface 上下文读取当前变体:
```vue
<script setup lang="ts">
import { inject } from "vue";
import { surfaceContextKey } from "@heroui-vue/components";
const surface = inject(surfaceContextKey, undefined);
// surface.variant.value 为 "transparent" | "default" | "secondary" | "tertiary"
</script>
```
## 相关组件
[Card 卡片](./card) · [Input 输入框](./input) · [Button 按钮](./button)