# Modal

> A dialog for a form or a detail, centred on a desktop and a sheet along the bottom of a phone.

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

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

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

  const copy = {
    open: 'جزئیات فاکتور',
    title: 'فاکتور ۱۰۴۲',
    description: 'صادرشده در ۱۲ آبان ۱۴۰۵',
    amount: 'مبلغ',
    amountValue: '۴۵۰٬۰۰۰ تومان',
    status: 'وضعیت',
    statusValue: 'پرداخت‌شده',
    method: 'روش پرداخت',
    methodValue: 'کارت به کارت',
    close: 'بستن',
    print: 'چاپ فاکتور',
  };

  // Centred from 640px up, a sheet along the bottom below it. The cancel first, the main action last.
  export default function Detail() {
    const [isOpen, setOpen] = useState(false);
    return (
      <>
        <Button variant="secondary" onPress={() => setOpen(true)}>
          {copy.open}
        </Button>
        <Modal
          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.print}</Button>
            </>
          }
        >
          <KeyValueList
            items={[
              { label: copy.amount, value: copy.amountValue, numeric: true },
              { label: copy.status, value: copy.statusValue },
              { label: copy.method, value: copy.methodValue },
            ]}
          />
        </Modal>
      </>
    );
  }
  ```
</Example>

## Import [#import]

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

## Usage [#usage]

Controlled only: `isOpen` and `onOpenChange`. `title`, `description` (under the title; it describes the
dialog), `children` (the body, which scrolls) and `footer` (the cancel first, the main action last). It
is a client component.

```tsx
<Modal
  isOpen={open}
  onOpenChange={setOpen}
  title={t.team.invite}
  footer={<Button onPress={send}>{t.team.send}</Button>}
>
  {fields}
</Modal>
```

From 640px up it is centred; below that it is a sheet along the bottom edge, edge to edge, up to 85% of
the visible height, clear of the safe areas. It is one element at every width, so turning a phone never
remounts it or loses what was typed. The phone sheet does not drag; a sheet that drags is `BottomSheet`.

## Examples [#examples]

### A detail [#a-detail]

Shown above. Open it at the 390 px width to see the phone sheet.

### A short message [#a-short-message]

`presentation="dialog"` keeps it a centred card on every screen, with a margin on a phone.

<Example name="modal/message" height="420">
  ```tsx title="modal/message.tsx"
  'use client';

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

  const copy = {
    open: 'دربارهٔ تمدید خودکار',
    title: 'تمدید خودکار',
    body: 'یک روز پیش از پایان اشتراک، اگر موجودی کیف پول کافی باشد، همان پلن دوباره خریده می‌شود. هر وقت بخواهید می‌توانید آن را از تنظیمات خاموش کنید.',
    ok: 'متوجه شدم',
  };

  // A short message: presentation="dialog" keeps it a centred card on every screen.
  export default function Message() {
    const [isOpen, setOpen] = useState(false);
    return (
      <>
        <Button variant="ghost" onPress={() => setOpen(true)}>
          {copy.open}
        </Button>
        <Modal
          isOpen={isOpen}
          onOpenChange={setOpen}
          presentation="dialog"
          size="sm"
          title={copy.title}
          footer={<Button onPress={() => setOpen(false)}>{copy.ok}</Button>}
        >
          <p>{copy.body}</p>
        </Modal>
      </>
    );
  }
  ```
</Example>

## Rules [#rules]

* A form with unsaved input passes `isDismissable={false}`: the backdrop and Escape close nothing, its
  close button and the host's Back still do. `FormDialog` asks before discarding instead.
* While a request runs, pass `isBusy`: nothing closes it until the request is over (after ten seconds
  busy, the close button and Back work again, so a stalled request cannot trap the person).
* A destructive or money-moving action asks in a `ConfirmDialog`, not a `Modal`.

## Accessibility [#accessibility]

It is a modal dialog named by its `title` and described by its `description`. The focus moves into it
when it opens, stays inside while it is open, and returns to the control that opened it when it closes.
Escape and the backdrop close it, unless it is not dismissable or busy. Its corner button is named
«بستن پنجره» (`closeLabel` renames it), so a «بستن» in the footer is the only button of that name. The
body is a named keyboard stop only when it scrolls. It registers as a layer, so Telegram's Back button
closes it.

## API [#api]

| Prop            | Type                           | Default  | Description                                                                                                                                                                 |
| --------------- | ------------------------------ | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `isOpen`        | `boolean`                      | required |                                                                                                                                                                             |
| `onOpenChange`  | `(isOpen: boolean) => void`    | required |                                                                                                                                                                             |
| `title`         | `ReactNode`                    | required |                                                                                                                                                                             |
| `description`   | `ReactNode`                    | —        | One or two sentences under the title; the dialog is described by it.                                                                                                        |
| `children`      | `ReactNode`                    | required |                                                                                                                                                                             |
| `footer`        | `ReactNode`                    | —        | Action buttons: the cancel first, the main action last. It stays in view under the body.                                                                                    |
| `size`          | `"lg" \| "md" \| "sm" \| "xs"` | —        |                                                                                                                                                                             |
| `presentation`  | `"dialog" \| "responsive"`     | —        | `"responsive"` (default): a centred dialog from 640px up, a sheet along the bottom edge below it. `"dialog"` keeps it centred on every screen (a short message, a picture). |
| `isDismissable` | `boolean`                      | —        | `false` for input a stray press must not lose: the backdrop and Escape close nothing; Back still does.                                                                      |
| `isBusy`        | `boolean`                      | —        | A request is running: nothing closes it, not even its close button, until it is over.                                                                                       |
| `closeLabel`    | `string`                       | —        | Names the close button; defaults to «بستن پنجره».                                                                                                                           |
| `className`     | `string`                       | —        |                                                                                                                                                                             |

## Related [#related]

`ConfirmDialog` (a destructive action), `FormDialog` (a form that asks before discarding),
`BottomSheet` (choices on a phone, with a drag) and `SidePanel` (a row's detail).