From ec9472f74d0c8246bf9ea7a08bc64a4943f01627 Mon Sep 17 00:00:00 2001 From: riccardoperra Date: Sat, 28 Nov 2026 13:48:52 +0100 Subject: [PATCH] fix(kit): put HoverCardContent attributes and handlers on the card Since the shared card, HoverCardContent put attributes and event handlers on a display: contents wrapper around the children. A pointerleave handler fired as soon as the pointer moved from the content onto the card's padding, and a mousedown handler missed the padding. The active card's attributes and handlers now go on the card itself, applied when it takes the card and removed when another card does. Co-Authored-By: Claude Opus 5.5 --- .changeset/hover-card-shared-surface.md | 2 +- .../docs/(kit)/components/hover-card.mdx | 4 +- packages/kit/api/kit.api.md | 9 +++-- .../src/components/HoverCard/HoverCard.tsx | 16 ++++---- .../components/HoverCard/HoverCardHost.tsx | 13 +++++++ .../HoverCard/surface-attributes.ts | 39 +++++++++++++++++++ packages/kit/test/hover-card-shared.test.tsx | 36 ++++++++++++++++- 7 files changed, 101 insertions(+), 18 deletions(-) create mode 100644 packages/kit/src/components/HoverCard/surface-attributes.ts diff --git a/.changeset/hover-card-shared-surface.md b/.changeset/hover-card-shared-surface.md index 313a3b14..11f39289 100644 --- a/.changeset/hover-card-shared-surface.md +++ b/.changeset/hover-card-shared-surface.md @@ -7,7 +7,7 @@ Hover cards share one card and move it between triggers. - All the hover cards of a page share one card. Moving the pointer or keyboard focus to another trigger while a card is open moves that card at once: it slides to the new trigger, resizes to the new content, and fades the new content in. For 400ms after a card closes, the next one opens at once and without the pop. The move lasts 200ms (`--cui-motion-duration-panel`) with the kit's ease-out. While the pointer is on its way to the open card, another trigger it crosses takes the card only after 150ms of rest. A move from the keyboard swaps the content in place with no motion; with reduced motion only the new content fades in. - `HoverCardGroup` gives the cards inside it a card of their own, with `skipDelayDuration` for the skip window. `shared={false}` gives one `HoverCard` its own surface; `forceMount` implies it. -- `HoverCardContent` is no longer polymorphic. Its `class`, `classes`, `style`, `ref`, `mount` and dismiss handlers go on the card; other attributes and event handlers go on a `display: contents` element around the children. +- `HoverCardContent` is no longer polymorphic. Its props (`class`, `classes`, `style`, `ref`, `mount`, dismiss handlers, and any other attribute or event handler) go on the shared card while that card shows it, and come off when another card takes it over. - `HoverCard` drops the Kobalte root options it never used (`id`). The trigger renders without a Kobalte popover anchor and still sets `data-expanded` and `data-closed`. - Overlays opened by hover tell pointer from keyboard more reliably: the input tracker also records `pointerover`, which the browser fires before the `pointermove` that crosses into an element. - A hover card opened from inside another card's content gets a surface of its own. The outer card stays open while the pointer is on the inner card and until it closes, and a press on the inner card does not close it. diff --git a/apps/docs/content/docs/(kit)/components/hover-card.mdx b/apps/docs/content/docs/(kit)/components/hover-card.mdx index 2d1d7965..cc4040f0 100644 --- a/apps/docs/content/docs/(kit)/components/hover-card.mdx +++ b/apps/docs/content/docs/(kit)/components/hover-card.mdx @@ -30,7 +30,7 @@ Put `HoverCardTrigger` and `HoverCardContent` in `HoverCard`. Hover the link, or | `HoverCardGroup` | Nothing. Gives the cards inside it a surface of their own, optional | none | none | none | | `HoverCard` | Nothing. Holds the open state and the timers | none | none | none | | `HoverCardTrigger` | `a`, or the element passed to `as` | none | `hovercard-trigger` | Link | -| `HoverCardContent` | The shared card, a `div` in a portal on `mount` or `document.body`, and a `display: contents` `div` around the children | `content` | `hovercard-content` | none | +| `HoverCardContent` | The shared card, a `div` in a portal on `mount` or `document.body` | `content` | `hovercard-content` | none | | `HoverCardList` | `ul`, optional | none | `hovercard-list` | List | | `HoverCardItem` | `li`, with an icon slot, an identifier and a title | `item`, `itemIcon`, `itemId`, `itemTitle` | `hovercard-item`, `hovercard-item-icon`, `hovercard-item-id`, `hovercard-item-title` | List item | @@ -128,7 +128,7 @@ A trigger that renders an `a`, alone or through `as={Link}`, has a dotted underl ### `HoverCardContent` -Renders its children into the shared card, which is portaled to `document.body` and placed next to the trigger. While this card is the one shown, its `class`, `classes`, `style`, `ref`, `mount` and dismiss handlers go on the card. Any other attribute or event handler, such as `id`, `aria-hidden` or `onPointerEnter`, goes on a `display: contents` element around the children. +Renders its children into the shared card, which is portaled to `document.body` and placed next to the trigger. While this card is the one shown, its props go on the card: `class`, `classes`, `style`, `ref`, `mount`, the dismiss handlers, and any other attribute or event handler, such as `id`, `aria-hidden` or `onPointerEnter`. When another card takes the card over, they come off and that card's props go on. Attributes take string, number or boolean values. diff --git a/packages/kit/api/kit.api.md b/packages/kit/api/kit.api.md index 8fbba078..aa69ee2a 100644 --- a/packages/kit/api/kit.api.md +++ b/packages/kit/api/kit.api.md @@ -4119,10 +4119,9 @@ export type HoverCardContentProps = Omit, "st /** * The card's content. It renders into the surface its `HoverCard` shares, which is portaled, * placed beside the trigger (on the right unless there is no room), and never takes focus. - * Its class, style, ref and dismiss handlers go on that surface while this card is the one - * shown. Other attributes and event handlers go on a `display: contents` element that wraps - * the children inside it. Hover cards inside the content get a surface of their own, so the - * card stays open beside them. + * Its class, style, ref, attributes and event handlers go on that surface while this card is + * the one shown. Hover cards inside the content get a surface of their own, so the card stays + * open beside them. */ export declare function HoverCardContent(props: HoverCardContentProps): JSX.Element; export type HoverCardListProps = JSX.HTMLAttributes & { @@ -4168,6 +4167,8 @@ export type HoverCardSurfaceOptions = HoverCardDismissHandlers & { style?: JSX.CSSProperties | string; mount?: globalThis.Element; ref: (element: HTMLElement) => void; + /** Every other prop of `HoverCardContent`: attributes and event handlers. */ + attributes: Record; }; export type Point = { x: number; diff --git a/packages/kit/src/components/HoverCard/HoverCard.tsx b/packages/kit/src/components/HoverCard/HoverCard.tsx index acd5cb4f..4c9f518b 100644 --- a/packages/kit/src/components/HoverCard/HoverCard.tsx +++ b/packages/kit/src/components/HoverCard/HoverCard.tsx @@ -352,10 +352,9 @@ function applyRef(ref: unknown, element: HTMLElement) { /** * The card's content. It renders into the surface its `HoverCard` shares, which is portaled, * placed beside the trigger (on the right unless there is no room), and never takes focus. - * Its class, style, ref and dismiss handlers go on that surface while this card is the one - * shown. Other attributes and event handlers go on a `display: contents` element that wraps - * the children inside it. Hover cards inside the content get a surface of their own, so the - * card stays open beside them. + * Its class, style, ref, attributes and event handlers go on that surface while this card is + * the one shown. Hover cards inside the content get a surface of their own, so the card stays + * open beside them. */ export function HoverCardContent(props: HoverCardContentProps) { const card = useHoverCard(); @@ -386,6 +385,7 @@ export function HoverCardContent(props: HoverCardContentProps) { return props.mount; }, ref: element => applyRef(props.ref, element), + attributes, onEscapeKeyDown: event => props.onEscapeKeyDown?.(event), onPointerDownOutside: event => props.onPointerDownOutside?.(event), onFocusOutside: event => props.onFocusOutside?.(event), @@ -396,11 +396,9 @@ export function HoverCardContent(props: HoverCardContentProps) { {slot => ( -
- - {props.children} - -
+ + {props.children} +
)}
diff --git a/packages/kit/src/components/HoverCard/HoverCardHost.tsx b/packages/kit/src/components/HoverCard/HoverCardHost.tsx index 8338818f..c5eaa9bd 100644 --- a/packages/kit/src/components/HoverCard/HoverCardHost.tsx +++ b/packages/kit/src/components/HoverCard/HoverCardHost.tsx @@ -23,6 +23,7 @@ import { LayerScope } from "../../utils/layer-scope"; import { pointerInitiated } from "../../utils/open-source"; import { createPopMotion, motionRef, PopMotionContext } from "../../utils/pop-motion"; import * as styles from "./HoverCard.css"; +import { applyAttributes } from "./surface-attributes"; export type HoverCardPositioning = Pick< KPopoverRootOptions, @@ -53,6 +54,8 @@ export type HoverCardSurfaceOptions = HoverCardDismissHandlers & { style?: JSX.CSSProperties | string; mount?: globalThis.Element; ref: (element: HTMLElement) => void; + /** Every other prop of `HoverCardContent`: attributes and event handlers. */ + attributes: Record; }; export type Point = { x: number; y: number }; @@ -364,6 +367,16 @@ function SurfaceContent(props: { host: HoverCardHost }) { if (element && ref) ref(element); }, ); + createEffect( + () => { + const element = host.surface(); + const attributes = options()?.attributes; + return element && attributes + ? { element, entries: Object.entries(attributes) } + : undefined; + }, + applied => (applied ? applyAttributes(applied.element, applied.entries) : undefined), + ); return ( 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); +});