fix: preserve ColorSwatch asChild ids and merged styles
This commit is contained in:
@@ -109,6 +109,8 @@ ColorSwatch 组件使用以下 CSS 类([查看源码样式](https://github.com
|
||||
|
||||
`class` 遵循 Vue 的 class 绑定,可接收字符串、数组和对象,并与组件基础类合并;不要使用 React 的 `className`。`color` 省略或为 `undefined` 时继承最近的共享颜色 owner;`null` 与空字符串是显式清空。无效颜色字符串按本库 `parseColor` 边界抛出 `RangeError`。
|
||||
|
||||
`style` 与 `styleFn` 可同时使用,合并顺序为组件基础样式 → `style` → `styleFn`;冲突属性由 `styleFn` 覆盖,普通样式不会因函数样式存在而丢失。
|
||||
|
||||
组件通过 `defineExpose` 暴露 `element`,指向实际根 DOM 元素;普通 `ref` 仍指向组件实例。`aria-roledescription` 与透明颜色的自动名称随 `ConfigProvider` 的 `locale` 切换;默认 `en-US`。
|
||||
|
||||
### Style Render
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
# 当前工作区交接
|
||||
|
||||
- 任务:无进行中任务;COLOR-SWATCH 已交付到任务分支。
|
||||
- 任务:无进行中任务;COLOR-SWATCH 两项复核问题已修复并交付到任务分支。
|
||||
- 工作区/分支:heroui-vue / feat/color-swatch-public(起点 60af6ee,未合 main、未 push)。
|
||||
- 结果入口:docs/tasks/results/COLOR-SWATCH.md。
|
||||
- 结果入口:docs/tasks/results/COLOR-SWATCH.md(含 CS-1/CS-2 返修记录)。
|
||||
- 下一步:由用户选择新任务;不自动开始下一任务。
|
||||
|
||||
这里只记录当前占用和接续入口,不重复任务状态表。单目录串行;新会话先核对git状态,有进行中任务时不切分支覆盖。支持范围统一见AGENTS.md。
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
| ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| 行为与共享 owner | `pnpm exec vitest run tests/color-swatch.test.ts tests/color-swatch-demos.test.ts tests/color-foundation.test.ts tests/color-picker-composition.test.ts` | 67 项通过;含默认/显式/空值/无效色/透明度/动态更新/灰色hue/精度/attrs/ref/插槽/as/asChild/style/styleFn |
|
||||
| 颜色协议回归 | 同一命令;另跑 `tests/color-field-protocol`、`color-input-foundation`、`color-picker-protocol`、`color-slider-foundation`、`regressions/color/*` | 10 个颜色套件 150 项通过;`Color` 构造与公共类型调整未改接受值语义 |
|
||||
| 全量客户端回归 | `pnpm test` | 88 文件 / 1293 项通过,无 skip;覆盖导航/总览新增分类 |
|
||||
| 全量客户端回归 | `pnpm test` | 89 文件 / 1295 项通过,无 skip;覆盖导航/总览新增分类与两条组合回归 |
|
||||
| 公开类型 | `pnpm typecheck` | 通过;正反例覆盖 shape/size/color/styleFn/ColorChannel/ColorSpace/ColorInput,无 unused `@ts-expect-error` |
|
||||
| 文档生产构建 | `pnpm build:docs` | 通过;7 例由同一 SFC 进入预览/高亮 |
|
||||
| 独立tarball | `node scripts/check-package.mjs color-swatch` | 通过:包根导出、`colorSwatchVariants`、CSS/棋盘/变体、`parseColor` alpha;strict NodeNext 与 bundler 契约通过;构建声明无 `reka-ui`/Wire 泄漏 |
|
||||
@@ -34,3 +34,32 @@
|
||||
- 未移植 React Stately 的色名表/`getColorName` 等全量 Color 辅助 API;无 `colorName` 时使用透明文案或 hex/hexa 作为自动描述,并已在文档记录。
|
||||
- 继承使用本库共享颜色 owner;ColorSwatchPicker 的 `ColorSwatchContext` 与其私有选择家族未迁移,当前公共消费者尚无公开 Picker owner 可组合。
|
||||
- `ColorSwatchPicker`、ColorArea/ColorField/ColorPicker/ColorSlider 等公共家族仍待后续任务;本提交不自动开始下一任务。
|
||||
|
||||
## 集成人复核(2026-10-26,270dea5)
|
||||
|
||||
结论:暂不通过,先修下面同一问题表,不安排新组件。本轮未修改实现。
|
||||
|
||||
| ID | 来源 | 问题 / 复现 | 当前证据 |
|
||||
| ---- | --------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------ |
|
||||
| CS-1 | 原组合契约遗漏 | ColorSwatch asChild,外层aria-labelledby="caption",插槽根id="child-id";Reka保留子元素ID,但组件仍输出自身生成ID作为aria-labelledby前缀,引用不存在的节点。动态更换子ID也须保持关联。 | tests/regressions/color/swatch-composition.test.ts 第一例失败:实际v-0 caption,DOM id为child-id |
|
||||
| CS-2 | Vue样式适配回归 | 模板同时传:style="{outline:'2px solid red'}"与:style-fn="()=>({opacity:0.5})",styleFn分支直接忽略标准style;两者声明均允许且没有互斥说明,调用方普通样式丢失。普通样式应与计算样式保留,并明确冲突属性优先级。 | 同文件第二例失败:outline为空;预期普通outline和函数opacity都保留 |
|
||||
|
||||
原4套/67项通过;新增2项均复现失败。保留这两项作为后续增量复核,不再按返修轮次新增探针或重复全库检查。CS-1修复需使用Vue公开VNode/透传能力,不能通过读取DOM后伪造ARIA或侵入私有ref结构;保留子元素/调用方ID、ref和动态更新。CS-2不需要新增另一种style API。
|
||||
|
||||
Color.toWire目前仍出现在公开class声明中,虽然没有泄漏Reka具名类型;它是既有桥接函数。请顺带核对是否应标为内部,而不要将其描述为完整RAC Color接口或为此重写颜色模型。本项为API边界提示,不新增本轮能力要求。
|
||||
|
||||
## 返修结果(CS-1 / CS-2)
|
||||
|
||||
- 实施:在当前 `feat/color-swatch-public` 分支修复;准确 SHA 见本文件所在提交。
|
||||
- CS-1:改为一次读取默认作用域插槽的公开 VNode,取唯一有效根元素的 `id` 作为 `aria-labelledby` 前缀;优先级为子元素 `id` → 调用方 `id` → 生成 id,和 Reka Slot 的实际合并规则一致。未读取 DOM 后伪造 ARIA,未访问私有 ref 结构;子元素 id 动态更新、普通 `attrs`、`ref` 仍由公开 VNode/透传路径保留。复核用例 `tests/regressions/color/swatch-composition.test.ts` 第一例通过。
|
||||
- CS-2:普通 `style` 与 `styleFn` 同时参与渲染,合并顺序为基础样式 → `style` → `styleFn`;冲突属性由 `styleFn` 覆盖。文档已明确该顺序。复核用例第二例通过。
|
||||
- `Color.toWire` 仍作为既有内部桥接保留在类上并标注 `@internal`;它返回本库 `ColorInput`,不泄漏 Reka 命名类型,也未新增为文档化 RAC API。
|
||||
|
||||
| 检查 | 结果 |
|
||||
| --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| 两条组合回归 | `pnpm exec vitest run tests/regressions/color/swatch-composition.test.ts`:2/2 通过 |
|
||||
| Swatch 定向套件 | `tests/color-swatch.test.ts`、`tests/color-swatch-demos.test.ts`:27/27 通过;任务卡五文件(含 foundation/picker 与回归)69/69 通过 |
|
||||
| 全量回归 | `pnpm test`:89 文件 / 1295 项通过,无 skip |
|
||||
| 类型与文档构建 | `pnpm typecheck`、`pnpm build:docs` 通过 |
|
||||
| 独立产物 | `node scripts/check-package.mjs color-swatch` 通过;最终声明仍暴露 `element` 且无 Reka/Wire 泄漏 |
|
||||
| 浏览器复验 | 5185 重新打开 `/components/color-swatch`:7 个预览、尺寸/圆角/透明度/棋盘、styleFn 发光/渐变、asChild `data-custom` 均正常;无控制台错误与横向溢出 |
|
||||
|
||||
@@ -1,13 +1,21 @@
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
Comment,
|
||||
Fragment,
|
||||
Text,
|
||||
computed,
|
||||
getCurrentInstance,
|
||||
h,
|
||||
isVNode,
|
||||
onBeforeUpdate,
|
||||
shallowRef,
|
||||
useAttrs,
|
||||
useId,
|
||||
useSlots,
|
||||
type CSSProperties,
|
||||
type StyleValue,
|
||||
type VNode,
|
||||
type VNodeChild,
|
||||
} from "vue";
|
||||
import { Primitive } from "reka-ui";
|
||||
import {
|
||||
@@ -57,6 +65,7 @@ const props = withDefaults(
|
||||
defineSlots<ColorSwatchSlots>();
|
||||
|
||||
const attrs = useAttrs();
|
||||
const slots = useSlots();
|
||||
const instance = getCurrentInstance()!;
|
||||
const owner = useColorStateContext();
|
||||
const config = useConfig();
|
||||
@@ -64,6 +73,30 @@ const generatedId = useId();
|
||||
const element = shallowRef<HTMLElement>();
|
||||
const TRANSPARENT = parseColor("#fff0");
|
||||
|
||||
function meaningfulVNodes(children: unknown): VNode[] {
|
||||
const out: VNode[] = [];
|
||||
const walk = (nodes: unknown[]) => {
|
||||
for (const node of nodes) {
|
||||
if (node == null || node === false || node === true) continue;
|
||||
if (Array.isArray(node)) {
|
||||
walk(node);
|
||||
continue;
|
||||
}
|
||||
if (!isVNode(node)) continue;
|
||||
if (node.type === Fragment) {
|
||||
walk(Array.isArray(node.children) ? node.children : []);
|
||||
continue;
|
||||
}
|
||||
if (node.type === Comment) continue;
|
||||
if (node.type === Text && String(node.children ?? "").trim() === "")
|
||||
continue;
|
||||
out.push(node);
|
||||
}
|
||||
};
|
||||
walk([children]);
|
||||
return out;
|
||||
}
|
||||
|
||||
function presentsColor() {
|
||||
const raw = instance.vnode.props ?? {};
|
||||
// `undefined` behaves as omitted so spread bindings can inherit; an explicit
|
||||
@@ -81,7 +114,6 @@ const resolvedColor = computed(() => {
|
||||
return candidate == null || candidate === "" ? null : copyColor(candidate);
|
||||
});
|
||||
const displayColor = computed(() => resolvedColor.value ?? TRANSPARENT);
|
||||
const renderProps = computed(() => ({ color: displayColor.value }));
|
||||
const classes = computed(() =>
|
||||
colorSwatchVariants({ shape: props.shape, size: props.size }),
|
||||
);
|
||||
@@ -96,12 +128,15 @@ const baseStyle = computed<CSSProperties>(() => {
|
||||
} as CSSProperties;
|
||||
});
|
||||
|
||||
const userStyle = computed<StyleValue | undefined>(() => {
|
||||
if (props.styleFn) return props.styleFn({ color: displayColor.value });
|
||||
return typeof props.style === "function"
|
||||
? props.style({ color: displayColor.value })
|
||||
: props.style;
|
||||
const ordinaryStyle = computed<StyleValue | undefined>(() => {
|
||||
const value = props.style;
|
||||
return typeof value === "function"
|
||||
? value({ color: displayColor.value })
|
||||
: value;
|
||||
});
|
||||
const computedStyle = computed<StyleValue | undefined>(() =>
|
||||
props.styleFn?.({ color: displayColor.value }),
|
||||
);
|
||||
|
||||
function accessibleName() {
|
||||
if (props.colorName) return props.colorName;
|
||||
@@ -112,10 +147,6 @@ function accessibleName() {
|
||||
);
|
||||
}
|
||||
|
||||
function resolvedId() {
|
||||
return attrs.id == null ? generatedId : String(attrs.id);
|
||||
}
|
||||
|
||||
function ariaLabel() {
|
||||
const explicit = attrs["aria-label"];
|
||||
return [accessibleName(), explicit == null ? "" : String(explicit)]
|
||||
@@ -123,17 +154,19 @@ function ariaLabel() {
|
||||
.join(", ");
|
||||
}
|
||||
|
||||
function ariaLabelledBy() {
|
||||
const labelled = attrs["aria-labelledby"];
|
||||
return labelled ? `${resolvedId()} ${String(labelled)}` : undefined;
|
||||
}
|
||||
|
||||
function roleDescription() {
|
||||
return locale.value.toLowerCase().startsWith("zh")
|
||||
? "颜色色板"
|
||||
: "color swatch";
|
||||
}
|
||||
|
||||
/** Reka Slot keeps a child's own ID over merged attrs; mirror that root ID. */
|
||||
function asChildRootId(children: VNodeChild) {
|
||||
const root = meaningfulVNodes(children)[0];
|
||||
const id = root?.props?.id;
|
||||
return id == null ? undefined : String(id);
|
||||
}
|
||||
|
||||
function setRoot(node: unknown) {
|
||||
const next =
|
||||
node instanceof HTMLElement
|
||||
@@ -142,24 +175,41 @@ function setRoot(node: unknown) {
|
||||
if (element.value !== next) element.value = next;
|
||||
}
|
||||
|
||||
/** Functional render layer so the default slot is read once per render. */
|
||||
const Render = () => {
|
||||
const children = slots.default?.({ color: displayColor.value }) as
|
||||
VNodeChild | undefined;
|
||||
const childId = props.asChild ? asChildRootId(children) : undefined;
|
||||
const effectiveId =
|
||||
childId ?? (attrs.id == null ? generatedId : String(attrs.id));
|
||||
const labelledBy = attrs["aria-labelledby"];
|
||||
|
||||
return h(
|
||||
Primitive,
|
||||
{
|
||||
...attrs,
|
||||
as: props.as,
|
||||
asChild: props.asChild,
|
||||
id: effectiveId,
|
||||
role: "img",
|
||||
"aria-roledescription": roleDescription(),
|
||||
"aria-label": ariaLabel(),
|
||||
"aria-labelledby": labelledBy
|
||||
? `${effectiveId} ${String(labelledBy)}`
|
||||
: undefined,
|
||||
// Ordinary styles remain; styleFn is the computed override layer.
|
||||
class: [classes.value, attrs.class],
|
||||
style: [baseStyle.value, ordinaryStyle.value, computedStyle.value],
|
||||
"data-slot": "color-swatch",
|
||||
ref: setRoot,
|
||||
},
|
||||
{ default: () => children },
|
||||
);
|
||||
};
|
||||
|
||||
defineExpose({ element });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Primitive
|
||||
:ref="setRoot"
|
||||
v-bind="attrs"
|
||||
:as="as"
|
||||
:as-child="asChild"
|
||||
:class="[classes, attrs.class]"
|
||||
:style="[baseStyle, attrs.style, userStyle]"
|
||||
:id="resolvedId()"
|
||||
role="img"
|
||||
:aria-roledescription="roleDescription()"
|
||||
:aria-label="ariaLabel()"
|
||||
:aria-labelledby="ariaLabelledBy()"
|
||||
data-slot="color-swatch"
|
||||
>
|
||||
<slot v-bind="renderProps" />
|
||||
</Primitive>
|
||||
<component :is="Render" />
|
||||
</template>
|
||||
|
||||
@@ -128,7 +128,7 @@ export class Color {
|
||||
: raw,
|
||||
);
|
||||
}
|
||||
/** Internal conversion bridge, omitted from the public Color instance type. */
|
||||
/** @internal Conversion bridge for library keying and tests. */
|
||||
static toWire(value: Color): ColorInput {
|
||||
return { ...value.#value };
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import { afterEach, expect, it } from "vitest";
|
||||
import { mount } from "@vue/test-utils";
|
||||
import { defineComponent, h, ref, nextTick } from "vue";
|
||||
import { ColorSwatch } from "../../../packages/vue/src";
|
||||
|
||||
const wrappers: ReturnType<typeof mount>[] = [];
|
||||
afterEach(() => {
|
||||
wrappers.splice(0).forEach((w) => w.unmount());
|
||||
document.body.innerHTML = "";
|
||||
});
|
||||
it("keeps asChild self-labelledby attached to the actual root ID", async () => {
|
||||
const id = ref("child-id");
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
setup: () => () =>
|
||||
h("div", [
|
||||
h("span", { id: "caption" }, "Brand"),
|
||||
h(
|
||||
ColorSwatch,
|
||||
{ asChild: true, color: "#0485F7", "aria-labelledby": "caption" },
|
||||
{
|
||||
default: () => h("div", { id: id.value }),
|
||||
},
|
||||
),
|
||||
]),
|
||||
}),
|
||||
{ attachTo: document.body },
|
||||
);
|
||||
wrappers.push(w);
|
||||
const root = w.get("[data-slot=color-swatch]");
|
||||
expect(root.attributes("aria-labelledby")).toBe(
|
||||
`${root.attributes("id")} caption`,
|
||||
);
|
||||
id.value = "next-child-id";
|
||||
await nextTick();
|
||||
expect(root.attributes("aria-labelledby")).toBe(
|
||||
`${root.attributes("id")} caption`,
|
||||
);
|
||||
});
|
||||
it("retains ordinary template styles together with styleFn", () => {
|
||||
const w = mount(
|
||||
defineComponent({
|
||||
components: { ColorSwatch },
|
||||
template: `<ColorSwatch color="#0485F7" :style="{ outline: '2px solid red' }" :style-fn="() => ({ opacity: 0.5 })" />`,
|
||||
}),
|
||||
);
|
||||
wrappers.push(w);
|
||||
const style = (w.element as HTMLElement).style;
|
||||
expect(style.outline).toBe("2px solid red");
|
||||
expect(style.opacity).toBe("0.5");
|
||||
});
|
||||
Reference in New Issue
Block a user