feat(styles): export shared field variant maps and restore the border fallback

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.
This commit is contained in:
maoyingjie
2026-10-28 23:36:59 +08:00
parent 35b602e5b9
commit dc859fb8ae
26 changed files with 538 additions and 36 deletions
+2
View File
@@ -83,6 +83,8 @@ HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态
Description 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/description.css)):
> 变体映射 `descriptionVariants` 从 `@heroui-vue/styles` 公开(`@heroui-vue/components` 再导出同一函数与 `DescriptionVariants` 类型);组件渲染的类名由该映射生成。
#### 基础类 [!toc]
- `.hv-description` - 基础描述样式,使用 `muted` 文本颜色
+2
View File
@@ -178,6 +178,8 @@ HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态
FieldError 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/field-error.css)):
> 变体映射 `fieldErrorVariants` 从 `@heroui-vue/styles` 公开(`@heroui-vue/components` 再导出同一函数与 `FieldErrorVariants` 类型);组件渲染的类名由该映射生成。
#### 基础类 [!toc]
- `.hv-field-error` - 基础错误样式,danger 色
+2
View File
@@ -88,6 +88,8 @@ HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态
Label 使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/label.css)):
> 变体映射 `labelVariants` 从 `@heroui-vue/styles` 公开(`@heroui-vue/components` 再导出同一函数与 `LabelVariants` 类型);组件渲染的类名由该映射生成,状态修饰类随 `isDisabled` / `isInvalid` / `isRequired` 输出。
#### 基础类 [!toc]
- `.hv-label` — 基础标签文本样式
+2
View File
@@ -182,6 +182,8 @@ SwitchGroup 组件使用以下 CSS 类([查看源码样式](https://github.com
- `.hv-switch-group--horizontal` - 横向布局
- `.hv-switch-group--vertical` - 纵向布局(默认)
> 变体映射 `switchGroupVariants` 从 `@heroui-vue/styles` 公开(`@heroui-vue/components` 再导出同一函数与 `SwitchGroupVariants` 类型),提供 `base` / `items` 两个槽;组件渲染的类名由该映射生成。
### 交互状态
该 Switch 同时支持 CSS 伪类与 data 属性,以提供更灵活的状态控制:
+2
View File
@@ -133,6 +133,8 @@ HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态
- `.hv-textfield` – 根容器,样式极少(`flex flex-col gap-1`)
> 变体映射 `textFieldVariants` 从 `@heroui-vue/styles` 公开(`@heroui-vue/components` 再导出同一函数与 `TextFieldVariants` 类型);`full-width` 输出 `.hv-textfield--full-width`,组件根类名由该映射生成。
> **提示:** 子组件([Label](/components/label)、[Input](/components/input)、[TextArea](/components/textarea)、[Description](/components/description)、[FieldError](/components/field-error))各自拥有 CSS 类与样式,定制方式请参见对应文档。
### 交互状态
+16
View File
@@ -115,6 +115,22 @@ React 的函数式 `className` / `style` 和 render props 不直接用于 Vue。
库 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` 当作普通属性传递。
+4
View File
@@ -290,3 +290,7 @@ ComboBox/Autocomplete的正式组合、原30项返修和本轮定位/ref/ARIA/lo
固定源六篇56章节的实现证据与差异见 [Handbook审计](handbook-audit.md)。修复 data-scrollbar 三模式、八类浮层/Skeleton/Drawer 的减少动效与动态取消动画后的进入标记清理。按需 CSS 子路径、公共 useTheme、局部主题自动跨 Teleport 及 Skeleton 动画变量的局部/动态读取仍未提供;不声明所有组件已完成全部规范的浏览器验收。逐项场景与实际验证见 [任务结果](tasks/results/HANDBOOK-IMPLEMENTATION-AUDIT.md)。
HD-01(2026-10-28):按钮/切换组与导航示例已恢复固定cn基线;ButtonGroup内Dropdown保留根attrs/事件,以容器位置适配接缝。HD01-R1已关闭;不升级整族API兼容状态,详见[结果](tasks/results/HD-01-BUTTON-NAV-DEMOS.md)。
## HD-05 styles 导出与全局边框回退(2026-10-28)
`@heroui-vue/styles` 增量补齐固定源缺失的 5 个 tv 映射与类型:`labelVariants`/`descriptionVariants`/`fieldErrorVariants`/`textFieldVariants`/`switchGroupVariants`(hv- 命名空间,变体集合与默认值与 `heroui/packages/styles/src/components/{label,description,field-error,textfield,switch-group}/*.styles.ts` 逐键一致),`@heroui-vue/components` 再导出同一组函数与 `*Variants` 类型。Label/Description/FieldError/useTextField/SwitchGroup 改为消费同一映射,状态类只保留一处来源,渲染类输出不变(tests/shared-field-styles.test.ts 钉住映射输出与组件输出一致)。恢复 `base` 层全局边框色回退 `border-color: var(--border, currentColor)`(与固定源 base/base.css 同选择器、同层序:preflight 之后);独立 tarball 消费者真实浏览器验证浅/深 `--border`、显式边框色(含字段透明边框)、调用方普通 CSS 覆盖(含与库同选择器的注入规则)与 `--border` 缺失时的 currentColor 回退,CSSOM 确认规则位于 base 层且无 `!important`。`view-transition-name`/`::view-transition` 两条上游全局规则经核查无使用证据(库与文档站均未使用 View Transitions API),不迁移、不建专项。chip `size:"md"`、kbd `variant:"default"` 等效默认类、reduced-motion、Spinner 定尺、Table th 行头、定位补偿与 HD-01~HD-04 成果均未改动。见 [任务结果](tasks/results/HD-05-STYLES.md)。
+5
View File
@@ -356,3 +356,8 @@ CSS/tv 仅加 `hv-` 前缀与 Apache-2.0 来源;`colorPickerVariants` 保持
## HD-01 示例与 Dropdown 组合(2026-10-28)
Button/ButtonGroup/ToggleButton/ToggleButtonGroup与指定Accordion/Disclosure示例按ce90ea3的cn源码恢复,保留Tabler图标适配。Vue Dropdown根容器承载attrs/原生事件;ButtonGroup CSS增加仅作用于标记直接成员的位置规则,圆角/outline数值沿用基线,来源许可保留。行为和证据见HD-01任务结果。
## HD-05 来源(2026-10-28)
- packages/styles/src/index.ts 的 `labelVariants`/`descriptionVariants`/`fieldErrorVariants`/`textFieldVariants`/`switchGroupVariants`:移植 `heroui/packages/styles/src/components/{label/label,description/description,field-error/field-error,textfield/textfield,switch-group/switch-group}.styles.ts`,仅加 `hv-` 前缀;变体键、默认值与 slot 集合逐键保留(Apache-2.0,见 third-party/HEROUI-LICENSE)。
- packages/styles/base/base.css:移植 `heroui/packages/styles/base/base.css` 的边框色继承规则(`*, ::after, ::before, ::backdrop, ::file-selector-button { border-color: var(--border, currentColor) }`),导入序保持 preflight 之后的 base 层。同文件的 `:root { view-transition-name: none }` 与 `::view-transition { pointer-events: none }` 未移植:库与文档站均无 View Transitions API 使用证据,按 HD-05 约定记录无需修改,不建立专项。
+17
View File
@@ -0,0 +1,17 @@
/* Adapted from HeroUI ce90ea3, packages/styles/base/base.css.
* Copyright 2025 NextUI Inc. Apache-2.0; see third-party/HEROUI-LICENSE. */
/* Border color inheritance
*
* Every border without an explicit color resolves to the theme border token
* instead of `currentColor`. This lives in the base layer right after
* Tailwind preflight, so component rules (components layer), utilities and
* consumer CSS all override it normally — no !important required. When
* `--border` is undefined the declaration falls back to `currentColor`. */
*,
::after,
::before,
::backdrop,
::file-selector-button {
border-color: var(--border, currentColor);
}
+3
View File
@@ -1,6 +1,9 @@
@layer theme, base, components, utilities;
@import "tailwindcss/theme.css" layer(theme);
@import "tailwindcss/preflight.css" layer(base);
/* Base defaults follow preflight so they win same-layer ties (border color
* fallback); upstream keeps them in base/base.css in the same order. */
@import "./base/base.css" layer(base);
@import "./themes/shared/theme.css";
@import "./themes/default/variables.css";
/* Mode overrides must follow theme defaults so an explicit mode on a theme root wins. */
+71
View File
@@ -119,6 +119,28 @@ export const switchVariants = tv({
export type SwitchVariants = VariantProps<typeof switchVariants>;
export const switchGroupVariants = tv({
defaultVariants: {
orientation: "vertical",
},
slots: {
base: "hv-switch-group",
items: "hv-switch-group__items",
},
variants: {
orientation: {
horizontal: {
base: "hv-switch-group--horizontal",
},
vertical: {
base: "hv-switch-group--vertical",
},
},
},
});
export type SwitchGroupVariants = VariantProps<typeof switchGroupVariants>;
export { tv, cn, type VariantProps } from "tailwind-variants";
export const closeButtonVariants = tv({
@@ -199,6 +221,21 @@ export const textAreaVariants = tv({
export type TextAreaVariants = VariantProps<typeof textAreaVariants>;
export const textFieldVariants = tv({
base: "hv-textfield",
defaultVariants: {
fullWidth: false,
},
variants: {
fullWidth: {
false: "",
true: "hv-textfield--full-width",
},
},
});
export type TextFieldVariants = VariantProps<typeof textFieldVariants>;
export const avatarVariants = tv({
defaultVariants: {
color: "default",
@@ -1746,6 +1783,40 @@ export const errorMessageVariants = tv({
export type ErrorMessageVariants = VariantProps<typeof errorMessageVariants>;
export const labelVariants = tv({
base: "hv-label",
defaultVariants: {
isDisabled: false,
isInvalid: false,
isRequired: false,
},
variants: {
isDisabled: {
true: "hv-label--disabled",
},
isInvalid: {
true: "hv-label--invalid",
},
isRequired: {
true: "hv-label--required",
},
},
});
export type LabelVariants = VariantProps<typeof labelVariants>;
export const descriptionVariants = tv({
base: "hv-description",
});
export type DescriptionVariants = VariantProps<typeof descriptionVariants>;
export const fieldErrorVariants = tv({
base: "hv-field-error",
});
export type FieldErrorVariants = VariantProps<typeof fieldErrorVariants>;
export const comboBoxVariants = tv({
defaultVariants: {
fullWidth: false,
@@ -1,12 +1,18 @@
<script setup lang="ts">
import { useAttrs } from "vue";
import { descriptionVariants } from "@heroui-vue/styles";
import { useFieldPart } from "../field/useFieldPart";
defineOptions({ inheritAttrs: false });
const attrs = useAttrs();
const { id } = useFieldPart("description", attrs);
</script>
<template>
<p v-bind="attrs" :id="id" class="hv-description" data-slot="description">
<p
v-bind="attrs"
:id="id"
:class="descriptionVariants()"
data-slot="description"
>
<slot />
</p>
</template>
@@ -1,5 +1,6 @@
<script setup lang="ts">
import { useAttrs } from "vue";
import { fieldErrorVariants } from "@heroui-vue/styles";
import { useFieldPart } from "./useFieldPart";
defineOptions({ inheritAttrs: false });
const attrs = useAttrs();
@@ -12,7 +13,7 @@ const { field, id } = useFieldPart("error", attrs);
:id="id"
data-slot="field-error"
data-visible
class="hv-field-error"
:class="fieldErrorVariants()"
role="alert"
>
<slot
@@ -18,6 +18,7 @@ import {
type VNode,
type VNodeChild,
} from "vue";
import { textFieldVariants } from "@heroui-vue/styles";
import {
fieldContextKey,
formContextKey,
@@ -325,10 +326,8 @@ export function useTextField(
Object.entries(attrs).filter(([key]) => !key.startsWith("aria-")),
),
class: [
options.rootClass?.() ?? [
"hv-textfield",
props.fullWidth && "hv-textfield--full-width",
],
options.rootClass?.() ??
textFieldVariants({ fullWidth: props.fullWidth }),
attrs.class,
],
"data-slot": options.rootSlot ?? "textfield",
+8 -6
View File
@@ -1,5 +1,6 @@
<script setup lang="ts">
import { inject, onUnmounted, useAttrs, useId, watch } from "vue";
import { labelVariants } from "@heroui-vue/styles";
import { useFieldPart } from "../field/useFieldPart";
import { useInheritedDisabled } from "../field/disabled";
import { fieldContextKey } from "../field/context";
@@ -68,12 +69,13 @@ onUnmounted(() => {
:for="
attrs.for ?? (field?.kind === 'text' ? field.controlId.value : undefined)
"
class="hv-label"
:class="{
'hv-label--required': props.isRequired || field?.required.value,
'hv-label--disabled': disabled,
'hv-label--invalid': props.isInvalid || field?.invalid.value,
}"
:class="
labelVariants({
isRequired: Boolean(props.isRequired || field?.required.value),
isDisabled: Boolean(disabled),
isInvalid: Boolean(props.isInvalid || field?.invalid.value),
})
"
data-slot="label"
>
<slot />
@@ -1,16 +1,19 @@
<script setup lang="ts">
withDefaults(defineProps<{ orientation?: "horizontal" | "vertical" }>(), {
import { computed } from "vue";
import { switchGroupVariants } from "@heroui-vue/styles";
const props = withDefaults(
defineProps<{ orientation?: "horizontal" | "vertical" }>(),
{
orientation: "vertical",
});
},
);
const styles = computed(() =>
switchGroupVariants({ orientation: props.orientation }),
);
</script>
<template>
<div
role="group"
class="hv-switch-group"
:class="`hv-switch-group--${orientation}`"
data-slot="switch-group"
>
<div class="hv-switch-group__items" data-slot="switch-group-items">
<div role="group" :class="styles.base()" data-slot="switch-group">
<div :class="styles.items()" data-slot="switch-group-items">
<slot />
</div>
</div>
+10
View File
@@ -38,6 +38,11 @@ export {
badgeVariants,
alertVariants,
inputGroupVariants,
labelVariants,
descriptionVariants,
fieldErrorVariants,
textFieldVariants,
switchGroupVariants,
type ButtonVariants,
type CardVariants,
type InputVariants,
@@ -56,6 +61,11 @@ export {
type BadgeVariants,
type AlertVariants,
type InputGroupVariants,
type LabelVariants,
type DescriptionVariants,
type FieldErrorVariants,
type TextFieldVariants,
type SwitchGroupVariants,
} from "@heroui-vue/styles";
export type {
ButtonProps,
+2
View File
@@ -110,6 +110,8 @@ const browser = ids.includes("b02-pickers")
? "b01"
: ids.includes("color-picker")
? "color-picker"
: ids.includes("shared-styles")
? "shared-styles"
: null;
if (browser) {
for (const file of ["index.html", "main.js", "style.css"])
+55 -13
View File
@@ -1,6 +1,7 @@
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import * as lib from "@heroui-vue/components";
import * as stylesLib from "@heroui-vue/styles";
const checks = JSON.parse(
readFileSync(new URL("./client-checks.json", import.meta.url), "utf8"),
@@ -202,10 +203,7 @@ for (const id of process.argv.slice(2)) {
assert(lib.ColorSwatchPickerSwatch);
assert(lib.ColorSwatchPickerIndicator);
assert(
lib
.colorSwatchPickerVariants()
.base()
.includes("hv-color-swatch-picker"),
lib.colorSwatchPickerVariants().base().includes("hv-color-swatch-picker"),
);
assert(
lib
@@ -214,19 +212,16 @@ for (const id of process.argv.slice(2)) {
.includes("hv-color-swatch-picker--stack"),
);
assert(
lib
.colorSwatchPickerVariants({ size: "xs" })
.item() === "hv-color-swatch-picker__item",
lib.colorSwatchPickerVariants({ size: "xs" }).item() ===
"hv-color-swatch-picker__item",
);
assert(
lib
.colorSwatchPickerVariants()
.swatch() === "hv-color-swatch-picker__swatch",
lib.colorSwatchPickerVariants().swatch() ===
"hv-color-swatch-picker__swatch",
);
assert(
lib
.colorSwatchPickerVariants()
.indicator() === "hv-color-swatch-picker__indicator",
lib.colorSwatchPickerVariants().indicator() ===
"hv-color-swatch-picker__indicator",
);
const pickerDts = readFileSync(
new URL(
@@ -360,5 +355,52 @@ for (const id of process.argv.slice(2)) {
);
}
}
if (id === "shared-styles") {
// The five shared mappings are importable from both entry points and are
// the same functions the components consume.
for (const name of spec.exports)
assert.equal(
stylesLib[name],
lib[name],
`${name} must match styles entry`,
);
// Variant outputs must mirror the classes the components actually emit.
assert.equal(lib.labelVariants(), "hv-label");
assert.equal(
lib.labelVariants({
isRequired: false,
isDisabled: false,
isInvalid: false,
}),
"hv-label",
);
assert(
lib.labelVariants({ isRequired: true }).includes("hv-label--required"),
);
assert(
lib.labelVariants({ isDisabled: true }).includes("hv-label--disabled"),
);
assert(
lib.labelVariants({ isInvalid: true }).includes("hv-label--invalid"),
);
assert.equal(lib.descriptionVariants(), "hv-description");
assert.equal(lib.fieldErrorVariants(), "hv-field-error");
assert(lib.textFieldVariants().includes("hv-textfield"));
assert(!lib.textFieldVariants().includes("full-width"));
assert(
lib
.textFieldVariants({ fullWidth: true })
.includes("hv-textfield--full-width"),
);
const group = lib.switchGroupVariants();
assert(group.base().includes("hv-switch-group--vertical"));
assert.equal(group.items(), "hv-switch-group__items");
assert(
lib
.switchGroupVariants({ orientation: "horizontal" })
.base()
.includes("hv-switch-group--horizontal"),
);
}
console.log(`${id}: packed ESM exports and CSS passed (client scope)`);
}
+21
View File
@@ -18,6 +18,27 @@
".hv-popover"
]
},
"shared-styles": {
"exports": [
"labelVariants",
"descriptionVariants",
"fieldErrorVariants",
"textFieldVariants",
"switchGroupVariants"
],
"css": [
"border-color:var(--border,currentColor)",
".hv-label--required",
".hv-label--disabled",
".hv-label--invalid",
".hv-description",
".hv-field-error",
".hv-textfield--full-width",
".hv-switch-group__items",
".hv-switch-group--horizontal",
".hv-switch-group--vertical"
]
},
"b01-menu-dropdown": {
"exports": [
"DropdownRoot",
@@ -0,0 +1,13 @@
# Shared styles 独立产物验收(styles 导出与全局边框回退)
先运行 `node scripts/check-package.mjs shared-styles`(可与受影响契约合跑)统一构建、打包、安装和类型验收;它会把本夹具放入独立消费者并打印路径和 Vite 命令。按输出显式选空闲端口启动,不另外复制安装流程;入口不会自动启动服务。main.js 只从包根/发布 CSS 导入,不用源码 alias、私有路径或 Vue 模板编译器。
在真实浏览器核对:
1. `#default-border`(无显式 border 颜色)的计算 `border-color` 等于当前主题的 `--border`(浅/深两值不同)。
2. `#explicit-border` 的显式红边框不被全局回退覆盖。
3. `#override-border` 的调用方普通 CSS(无 `!important`)覆盖生效;另在页面注入与库规则**同等选择器**的普通样式,验证库规则不是高优先级、不破坏消费者定制。
4. `#fallback-border`(`--border: initial`)边框色回退为 `currentColor`(元素文字色)。
5. `#component-border` 内 `.hv-input-otp__slot` 的显式 `var(--field-border)`(默认 transparent)不被全局回退覆盖;点 `#theme-toggle` 切深色后上述 `--border` 期望值同步变化。
记录计算样式实值与截图;不能因 CSS 文本包含 `var(--border` 就通过。类型正反例使用上级 `shared-styles-contract.ts` 的两种解析模式。
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Shared styles 独立产物验收</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/main.js"></script>
</body>
</html>
@@ -0,0 +1,85 @@
import { createApp, defineComponent, h, shallowRef } from "vue";
import {
Button,
InputOTP,
InputOTPGroup,
InputOTPSlot,
} from "@heroui-vue/components";
import "@heroui-vue/styles/css";
import "./style.css";
const theme = shallowRef("light");
const app = defineComponent({
setup() {
const toggleTheme = () => {
theme.value = theme.value === "light" ? "dark" : "light";
document.documentElement.dataset.theme = theme.value;
};
return () =>
h("main", {}, [
h(
Button,
{ id: "theme-toggle", variant: "secondary", onClick: toggleTheme },
{ default: () => `主题:${theme.value}` },
),
h(
"div",
{
id: "default-border",
class: "box default-border",
"data-testid": "default-border",
},
"默认边框(无显式颜色)",
),
h(
"div",
{
id: "explicit-border",
class: "box explicit-border",
"data-testid": "explicit-border",
},
"显式 border 颜色",
),
h(
"div",
{
id: "override-border",
class: "box override-border",
"data-testid": "override-border",
},
"调用方覆盖",
),
h(
"div",
{
id: "fallback-border",
class: "box fallback-border",
"data-testid": "fallback-border",
},
"缺少 --border 时回退 currentColor",
),
h("div", { id: "component-border" }, [
h(
InputOTP,
{ "aria-label": "验证码", "max-length": 4, modelValue: "" },
{
default: () =>
h(
InputOTPGroup,
{},
{
default: () =>
[0, 1, 2, 3].map((index) =>
h(InputOTPSlot, { index, key: index }),
),
},
),
},
),
]),
]);
},
});
createApp(app).mount("#app");
@@ -0,0 +1,35 @@
/* Consumer-side CSS: plain unlayered rules must override the library's
* base-layer border fallback without !important. */
main {
display: flex;
flex-direction: column;
gap: 24px;
padding: 32px;
}
.box {
padding: 8px;
border-style: solid;
border-width: 2px;
}
/* 1. default border: no explicit color -> theme --border */
.default-border {
background: var(--surface);
}
/* 2. explicit color survives the global fallback */
.explicit-border {
border-color: rgb(200, 0, 0);
}
/* 3. consumer override wins without !important */
.override-border {
border-color: rgb(0, 128, 0);
}
/* 4. missing --border falls back to currentColor */
.fallback-border {
--border: initial;
color: rgb(0, 0, 255);
}
+55
View File
@@ -0,0 +1,55 @@
import {
descriptionVariants,
fieldErrorVariants,
labelVariants,
switchGroupVariants,
textFieldVariants,
type DescriptionVariants,
type FieldErrorVariants,
type LabelVariants,
type SwitchGroupVariants,
type TextFieldVariants,
type VariantProps,
} from "@heroui-vue/styles";
import {
labelVariants as componentsLabelVariants,
type LabelVariants as ComponentsLabelVariants,
} from "@heroui-vue/components";
const required: LabelVariants = { isRequired: true, isDisabled: false };
labelVariants(required);
labelVariants({ isInvalid: true });
descriptionVariants();
fieldErrorVariants();
const wide: TextFieldVariants = { fullWidth: true };
textFieldVariants(wide);
const horizontal: SwitchGroupVariants = { orientation: "horizontal" };
switchGroupVariants(horizontal);
switchGroupVariants({ orientation: "vertical" }).items();
// Components re-export the same mapping and type.
const viaComponents: ComponentsLabelVariants = { isDisabled: true };
componentsLabelVariants(viaComponents);
type Description = VariantProps<typeof descriptionVariants>;
const base: Description = {};
descriptionVariants(base);
// @ts-expect-error variant flags stay boolean-typed
const badRequired: LabelVariants = { isRequired: "yes" };
// @ts-expect-error unknown label variant keys stay rejected
const badLabelKey: LabelVariants = { isDanger: true };
// @ts-expect-error fullWidth stays boolean-typed
const badWidth: TextFieldVariants = { fullWidth: "yes" };
// @ts-expect-error orientation stays a closed union
const badOrientation: SwitchGroupVariants = { orientation: "diagonal" };
// @ts-expect-error the exported description shape stays non-nullable object-typed
const badDescription: DescriptionVariants = null;
// @ts-expect-error the exported field-error shape stays non-nullable object-typed
const badFieldError: FieldErrorVariants = null;
void badRequired;
void badLabelKey;
void badWidth;
void badOrientation;
void badDescription;
void badFieldError;
+90
View File
@@ -0,0 +1,90 @@
import { expect, it, describe } from "vitest";
import { mount } from "@vue/test-utils";
import {
descriptionVariants,
fieldErrorVariants,
labelVariants,
switchGroupVariants,
textFieldVariants,
} from "@heroui-vue/styles";
import Label from "../packages/vue/src/components/label/Label.vue";
import Description from "../packages/vue/src/components/description/Description.vue";
import FieldError from "../packages/vue/src/components/field/FieldError.vue";
import SwitchGroup from "../packages/vue/src/components/switch/SwitchGroup.vue";
import {
labelVariants as componentsLabelVariants,
descriptionVariants as componentsDescriptionVariants,
fieldErrorVariants as componentsFieldErrorVariants,
textFieldVariants as componentsTextFieldVariants,
switchGroupVariants as componentsSwitchGroupVariants,
} from "../packages/vue/src";
describe("shared field style mappings", () => {
it("mirror the pinned upstream variant sets and defaults", () => {
expect(labelVariants()).toBe("hv-label");
expect(labelVariants({ isRequired: true })).toContain("hv-label--required");
expect(labelVariants({ isDisabled: true })).toContain("hv-label--disabled");
expect(labelVariants({ isInvalid: true })).toContain("hv-label--invalid");
expect(labelVariants({ isRequired: false })).toBe("hv-label");
expect(descriptionVariants()).toBe("hv-description");
expect(fieldErrorVariants()).toBe("hv-field-error");
expect(textFieldVariants()).toContain("hv-textfield");
expect(textFieldVariants()).not.toContain("full-width");
expect(textFieldVariants({ fullWidth: true })).toContain(
"hv-textfield--full-width",
);
const group = switchGroupVariants();
expect(group.base()).toContain("hv-switch-group--vertical");
expect(group.items()).toBe("hv-switch-group__items");
expect(switchGroupVariants({ orientation: "horizontal" }).base()).toContain(
"hv-switch-group--horizontal",
);
});
it("are re-exported unchanged from the components entry", () => {
expect(componentsLabelVariants).toBe(labelVariants);
expect(componentsDescriptionVariants).toBe(descriptionVariants);
expect(componentsFieldErrorVariants).toBe(fieldErrorVariants);
expect(componentsTextFieldVariants).toBe(textFieldVariants);
expect(componentsSwitchGroupVariants).toBe(switchGroupVariants);
});
it("components render exactly the shared mapping output for their states", () => {
const label = mount(Label, {
props: { isRequired: true, isInvalid: true },
slots: { default: "名称" },
attachTo: document.body,
});
expect(label.classes().sort()).toEqual(
labelVariants({ isRequired: true, isInvalid: true }).split(" ").sort(),
);
label.unmount();
const description = mount(Description, {
slots: { default: "说明" },
attachTo: document.body,
});
expect(description.classes()).toEqual(descriptionVariants().split(" "));
description.unmount();
const error = mount(FieldError, {
slots: { default: "错误" },
attachTo: document.body,
});
expect(error.classes()).toContain("hv-field-error");
error.unmount();
const group = mount(SwitchGroup, {
props: { orientation: "horizontal" },
slots: { default: "选项" },
attachTo: document.body,
});
const expected = switchGroupVariants({ orientation: "horizontal" });
expect(group.classes().sort()).toEqual(expected.base().split(" ").sort());
expect(group.find('[data-slot="switch-group-items"]').classes()).toEqual(
expected.items().split(" "),
);
group.unmount();
});
});