---
title: "Tooltip"
description: "Centered contextual hints on the top, right, bottom or left."
---

# Tooltip

Centered contextual hints on the top, right, bottom or left.

## Installation

Install the shared package once in your Exact app. This version is a beta.

```sh
bun add --exact @rightui/ui@0.4.0-beta.4
```

Or choose another package manager:

```sh
npm install --save-exact @rightui/ui@0.4.0-beta.4
pnpm add --save-exact @rightui/ui@0.4.0-beta.4
yarn add --exact @rightui/ui@0.4.0-beta.4
```

Exact compilation uses Bun 1.4.2. Register Geist and Newsreader, declare the font marker and add the root provider using [Installation](/docs/installation).

### Manual source setup

Install an editable copy of the complete package:

```sh
mkdir -p vendor/rightui vendor/rightui-ui
npm pack @rightui/ui@0.4.0-beta.4 --pack-destination vendor/rightui
tar -xzf ./vendor/rightui/rightui-ui-0.4.0-beta.4.tgz -C vendor/rightui-ui --strip-components=1
bun add file:./vendor/rightui-ui
```

Keep package.json, the Contract files, ui.tamber, assets and licenses together.

### Import

```contract
use Tooltip, TooltipAdvanced from "@rightui/ui"
```

## Usage

Use Tooltip for a top hint, or TooltipAdvanced with side and reduced to select placement and respect reduced motion.

## Example

```contract
use FontWeights from "@rightui/ui"
use uiFontFamily, TooltipAdvanced from "@rightui/ui"

component Demo
  props
    width: number
    reduced: bool
  inject
    uiWeights: FontWeights
    uiFont: string
  view
    column gap="ui.space-24" min-width=0 max-width="100%" align-items="center"
      TooltipAdvanced(id="catalog-tooltip", label="Hover", description="Add to library", side="top", reduced=reduced)
      text "Placement" font-size="ui.text-sm" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      column gap="ui.space-12" align-items="center" padding="ui.space-24 0px"
        TooltipAdvanced(id="tooltip-top", label="Top", description="Add to library", side="top", reduced=reduced)
        row flex-direction=(width < 600 ? "column" : "row") gap="ui.space-24" align-items="center" justify-content="center"
          TooltipAdvanced(id="tooltip-left", label="Left", description="Add to library", side="left", reduced=reduced)
          TooltipAdvanced(id="tooltip-right", label="Right", description="Add to library", side="right", reduced=reduced)
        TooltipAdvanced(id="tooltip-bottom", label="Bottom", description="Add to library", side="bottom", reduced=reduced)
      text "Hover, focus or tap a button. Press Escape to close." font-size="ui.text-sm" text-align="center" color="ui.muted-foreground" font-family=uiFontFamily(uiFont) data-rightui-font="body"

```

## Keyboard

Hover, tap or focus the trigger to show the hint. Escape dismisses it; moving focus away closes it.

## API Reference

| Property | Type |
| --- | --- |
| Tooltip.id | string |
| Tooltip.label | string |
| Tooltip.description | string |
| TooltipAdvanced.id | string |
| TooltipAdvanced.label | string |
| TooltipAdvanced.description | string |
| TooltipAdvanced.side | string |
| TooltipAdvanced.reduced | bool |

## Source

File: packages/ui/tooltip.contract

```contract
use FontWeights from "./typography-context.contract"
use uiFontFamily from "./typography-context.contract"
use Control from "./styles.contract"
use buttonFill, buttonInk from "./button.contract"

keyframes uiTooltipTop
  from opacity=0 translate="0px 4px"
  to opacity=1 translate="0px 0px"
keyframes uiTooltipBottom
  from opacity=0 translate="0px -4px"
  to opacity=1 translate="0px 0px"
keyframes uiTooltipLeft
  from opacity=0 translate="4px 0px"
  to opacity=1 translate="0px 0px"
keyframes uiTooltipRight
  from opacity=0 translate="-4px 0px"
  to opacity=1 translate="0px 0px"

component Tooltip
  props
    id: string
    label: string
    description: string
  view
    TooltipAdvanced(id=id, label=label, description=description, side="top", reduced=false)

component TooltipAdvanced
  props
    id: string
    label: string
    description: string
    side: string
    reduced: bool
  inject
    uiWeights: FontWeights
    uiFont: string
  derive placement = side == "bottom" or side == "left" or side == "right" ? side : "top"
  derive vertical = placement == "top" or placement == "bottom"
  state focused = false
  state hovered = false
  state expanded = false
  action show
    expanded = true
    showPopover(id + "-tip", source=id)
  action enter
    focused = true
    show()
  action leave(e: FocusEvent)
    match e.relatedTarget
      case some(target)
        if not target.startsWith(id + "-")
          focused = false
          if not hovered
            hidePopover(id + "-tip")
      case none
        focused = false
        if not hovered
          hidePopover(id + "-tip")
  action hover(inside: bool)
    hovered = inside
    if inside
      show()
    else if not focused
      hidePopover(id + "-tip")
  action changed(e: ToggleEvent)
    expanded = e.newState == "open"
  action keys(k: string)
    if k == "Escape"
      preventDefault()
      hidePopover(id + "-tip")
  view
    row align-items="center" hover=hover focusin=enter focusout=leave key=keys
      button id=id testId=id class=Control font-weight=uiWeights.medium aria-describedby=(id + "-tip") press=show popovertarget=(id + "-tip") popovertargetaction="show"
        display="flex" flex-shrink=0 align-items="center" justify-content="center" height="ui.control" min-height="ui.control" padding="ui.space-6 ui.space-16" gap="ui.control-gap"
        border-width="ui.border-width" border-style="solid" border-color="ui.input" border-radius="ui.radius-control" background-color=buttonFill("outline", hovered) color=buttonInk("outline") box-shadow="ui.shadow-control"
        text label font-family=uiFontFamily(uiFont) data-rightui-font="body"
      view id=(id + "-tip") testId=(id + "-tip") role="tooltip" popover="auto" toggle=changed hover=hover
        position-area=placement position-try-fallbacks="none"
        width="auto" max-width="min(20rem, calc(100vw - ui.space-32))" box-sizing="border-box" margin="ui.space-6" padding="ui.space-6 ui.space-12"
        background-color="ui.foreground" color="ui.background" border="0" border-radius="ui.radius-item" overflow="visible" box-shadow="none"
        animation=(reduced or not expanded ? "none" : placement == "left" ? "uiTooltipLeft ui.duration-fast ui.easing both" : placement == "right" ? "uiTooltipRight ui.duration-fast ui.easing both" : placement == "bottom" ? "uiTooltipBottom ui.duration-fast ui.easing both" : "uiTooltipTop ui.duration-fast ui.easing both")
        text description font-size="ui.text-xs" line-height="ui.leading-normal" font-family=uiFontFamily(uiFont) data-rightui-font="body"
        view aria-hidden=true pointer-events="none" position="absolute" width="ui.space-10" height="ui.space-10" rotate="45deg" border-radius="ui.radius-detail" background-color="ui.foreground"
          left=(vertical ? "50%" : placement == "right" ? "ui.space-negative-5" : "auto") right=(placement == "left" ? "ui.space-negative-5" : "auto")
          top=(vertical ? placement == "bottom" ? "ui.space-negative-5" : "auto" : "50%") bottom=(placement == "top" ? "ui.space-negative-5" : "auto")
          translate=(vertical ? "-50% 0px" : "0px -50%")

```
