diff --git a/.changeset/tooltip-empty-content.md b/.changeset/tooltip-empty-content.md new file mode 100644 index 00000000..428235fc --- /dev/null +++ b/.changeset/tooltip-empty-content.md @@ -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. diff --git a/.claude/skills/verify-codeui/features/feedback.md b/.claude/skills/verify-codeui/features/feedback.md index bd0696ba..ba3b38ba 100644 --- a/.claude/skills/verify-codeui/features/feedback.md +++ b/.claude/skills/verify-codeui/features/feedback.md @@ -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 @@ -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')`). diff --git a/.claude/skills/verify-codeui/journeys/tooltip-empty.mjs b/.claude/skills/verify-codeui/journeys/tooltip-empty.mjs new file mode 100644 index 00000000..fecafb6f --- /dev/null +++ b/.claude/skills/verify-codeui/journeys/tooltip-empty.mjs @@ -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, + })); +}; diff --git a/packages/kit/api/kit.api.md b/packages/kit/api/kit.api.md index aa69ee2a..4e204e78 100644 --- a/packages/kit/api/kit.api.md +++ b/packages/kit/api/kit.api.md @@ -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 & { + /** 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; diff --git a/packages/kit/src/components/Tooltip/Tooltip.tsx b/packages/kit/src/components/Tooltip/Tooltip.tsx index 0197e8f9..9d21b02d 100644 --- a/packages/kit/src/components/Tooltip/Tooltip.tsx +++ b/packages/kit/src/components/Tooltip/Tooltip.tsx @@ -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"; @@ -9,6 +9,7 @@ type TooltipSlot = "trigger" | "content"; export type TooltipProps = P.RootProps & TooltipVariants & SlotProp & { + /** 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; @@ -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 ( - + {props.children} - - - {props.content} - - + + + + {props.content} + + + ); } diff --git a/packages/kit/test/tooltip-keyboard.test.tsx b/packages/kit/test/tooltip-keyboard.test.tsx index 1032ae91..cf8a01c5 100644 --- a/packages/kit/test/tooltip-keyboard.test.tsx +++ b/packages/kit/test/tooltip-keyboard.test.tsx @@ -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(() => ( + + + + )); + 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"); +}); diff --git a/packages/storybook/src/stories/Tooltip.stories.tsx b/packages/storybook/src/stories/Tooltip.stories.tsx index 515a40bf..db962182 100644 --- a/packages/storybook/src/stories/Tooltip.stories.tsx +++ b/packages/storybook/src/stories/Tooltip.stories.tsx @@ -105,3 +105,26 @@ export const CustomJsxComponent: Story = { ), }; + +export const EmptyContent: Story = { + name: "Empty content", + args: { + content: undefined, + }, + render: () => ( +
+ + + + + + + + + + + + +
+ ), +};