159 lines
6.4 KiB
Markdown
159 lines
6.4 KiB
Markdown
---
|
||
title: 组合
|
||
description: 使用组件组合模式构建灵活的 UI
|
||
---
|
||
|
||
组合的目标是在保留组件语义、状态和可访问行为的同时调整外观与结构。Vue 使用具名部件、普通属性、`v-model`、具名/作用域插槽,以及按需支持的 `as` / `asChild`。
|
||
|
||
## 与框架无关的样式
|
||
|
||
`@heroui-vue/styles` 提供变体函数和 CSS,不运行 React。只想把组件样式用于原生元素时,可以从样式包导入函数:
|
||
|
||
```ts
|
||
import { buttonVariants, tv, type VariantProps } from "@heroui-vue/styles";
|
||
import "@heroui-vue/styles/css";
|
||
```
|
||
|
||
当前完整 CSS 通过 `/css` 导入,JS 根导出提供变体函数;没有公开官方那套 `/components/button.css`、`/themes/default` 等子路径。按需入口仍是后续任务,见[导入策略](/themes#导入策略)。
|
||
|
||
## 多态样式
|
||
|
||
可将样式应用到符合实际用途的原生元素。例如导航链接可以采用按钮外观,而实际操作继续使用 button:
|
||
|
||
<ComponentPreview name="handbook-composition" />
|
||
|
||
第一个元素仍是链接,后两个示例展示实际 Button 与 Link。样式相似不代表键盘模型相同;不要把所有元素统一改成 div,也不要用 `role="button"` 掩盖缺少键盘处理的问题。
|
||
|
||
## 直接应用类名
|
||
|
||
用完整 BEM 类名可复用预编译样式:
|
||
|
||
```html
|
||
<a class="hv-button hv-button--secondary hv-button--md" href="/settings">
|
||
Settings
|
||
</a>
|
||
```
|
||
|
||
此时只有样式,禁用、加载、焦点管理和表单行为由元素与应用负责。本库组件通常是获得完整交互的更直接方式。
|
||
|
||
## 使用变体函数
|
||
|
||
变体函数决定类名和默认变体,不建立 Vue 状态:
|
||
|
||
```vue
|
||
<script setup lang="ts">
|
||
import { buttonVariants } from "@heroui-vue/styles";
|
||
</script>
|
||
|
||
<template>
|
||
<a
|
||
href="/settings"
|
||
:class="buttonVariants({ variant: 'secondary', size: 'sm' })"
|
||
>
|
||
Settings
|
||
</a>
|
||
</template>
|
||
```
|
||
|
||
Button 的函数返回类名字符串;Card、Popover 等带 slots 的函数返回部件函数集合。按函数真实返回类型使用,例如 `cardVariants().base()`,不要假定所有 variants 都有相同的返回结构。
|
||
|
||
## 复合组件
|
||
|
||
复杂组件导出协同工作的具名部件,不采用默认的 React `Popover.Trigger` 对象写法:
|
||
|
||
```vue
|
||
<script setup lang="ts">
|
||
import {
|
||
Button,
|
||
Popover,
|
||
PopoverContent,
|
||
PopoverDialog,
|
||
PopoverHeading,
|
||
} from "@heroui-vue/components";
|
||
</script>
|
||
|
||
<template>
|
||
<Popover>
|
||
<Button>打开</Button>
|
||
<PopoverContent>
|
||
<PopoverDialog>
|
||
<PopoverHeading>标题</PopoverHeading>
|
||
内容
|
||
</PopoverDialog>
|
||
</PopoverContent>
|
||
</Popover>
|
||
</template>
|
||
```
|
||
|
||
`PopoverRoot` 是 `Popover` 的同一实现别名;按各组件公开导出选择。需要上下文的 Trigger、Item、Content 不能脱离对应 Root 任意放置。复杂包装应透传显式数据和插槽,不通过执行未知子组件的 setup/render 来探测元数据。
|
||
|
||
普通值使用 `v-model`;展开状态通常使用 `v-model:open`,二态控件按 API 使用 `v-model:checked`。Root 与子控件不要绑定两个相互冲突的值源。受控组件会发出更新提案,父级可以拒绝,显示值仍应保持父级接受值。
|
||
|
||
## 混合使用变体函数
|
||
|
||
`cn` 可以合并类名,`tv` 可以扩展变体。注意多个组件的根样式可能同时定义尺寸、布局和 transform,不应仅为拼出外观就叠加互相冲突的根类。
|
||
|
||
```ts
|
||
import { cn, buttonVariants } from "@heroui-vue/styles";
|
||
|
||
const classes = cn(buttonVariants({ variant: "ghost" }), "font-semibold");
|
||
```
|
||
|
||
应用新增的工具类需要由自己的构建产出。合并函数不会生成 CSS,也不会让未公开的变体名称自动成为组件 prop。
|
||
|
||
## 自定义组件
|
||
|
||
把可复用业务布局包装成 Vue 组件时,明确以下边界:
|
||
|
||
- `$attrs`、class/style、ARIA 和原生事件落到哪个实际节点。
|
||
- `v-model` 是否保留受控接受值和更新事件。
|
||
- 默认、具名和作用域插槽分别传给哪个部件。
|
||
- ref 返回包装组件还是实际控件;需要的方法通过 `defineExpose` 明确公开。
|
||
|
||
多根组件设置 `inheritAttrs: false` 并显式转交属性。事件取消、capture 和子节点监听必须保持正确顺序,不能同时手动调用和再次透传导致双触发。包装示例见[创建包装组件](/styling#创建包装组件)。
|
||
|
||
## 自定义变体
|
||
|
||
扩展样式函数,不改写原组件已定义的 prop 取值:
|
||
|
||
```ts
|
||
import { buttonVariants, tv, type VariantProps } from "@heroui-vue/styles";
|
||
|
||
export const customButton = tv({
|
||
extend: buttonVariants,
|
||
variants: {
|
||
emphasis: { quiet: "opacity-80", strong: "font-bold" },
|
||
},
|
||
defaultVariants: { emphasis: "quiet" },
|
||
});
|
||
export type CustomButtonVariants = VariantProps<typeof customButton>;
|
||
```
|
||
|
||
将计算结果通过 class 传给 Button;`emphasis` 属于你的包装组件,不是库 Button 新增的 API。不要直接向 Button 传入不存在的 `variant="gradient"`。
|
||
|
||
## 自定义 DOM 元素
|
||
|
||
支持 `asChild` 的部件把自身属性、交互与默认插槽中的唯一根节点合并。Button 的根仍需是原生 button:
|
||
|
||
```vue
|
||
<Button as-child v-slot="{ isPressed }">
|
||
<button type="button" :data-custom="isPressed ? 'pressed' : 'idle'">点按</button>
|
||
</Button>
|
||
```
|
||
|
||
遵循组件期待的元素语义;保留唯一根;自定义 Vue 组件继续把属性、事件和 ref 边界转交到实际 DOM。原生 submit/reset、disabled、focus 和 ARIA 不能因换根而丢失。Button 与 Link 的行为模型不同,支持 `asChild` 不等于任意元素都可互换。
|
||
|
||
`as` 一般选择组件支持的标签,`asChild` 合并已有根;两者范围由各组件 API 决定。不要将 React 的 `render` 或 `ref` prop 原样传入 Vue。
|
||
|
||
## 框架集成
|
||
|
||
Vue 客户端应用可直接使用原生链接。路由器适配可用它提供的自定义链接插槽或把导航外观用于路由链接,前提是仍生成正确的 anchor 并透传 href、事件与可访问属性。基础包不强依赖 Vue Router。
|
||
|
||
Next.js 示例不适用于 Vue;当前不提供 Nuxt 服务端消费承诺。第三方路由和动画包装的全部组合尚未逐项验收,应按真实 DOM、键盘、事件取消和实例方法检查,不能只凭 TypeScript 编译通过判断可用。
|
||
|
||
## 下一步
|
||
|
||
- [样式](/styling):类名、CSS Modules 与状态。
|
||
- [动画](/animation):动画和卸载的协调。
|
||
- [所有组件](/components):逐组件的 Props、插槽和状态契约。
|