dc859fb8ae
HD-05 scope, against heroui ce90ea3 styles sources: - add labelVariants/descriptionVariants/fieldErrorVariants/ textFieldVariants/switchGroupVariants (+ VariantProps types) to @heroui-vue/styles and re-export them from @heroui-vue/components; variant keys, defaultVariants and slot sets match the pinned *.styles.ts files key for key (hv- namespace only) - Label/Description/FieldError/useTextField/SwitchGroup now consume those mappings instead of inline class strings, so the state classes have a single source; rendered class output is unchanged and pinned by tests/shared-field-styles.test.ts (mapping output <-> component output) - restore the global border-color fallback (*, ::after, ::before, ::backdrop, ::file-selector-button -> border-color: var(--border, currentColor)) in a new base/base.css, imported layer(base) right after preflight like the pinned entry: a low-priority default that components, utilities and consumer CSS override without !important - view-transition globals from the pinned base.css stay unported: no View Transitions API usage exists in the library or docs site - shared-styles package contract: packed runtime exports from both entries, variant-output assertions, CSS tokens incl. the fallback, strict nodenext+bundler type contract, and a real-browser consumer fixture (light/dark --border, explicit colors, consumer overrides, currentColor fallback) wired into check-package.mjs - docs: styling layers/border fallback/exports notes, five component API pages, compatibility matrix and upstream source records Full suite 111 files / 1634 tests, typecheck and build:docs pass; shared-styles + api-root-aliases + handbook-styles verified against the final tarballs.
215 lines
7.8 KiB
Markdown
215 lines
7.8 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()` 或直接给该部件添加类,不在文档壳中隐藏组件问题。
|
|
|
|
库的基础层还提供全局边框色回退(与固定源一致):
|
|
|
|
```css
|
|
*,
|
|
::after,
|
|
::before,
|
|
::backdrop,
|
|
::file-selector-button {
|
|
border-color: var(--border, currentColor);
|
|
}
|
|
```
|
|
|
|
没有显式 `border-color` 的边框默认使用主题的 `--border`(未定义时回退 `currentColor`)。该规则位于 `base` 层且不带 `!important`,组件规则、工具类与调用方 CSS 都能正常覆盖它;显式边框色(包括 `transparent` 的字段边框)不受影响。
|
|
|
|
组件与类名的变体映射从 `@heroui-vue/styles` 公开(`buttonVariants`、`labelVariants`、`descriptionVariants`、`fieldErrorVariants`、`textFieldVariants`、`switchGroupVariants` 等),`@heroui-vue/components` 再导出同一组函数与 `*Variants` 类型;组件内部消费的也是这些映射,状态类只有这一处来源。
|
|
|
|
## 创建包装组件
|
|
|
|
使用公开 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 和具名部件。
|