Skip to Content
CodeUI

Tooltip

@codeui/styles v0.3.0

A short label next to a control, in plain HTML.

Load @codeui/styles/tooltip.css, or all.css. The Solid component is documented in Tooltip.

Demo

tooltip/basic.html
<div class="cui-stack" data-align="center">  <div class="cui-stack" data-gap="1" data-align="center">    <span class="cui-tooltip-trigger" data-cui="tooltip-trigger" data-expanded>      <button type="button" aria-describedby="copy-tip" class="cui-button" data-cui="button" data-variant="secondary">Copy</button>    </span>    <div id="copy-tip" role="tooltip" class="cui-tooltip-content" data-cui="tooltip-content" data-variant="secondary" data-expanded>Copy the snippet to the clipboard</div>  </div></div>

The demos render the open tooltip in normal flow, under its trigger. On a page, the tooltip floats and your positioning code places it.

Markup

PartElementClassdata-cui
Triggerspan or div around the controlcui-tooltip-triggertooltip-trigger
Contentdivcui-tooltip-contenttooltip-content

The trigger wrapper is an inline block, so it takes the size of the control inside it. You can also leave the wrapper out and point the tooltip at the control itself.

AttributeValuesDefault
data-variant on the contentprimary, secondaryNone, the neutral fill
data-expanded on the contentPresenceOff
data-motion on the contentnoneNone

Variants

Colors

tooltip/themes.html
<div class="cui-stack" data-direction="row" data-gap="6" data-justify="center" data-wrap="true">  <div class="cui-stack" data-gap="1" data-align="center">    <span class="cui-tooltip-trigger" data-cui="tooltip-trigger" data-expanded>      <button type="button" aria-describedby="primary-tip" class="cui-button" data-cui="button" data-variant="secondary">primary</button>    </span>    <div id="primary-tip" role="tooltip" class="cui-tooltip-content" data-cui="tooltip-content" data-variant="primary" data-expanded>Primary tooltip</div>  </div>  <div class="cui-stack" data-gap="1" data-align="center">    <span class="cui-tooltip-trigger" data-cui="tooltip-trigger" data-expanded>      <button type="button" aria-describedby="secondary-tip" class="cui-button" data-cui="button" data-variant="secondary">secondary</button>    </span>    <div id="secondary-tip" role="tooltip" class="cui-tooltip-content" data-cui="tooltip-content" data-variant="secondary" data-expanded>Secondary tooltip</div>  </div></div>

primary fills the tooltip with the brand blue. secondary uses the neutral fill. Inkstone keeps the accent for focus, selection, and the one primary action, so prefer secondary in toolbars and panels.

Without data-variant, the tooltip uses the neutral base fill. The Solid component sets primary by default, so set a variant to match it.

Icon button trigger

tooltip/icon-button.html
<div class="cui-stack" data-gap="1" data-align="center">  <span class="cui-tooltip-trigger" data-cui="tooltip-trigger" data-expanded>    <button type="button" aria-label="Copy snippet" class="cui-button cui-iconbutton" data-cui="iconbutton" data-size="sm" data-variant="secondary">      <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true">        <rect x="8" y="8" width="12" height="12" rx="2" />        <path d="M16 8V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h2" />      </svg>    </button>  </span>  <div role="tooltip" class="cui-tooltip-content" data-cui="tooltip-content" data-variant="secondary" data-expanded>Copy snippet</div></div>

An icon button needs its own aria-label. The tooltip shows the same label to sighted users.

States

Open

data-expanded plays the pop-in: a fade from scale(0.96). The animation grows from --cui-popper-content-transform-origin, so set it to the side that faces the trigger. Without data-expanded, the content plays its exit animation and stays transparent. Set it on an open tooltip even without a script.

Closing

Remove data-expanded while the tooltip is still in the page, and remove it when the animation ends. data-motion="none" turns off both animations. Use it when the tooltip opens from keyboard focus. With reduced motion, the tooltip only fades.

What you supply

  • Showing the tooltip on hover after a short delay, 200ms in the kit, and at once on keyboard focus. Hide it on pointer leave, blur, and Escape.
  • Placement next to the trigger, a flip when it does not fit, and --cui-popper-content-transform-origin.
  • role="tooltip" on the content and aria-describedby from the control to it. An icon-only control still needs an aria-label.
  • Text only. A tooltip cannot take focus, so put links or buttons in a Popover.

Helper

import "@codeui/styles/tooltip.css";
import { applyStyleProps } from "@codeui/styles";
import { tooltipStyleProps } from "@codeui/styles/tooltip";

const tip = document.querySelector<HTMLElement>("[role=tooltip]")!;
applyStyleProps(tip, tooltipStyleProps({ variant: "secondary" }));
// class="cui-tooltip-content" data-cui="tooltip-content" data-variant="secondary"

On this page