Tooltip component provides context or additional information for the element it is attached to.

Open in new tab
import { Button, Tooltip } from "@kesko/components";
import { IconQuestion } from "@kesko/iconography/react";

export default () => (
  <>
    <Button appearance="text" square className="k-cursor-help" aria-describedby="delivery-help">
      <IconQuestion label="About delivery times" />
    </Button>
    <Tooltip id="delivery-help" defaultOpen>
      Orders placed before noon are delivered the same day.
    </Tooltip>
  </>
);
import { Button, Tooltip } from "@kesko/components";
import { IconQuestion } from "@kesko/iconography/react";

export default () => (
  <>
    <Button appearance="text" square className="k-cursor-help" aria-describedby="loyalty-help">
      <IconQuestion label="About Plussa points" />
    </Button>
    <Tooltip
      id="loyalty-help"
      style={{
        "--k-tooltip-bg": "var(--k-color-bg-inverted)",
        "--k-tooltip-text": "var(--k-color-text-inverted)",
        "--k-tooltip-radius": "var(--k-radius-sm)",
      }}
    >
      Plussa points are added to your account within two days.
    </Tooltip>
  </>
);
import { Button, Tooltip } from "@kesko/components";
import { IconQuestion } from "@kesko/iconography/react";

export default () => (
  <>
    <Button appearance="text" square className="k-cursor-help" aria-describedby="pickup-help">
      <IconQuestion label="About pickup" />
    </Button>
    <Tooltip id="pickup-help" position="bottom" open>
      Pickup orders are ready two hours after checkout.
    </Tooltip>
  </>
);
import { Button, Grid, Tooltip } from "@kesko/components";

const positions = [
  "top-start",
  "top",
  "top-end",
  "bottom-start",
  "bottom",
  "bottom-end",
  "start-top",
  "start",
  "start-bottom",
  "end-top",
  "end",
  "end-bottom",
] as const;

export default () => (
  <Grid columns={3} gap="xs">
    {positions.map(position => {
      const id = `position-${position}`;
      return (
        <div key={id}>
          <Button size="sm" appearance="outline" expand className="k-cursor-help" aria-describedby={id}>
            {position}
          </Button>
          <Tooltip id={id} position={position}>
            Opens at {position}. Longer text grows the bubble away from the arrow.
          </Tooltip>
        </div>
      );
    })}
  </Grid>
);
import { Button, Stack, Tooltip } from "@kesko/components";

const rows = ["Weekly groceries", "Garden tools", "Winter tyres"];

export default () => (
  <Stack gap="sm">
    {rows.map(row => (
      <Stack key={row} direction="row" justify="between" alignment="center" gap="lg">
        <span>{row}</span>
        <Button size="sm" appearance="outline" aria-describedby="edit-row">
          Edit
        </Button>
      </Stack>
    ))}
    <Tooltip id="edit-row">Edit this order.</Tooltip>
  </Stack>
);
import { Button, Tooltip } from "@kesko/components";
import { IconQuestion } from "@kesko/iconography/react";

export default () => (
  <>
    <Button appearance="text" square className="k-cursor-help" aria-describedby="returns-help">
      <IconQuestion label="About returns" />
    </Button>
    <Tooltip id="returns-help" arrow={false}>
      Returns are free for 30 days.
    </Tooltip>
  </>
);

Properties #

NameDescriptionTypeDefault
id

The id that triggers point at. Any element whose aria-describedby lists this id shows the tooltip on hover and keyboard focus, so the same text can describe several triggers.

string—
position

The side of the trigger the tooltip opens on, and optionally which edge of the tooltip the arrow sits at. start and end follow the writing direction. The tooltip flips to the opposite side when there is no room, and opens above or below when neither start nor end has room.

"top" | "top-start" | "top-end" | "bottom" | "bottom-start" | "bottom-end" | "start" | "start-top" | "start-bottom" | "end" | "end-top" | "end-bottom""top"
arrow

Shows the arrow that points at the trigger.

booleantrue
delay

Milliseconds the pointer rests on the trigger before the tooltip opens. Keyboard focus and clicking or tapping the trigger open it right away.

number300
defaultOpen

Shows the tooltip as soon as it mounts, as a one-time hint. It closes like any other tooltip and then follows hover and focus.

booleanfalse
open

Keeps the tooltip open regardless of hover and focus, for example in a showcase. Leave it out for a tooltip that people trigger themselves.

boolean—

Styles #

CSS custom properties provide finer-grained control than properties. See Styling API for more details. Prefer the Properties API when it suits your need.

NameDescriptionDefault
--k-tooltip-bg

Overrides the background color.

var(--k-color-bg-base)
--k-tooltip-text

Overrides the text color.

var(--k-color-text-base)
--k-tooltip-radius

Overrides the border radius.

var(--k-radius-lg)
--k-tooltip-shadow

Overrides the shadow of the bubble. The arrow keeps a small shadow of its own.

var(--k-shadow-float)
--k-tooltip-max-size

Overrides the maximum width. The tooltip hugs its text up to this.

15rem

Accessibility #

Tooltip is built on a <k-tooltip> element with role="tooltip". The trigger points at it with aria-describedby, so a screen reader reads the tooltip text right after the trigger’s name.

  • The trigger needs a name of its own. A tooltip describes, it does not label. An icon-only button gets its name from the icon’s label or from aria-label, and the tooltip adds detail on top. Never use the title attribute instead.
  • Only focusable elements can trigger it. Keyboard users reach the tooltip through focus, so put aria-describedby on a button, link or form control, not on a plain icon or span.
  • Plain text only. A tooltip disappears when the pointer leaves, so people cannot interact with anything inside it. Links, buttons and formatted content belong in a popover.
  • Never put essential information in a tooltip. Touch users cannot hover, and the text is easy to miss. Say what matters in the interface and use the tooltip for a hint.
  • Dismissing is handled for you. The tooltip stays open while hovered, closes on Esc or a click elsewhere, and opens on tap.
  • One at a time. Opening a tooltip closes any other tooltip on the page, so the screen never fills up with hints.
  • Check contrast if you override colors. --k-tooltip-text needs 4.5:1 against --k-tooltip-bg. Verify with the WebAIM contrast checker.

See the accessibility guidelines and the A11Y checklist for more details.


Usage #

There are two necessary parts to using the Tooltip component. The @kesko/components package and the CSS Framework are required. Everything else is optional:

npm install @kesko/components @kesko/css

Once installed, load the CSS Framework in your app’s entry point or root layout:

import "@kesko/css";

Then, you can import the Tooltip:

import { Tooltip } from "@kesko/components";

Projects without React can get the same styles from @kesko/css by importing the components stylesheet as well:

@import "@kesko/css";
@import "@kesko/css/components";

Resources #