# Drawer

A drawer panel that slides in over the `bottom`, `top`, `left` or `right` edge of its container. Place it as a **sibling** of the content it covers, inside a positioned container — the element stretches to fill that container and slides the drawer over it. The drawer can be resized from its inner edge, optionally snaps to forced positions, can be dragged in and out even over scrolling content, has a header that can hold arbitrary controls (or render vertically on left/right drawers), and supports a floating handle mode.

```html
<div class="panel" style="position: relative; overflow: hidden">
  <div>Main content behind the drawer</div>
  <ixfx-drawer label="Inspector" open>
    Drawer content here
  </ixfx-drawer>
</div>
```

The container must establish a positioning context (`position: relative`, `absolute`, or `fixed`) and should set `overflow: hidden` so the drawer is clipped while closed or animating. The element itself is `pointer-events: none` except over the drawer, so the sibling content underneath stays interactive.

---

## `ixfx-drawer`

### Properties / attributes

| Attribute / Property   | Type               | Default       | Description                                              |
|------------------------|--------------------|---------------|----------------------------------------------------------|
| `edge`                 | `'bottom' \| 'top' \| 'left' \| 'right'` | `bottom` | Which edge of the container the drawer is anchored to |
| `open`                 | boolean            | `false`       | Whether the drawer is visible                            |
| `size`                 | number             | `320`         | Extent in px along the drag axis — height for top/bottom, width for left/right |
| `min-size`             | number             | `160`         | Minimum size before fade-out begins                      |
| `dismiss-threshold`    | number             | `96`          | Drag below this size to close the drawer                 |
| `max-size-ratio`       | number             | `0.85`        | Max size as a fraction of the container's length on the drag axis |
| `snap-points`          | string / `number[]` | none         | Forced resting positions as fractions of the container length, e.g. `"25%, 70%"` (see Snap points) |
| `flick-velocity`       | number             | `0.4`         | Release speed (px/ms) that counts as a flick when `snap-points` is set |
| `vertical-title`       | boolean            | `false`       | `left`/`right` only: render the header as a vertical strip (see Custom title content) |
| `label`                | string             | `Drawer`      | Header title and accessible label                        |
| `icon-name`            | string             | `''`          | Header icon from the icon registry                       |
| `closable`             | boolean            | `true`        | Show the header close button                             |
| `collapsible`          | boolean            | `true`        | Show the expand/collapse caret                           |
| `maintain-handle`      | boolean            | `false`       | Keep a drag handle docked to the edge when collapsed     |
| `name`                 | string             | `''`          | Command namespace (see Commands section)                 |
| `registry`             | CommandRegistry    | —             | `@clinth/ui-commands` registry for named commands        |

### Methods

| Method            | Description                                                         |
|-------------------|---------------------------------------------------------------------|
| `show()`          | Open the drawer                                                     |
| `close()`         | Close the drawer (fires `drawer-close`)                             |
| `toggle()`        | Open if closed, close if open                                       |
| `snapTo(index)`   | Resize to the snap point at `index`, opening the drawer if needed   |

### Slots

| Slot             | Description                                                    |
|------------------|----------------------------------------------------------------|
| default          | Drawer body content                                            |
| `title`          | Arbitrary header content (inputs, buttons, tabs…) shown in place of the `label` text |
| `toolbar`        | Header toolbar items (forwarded to titlebar)                   |
| `title-context`  | Content next to the header title (forwarded to titlebar), e.g. an `ixfx-led` |

### Events

| Event            | Detail                        | Description                          |
|------------------|-------------------------------|--------------------------------------|
| `drawer-close`   | —                             | Fired when drawer is closed          |
| `drawer-resize`  | `{ size: number, snapIndex?: number }` | Fired when a resize is committed; `snapIndex` is set when `snap-points` is in use |

### Commands

When a `name` is set, the drawer registers commands as `<name>.<command>`:

| Command      | Description                              |
|--------------|------------------------------------------|
| `expand`     | Open the drawer                          |
| `collapse`   | Close the drawer (fires `drawer-close`)  |
| `toggle`     | Toggle open/closed state                  |

```js
// Invoke commands via the registry
drawer.registry?.invoke('inspector.expand');
drawer.registry?.invoke('inspector.toggle');
```

Share a registry across multiple drawers to control them globally:

```js
const shared = createCommandRegistry();
firstDrawer.registry = shared;
secondDrawer.registry = shared;
```

---

## Resizing

The drawer's inner edge (the one facing the content) is a resize grip. Drag it to resize; dragging the header does the same. Keyboard users can focus the grip and use the arrow keys — the one pointing away from the anchored edge grows the drawer, the opposite one shrinks it (and closes it below the dismiss threshold):

| Edge     | Grow         | Shrink       |
|----------|--------------|--------------|
| `bottom` | `ArrowUp`    | `ArrowDown`  |
| `top`    | `ArrowDown`  | `ArrowUp`    |
| `left`   | `ArrowRight` | `ArrowLeft`  |
| `right`  | `ArrowLeft`  | `ArrowRight` |

- Arrow keys resize by 16px (64px with Shift)
- `Home` — resize to the minimum size
- `End` — resize to the maximum size

When closed, keyboard resize on the grip opens the drawer.

---

## Snap points

Set `snap-points` to force the drawer to rest only at certain positions along its drag axis, as fractions of the container's length on that axis. Percentages (`"25%"`) and plain fractions (`"0.25"`) are both accepted.

```html
<!-- Left drawer: rests at either 25% or 70% of the container's width -->
<ixfx-drawer edge="left" label="Library" snap-points="25%, 70%" open>…</ixfx-drawer>
```

```js
drawer.snapPoints = [0.25, 0.7];
drawer.snapTo(1); // jump to 70%
```

- While dragging, the drawer follows the pointer freely; on release it settles on a snap point.
- A **slow release** lands on the nearest snap point.
- A **quick flick** (release speed ≥ `flick-velocity`, default `0.4` px/ms) moves to the *next* snap point in the flick direction, even if you hadn't dragged past the halfway mark. Flicking toward the anchored edge from the smallest snap point closes the drawer.
- A size set programmatically (`size`) is forced onto the nearest snap point. Snap points may exceed `max-size-ratio`.
- Keyboard: the grow/shrink arrows step between snap points; `Home` / `End` jump to the first / last.
- `drawer-resize` reports `snapIndex`.

---

## Dragging over scrolling content

Drawers often hold long, scrollable content, which normally steals the touch gesture. On touch, a drag that starts in the drawer body and runs along the drawer's axis is arbitrated:

- **Pulling out** (growing): the drawer follows the finger until it reaches its maximum size; only then does the content scroll.
- **Pushing in** (shrinking): the content scrolls first. Once every scrollable element under the finger is at its start (for `top`/`left` drawers: its end), the drag moves the drawer instead.
- Drags that are mostly across the axis scroll natively.

The decision is made once, when the drag begins, and stays in effect until the finger lifts. Mouse users have no drag-to-scroll, so they use the grip or the header (buttons in the header keep working).

## Maintain handle mode

When `maintain-handle` is `true`, a small draggable tab remains docked at the drawer's edge when it is closed. Drag it out to reopen.

```html
<div class="panel" style="position: relative; overflow: hidden">
  <div>Main content</div>
  <ixfx-drawer label="Inspector" maintain-handle>
    Inspector content
  </ixfx-drawer>
</div>
```

---

## With toolbar

Toolbar items slotted into `toolbar` are forwarded into the `ixfx-titlebar` toolbar:

```html
<div class="panel" style="position: relative; overflow: hidden">
  <div>Main content</div>
  <ixfx-drawer label="Inspector" open>
    <div slot="toolbar">
      <button type="button" data-tooltip="Refresh">
        <ixfx-icon name="refresh"></ixfx-icon>
      </button>
    </div>
    Inspector content
  </ixfx-drawer>
</div>
```

---

## Custom title content

Slot any content into `title` to replace the label text — handy for embedding controls in the header. `label` still names the drawer for assistive technology.

```html
<ixfx-drawer label="Search results" open>
  <div slot="title" style="display: flex; gap: 4px">
    <input type="search" placeholder="Filter…" />
    <button type="button">Errors</button>
  </div>
  Results…
</ixfx-drawer>
```

Native controls, ARIA buttons, editable content, custom elements and anything marked `data-drawer-no-drag` keep their own pointer behaviour; dragging any other part of the header still resizes the drawer.

### Vertical title bar

On `left` / `right` drawers, `vertical-title` renders the header as a vertical strip along the anchored side. The title text runs sideways, toolbar items stack down the strip, and `title`-slotted content stays upright. It has no effect on `top` / `bottom` drawers.

```html
<ixfx-drawer edge="left" vertical-title label="Outline" open>…</ixfx-drawer>
```

---

## Title context

Use `title-context` for status content next to the header title. It is forwarded to the `ixfx-titlebar` and stays in the light DOM, so it can be updated from elsewhere:

```html
<ixfx-drawer label="Inspector" open>
  <ixfx-led slot="title-context" id="inspector-status" color="oklch(70% 0.2 145)"></ixfx-led>
  Inspector content
</ixfx-drawer>

<script>
  document.querySelector('#inspector-status').lit = true;
</script>
```

---

## CSS custom properties

| Property                               | Default                                      | Description                              |
|----------------------------------------|----------------------------------------------|------------------------------------------|
| `--drawer-background`           | `color-mix(in srgb, var(--surface-2) 84%, transparent)` | Drawer fill                     |
| `--drawer-border`               | `1px solid color-mix(in srgb, var(--surface-2-text) 14%, transparent)` | Drawer border |
| `--drawer-radius`               | `var(--radius-l)`                            | Radius of the corners facing the content |
| `--drawer-blur`                 | `18px`                                       | Backdrop blur amount                     |
| `--drawer-shadow`               | `var(--shadow-l)`                            | Drop shadow                              |
| `--drawer-collapsed-width`      | `96px`                                       | Length of floating pill (maintain-handle) |
| `--drawer-title-background`     | `transparent`                                | Header fill                              |
| `--ixfx-handle-*`                     | (see handle docs)                            | Resize grip styling                      |

---

## TypeScript types

```ts
import type {
  DrawerCloseEvent,
  DrawerCommand,
  DrawerEdge,
  DrawerElement,
  DrawerResizeDetail,
  DrawerResizeEvent,
} from 'ixfxfun-components';
import { drawerCommandId } from 'ixfxfun-components';
```

`drawerCommandId(name, command)` builds the command id for a named drawer, e.g. `drawerCommandId('inspector', 'expand')` → `'inspector.expand'`.

---

## See also

- [`ixfx-titlebar`](./titlebar.md) — Header component used by the drawer
- [`ixfx-icon`](./icons.md) — Icon component for header icons
