# Popover

Crate: `libero`
Import: `use libero::hooks::{use_element, use_popover, Align, PopoverOptions, PopoverWidth, Side};`
Feature: `use-popover` (on by default, turns on `use-focus-return`; [features.md](features.md))
Source: <https://github.com/tdymel/libero/tree/main/libero/src/hooks/popover/mod.rs>
Index: [index.md](index.md) lists every other page
Description: A hook that anchors a portaled box to a trigger, flipping and shifting it to stay on screen.

A popover is a hook, not a component. A dropdown, a menu and a hover card share
where the box goes, not how it looks. `use_popover` portals the box, so no
`overflow: hidden` ancestor clips it, and flips and shifts it to stay on
screen.

## Usage

The role and the trigger's aria are yours, so the example carries them.

```rust
use dioxus::prelude::*;
use libero::{
    components::{Box, Button},
    hooks::{use_element, use_id, use_popover, Align, PopoverOptions, Side},
    use_theme,
};

#[component]
fn Demo() -> Element {
    let theme = use_theme();
    let mut opened = use_signal(|| false);
    let anchor = use_element();
    let box_id = use_id();
    let popover = use_popover(anchor, opened(), PopoverOptions::new(4.0, theme.popover.padding)
        .side(Side::Bottom)
        .align(Align::Start)
        .dismiss(true));
    popover.on_dismiss(move || opened.set(false));

    popover.show(opened().then(|| rsx! {
        Box {
            id: "{box_id}",
            role: "dialog",
            aria_label: "Example popover",
            tabindex: "-1",
            attributes: popover.floating_events(),
            style: popover.style(),
            onmounted: popover.floating().mount(),
            "Popover content"
        }
    }));

    rsx! {
        Button {
            onmounted: anchor.mount(),
            onclick: move |_| opened.toggle(),
            attributes: popover.anchor_events(),
            aria_haspopup: "dialog",
            aria_expanded: "{opened()}",
            aria_controls: "{box_id}",
            "Popover"
        }
    }
}
```

## PopoverOptions

`PopoverOptions::new(gap, padding)` takes the theme's values, so it is not
`Default`. Every other field has a builder method of the same name.

| Prop | Type | Default | Description |
|---|---|---|---|
| `side` | `Side` | `bottom` | The preferred side of the anchor. Flipping may override it. `new` starts at `Bottom`; `theme.popover.side` is what to start from to follow the theme. `Start`/`End` are logical: `Start` is the left under `dir="ltr"`, the right under `rtl`. |
| `align` | `Align` | `start` | Where the box lines up along that side. `new` starts at `Start`; `theme.popover.align` follows the theme. Across `Top`/`Bottom` it follows the direction too. |
| `gap` | `f64` | `theme.popover.gap` | Pixels between the anchor and the box. |
| `padding` | `f64` | `theme.popover.padding` | How close to a viewport edge the box may come before it flips or shifts. The box is never wider than the viewport less this on both sides. |
| `flip` | `bool` | `true` | Moves to the opposite side when the preferred one has no room. |
| `shift` | `bool` | `true` | Slides along the side to stay on screen when flipping does not help. |
| `width` | `PopoverWidth` | `Auto` | `Auto` follows the content, `Match` takes the anchor's width, and `Min` is at least the anchor's width. |
| `remeasure` | `u64` | `0` | Changing it measures the box again. Use it for an anchor that resizes while the box is open. |
| `dismiss` | `bool` | `false` | Escape and a press outside close the box, through `on_dismiss`. Spread `anchor_events()` on the trigger and `floating_events()` on the box. |

## PopoverHandle

`use_popover(anchor: ElementHandle, open: bool, options: PopoverOptions) -> PopoverHandle`

| Method | Type | Description |
|---|---|---|
| `floating()` | `&ElementHandle` | Mount it on the box. Nothing is placed until it is attached. |
| `placed()` | `bool` | Whether the box has been measured. `false` on the render that opens it. Focus a control in the box only once it is `true`: before that the box is `visibility: hidden`, and `focus()` answers `Ok(())` and moves nothing. |
| `placement()` | `Placement` | The side and align the box landed on, after flipping. |
| `style()` | `String` | The box's `style`, with its position and width. |
| `show(content)` | `Option<Element>` | Renders the box, and owns no open state: `None` removes a closed one. The box renders at the portal outlet, at the document root, so it inherits none of the context around the call site: re-provide what the content needs inside it. Popovers nest: anchor the inner one to a row in the outer box, and the one shown later paints on top. |
| `on_dismiss(f)` | `impl FnMut()` | What Escape and a press outside call, with `dismiss` on. Call it on every render. |
| `anchor_events()` | `Vec<Attribute>` | Spread on the trigger: `dismiss`, and on a WebView the link `Hotkey::within` follows into the box. |
| `floating_events()` | `Vec<Attribute>` | Spread on the box, as `anchor_events()` on the trigger; the WebView link needs both. |

## Accessibility

### Keyboard

| Key | Action |
|---|---|
| `Escape` | With `dismiss(true)`: closes the box and returns focus to the trigger. |
| `Tab` | Closes the box and moves on. Focus is not trapped. |

### Libero handles

- The hook adds no role and no keys of its own. `dismiss(true)` adds the
  Escape key and closing on focus leaving.
- With `dismiss(true)`, focus leaving the trigger and the box closes it.
- With `dismiss(true)`, Android's Back button closes the box as Escape does,
  rather than the app.

### You must

- Put a `role` on the box, and `aria-haspopup`, `aria-expanded` and
  `aria-controls` on the trigger, as the example does.
- Drive `aria-expanded` from the same signal the hook gets, or a screen reader
  hears the wrong state.
- Make Escape close the box (WCAG 1.4.13): turn on `dismiss(true)`, or handle
  it yourself.
- Give the box `tabindex="-1"`, or a click on its text moves focus out and
  closes it.
- If you animate the close, give the closing box `visibility: hidden` or
  `inert`. Until it unmounts, it is still tabbable and still announced.

### Example

A "Filter" button that opens a box with `use_popover` and `dismiss(true)`, the
box `role: "dialog"` and `tabindex: "-1"`, the button `aria-expanded` from the
same signal: Escape closes it and puts focus back on Filter.

### Limits

- The hook draws no arrow pointing at the anchor. Only `Tooltip` and `HoverCard`
  draw one.
- Safari does not focus a button on click, so there a press outside a box
  opened by pointer does not close it.
- On Android, a box opened without a tap (on mount or from a timer) may let
  Back close the app.

## Theme defaults

`PopoverDefaults` on the theme holds `side`, `align`, and `gap` and `padding`
in pixels. `side` and `align` are what your own `PopoverOptions` can start from;
`new` and the built-in dropdowns keep `Bottom` and `Start`.

## CSS variables

| Variable | Description |
|---|---|
| `--lsx-popover-gap` | The theme's `gap`. |
| `--lsx-popover-padding` | The theme's `padding`, often reused as the box's inner padding. |
| `--lsx-z-index-popover` | The stacking level for a floating box. |
