# Floats and modals

<a data-kui-example="400" href="live/gallery.html?example=tutorial/07_floats">Live chapter example</a>

At the end of this chapter, hovering a badge shows a tooltip, and a
Delete button opens a dialog that Escape closes.

## A float is out of the flow

Everything so far sat inside its parent and took up room. A **float**
does neither: it is positioned against an anchor and drawn on top.
Tooltips, menus and dialogs are floats.

## The tooltip prop

```rust,noplayground
                // `tooltip` is a prop: the node tracks its own hover, and
                // the core floats the hint below it while hovered. The
                // same text is what a screen reader says after the name.
                ui.text_in(
                    NodeSpec::row()
                        .pad_xy(10.0, 6.0)
                        .radius(8.0)
                        .bg(t.raised)
                        .border(1.0, t.border)
                        .tooltip("A float: out of the flow, on top, unclipped"),
                    "hover me",
                    TextStyle::new(13.0).color(t.fg),
                );
```

`.tooltip(text)` is enough. The node tracks its own hover, and kui
floats the text below it while the pointer is over it or over anything
inside it; with tooltips nested, the innermost one shows. Near the bottom of the window
it flips above, so it never hangs off the edge. The same text is what a
screen reader says after the node's name.

## A modal the app declares

A dialog is a float you declare when the model says so and stop
declaring when it is answered:

```rust,noplayground
                // Declared last, so it stacks on top. `modal` scopes Tab
                // to it, keeps the pointer from what is behind, and turns
                // Escape or a press outside into a `dismiss` event.
                if self.asking {
                    ui.with_keyed(
                        "dialog",
                        NodeSpec::column()
                            .float(FloatConfig::viewport().inside(Align::Center, Align::Center))
                            .modal(Msg::Dialog)
                            .label("Delete?")
                            .width(260.0)
                            .pad(16.0)
                            .gap(12.0)
                            .bg(t.raised)
                            .radius(10.0)
                            .border(1.0, t.border_strong),
                        |ui| {
                            ui.text("Delete the thing?", TextStyle::new(15.0).color(t.fg));
                            ui.text(
                                "This cannot be undone.",
                                TextStyle::new(12.0).color(t.muted),
                            );
                            ui.with(NodeSpec::row().gap(8.0).main_align(Align::End), |ui| {
                                widgets::button(ui, "Cancel", Msg::Cancel);
                                widgets::button(ui, "Delete", Msg::Confirm);
                            });
                        },
                    );
                }
```

Three props do the work:

- **`.float(FloatConfig::viewport().inside(Center, Center))`** —
  anchored to the window, centred. `FloatConfig::parent()` anchors to
  the parent box instead, and `.offset(x, y)` nudges either.
- **`.modal(msg)`** — while this float is declared, Tab stays inside
  it, the pointer cannot reach what is behind it, and Escape or a press
  outside arrives as `msg`, a `dismiss` event with its reason inside.
- **`.label("Delete?")`** — the dialog's name. A button is named by
  the text inside it, but a dialog is not: a screen reader announcing
  "dialog" with nothing after it tells the user nothing, and the
  platforms do not build a name from a dialog's contents. So a modal
  needs a `label`, and kui warns with `modal-without-name` if it has
  none. The label is not drawn; the title text inside the dialog is a
  separate node, and it is fine for the two to say the same thing.

kui does not close the dialog. It reports `dismiss`, and the app
decides. Here it stops declaring:

```rust,noplayground
    fn on_event(&mut self, ev: UiEvent) {
        match ev.message::<Msg>() {
            Some(Msg::Ask) => self.asking = true,
            Some(Msg::Confirm) => {
                self.deleted += 1;
                self.asking = false;
            }
            // The core closes nothing. Both a Cancel click and a dismiss
            // end the same way: the view stops declaring the dialog.
            Some(Msg::Cancel) | Some(Msg::Dialog) => self.asking = false,
            None => {}
        }
    }
```

## Order matters

Floats stack in the order they are declared. The dialog is declared
last in `view`, so it is on top. Declare it first and the page would
paint over it — and kui would warn, `modal-behind-content`.

## Where a float is cut

A float anchored to the window, like this dialog, is placed against the
window and shows whatever box it was declared in. One anchored to its
parent (`FloatConfig::parent()`, `below()`, `above()`) is part of what
that parent holds: the nearest box above it that clips or scrolls cuts
it, and it takes presses only where it shows. A menu hung off a button in
a scroller says it leaves, `.escaping()`, so it opens whole over
whatever is beside the scroller; the hint a `.tooltip` floats leaves on
its own. Nothing else takes a node out of its box — not a move, a turn
or an animation ([ADR
0074](https://github.com/qxuken/kui/blob/main/docs/adr/0074-paint-stays-in-the-box-it-is-in.md)).

## Try this

- Right-click anywhere and open a menu there. `.on_context_menu(msg)`
  on the root delivers `msg` as a `contextmenu` event, the point inside
  it (`ev.split` reads it as `Event::ContextMenu(at)`); declare
  a modal float at `FloatConfig::viewport().inside(Start, Start)
  .offset(x, y).fit()`. The reference counter does exactly this.
- Give the dialog `.initial_focus()` on the Cancel button, so Enter
  cancels.

## Where this is decided

- The reference examples:
  [`widgets/tooltip.rs`](examples/widgets/tooltip.md),
  [`features/modal.rs`](examples/features/modal.md),
  [`apps/counter.rs`](examples/apps/counter.md) for the
  context menu.
