Tooltip
@codeui/solid v0.3.0
A short label that appears when the user hovers or focuses an element.
When to use
Use a tooltip to name an icon button or to explain a value in a few words. It opens on hover and on keyboard focus, so keyboard users get the same label as mouse users.
When to choose another component
- Popover holds content the user can interact with, such as buttons or fields. A tooltip holds text only.
- A visible label is better than a tooltip when there is room for it.
- Do not use the native SVG
titleattribute as a tooltip. Inkstone replaces it with this component.
Live demo
import { Button, Tooltip } from "@codeui/solid";
export default function TooltipBasic() {
return (
<Tooltip content="Copy the snippet to the clipboard" variant="secondary">
<Button variant="secondary">Copy</Button>
</Tooltip>
);
}Wrap the element in Tooltip and pass the label as content. Hover the button, or move focus to it with Tab.
Anatomy
Tap a part to outline it in the demo.
- 01Slot classes.trigger
tooltip-triggerslotClasses.trigger - 02Slot classes.content
tooltip-contentslotClasses.content
| Export | Renders | classes key | data-cui | Role |
|---|---|---|---|---|
Tooltip | Nothing. Holds the open state | none | none | none |
| Trigger | div, display: inline-block, around the children | slotClasses.trigger | tooltip-trigger | Wrapper that listens for hover and focus |
| Content | div in a portal on mount, or document.body | slotClasses.content | tooltip-content | role="tooltip" |
Tooltip takes slotClasses, not classes. The content renders in a portal on document.body unless you pass mount.
Variants
Colors
import { Button, Tooltip } from "@codeui/solid";
export default function TooltipThemes() {
return (
<>
<Tooltip content="Primary tooltip" variant="primary">
<Button variant="secondary">primary</Button>
</Tooltip>
<Tooltip content="Secondary tooltip" variant="secondary">
<Button variant="secondary">secondary</Button>
</Tooltip>
</>
);
}variant="secondary" uses the neutral fill. It is the default. variant="primary" fills the tooltip with the brand blue.
Inkstone keeps the accent for focus, selection, and the one primary action, so keep secondary in toolbars and panels.
Placement
import { For } from "solid-js";
import { Button, Tooltip } from "@codeui/solid";
const placements = ["top", "right", "bottom", "left", "top-start", "bottom-end"] as const;
export default function TooltipPlacements() {
return (
<For each={placements}>
{placement => (
<Tooltip content={`Opens on ${placement}`} placement={placement}>
<Button variant="secondary" size="sm">
{placement}
</Button>
</Tooltip>
)}
</For>
);
}placement sets the side and the alignment. The default is bottom. When the tooltip does not fit, Kobalte flips it to the opposite side.
gutter sets the distance from the trigger. The kit sets it to 4px.
Delays
import { Button, Tooltip } from "@codeui/solid";
export default function TooltipDelays() {
return (
<>
<Tooltip content="Opens after 200ms" variant="secondary">
<Button variant="secondary">Default delay</Button>
</Tooltip>
<Tooltip content="Opens at once" variant="secondary" openDelay={0}>
<Button variant="secondary">No delay</Button>
</Tooltip>
<Tooltip content="Stays 300ms after you leave" variant="secondary" closeDelay={300}>
<Button variant="secondary">Close delay</Button>
</Tooltip>
</>
);
}The tooltip opens 700ms after the pointer enters and closes at once when it leaves. Change this with openDelay and closeDelay.
Keyboard focus ignores openDelay and opens the tooltip at once.
Defaults for a whole app
Wrap the app in TooltipProvider to set the defaults of every tooltip below it. A prop on one tooltip still wins.
import { TooltipProvider } from "@codeui/solid";
<TooltipProvider openDelay={500} closeDelay={100}>
<App />
</TooltipProvider>;It takes openDelay, closeDelay, skipDelayDuration, gutter, shift and variant. With parts, import it as Provider from @codeui/solid/tooltip/parts.
skipDelayDuration is how long after a tooltip closes the next one opens at once, without its delay or its animation. The kit default is 400ms.
Icon button trigger
import { IconButton, icons, Tooltip } from "@codeui/solid";
export default function TooltipIconButton() {
return (
<div style={{ display: "flex", gap: "4px" }}>
<Tooltip content="Previous snippet" variant="secondary">
<IconButton variant="secondary" aria-label="Previous snippet">
<icons.PreviousIcon />
</IconButton>
</Tooltip>
<Tooltip content="Next snippet" variant="secondary">
<IconButton variant="secondary" aria-label="Next snippet">
<icons.NextIcon />
</IconButton>
</Tooltip>
</div>
);
}Any JSX works as the trigger. For an icon button, give the button an aria-label too. The tooltip shows the label, and the aria-label gives the button its accessible name.
States
Disabled
import { createSignal } from "solid-js";
import { Button, Switch, Tooltip } from "@codeui/solid";
export default function TooltipDisabled() {
const [hints, setHints] = createSignal(false);
return (
<>
<Switch label="Show hints" checked={hints()} onChange={setHints} />
<Tooltip content="Format the code with Prettier" disabled={!hints()}>
<Button variant="secondary">Format</Button>
</Tooltip>
</>
);
}disabled keeps the tooltip closed on hover and on focus. The trigger still works.
Controlled
Without open, the tooltip manages its own state. To control it, pass open and onOpenChange.
Ready API
Tooltip
Wraps its children in a trigger div and portals content to document.body.
Prop
Type
Inherited from Kobalte's tooltip root:
Prop
Type
Styled parts API
Use @codeui/solid/tooltip/parts to build a reusable tooltip with a specific trigger and content. Put Trigger and Portal in Root, and put Content inside Portal. Render the trigger as a focusable control, for example with as={Button}. Use Content for the description and keep interactive actions outside the tooltip.
Accessibility
Keyboard
| Key | Action |
|---|---|
| Tab | Moving focus to the element inside the trigger opens the tooltip at once. Moving focus away closes it. |
| Escape | Closes the tooltip. Focus stays on the element. |
A click on the trigger also closes the tooltip.
ARIA
- The content has
role="tooltip". - While the tooltip is open, Kobalte sets
aria-describedbyon the triggerdiv, not on the focusable element inside it. Screen readers may not read the tooltip when they announce the button. Give the element its own accessible name, such asaria-labelon an icon button. - The trigger
divhas notypeattribute and no role, so it adds nothing to the accessibility tree.
Focus
The tooltip never takes focus. Focus stays on the element inside the trigger while the tooltip is open.
SSR notes
The trigger div and its children render on the server. The content renders on the client, and only while the tooltip is open.
Theming hooks
Tooltip reads these --cui-* variables. See Tokens for the defaults.
| Variable | Used for |
|---|---|
--cui-action-brand-base, --cui-action-brand-text | Fill and text of primary |
--cui-action-neutral-base, --cui-action-neutral-text | Fill and text of secondary |
--cui-font-size-sm, --cui-font-line-height-compact | Text size and line height (14px / 1.15) |
--cui-space-2, --cui-space-4 | Padding |
--cui-radius-xl | Radius |
--cui-shadow-md | Shadow |
--cui-motion-duration-normal, --cui-motion-duration-fast, --cui-motion-ease-out | Pop: in from scale(0.96) over 160ms, out to scale(0.98) over 120ms |
A tooltip opened by hover pops from its trigger. One opened by keyboard focus, and the next tooltip opened within 400ms of another closing, appear with no animation (data-motion="none"), as Inkstone asks. The description goes on the first focusable element inside the trigger, so a screen reader reads it with the focused control.
Composition examples
- Nest overlays puts a tooltip inside a dialog.
Related
- Popover holds interactive content and takes focus.
- IconButton is the usual trigger for a tooltip.