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
+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;