diff --git a/packages/kit/src/components/HoverCard/surface-attributes.ts b/packages/kit/src/components/HoverCard/surface-attributes.ts
new file mode 100644
index 00000000..55a495d3
--- /dev/null
+++ b/packages/kit/src/components/HoverCard/surface-attributes.ts
@@ -0,0 +1,39 @@
+type Handler = (event: Event) => void;
+
+function handlerOf(value: unknown): Handler | undefined {
+ if (typeof value === "function") return value as Handler;
+ // Solid's bound form: [handler, data] calls handler(data, event).
+ if (Array.isArray(value) && typeof value[0] === "function") {
+ const [handler, data] = value as [(data: unknown, event: Event) => void, unknown];
+ return event => handler(data, event);
+ }
+}
+
+/**
+ * Puts the active card's attributes and event handlers on the shared surface, and takes
+ * them off when another card takes the surface. Kobalte reads a spread once, so the
+ * surface cannot take them through JSX.
+ */
+export function applyAttributes(element: HTMLElement, entries: [string, unknown][]) {
+ const cleanups: (() => void)[] = [];
+ for (const [name, value] of entries) {
+ const handler = name.startsWith("on") ? handlerOf(value) : undefined;
+ if (handler) {
+ const type = name.slice(2).toLowerCase();
+ element.addEventListener(type, handler);
+ cleanups.push(() => element.removeEventListener(type, handler));
+ continue;
+ }
+ if (value == null || value === false || typeof value === "object") continue;
+ const previous = element.getAttribute(name);
+ element.setAttribute(name, value === true ? "" : String(value));
+ cleanups.push(() =>
+ previous === null
+ ? element.removeAttribute(name)
+ : element.setAttribute(name, previous),
+ );
+ }
+ return () => {
+ for (const cleanup of cleanups.reverse()) cleanup();
+ };
+}
diff --git a/packages/kit/test/hover-card-shared.test.tsx b/packages/kit/test/hover-card-shared.test.tsx
index 3d65dc29..4f43a0cb 100644
--- a/packages/kit/test/hover-card-shared.test.tsx
+++ b/packages/kit/test/hover-card-shared.test.tsx
@@ -68,8 +68,7 @@ it("moves the open card to the next trigger at once, as the same element", async
});
expect(cards()).toEqual([surface]);
expect(surface).not.toHaveTextContent("Details of A");
- expect(surface).toContainElement(document.getElementById("card-b"));
- expect(document.getElementById("card-a")).toBeNull();
+ expect(surface.id).toBe("card-b");
expect(onA).toHaveBeenLastCalledWith(false);
});
@@ -224,3 +223,36 @@ it("keeps a card open beside a card opened from inside it", async () => {
press("Escape");
await waitFor(() => expect(cards()).toEqual([outer]));
});
+
+it("puts the active card's attributes and handlers on the card itself", async () => {
+ const enterA = vi.fn();
+ const enterB = vi.fn();
+ render(() => (
+
+
+ Thread A
+
+ Details of A
+
+
+
+ Thread B
+ Details of B
+
+
+ ));
+ const surface = await openA();
+ expect(surface).toHaveAttribute("aria-label", "Card A");
+ expect(surface).toHaveAttribute("data-kind", "a");
+ fireEvent.pointerEnter(surface);
+ expect(enterA).toHaveBeenCalledTimes(1);
+ hover(screen.getByRole("link", { name: "Thread B" }));
+ await waitFor(() => expect(surface).toHaveTextContent("Details of B"));
+ expect(surface).not.toHaveAttribute("aria-label");
+ expect(surface).not.toHaveAttribute("data-kind");
+ fireEvent.pointerEnter(surface);
+ expect(enterB).toHaveBeenCalledTimes(1);
+ expect(enterA).toHaveBeenCalledTimes(1);
+});