# Drawer

Crate: `libero`
Import: `use libero::hooks::{DrawerOptions, ModalScope, use_drawer};`
Feature: `drawer` (off by default, turns on `dialog`, `modal`, `float`, `use-swipe`, `use-timers`; [features.md](features.md))
Source: <https://github.com/tdymel/libero/tree/main/libero/src/components/overlay/use_drawer.rs>
Index: [index.md](index.md) lists every other page
Description: A dimmed, focus-trapped panel docked to one edge, `use_modal` with the docking around it.

A dimmed, focus-trapped panel docked to one edge. `use_drawer` is
[`use_modal`](modal.md) with the docking around it, so it has the same handle,
arguments and results. The panel is a [Dialog](dialog.md) surface. For a panel
in the page flow, use [Sidebar](sidebar.md).

## Usage

`DrawerOptions` holds what every opening shares. The closure is the panel's
content.

```rust
use dioxus::prelude::*;
use libero::{
    components::{Button, Flex, Options, Tabs, Text},
    hooks::{DrawerOptions, ModalScope, use_drawer},
    sx::sx,
};

#[derive(Clone, PartialEq, Options)]
enum Setting {
    General,
    Account,
    Billing,
    Privacy,
    Security,
    Alerts,
    Language,
    Advanced,
}

#[component]
fn Demo() -> Element {
    let mut setting = use_signal(|| Setting::Advanced);

    let nav = use_drawer(
        DrawerOptions {
            anchor: "start".into(),
            size: "md".into(),
            title: Some("Temporary drawer".into()),
            close_button: true,
            ..Default::default()
        },
        move |s: ModalScope<()>| rsx! {
            Flex {
                direction: "column",
                gap: "md",
                sx: sx().padding("16px"),
                Tabs {
                    aria_label: "Settings",
                    placement: "start",
                    sx: sx().selector("& > [role=\"tablist\"]", sx().max_height("7rem")),
                    value: setting(),
                    onchange: move |next| setting.set(next),
                    panel: |setting: Setting| rsx! { Text { "{setting.label()}" } },
                }
                Button { variant: "outlined", onclick: move |_| s.close(), "Done" }
            }
        },
    );

    rsx! {
        Button { variant: "outlined", onclick: move |_| { nav.open(); }, "Open drawer" }
    }
}
```

## API

### `use_drawer`

```rust,ignore
pub fn use_drawer<S: Clone + 'static, R: Clone + 'static>(
    options: DrawerOptions,
    render: impl FnMut(ModalScope<S, R>) -> Element + 'static,
) -> ModalHandle<S, R>
```

Returns the same `ModalHandle` as `use_modal`. It, `ModalScope` and `Opening`
behave the same. See [Modal](modal.md).

### `DrawerOptions`

`DrawerOptions` implements `Default`, so a literal sets only what it needs,
`DrawerOptions { anchor: "end".into(), ..Default::default() }`.

| Field | Type | Default | Description |
|---|---|---|---|
| `anchor` | `Input<Responsive<DrawerAnchor>>` | `start` | The edge the panel docks to. `start` is the right edge under `dir="rtl"`. It is read on every render, so an anchor in a signal moves an open drawer. Or one per breakpoint, `responsive(DrawerAnchor::Bottom).md(DrawerAnchor::End).into()`: a bottom sheet on a phone, a side panel from `md`. Read in Rust, so a drawer open while the page is server-rendered switches after mount. Breakpoints follow the window, not the parent. |
| `size` | `Input<Responsive<ThemeAwareValue>>` | `md` | Width when docked start or end, height when docked top or bottom: a size word or any CSS, e.g. `size: "24rem".into()`. Or one per breakpoint, `size: responsive(Size::Sm).md(Size::Lg)`. Breakpoints follow the window, not the parent. |
| `z_index` | `Input<ThemeAwareValue>` | - | Stacking order of the panel. |
| `aria_label` | `Option<String>` | - | Names the panel, which is a dialog. With neither this nor `title`, it warns in a debug build. |
| `title` | `Option<String>` | - | A visible heading at the top of the panel that names it. |
| `sx` | `Input<Sx>` | - | Styles the panel. The content is yours to style; a bottom drawer's swipe strip is `& > [data-slot='handle']`. |
| `ondismiss` | `Option<Callback<Dismiss, bool>>` | - | Whether Escape, the backdrop, Back, a swipe or the header close button may close it; `false` keeps it open, e.g. over an unsaved draft. Unset, all close. |
| `handle` | `bool` | `false` | Docked bottom, a touch swipe down on the panel closes it; `true` draws a grab bar in its top padding. Other anchors ignore it. Let go far enough and, once `ondismiss` allows `Dismiss::Swipe`, it slides off and closes; a short swipe springs back. Under reduced motion it closes at once. |
| `close_button` | `bool` | `false` | Draws a close button in the header, beside `title`. As a `Dialog`'s, it asks `ondismiss` with `Dismiss::CloseButton`. |

## Accessibility

### Keyboard

| Key | Action |
|---|---|
| `Escape` | Closes the drawer, as a backdrop click does, unless `ondismiss` returns `false`. |
| `Tab` or `Shift+Tab` | Moves the focus within the panel. It does not leave while the drawer shows. |

### Libero handles

- Focus moves into the panel, and back to the trigger once it closes, as with
  `use_modal`.
- A dismissal settles the `Opening` with `None`, so a result handler runs with
  `None`: match `Some(answer)` to act on an answer only.
- A `title` is a heading that names the panel; without it or `aria_label` the
  panel warns in a debug build.
- Android's Back button closes the drawer, as Escape does, rather than the app.
- Docked bottom, a touch swipe down anywhere on the panel closes it, as an
  Android bottom sheet: content scrolled down scrolls back first, and a control
  that drags itself, such as a slider or a text field, keeps its gesture. It is
  an extra route: Escape, Back, the backdrop and the content's own close button
  still work.

### You must

- Set `DrawerOptions::title` or `aria_label`, since the panel is a dialog with
  no name of its own.
- Give it a visible way to close: `close_button: true`, or a button in the content.

### Example

A filters drawer, `DrawerOptions { aria_label: Some("Filters".into()), .. }`:
focus moves into the panel, Tab stays inside it, and Escape or the panel's own
Done button closes it and returns focus to the trigger.

### Limits

- On Android, a drawer opened without a tap (on mount or from a timer) may let
  Back close the app.
- In a desktop WebView or on Android, Tab and Shift+Tab move between the
  controls but can leave the drawer at its ends.
- A pen that fires no touch events, as in Firefox or with a Windows pen, swipes
  a bottom drawer down from its grab bar only: the browser's own pan takes it
  anywhere else.

## Theme defaults

`DrawerDefaults` on the theme.

| Field | Type | Description |
|---|---|---|
| `size` | `Size` | Default `size`, `md`. |
| `sizes` | `Sizes<u16>` | Panel extent per size step, in px. |

The panel's background, shadow and padding come from the `Dialog` defaults.

## CSS variables

| Variable | Description |
|---|---|
| `--lsx-drawer-size-<size>` | Panel extent for that size step. |

Stacking order goes through `Float`'s `--lsx-z-index-float-override`.

## Data attributes

State tokens on the panel's `data-state`, space separated.

| Token | Condition |
|---|---|
| `anchor-start` / `anchor-end` / `anchor-top` / `anchor-bottom` | The `anchor` in effect. |
| `size-<size>` | The `size` in effect. |
