Files
heroui-vue/apps/docs/styling.md
T

199 lines
7.0 KiB
Markdown

---
title: 样式
description: 使用 CSS、Tailwind 或 CSS-in-JS 为 HeroUI 组件设置样式
---
组件提供变体属性、Vue `class` / `style`、BEM 类名和状态属性。样式包提供预编译 CSS;Tailwind 是构建自定义工具类的可选方案,不是消费组件的必要运行时。
## 基础样式
通过组件属性选择基线变体;用 Vue 的 `class`(字符串、数组、对象)和 `style` 增加局部样式。
```vue
<script setup lang="ts">
import { Button } from "@heroui-vue/components";
</script>
<template>
<Button
variant="secondary"
class="custom-button"
:style="{ fontWeight: 600 }"
>
Custom Button
</Button>
</template>
```
`class` 通常落在当前部件的实际 DOM 上;多部件组件应将样式放到目标部件。PopoverContent、TableScrollContainer 与输入控件不是同一个节点,不能把所有样式都加在 Root 上。
## 滚动条
库的滚动区域通过共享变量使用标准 CSS 滚动条。`data-scrollbar` 可放在滚动槽、组件根或祖先:
| 属性值 | 结果 |
| --------- | ------------------------- |
| `thin` | 主题化纤细滚动条 |
| `default` | 浏览器/操作系统默认滚动条 |
| `none` | 隐藏滚动条,仍可滚动 |
```vue
<Table data-scrollbar="default">
<TableScrollContainer><!-- 实际表格部件 --></TableScrollContainer>
</Table>
```
自定义滚动区可直接使用 `scrollbar-width: var(--scrollbar-width)`、`scrollbar-color: var(--scrollbar-color)` 和 `scrollbar-gutter: var(--scrollbar-gutter)`。官方 `scrollbar` / `scrollbar-thin` / `scrollbar-default` / `scrollbar-none` 工具在库源码中存在,但当前没有公开 utilities 源 CSS 入口;不能仅凭它们出现在官方文档中,就在消费者项目直接使用未生成的工具类。更多变量见[主题](/themes#滚动条)。
## 基于状态的样式
原生伪类与组件数据属性共同驱动视觉状态。以下规则用于 Button:
```css
.hv-button[data-hovered="true"],
.hv-button:hover {
/* 悬停 */
}
.hv-button[data-pressed="true"],
.hv-button:active {
/* 按下 */
}
.hv-button[data-focus-visible="true"],
.hv-button:focus-visible {
/* 键盘焦点 */
}
.hv-button[aria-disabled="true"],
.hv-button:disabled {
/* 禁用 */
}
```
<ComponentPreview name="handbook-states" />
不是所有部件都会生成全部数据属性。有的输入依赖原生 `:focus`,有的 Reka 部件采用 `data-state` 或存在式属性;请使用各组件文档公开的状态。禁用还需实际阻止操作、退出 Tab 顺序或符合该控件的焦点模型,不能只降低透明度。
## 渲染属性
React 的函数式 `className` / `style` 和 render props 不直接用于 Vue。需要组件内部状态时,使用该组件提供的作用域插槽。Button 的状态与自定义根节点可这样组合:
```vue
<Button as-child v-slot="{ isPressed }">
<button :class="{ 'is-pressed': isPressed }">点按</button>
</Button>
```
只有默认插槽中的唯一根节点接收 asChild 的合并属性。Input 等组件不提供同一套状态插槽,不能将 Button 的插槽契约推广到整库。部分颜色组件的 `styleFn` 是其专有 API。
## BEM 类名
本库沿用 BEM,并使用 `hv-` 命名空间:
```css
.hv-button {
/* Block */
}
.hv-accordion__trigger {
/* Element */
}
.hv-button--secondary {
/* Modifier */
}
.hv-button--lg {
/* Size modifier */
}
```
可在全局样式中覆盖:
```css
@layer components {
.hv-button {
font-weight: 600;
}
}
```
库 CSS 使用 `theme, base, components, utilities` 层顺序。需要确保覆盖优先级时,应检查实际层、选择器与导入顺序;无层的应用 CSS 会优先于普通有层规则。Vue scoped 样式触及嵌套部件时使用 `:deep()` 或直接给该部件添加类,不在文档壳中隐藏组件问题。
## 创建包装组件
使用公开 Props 类型与 `tv` 扩展变体。包装组件需要显式管理 `$attrs`、插槽、事件和需要公开的方法,不能把 DOM `ref` 当作普通属性传递。
```vue
<script setup lang="ts">
import { computed, useAttrs } from "vue";
import { Button } from "@heroui-vue/components";
import { buttonVariants, tv } from "@heroui-vue/styles";
defineOptions({ inheritAttrs: false });
const props = defineProps<{ emphasis?: "quiet" | "strong" }>();
const attrs = useAttrs();
const custom = tv({
extend: buttonVariants,
variants: { emphasis: { quiet: "opacity-80", strong: "font-bold" } },
});
const classes = computed(() => custom({ emphasis: props.emphasis }));
</script>
<template>
<Button v-bind="attrs" :class="classes"><slot /></Button>
</template>
```
此例为展示包装层:它不额外暴露 Button 实例。应用若需要 `focus()`,应在支持该方法的部件上绑定实例,并通过 `defineExpose` 明确转交。完整组合原则见[组合](/composition)。
## CSS-in-JS 集成
只要工具产出类名或 Vue 可接受的 style 对象,就可以通过现有边界传入。官方 styled-components/Emotion React 示例不直接适用;当前没有验证所有 Vue CSS-in-JS 绑定,也不要求消费者增加此类依赖。
## 响应式设计
自定义内容可使用标准 CSS 媒体查询:
```css
.custom-button {
width: 100%;
}
@media (min-width: 48rem) {
.custom-button {
width: auto;
}
}
```
使用 `md:` 等 Tailwind 工具类时,需要应用自行安装并配置 Tailwind。静态声明完整类名,不通过动态拼接 `bg-${color}` 等字符串期待构建器生成样式。
## CSS 模块
Vue SFC 的 CSS Modules 可以把生成的类传给组件:
```vue
<template>
<Button :class="$style.button">Scoped Button</Button>
</template>
<style module>
.button {
border: 1px solid var(--border);
}
</style>
```
示例中的 Button 需按前述方式导入。CSS Modules 负责类名隔离,不改变组件透传或 CSS 层优先级;独立消费者的完整覆盖仍列入后续排查。
## 组件类名参考
| 组件 | 常见类名 |
| --------- | ---------------------------------------------------------------------------------------- |
| Button | `.hv-button`、`.hv-button--{variant}`、`.hv-button--{size}`、`.hv-button--icon-only` |
| Accordion | `.hv-accordion`、`.hv-accordion__item`、`.hv-accordion__trigger`、`.hv-accordion__panel` |
| Input | `.hv-input`、`.hv-input--secondary` |
| Popover | `.hv-popover`、`.hv-popover__dialog`、`.hv-popover__heading` |
完整部件、状态与 API 以各组件页为准:[Button](/components/button)、[Accordion](/components/accordion)、[Input](/components/input)、[Popover](/components/popover)。这些类名赋予样式,不自动赋予键盘交互和 ARIA 语义。
## 下一步
- [动画](/animation):进入、退出与减少动态效果。
- [主题](/themes):语义变量、层与导入策略。
- [组合](/composition):变体、自定义 DOM 和具名部件。