# Tooltip

Crate: `libero`
Import: `use libero::components::Tooltip;`
Feature: `tooltip` (on by default, turns on `use-timers`, `box`, `use-popover`; [features.md](features.md))
Source: <https://github.com/tdymel/libero/tree/main/libero/src/components/overlay/tooltip.rs>
Index: [index.md](index.md) lists every other page
Description: A label that appears while its child is hovered or focused by keyboard, portaled so nothing clips it.

A label that appears while its child is hovered or focused by keyboard. The
bubble is portaled, so no `overflow: hidden` ancestor clips it, and it flips
when its side has no room. `sx`, `class`, `states` and extra attributes land on
the bubble, not the trigger.

## Usage

```rust
use dioxus::prelude::*;
use libero::{
    components::{Box, Button, Tooltip},
    sx::sx,
};

#[component]
fn Demo() -> Element {
    rsx! {
        Box {
            sx: sx().padding("40px"),
            Tooltip {
                label: rsx! { "Saves the current draft" },
                label_id: "draft-tip",
                arrow: true,
                Button { variant: "outlined", aria_describedby: "draft-tip", "Save" }
            }
        }
    }
}
```

## Props

| Prop | Type | Default | Description |
|---|---|---|---|
| `label` | `Element` | required | The bubble's content. |
| `side` | `Responsive<Side>` | `top` | The preferred side of the trigger. The bubble flips when that side has no room. `Start`/`End` are logical: `Start` is the left under `dir="ltr"`, the right under `rtl`. Or one per breakpoint, `side: responsive(Side::Bottom).md(Side::Top)`, read when the bubble opens and on a resize. Breakpoints follow the window, not the parent. |
| `gap` | `Responsive<ThemeAwareValue>` | `xs` | Distance to the trigger, or a `px` or `rem` length, e.g. `gap: "6px"`. The pointer can cross it without closing the bubble. Other CSS places the bubble by the theme's gap. Or one per breakpoint, `gap: responsive(Size::Xs).md(Size::Md)`: read in Rust when the bubble opens. Breakpoints follow the window, not the parent. |
| `arrow` | `bool` | `false` | Draws an arrow from the bubble to the trigger's centre, in the bubble's background. It stays on the trigger when the bubble shifts to fit the viewport, and `gap` runs from its tip. |
| `size` | `Responsive<Size>` | `lg` | Font size of the bubble. Or one per breakpoint, `size: responsive(Size::Sm).md(Size::Lg)`. Breakpoints follow the window, not the parent. |
| `z_index` | `ThemeAwareValue` | the popover layer | Overrides the stacking level, for a bubble hidden by another overlay. |
| `open_delay` | `u32` | `0` | Milliseconds the pointer must rest before the bubble appears. |
| `close_delay` | `u32` | `0` | Milliseconds the bubble stays after the pointer leaves. While it counts down, the bubble carries `data-closing`. |
| `touch_open_delay` | `u32` | `500` | Milliseconds a touch must hold the trigger before the bubble appears. A shorter tap shows nothing. |
| `touch_close_delay` | `u32` | `1500` | Milliseconds the bubble stays after the touch lifts. |
| `open` | `bool` | unset | Forces the bubble open or closed. Unset, hover and focus decide. A bubble forced open ignores Escape. |
| `disabled` | `bool` | `false` | No bubble. The trigger keeps its wrapper, so enabling or disabling does not remount it or drop its focus. |
| `label_id` | `String` | - | The bubble's `id`, for the trigger's `aria-describedby`. It exists while the bubble is closed, too, except while `disabled`: then nothing describes the trigger. |
| `children` | `Element` | required | The trigger. |

The defaults come from `TooltipDefaults` on the theme.

Like every component, `Tooltip` also takes the shared props `sx`, `class`,
`style`, `states`, `hidden_from`, `visible_from`, and any extra HTML attributes. They land on the bubble.

## Accessibility

### Keyboard

| Key | Action |
|---|---|
| `Escape` | Hides the bubble until the pointer or focus comes back. |

### Libero handles

- Keyboard focus anywhere inside `Tooltip` shows the bubble, a click does not.
- On touch, a tap shows nothing: a 500 ms hold shows the bubble, and it stays
  1.5 s after the release. `touch_open_delay` and `touch_close_delay` change
  both times.
- On touch, a tap elsewhere hides a bubble a long press opened. In a browser
  this needs a focusable trigger that is no text field: the long press focuses
  it, and a text field would raise the keyboard. Other triggers keep the bubble
  for the 1.5 s.

### You must

- Give the bubble an id with `label_id` and point your trigger's
  `aria-describedby` at it, so a screen reader reads the label.
- Keep a disabled trigger focusable: `Button { disabled }` drops out of the
  Tab order, so its tooltip is mouse-only. Set `focusable_when_disabled: true`
  to keep its tab stop.

```rust
use dioxus::prelude::*;
use libero::components::{Button, Tooltip};

#[component]
fn Demo() -> Element {
    rsx! {
        Tooltip {
            label_id: "save-tip",
            label: rsx! { "Saves the current draft" },
            Button { aria_describedby: "save-tip", "Save" }
        }
    }
}
```

### Example

A Save icon button with a "Save (Ctrl+S)" tooltip and `label_id: "save-tip"`:
the button's `aria-describedby: "save-tip"` makes a screen reader read the
tip, Tab to it shows the bubble, and Escape hides it.

## Theme defaults

`TooltipDefaults` on the theme.

| Field | Type | Description |
|---|---|---|
| `side` | `Side` | Default `side`. |
| `gap` | `Size` | Default distance between trigger and bubble. |
| `size` | `Size` | Default `size`. |
| `open_delay` | `u32` | Default milliseconds before the bubble appears. |
| `close_delay` | `u32` | Default milliseconds before it disappears. |
| `duration` | `u32` | Fade-in duration, in milliseconds. |
| `font_sizes` | `Sizes<u16>` | Bubble font size per size step, in px at a 16px root, written as `rem`. |
| `background` | `&'static str` | Bubble background. |
| `color` | `&'static str` | Bubble text color. |

## CSS variables

| Variable | Description |
|---|---|
| `--lsx-tooltip-font-size-<size>` | Bubble `font-size` for that size step. |
| `--lsx-tooltip-background` | Bubble background. |
| `--lsx-tooltip-color` | Bubble text color. |
| `--lsx-tooltip-duration` | Fade-in duration. The bubble unmounts on close, with no fade out. |
| `--lsx-tooltip-gap` | Distance to the trigger, written per instance from `gap`. |

The bubble sits on the popover layer, `--lsx-z-index-popover`, above modals.
`z_index` overrides it.

## Data attributes

On the bubble:

| Token | Condition |
|---|---|
| `side-top` / `side-end` / `side-bottom` / `side-start` | The side it landed on, after any flip. |
| `size-<size>` | The `size` in effect. |
