Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Toggle Tooltip for UI SDK

A tooltip for React and UI SDK that opens on click or tap instead of hover, works on touch screens, and stays open until it is dismissed.

On this page9 sections

A toggle tooltip is an info note that opens when someone clicks or taps its trigger, so it works on a phone, and it stays open until they click again, click elsewhere or press Escape. It looks like a tooltip, but every version of this kit builds it on Ark UI's popover, which is why it can hold a link.

For a hint that only needs to show on hover or keyboard focus, a Tooltip is lighter. For a form or several paragraphs, use a Popover.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Toggle Tooltip anatomy: content, arrow, trigger

Usage

import { InfoIcon } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
  ToggleTooltip, ToggleTooltipContent, ToggleTooltipTrigger,
} from "@/components/ui/toggle-tooltip";
const Example = () => (
  <ToggleTooltip>
    <ToggleTooltipTrigger
      render={
        <Button
          aria-label="More information"
          size="icon-md"
          variant="outline"
        />
      }
    >
      <InfoIcon aria-hidden="true" />
    </ToggleTooltipTrigger>
    <ToggleTooltipContent className="max-w-52">
      Opens on click or tap, and closes on a second click, a click outside or
      Escape.
    </ToggleTooltipContent>
  </ToggleTooltip>
);

Accessibility

The trigger is a button that reports aria-expanded and points at the note with aria-controls. The note is a non-modal role="dialog", and opening it moves focus inside, onto its first link or onto the note itself. Escape closes it and puts focus back on the trigger. The trigger's name is left to you: an icon-only trigger needs an aria-label that says what it explains, such as Info about Sales. The note takes the trigger's name through aria-labelledby, so a screen reader announces it as the Info about Sales dialog. Give ToggleTooltipContent an aria-label of its own when the trigger's name doesn't describe the note.

Enter
When focus is on the trigger, opens the note, or closes it when it is open.
Space
When focus is on the trigger, opens the note, or closes it when it is open.
Tab
Moves focus to the next focusable element, and closes the note once focus leaves it.
Escape
Closes the note and moves focus back to the trigger.

Demos

Info icon

A data list of three figures, New Users, Sales and Revenue, each with an outline info button beside its label that opens one sentence about that number. Every button is named Info about plus its label, since an icon alone doesn't say which figure it explains. Use it for definitions that would clutter a dashboard row, and keep each note to a sentence.

Preview width

Side

Four buttons, left, top, bottom and right, each opening its note on that side through positioning with a placement. The default is top, so set a side when the trigger sits against an edge or a toolbar and the note would cover what it explains. If that side has no room, the note flips to the opposite one.

Preview width

An info button whose note holds a sentence with a real link in it. The note stays open until a second click, a click elsewhere or Escape, and opening it moves focus onto the link, so the link is in reach by touch and by keyboard. Keep it to a link or two, and move a form into a Popover.

Preview width

Controlled

The Seats label has an info button, and the How are seats counted? button under it, styled as a link, opens the same note by setting open to true. onOpenChange writes every close back to that state, whether it comes from Escape, a click outside or the trigger. Use it when another control, such as a help link or a tour step, has to open the note.

Preview width

FAQ

What is a Toggle Tooltip and when should I use it?

It is a small note that opens when its trigger is clicked or tapped rather than hovered, so it works on touch screens where hover doesn't exist. Use it for an info icon next to a figure, a form label or a setting, where the explanation is a sentence or two.

The note isn't in the page until it opens, so don't put anything there that people must read to finish a task. Text like that belongs on the page itself.

What is the difference between a Toggle Tooltip, a Tooltip and a Popover?

A Tooltip opens on hover or keyboard focus, and a tap on a touch screen doesn't count as hover, so on a phone it does little. A Toggle Tooltip opens on click or tap, stays open until it is dismissed, and moves focus into the note, so it can hold a link, as the With link demo does.

A Popover is the same idea at a larger size, with room for a heading, a form and a close button. The toggle tooltip keeps the tooltip's small dark note and its arrow.

How do I change where the Toggle Tooltip opens in React?

Pass positioning with a placement of top, bottom, left or right, as the Side demo does. Without the prop the note opens on top, and it flips to the opposite side when there isn't room on the one you chose.

The object you pass replaces the default rather than merging with it. If you set other positioning options, include placement too, or Ark UI's own default puts the note below the trigger.

How do I open the Toggle Tooltip from code?

Keep the open state yourself, pass it to ToggleTooltip as open, and write each change back from onOpenChange, as the Controlled demo does with a button that opens the note beside Seats. onOpenChange receives details.open for every open and close, including a click outside and Escape.

To start with the note open and leave it alone after that, set defaultOpen instead. A note opened from code moves focus inside it just as one opened by its trigger does, and Escape puts focus back on the trigger.

How do I close the Toggle Tooltip?

It closes on a second click of its trigger, a click anywhere outside it, Escape, or focus moving out of the note. It doesn't trap focus or block the page, so people can keep working while it is open.

To keep it open through outside clicks, set closeOnInteractOutside to false on ToggleTooltip, and set closeOnEscape to false to ignore Escape. Leave at least one of them on, or a keyboard user has no quick way to close it.

How do I make the Toggle Tooltip trigger accessible?

Render the trigger onto a Button through asChild, as every demo does, so it is a real button in the tab order that reports aria-expanded as the note opens and closes. An icon-only button needs an aria-label that names what it explains, like the Info about labels in the Info icon demo, because Info alone repeated down a list tells nobody which figure it belongs to.

A trigger with visible text, such as the buttons in the Side demo, needs nothing more, because its text is its name.

How do I change the Toggle Tooltip's color or width?

Add classes to ToggleTooltipContent. Its default look is bg-foreground with text-background and text-xs, the page's text color turned around, and it is w-fit with no maximum, so a long sentence runs wide until you add a class such as max-w-52, as the first preview does.

The arrow is drawn separately and reads its color from --arrow-background, which ToggleTooltipArrow sets to the foreground color. When you change the note's background, change that variable in the same file, or the arrow keeps the old color. ToggleTooltipContent already renders the arrow, so adding a ToggleTooltipArrow of your own draws a second one.

API Reference

ToggleTooltip

PropType
open?boolean

Whether the note is open. Pass it to control the note.

defaultOpen?boolean

Whether the note starts open, when uncontrolled.

onOpenChange?(details: { open: boolean }) → void

Runs when the note opens or closes, with { open }.

Type
(details: { open: boolean }) → void
Parameters

details – { open: boolean }

Returns
void
container?DocumentFragment | Element | null

Where the note renders: the docs preview, else the body, by default; null renders it in place.

Type
DocumentFragment | Element | null
lazyMount?boolean

Kept for the Ark UI version's markup: UI SDK renders the note only once it opens.

Default
true
modal?boolean

Whether the rest of the page is inert while the note is open. @default false.

Default
false
positioning?object

The type's default: { placement: "top" }.

Type
{ arrowPadding?: number; gutter?: number; overflowPadding?: number; placement?: Placement; shift?: number }
Default
defaultPositioning
unmountOnExit?boolean

Whether the note leaves the page once it's closed. @default true.

Default
true

ToggleTooltipTrigger

<button> data-slot="popover-trigger"

PropType
controller?PopupController<any, PopoverOpenChangeReason, {}, string, PopoverPayloadChangeReason>

Connects a trigger outside the popover, like one per table row, to the popover with this controller.

Type
PopupController<any, PopoverOpenChangeReason, {}, string, PopoverPayloadChangeReason>
disabled?boolean

Stops the trigger from opening the popover. A native button can't be focused or pressed, unless focusableWhenDisabled is set.

Default
false
focusableWhenDisabled?boolean

Keeps a disabled trigger in the tab order with aria-disabled instead of the native attribute, so it can host a tooltip that says why. Presses still do nothing.

Default
false
payload?unknown

Data for the content to render when this trigger opens the popover, like the row it belongs to.

shortcut?string

A keyboard shortcut that presses the trigger, like "mod+i". mod is Command on Apple platforms and Control elsewhere. Sets aria-keyshortcuts.

render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
AttributeType
data-closed?string

Present while the popover is closed, including while it animates out.

data-disabled?string

Present when the trigger is disabled.

data-open?string

Present while the popover is open. On a trigger, while it's open from that trigger.

ToggleTooltipContent

<div> data-slot="popover-content"

PropType
align?Align

How the popup lines up with the anchor along that side. start and end follow the reading direction. How the popover lines up with the trigger along that side.

Default
"center"
alignOffset?number

Shift along the anchor, in pixels. Shift along the trigger, in pixels.

Default
0
anchor?string | Anchor | null

Positions the popup against this instead of its trigger: an element, a CSS selector, a point like { x, y }, or anything with getBoundingClientRect(). It's read on every check, so it can move. A controller's open({ anchor }) wins for that open.

Type
string | Anchor | null
arrowPadding?number

Space to keep between the arrow and the popup's corners, in pixels.

Default
6
collisionPadding?number

Space to keep from the viewport edges, in pixels.

Default
8
container?DocumentFragment | Element | null

Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.

Type
DocumentFragment | Element | null
finalFocus?PopupFocusTarget

Where focus goes when the popover closes while focus is inside it.

Default
"trigger"
initialFocus?PopoverInitialFocus

Where focus goes when the popover opens. Opening on hover never moves focus, and opening with touch focuses the popover itself so no on-screen keyboard appears, unless this is set to something other than "auto".

Default
"auto"
inline?boolean

Renders the popover where it is in the page instead of above it, inside its parent's layout and stacking, and leaves placing it to your CSS: side, align and the offsets don't apply.

Default
false
keepMounted?boolean

Whether the popover stays in the page, hidden, while closed. Its content keeps its state between opens.

Default
false
side?Side

Which side of the anchor the popup opens on. Flips to the other side when there isn't room. Which side of the trigger the popover opens on.

Default
"bottom"
sideOffset?number

Gap between the anchor and the popup, in pixels. Gap between the trigger and the popover, in pixels.

Default
4
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-align"start" | "center" | "end"

How the popover lines up with its trigger.

Type
"start" | "center" | "end"
data-closed?string

Present while the popover is closed, including while it animates out.

data-ending-style?string

Present while the popover animates out. Style the state you animate to.

data-open?string

Present while the popover is open. On a trigger, while it's open from that trigger.

data-presentation"popover" | "sheet"

How the popover presents this time it's open.

Type
"popover" | "sheet"
data-side"top" | "right" | "bottom" | "left"

Where the popover ended up, after flipping away from edges. Set on the arrow too.

Type
"top" | "right" | "bottom" | "left"
data-starting-style?string

Present for the first frame. Style the state you animate from.

CSS variableType
--anchor-heightstring

Height of the anchor.

--anchor-widthstring

Width of the anchor, usually the trigger. Use it to match widths.

--arrow-offsetstring

Where the anchor's center falls along the popup's edge, kept arrowPadding from the corners. Position a ::before arrow with it.

--available-heightstring

Space between the anchor and the viewport edge. Use it for max-height. Not inherited, so a child that needs it copies it on the popup, like [--list-height:var(--available-height)], and reads max-h-(--list-height). The first popup registers both names for the whole page with CSS.registerProperty, so they don't inherit anywhere. A page that needs them to inherit calls it first, with inherits: true. An @property rule can't, since a registration in script wins.

--available-widthstring

Space beside the popup, up to the viewport edge. Not inherited either.

--drawer-dimstring

While it presents as a sheet: how dim the page behind is, from 0 to 1. The backdrop's opacity follows it.

--drawer-expandstring

While it presents as a sheet: 0 at the second-largest snap point and below, 1 at the largest. With one snap point, the whole travel.

--drawer-progressstring

While it presents as a sheet: how far open the sheet is, from 0 closed to 1 at its largest snap point. Never past 1.

--drawer-stackstring

While it presents as a sheet: how many sheets opened from inside this one are open above it, rising smoothly as each one opens. Push it back with it.

--sheet-backdropstring

Set it yourself: the color the page dims to while it presents as a sheet. Black at 0.2 by default.

--transform-originstring

Where the content scales from, on its trigger's side.

ToggleTooltipArrow

<div> data-slot="popover-arrow"

PropType
style?React.CSSProperties
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-align"start" | "center" | "end"

How the popover lines up with its trigger.

Type
"start" | "center" | "end"
data-arrow-clamped?string

Present on the arrow when it stops short of the middle of the trigger, held back by arrowPadding.

data-closed?string

Present while the popover is closed, including while it animates out.

data-open?string

Present while the popover is open. On a trigger, while it's open from that trigger.

data-side"top" | "right" | "bottom" | "left"

Where the popover ended up, after flipping away from edges. Set on the arrow too.

Type
"top" | "right" | "bottom" | "left"
CSS variableType
--arrow-backgroundstring
--arrow-sizestring