# SidePanel

> A row's detail and its edit form, a drawer from the inline-end edge on a desktop and a bottom sheet on a phone.

- Import: `import { SidePanel } from '@nyxon/nasim';`
- Server Component: no, it needs a client component
- Since: 0.8.0; status: stable
- Source: libs/nasim/src/composites/SidePanel.tsx
- Describes: @nyxon/nasim unreleased, after 0.12.0
- Page: https://nasim.nyxon.tech/docs/components/overlays/side-panel

<Example name="side-panel/detail" height="560">
  ```tsx title="side-panel/detail.tsx"
  'use client';

  import {
    Button,
    CopyText,
    KeyValueList,
    SidePanel,
    StatusChip,
  } from '@nyxon/nasim';
  import { useState } from 'react';

  const copy = {
    open: 'جزئیات سرویس',
    title: 'ماهانه ۵۰ گیگ',
    description: 'خریده‌شده در ۱ آبان ۱۴۰۵',
    status: 'وضعیت',
    active: 'فعال',
    left: 'باقی‌مانده',
    leftValue: '۱۸ روز، ۳۲ گیگابایت',
    id: 'شناسهٔ سرویس',
    close: 'بستن',
    renew: 'تمدید',
  };
  // A sample id; a real one comes from the API.
  const serviceId = 'srv-1042';

  // A row's detail: a drawer from the inline-end edge on a desktop, the bottom sheet on a phone.
  export default function Detail() {
    const [isOpen, setOpen] = useState(false);
    return (
      <>
        <Button variant="secondary" onPress={() => setOpen(true)}>
          {copy.open}
        </Button>
        <SidePanel
          isOpen={isOpen}
          onOpenChange={setOpen}
          title={copy.title}
          description={copy.description}
          footer={
            <>
              <Button variant="tertiary" onPress={() => setOpen(false)}>
                {copy.close}
              </Button>
              <Button onPress={() => setOpen(false)}>{copy.renew}</Button>
            </>
          }
        >
          <KeyValueList
            layout="stacked"
            items={[
              {
                label: copy.status,
                value: <StatusChip tone="success" label={copy.active} />,
              },
              { label: copy.left, value: copy.leftValue },
              {
                label: copy.id,
                value: <CopyText value={serviceId} label={copy.id} isLtr />,
              },
            ]}
          />
        </SidePanel>
      </>
    );
  }
  ```
</Example>

## Import [#import]

```tsx
import { SidePanel } from '@nyxon/nasim';
```

## Usage [#usage]

Controlled only: `isOpen` and `onOpenChange`. `title`, an optional `description` (also its accessible
description), `children` (the body, which scrolls) and `footer` (the cancel first, the main action last;
it stays under the body, so it is always in reach). `size` is `"sm"` (420 px), `"md"` (480 px, the default)
or `"lg"` (560 px); a narrower screen takes what it has. It is a client component.

From `sm` up it is a drawer from the inline-end edge: the left in a right-to-left page, the right with
`dir="ltr"`. On a phone it is the bottom sheet, with its drag handle and its safe areas.

```tsx
<SidePanel
  isOpen={open}
  onOpenChange={setOpen}
  title={t.services.detailTitle}
  footer={actions}
>
  {details}
</SidePanel>
```

## Examples [#examples]

### A row's detail [#a-rows-detail]

Shown above. Switch the preview to LTR to see the drawer change sides, or to 390 px to see the sheet.

### A long body [#a-long-body]

The body scrolls inside the panel and the page behind it does not move. A panel without a `footer` draws
none.

<Example name="side-panel/activity" height="560">
  ```tsx title="side-panel/activity.tsx"
  'use client';

  import { Button, SidePanel } from '@nyxon/nasim';
  import { useState } from 'react';

  const copy = {
    open: 'گزارش فعالیت',
    title: 'فعالیت‌های این هفته',
  };
  const events = [
    'ورود از دستگاه تازه، تهران',
    'تمدید خودکار «ماهانه ۵۰ گیگ»',
    'تغییر رمز عبور',
    'افزودن کارت بانکی',
    'خرید «هفتگی ۱۰ گیگ»',
    'ورود از تلگرام',
    'درخواست بازگشت وجه',
    'پاسخ پشتیبانی به تیکت ۸۸',
    'ورود از دستگاه تازه، شیراز',
    'تغییر شمارهٔ تماس',
    'خروج از همهٔ دستگاه‌ها',
    'خرید «ماهانه ۵۰ گیگ»',
  ];

  // A long body scrolls inside the panel (a named, focusable region); the page behind does not move.
  // No footer: a panel without one draws none.
  export default function Activity() {
    const [isOpen, setOpen] = useState(false);
    return (
      <>
        <Button variant="ghost" onPress={() => setOpen(true)}>
          {copy.open}
        </Button>
        <SidePanel
          isOpen={isOpen}
          onOpenChange={setOpen}
          title={copy.title}
          size="sm"
        >
          <ol className="flex flex-col divide-y divide-separator">
            {events.map((event) => (
              <li key={event} className="py-3">
                {event}
              </li>
            ))}
          </ol>
        </SidePanel>
      </>
    );
  }
  ```
</Example>

## Rules [#rules]

* A good detail panel, read from the top: who or what it is, small muted labels over the groups, the
  fields as a `KeyValueList` with a `CopyText` beside every id.
* `isDismissable={false}` for a form with unsaved input: only its own buttons and the host's Back close it.
  `isBusy` closes it by nothing while a request runs; after ten seconds busy the close button and the
  host's Back work again (the backdrop, a drag and Escape stay blocked), and the request is not
  cancelled, so report its outcome from your own handlers.

## Accessibility [#accessibility]

It behaves like `Modal`: a modal dialog named by its `title`, the focus inside while it is open and back on
the opener when it closes. A body that scrolls is a named, focusable region, so a keyboard can scroll it,
and scrolling stays inside it (`overscroll-behavior: contain`). It registers as a layer, so Telegram's Back
button closes it. Under `prefers-reduced-motion: reduce` it fades in and out without travelling.

## API [#api]

| Prop            | Type                        | Default  | Description                                                                               |
| --------------- | --------------------------- | -------- | ----------------------------------------------------------------------------------------- |
| `isOpen`        | `boolean`                   | required |                                                                                           |
| `onOpenChange`  | `(isOpen: boolean) => void` | required |                                                                                           |
| `title`         | `string`                    | required |                                                                                           |
| `description`   | `ReactNode`                 | —        | Under the title; the panel is described by it.                                            |
| `children`      | `ReactNode`                 | required | The scrolling body.                                                                       |
| `footer`        | `ReactNode`                 | —        | Action buttons: the cancel first, the main action last. It stays in view under the body.  |
| `size`          | `"lg" \| "md" \| "sm"`      | —        | 420, 480 or 560 px wide from `sm` up; a phone gets the bottom sheet. Default `"md"`.      |
| `isDismissable` | `boolean`                   | —        | `false` for a form with unsaved input: a backdrop press, a drag and Escape close nothing. |
| `isBusy`        | `boolean`                   | —        | A request is running: nothing closes the panel until it is over.                          |
| `closeLabel`    | `string`                    | —        | Names the close button; defaults to «بستن».                                               |
| `className`     | `string`                    | —        |                                                                                           |

## Related [#related]

`BottomSheet` (what it is on a phone), `Modal`, `KeyValueList` and `CopyText` (what a detail holds).