Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .changeset/hover-card-shared-surface.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions apps/docs/content/docs/(kit)/components/hover-card.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

Expand Down Expand Up @@ -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.

<PropsTable name="HoverCardContent" />

Expand Down
9 changes: 5 additions & 4 deletions packages/kit/api/kit.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4119,10 +4119,9 @@ export type HoverCardContentProps = Omit<JSX.HTMLAttributes<HTMLDivElement>, "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<HTMLUListElement> & {
Expand Down Expand Up @@ -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<string, unknown>;
};
export type Point = {
x: number;
Expand Down
16 changes: 7 additions & 9 deletions packages/kit/src/components/HoverCard/HoverCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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),
Expand All @@ -396,11 +396,9 @@ export function HoverCardContent(props: HoverCardContentProps) {
<Show when={card.slot()}>
{slot => (
<Portal mount={slot()}>
<div {...attributes} style={{ display: "contents" }}>
<HoverCardHostProvider host={card.nested}>
{props.children}
</HoverCardHostProvider>
</div>
<HoverCardHostProvider host={card.nested}>
{props.children}
</HoverCardHostProvider>
</Portal>
)}
</Show>
Expand Down
13 changes: 13 additions & 0 deletions packages/kit/src/components/HoverCard/HoverCardHost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<string, unknown>;
};

export type Point = { x: number; y: number };
Expand Down Expand Up @@ -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 (
<Show when={popover.contentPresent()}>
<HydratablePortal mount={mount()}>
Expand Down
39 changes: 39 additions & 0 deletions packages/kit/src/components/HoverCard/surface-attributes.ts
Original file line number Diff line number Diff line change
@@ -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();
};
}
36 changes: 34 additions & 2 deletions packages/kit/test/hover-card-shared.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down Expand Up @@ -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(() => (
<div
style={{ display: "flex", "flex-direction": "column", gap: "8px", width: "160px" }}
>
<HoverCard openDelay={0}>
<HoverCardTrigger href="#a">Thread A</HoverCardTrigger>
<HoverCardContent aria-label="Card A" data-kind="a" onPointerEnter={enterA}>
Details of A
</HoverCardContent>
</HoverCard>
<HoverCard openDelay={0}>
<HoverCardTrigger href="#b">Thread B</HoverCardTrigger>
<HoverCardContent onPointerEnter={enterB}>Details of B</HoverCardContent>
</HoverCard>
</div>
));
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);
});
Loading