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
5 changes: 5 additions & 0 deletions .changeset/tooltip-empty-content.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@codeui/kit": patch
---

Tooltip no longer opens an empty bubble when `content` is `undefined`, `null` or `""`. Hover and focus leave it closed, and the trigger gets no `aria-describedby`, so passing `content={undefined}` turns the tooltip off. The trigger and its children render as before.
3 changes: 3 additions & 0 deletions .claude/skills/verify-codeui/features/feedback.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ Tooltip shows a short label next to its trigger on hover and should also show on
- `tooltip-dismiss` hides it on Escape and when focus or the pointer leaves.
- `tooltip-variants` renders primary, secondary, disabled and custom JSX content.
- `tooltip-motion` pops on hover; keyboard focus opens it with `data-motion="none"`, and the focused child (not the wrapper) carries `aria-describedby`.
- `tooltip-empty` never opens when `content` is `undefined`, `null` or `""`, on hover or focus, and the focused trigger gets no `aria-describedby`.
- `toast-rise` (Toaster) renders in the inverted theme (`data-cui-theme` opposite the page) and sits 80px above the bottom of its region's container.

## How to get to it (user POV)

- Hover or Tab to `Hover me` in `designsystem-tooltip--tooltip-story`; also `--tooltip-secondary`, `--tooltip-disabled`, `--custom-jsx-component`.
- Hover or Tab through `Undefined`, `Null`, `Empty string` and `With content` in `designsystem-tooltip--empty-content`.

## Driving it with drive.mjs

Expand All @@ -24,6 +26,7 @@ Preconditions:
- **Hover.** `page.getByRole('button', { name: 'Hover me' }).hover()`, wait ~1 s for the open delay, then `getByRole('tooltip')` count is 1. Observed in a probe on this base.
- **Focus and Escape.** Run `node .claude/skills/verify-codeui/scripts/drive.mjs designsystem-tooltip--tooltip-story --journey .claude/skills/verify-codeui/journeys/tooltip-keyboard.mjs`. Tab opens the tooltip. Escape closes it and focus stays on `Hover me`.
- **Motion and description.** `drive.mjs designsystem-tooltip--tooltip-story --journey .claude/skills/verify-codeui/journeys/tooltip-motion.mjs`.
- **Empty content.** `drive.mjs designsystem-tooltip--empty-content --journey .claude/skills/verify-codeui/journeys/tooltip-empty.mjs`.
- **Toast.** `drive.mjs designsystem-toaster--with-action --journey .claude/skills/verify-codeui/journeys/toast-rise.mjs`.
- **Dismiss.** After hover, press Escape; expect count 0.
- **Theme.** Screenshot the open tooltip in both themes (`step('hovered')`).
Expand Down
69 changes: 69 additions & 0 deletions .claude/skills/verify-codeui/journeys/tooltip-empty.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Tooltip: empty content (undefined, null, "") never opens on hover or focus and adds no aria-describedby;
// a sibling tooltip with content still opens on both.
export default async ({ page, step, check, focus, axe }) => {
const tooltip = page.getByRole("tooltip");
const empties = ["Undefined", "Null", "Empty string"];
const bubbles = () => page.locator("[data-cui=tooltip-content]").count();

for (const name of empties) {
await step(`hover ${name}`, async () => {
await page.getByRole("button", { name }).hover();
await page.waitForTimeout(1000);
});
await check(`hover ${name}: no bubble`, async () => ({
pass: (await bubbles()) === 0,
count: await bubbles(),
}));
await page.mouse.move(1200, 850);
}

for (const [i, name] of empties.entries()) {
await step(`Tab to ${name}`, async () => {
await page.keyboard.press("Tab");
await page.waitForTimeout(1000);
});
await check(`focus ${name}: no bubble, no aria-describedby`, async () => {
const f = await focus();
const describedBy = await page.evaluate(() =>
document.activeElement.getAttribute("aria-describedby"),
);
const count = await bubbles();
return {
pass: f.name === name && count === 0 && describedBy === null,
focus: f,
count,
describedBy,
};
});
}
await axe("empty-focused");

await step("Tab to With content", async () => {
await page.keyboard.press("Tab");
await tooltip.waitFor();
});
await check("focus With content: bubble opens and describes the button", async () => {
const id = await tooltip.getAttribute("id");
const own = await page.evaluate(() =>
document.activeElement.getAttribute("aria-describedby"),
);
const text = await tooltip.textContent();
return {
pass: own === id && text.includes("My tooltip content."),
id,
own,
text,
focus: await focus(),
};
});
await step("Escape, blur, hover With content", async () => {
await page.keyboard.press("Escape");
await page.mouse.click(5, 5);
await page.waitForTimeout(400);
await page.getByRole("button", { name: "With content" }).hover();
await tooltip.waitFor();
});
await check("hover With content: bubble opens", async () => ({
pass: (await tooltip.isVisible()) && (await bubbles()) === 1,
}));
};
1 change: 1 addition & 0 deletions packages/kit/api/kit.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5243,6 +5243,7 @@ import type { SlotProp } from "../../utils/component.js";
import type { TooltipVariants } from "./Tooltip.css.js";
type TooltipSlot = "trigger" | "content";
export type TooltipProps = P.RootProps & TooltipVariants & SlotProp<TooltipSlot> & {
/** Tooltip body. When `undefined`, `null`, `false` or `""`, the tooltip never opens. */
content: JSX.Element;
/** Portal target for the content. Defaults to `document.body`. */
mount?: globalThis.Element;
Expand Down
24 changes: 17 additions & 7 deletions packages/kit/src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { omit } from "solid-js";
import { omit, Show } from "solid-js";
import { type JSX } from "@solidjs/web";
import * as P from "../../parts/tooltip";
import type { SlotProp } from "../../utils/component";
Expand All @@ -9,6 +9,7 @@ type TooltipSlot = "trigger" | "content";
export type TooltipProps = P.RootProps &
TooltipVariants &
SlotProp<TooltipSlot> & {
/** Tooltip body. When `undefined`, `null`, `false` or `""`, the tooltip never opens. */
content: JSX.Element;
/** Portal target for the content. Defaults to `document.body`. */
mount?: globalThis.Element;
Expand All @@ -17,14 +18,23 @@ export type TooltipProps = P.RootProps &
export function Tooltip(props: TooltipProps) {
const others = omit(props, "variant", "slotClasses", "mount", "children", "content");

// Empty content turns the tooltip off: Kobalte's `disabled` stops hover and focus from opening it,
// so the trigger never gets an `aria-describedby` that points at a missing bubble.
const hasContent = () => {
const content = props.content;
return content != null && content !== false && content !== "";
};

return (
<P.Root {...others}>
<P.Root {...others} disabled={props.disabled || !hasContent()}>
<P.Trigger class={props.slotClasses?.trigger}>{props.children}</P.Trigger>
<P.Portal mount={props.mount}>
<P.Content variant={props.variant} class={props.slotClasses?.content}>
{props.content}
</P.Content>
</P.Portal>
<Show when={hasContent()}>
<P.Portal mount={props.mount}>
<P.Content variant={props.variant} class={props.slotClasses?.content}>
{props.content}
</P.Content>
</P.Portal>
</Show>
</P.Root>
);
}
20 changes: 20 additions & 0 deletions packages/kit/test/tooltip-keyboard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,23 @@ it("opens on child keyboard focus and closes on Escape without moving focus", as
await expectDismissed(tooltip);
expect(document.activeElement).toBe(button);
});

it.each([
["undefined", undefined],
["null", null],
["an empty string", ""],
])("never opens and adds no description when content is %s", async (_, content) => {
render(() => (
<Tooltip content={content} slotClasses={{ trigger: "my-trigger" }}>
<button>Copy</button>
</Tooltip>
));
const button = screen.getByRole("button", { name: "Copy" });
expect(button.parentElement).toHaveClass("my-trigger");
button.focus();
await tick();
await new Promise(resolve => setTimeout(resolve, 300));
expect(screen.queryByRole("tooltip")).toBeNull();
expect(document.querySelector("[data-cui=tooltip-content]")).toBeNull();
expect(button).not.toHaveAttribute("aria-describedby");
});
23 changes: 23 additions & 0 deletions packages/storybook/src/stories/Tooltip.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,26 @@ export const CustomJsxComponent: Story = {
</Tooltip>
),
};

export const EmptyContent: Story = {
name: "Empty content",
args: {
content: undefined,
},
render: () => (
<div style={{ display: "flex", gap: "12px" }}>
<Tooltip content={undefined}>
<Button variant="secondary">Undefined</Button>
</Tooltip>
<Tooltip content={null}>
<Button variant="secondary">Null</Button>
</Tooltip>
<Tooltip content="">
<Button variant="secondary">Empty string</Button>
</Tooltip>
<Tooltip content="My tooltip content.">
<Button variant="primary">With content</Button>
</Tooltip>
</div>
),
};
Loading