feat: initialize Vue component library and aligned documentation

This commit is contained in:
maoyingjie
2026-10-21 09:06:28 +08:00
commit 9d40dc0ecb
149 changed files with 13208 additions and 0 deletions
+142
View File
@@ -0,0 +1,142 @@
---
title: Button 按钮
description: 可点击的按钮组件,支持多种变体与状态
component: button
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import { Button } from "@heroui-vue/components";
</script>
```
<ComponentPreview name="button-basic" />
## 示例
### 变体
<ComponentPreview name="button-variants" />
### 尺寸
<ComponentPreview name="button-sizes" />
### 带图标
<ComponentPreview name="button-with-icons" />
### 仅图标
<ComponentPreview name="button-icon-only" />
### 加载中
此例固定展示上传中的外观;加载期间阻止重复提交,但保留键盘焦点。要体验开始、完成和再次上传,请使用下一例。
<ComponentPreview name="button-loading" />
### 加载中(点击触发)
点击“上传文件”后进入加载状态,2 秒后恢复,可重复操作。
<ComponentPreview name="button-loading-state" />
### 宽度充满
<ComponentPreview name="button-full-width" />
### 禁用状态
<ComponentPreview name="button-disabled" />
### 社交媒体按钮
<ComponentPreview name="button-social" />
### 渲染函数
Vue 使用 `asChild` 将按钮属性与交互合并到一个自定义原生 button,作用域插槽提供状态。对应上游 render 函数示例,不将 render prop 原样搬入 Vue。
<ComponentPreview name="button-render-function" />
### 添加自定义变体
可通过包装组件并添加自定义变体来扩展组件。下面使用与上游相同的 `tv` 扩展方式,提供额外的圆角与尺寸配置。
<ComponentPreview name="button-custom-variants" />
### 添加涟漪效果
通过组合添加涟漪。本示例使用 Vue 和 CSS 实现,支持指针、Enter 和空格触发;不直接依赖上游 React 的 m3-ripple,减少动态效果时缩短动画。
<ComponentPreview name="button-ripple-effect" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="button-custom-styles" />
使用标准 `class` 属性添加工具类。组件保持上游的尺寸、间距、BEM 结构与状态钩子;类名前缀改为 `hv-`。
```vue
<Button class="rounded-lg">自定义圆角</Button>
```
### 全局 CSS
```css
@layer components {
.hv-button--primary {
--button-bg: oklch(0.6 0.2 300);
}
}
```
## 样式参考
基础类为 `.hv-button`,变体为 `.hv-button--primary` 等;尺寸为 `.hv-button--sm` / `md` / `lg`。支持 `.hv-button--icon-only` 和 `.hv-button--full-width`。
### 交互状态
保留 `data-hovered`、`data-pressed`、`data-focused`、`data-focus-visible`、`data-pending` 状态;键盘焦点样式与鼠标焦点区分。加载期间保留焦点并阻止重复点击,通过 aria-disabled 呈现半透明的原变体颜色。
## API 参考
### Button
| Prop | 类型 | 默认值 | 描述 |
| ------------ | --------------------------------------------------------------------------------------------- | ----------- | --------------------------------------------- |
| `variant` | `'primary' \| 'secondary' \| 'tertiary' \| 'outline' \| 'ghost' \| 'danger' \| 'danger-soft'` | `'primary'` | 视觉样式变体 |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | 按钮尺寸 |
| `fullWidth` | `boolean` | `false` | 是否占满容器宽度 |
| `isDisabled` | `boolean` | `false` | 是否禁用 |
| `isPending` | `boolean` | `false` | 是否处于加载状态 |
| `isIconOnly` | `boolean` | `false` | 是否仅包含图标 |
| `asChild` | `boolean` | `false` | 将属性与交互合并到默认插槽中唯一的原生 button |
| `type` | `'button' \| 'submit' \| 'reset'` | `'button'` | 原生按钮类型 |
### 事件与插槽
Vue 使用 `@click` 和默认作用域插槽。插槽提供 `isPending`、`isPressed`、`isHovered`、`isFocused`、`isFocusVisible`、`isDisabled`。原生属性、class、style、事件透传至 button。
`onPress` / PressEvent 与 `@click` 的事件模型不同,不宣称完全等价。初版 `disabled` / `loading` 继续作为兼容别名;新代码使用 `isDisabled` / `isPending`。`isPending` 不自动插入加载图标,采用上游的组合方式。`loading` 是相同加载语义的兼容别名,额外自动显示旋转图标;不会设置原生 disabled。`asChild` 下内容由调用者完整组合,默认插槽必须只包含一个原生 button。
### 作用域插槽状态
| 参数 | 类型 | 描述 |
| ---------------- | --------- | -------------------------------- |
| `isPending` | `boolean` | 是否处于加载状态 |
| `isPressed` | `boolean` | 是否正在按下 |
| `isHovered` | `boolean` | 是否悬停 |
| `isFocused` | `boolean` | 是否聚焦 |
| `isFocusVisible` | `boolean` | 是否显示键盘焦点指示器 |
| `isDisabled` | `boolean` | 是否由禁用属性禁用,不与加载混用 |
## 相关组件
[Card 卡片](./card) · [Input 输入框](./input) · [Switch 开关](./switch)
+121
View File
@@ -0,0 +1,121 @@
---
title: Card 卡片
description: 灵活容器组件,用于分组相关内容与操作
component: card
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import {
Card,
CardHeader,
CardTitle,
CardDescription,
CardContent,
CardFooter,
} from "@heroui-vue/components";
</script>
```
<ComponentPreview name="card-default" />
## 组件结构
```vue
<Card>
<CardHeader>
<CardTitle />
<CardDescription />
</CardHeader>
<CardContent />
<CardFooter />
</Card>
```
## 示例
### 变体
Card 提供语义变体,描述 prominence 级别而非具体视觉样式,允许主题以不同方式解读:
<ComponentPreview name="card-variants" />
- `transparent`:透明背景,无阴影,适合嵌套卡片。
- `default`:标准卡片,使用 `--surface`。
- `secondary`:使用 `--surface-secondary`。
- `tertiary`:使用 `--surface-tertiary`。
基线中文说明中的部分表面映射与实际 CSS 不一致;这里以固定提交的 CSS 为准。
### 水平布局
<ComponentPreview name="card-horizontal" />
### 带头像
<ComponentPreview name="card-with-avatar" />
### 带图片
<ComponentPreview name="card-with-images" />
### 搭配表单
<ComponentPreview name="card-with-form" />
头像使用 Reka Avatar 组合;链接与表单使用原生 HTML,标签与输入显式关联。此示例不发送登录请求;图片沿用上游公开资源地址。
## 自定义样式
### Tailwind CSS
<ComponentPreview name="card-custom-styles" />
### 全局 CSS
```css
@layer components {
.hv-card__title {
font-weight: 700;
}
}
```
## 样式参考
| 部件 | CSS 类 | data-slot | 默认元素 |
| --------------- | ----------------------- | ------------------ | -------- |
| Card | `.hv-card` | `card` | div |
| CardHeader | `.hv-card__header` | `card-header` | div |
| CardTitle | `.hv-card__title` | `card-title` | h3 |
| CardDescription | `.hv-card__description` | `card-description` | p |
| CardContent | `.hv-card__content` | `card-content` | div |
| CardFooter | `.hv-card__footer` | `card-footer` | div |
## API 参考
### Card
| Prop | 类型 | 默认值 | 描述 |
| --------- | --------------------------------------------------------- | ----------- | ------------------------------ |
| `variant` | `'transparent' \| 'default' \| 'secondary' \| 'tertiary'` | `'default'` | 表示 prominence 级别的语义变体 |
| `as` | `string` | `'div'` | 渲染的 HTML 元素 |
| `class` | `string / object / array` | — | Vue 标准类名合并 |
### CardHeader / CardTitle / CardDescription / CardContent / CardFooter
各部件支持 `as`、默认插槽和原生属性。默认标签见上表。Vue 使用独立命名导出替代 `Card.Header` 等 React 部件访问方式。
## 无障碍
```vue
<Card as="article" aria-labelledby="card-title">
<CardHeader><CardTitle id="card-title">Article Title</CardTitle></CardHeader>
</Card>
```
## 相关组件
[Button 按钮](./button) · [Input 输入框](./input)
+98
View File
@@ -0,0 +1,98 @@
---
title: Input 输入框
description: 接受标准 HTML 属性的原生单行文本输入组件
component: input
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import { Input } from "@heroui-vue/components";
</script>
```
<ComponentPreview name="input-basic" />
::: info 字段组合
校验、标签与错误消息由 TextField 体系负责。TextField 尚未迁移,当前请使用原生 label、aria-describedby 和 aria-invalid 关联。
:::
## 示例
### 变体
Input 组件支持两种视觉变体:
- **primary**(默认):标准样式带阴影,适用于大多数场景。
- **secondary**:低强调变体无阴影,适用于 Surface 组件内。
<ComponentPreview name="input-variants" />
### 表面样式
在表面容器内使用 `variant="secondary"` 以应用适合背景的低强调变体。
<ComponentPreview name="input-on-surface" />
### 宽度充满
<ComponentPreview name="input-full-width" />
### 输入类型
<ComponentPreview name="input-types" />
### 受控组件
<ComponentPreview name="input-controlled" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="input-custom-styles" />
### 全局 CSS
```css
@layer components {
.hv-input {
border-radius: 8px;
}
}
```
## 样式参考
基础类 `.hv-input`;变体 `.hv-input--primary` / `.hv-input--secondary`;宽度修饰符 `.hv-input--full-width`。保留原生 hover / focus / disabled 和 `data-invalid` 状态。
## API 参考
### Input
Input 接受标准 HTML input 属性,以及以下 props:
| Prop | 类型 | 默认值 | 描述 |
| --------------------------------------------------- | -------------------------- | ----------- | --------------------------------- |
| `modelValue` | `string` | — | 受控值,使用 v-model |
| `defaultValue` | `string` | `''` | 非受控初始值 |
| `variant` | `'primary' \| 'secondary'` | `'primary'` | 视觉变体 |
| `fullWidth` | `boolean` | `false` | 是否占满容器宽度 |
| `type` | `string` | `'text'` | 原生输入类型 |
| `invalid` | `boolean` | `false` | 设置 aria-invalid 和 data-invalid |
| `disabled` / `readonly` / `required` | `boolean` | `false` | 原生表单状态 |
| `name` / `placeholder` / `autocomplete` / `pattern` | `string` | — | 原生输入属性 |
| `maxlength` / `minlength` | `number` | — | 长度限制 |
| `min` / `max` / `step` | `string / number` | — | 数值与日期约束 |
### 事件与方法
`update:modelValue` 传递字符串;`@input` / `@change` 保留原生事件。`focus()` 可通过模板引用调用。原生表单 reset 还原非受控初始值;受控值由父组件管理。
不把 React 的 `value` + `onChange` 直接复制为 Vue 状态 API。`className` 改为 `class`。TextField 上下文的自动继承尚未实现。
## 相关组件
[Button 按钮](./button) · [Card 卡片](./card)
+179
View File
@@ -0,0 +1,179 @@
---
title: Switch 开关
description: 用于布尔状态的开关组件。
component: switch
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import {
Switch,
SwitchContent,
SwitchControl,
SwitchThumb,
Description,
Label,
SwitchIcon,
SwitchGroup,
FieldError,
} from "@heroui-vue/components";
</script>
```
<ComponentPreview name="switch-basic" />
## 组件结构
```vue
<Switch>
<SwitchContent>
<SwitchControl><SwitchThumb /></SwitchControl>
启用通知
</SwitchContent>
<Description>允许接收通知</Description>
</Switch>
```
请把 SwitchControl 与标签文本放入 SwitchContent;Description 作为其兄弟节点。Vue 底座使用原生 button 承载 role="switch",不是 React Aria 的 label 结构。
## 示例
### 尺寸
<ComponentPreview name="switch-sizes" />
### 带图标
<ComponentPreview name="switch-with-icons" />
### 禁用
<ComponentPreview name="switch-disabled" />
### 无标签
<ComponentPreview name="switch-without-label" />
### 带描述
<ComponentPreview name="switch-with-description" />
### 默认选中
<ComponentPreview name="switch-default-selected" />
### 受控组件
<ComponentPreview name="switch-controlled" />
### 标签位置
<ComponentPreview name="switch-label-position" />
### 分组
<ComponentPreview name="switch-group" />
### 横向分组
<ComponentPreview name="switch-group-horizontal" />
### 表单集成
<ComponentPreview name="switch-form" />
### 渲染属性
Vue 使用作用域插槽读取选择状态。
<ComponentPreview name="switch-render-props" />
### 渲染函数
Vue 使用 `asChild` 合并根容器属性,默认插槽必须只有一个自定义根元素;内部仍需放置 SwitchContent。
<ComponentPreview name="switch-render-function" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="switch-custom-styles" />
### 全局 CSS
```css
@layer components {
.hv-switch__control {
border-radius: 12px;
}
}
```
## 样式参考
保留字段根容器、Content、Control、Thumb、Icon 的组合结构和 data-slot;类名前缀为 `hv-`。选中状态使用 `data-selected="true"`,禁用使用 `data-disabled="true"`。尺寸与滑块比例移植自基线 CSS。
## API 参考
### Switch
| Prop | 类型 | 默认值 | 描述 |
| -------------------- | --------------------------------------------------------------------- | ---------- | -------------------------------- |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | Switch 尺寸 |
| `checked` | `boolean` | — | Vue 受控值,使用 v-model:checked |
| `isSelected` | `boolean` | — | 上游同名受控属性,优先于 checked |
| `defaultSelected` | `boolean` | `false` | 非受控初始值 |
| `isDisabled` | `boolean` | `false` | 是否禁用 |
| `isReadOnly` | `boolean` | `false` | 是否只读 |
| `isInvalid` | `boolean` | `false` | ARIA 无效状态 |
| `isRequired` | `boolean` | `false` | 是否必须开启 |
| `name` | `string` | — | 表单字段名称 |
| `value` | `string` | — | 表单提交值 |
| `asChild` | `boolean` | `false` | 合并根属性到唯一自定义容器 |
| `validate` | `(value: boolean) => string \| string[] \| true \| null \| undefined` | — | 自定义校验 |
| `validationBehavior` | `'native' \| 'aria'` | `'native'` | 校验与错误展示方式 |
### SwitchContent / SwitchControl / SwitchThumb / SwitchIcon
分别对应上游 Content、Control、Thumb、Icon。Content 提供可点击的开关语义,其余为可组合样式部件。空默认插槽会自动渲染轨道与滑块;使用自定义插槽时由调用者组合部件。
### 事件与插槽
`update:checked`、`update:isSelected` 与 `change` 都传递 boolean。根作用域插槽提供 isSelected、isDisabled、isReadOnly、isInvalid、isRequired。根 attrs 的 class/style/data-* 应用到字段容器,ARIA 与 DOM 事件透传到 Content 按钮。
### 表单与校验
`validate(value)` 返回错误字符串、字符串数组,或 `true` / `null` / `undefined` 表示通过。`validationBehavior="native"`(默认)通过原生 checkbox 的约束阻止无效表单提交,校验失败后展示错误并将焦点交给开关;`"aria"` 立即设置 ARIA 错误状态,提交由调用者处理。
```vue
<Switch
name="consent"
is-required
:validate="(value) => (value ? true : '请先开启此选项。')"
>
<SwitchContent><SwitchControl><SwitchThumb /></SwitchControl>同意接收通知</SwitchContent>
<Description>可随时在设置中关闭。</Description>
<FieldError />
</Switch>
```
Description 与 FieldError 自动生成唯一 ID,并关联到 Content 的 aria-describedby。表单 reset 恢复非受控初值;受控值由父组件管理。
### SwitchGroup
| Prop | 类型 | 默认值 | 描述 |
| ------------- | ---------------------------- | ------------ | ---------- |
| `orientation` | `'horizontal' \| 'vertical'` | `'vertical'` | 组布局方向 |
SwitchGroup 是布局容器,每个 Switch 独立维护值;请提供 aria-label 或 aria-labelledby 标注分组。
### 当前差异
`disabled` / `required` / `defaultChecked` 作为兼容别名保留。`change` 事件传递 boolean;React Aria 的 PressEvent 仍不等价于 DOM click。根组件的 class/style/data-* 作用于字段容器,ARIA 与 DOM 事件作用于 Content 按钮;Content 上的显式属性可覆盖默认透传。`asChild` 替代 React render prop。FieldError 当前仅接入 Switch 字段,尚未提供完整 TextField / Form 体系。
## 相关组件
[Button 按钮](./button) · [Input 输入框](./input)