fix: retain fallback ColorSwatch ids on slot roots

This commit is contained in:
maoyingjie
2026-10-26 13:43:39 +08:00
parent b18949c071
commit d74ad1b711
5 changed files with 28 additions and 13 deletions
@@ -3,6 +3,7 @@ import {
Comment,
Fragment,
Text,
cloneVNode,
computed,
getCurrentInstance,
h,
@@ -160,13 +161,6 @@ function roleDescription() {
: "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
@@ -177,11 +171,18 @@ function setRoot(node: unknown) {
/** Functional render layer so the default slot is read once per render. */
const Render = () => {
const children = slots.default?.({ color: displayColor.value }) as
let children = slots.default?.({ color: displayColor.value }) as
VNodeChild | undefined;
const childId = props.asChild ? asChildRootId(children) : undefined;
const childNodes = props.asChild ? meaningfulVNodes(children) : undefined;
const childId = childNodes?.[0]?.props?.id;
const effectiveId =
childId ?? (attrs.id == null ? generatedId : String(attrs.id));
// Slot gives child props precedence, including an explicit undefined ID.
// Put the resolved ID on that VNode too, preserving Vue's ref and key.
if (childNodes?.length) {
childNodes[0] = cloneVNode(childNodes[0]!, { id: effectiveId });
children = childNodes;
}
const labelledBy = attrs["aria-labelledby"];
return h(