# BottomSheet

> A sheet from the bottom edge for choices and short forms, which drags down to close.

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

<Example name="bottom-sheet/payment" height="560">
  ```tsx title="bottom-sheet/payment.tsx"
  'use client';

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

  const copy = {
    open: 'انتخاب روش پرداخت',
    title: 'روش پرداخت',
    methods: 'روش‌های پرداخت',
    pay: 'پرداخت ۴۵۰٬۰۰۰ تومان',
  };
  const methods = [
    {
      value: 'wallet',
      title: 'کیف پول',
      description: 'موجودی: ۶۲۰٬۰۰۰ تومان',
    },
    {
      value: 'card',
      title: 'کارت به کارت',
      description: 'تأیید رسید تا ۱۵ دقیقه',
    },
    {
      value: 'gateway',
      title: 'درگاه بانکی',
      description: 'پرداخت آنلاین با همهٔ کارت‌های عضو شتاب',
    },
  ];

  // A choice on a phone: a sheet from the bottom edge, with the action in its footer.
  export default function Payment() {
    const [isOpen, setOpen] = useState(false);
    const [method, setMethod] = useState('wallet');
    return (
      <>
        <Button onPress={() => setOpen(true)}>{copy.open}</Button>
        <BottomSheet
          isOpen={isOpen}
          onOpenChange={setOpen}
          title={copy.title}
          footer={
            <Button size="lg" onPress={() => setOpen(false)}>
              {copy.pay}
            </Button>
          }
        >
          <OptionCards
            label={copy.methods}
            hideLabel
            value={method}
            onChange={setMethod}
            options={methods}
          />
        </BottomSheet>
      </>
    );
  }
  ```
</Example>

## Import [#import]

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

## Usage [#usage]

Controlled: `isOpen` and `onOpenChange`. `title`, an optional `description` (it describes the sheet),
the body as children and the actions in `footer`; footer buttons fill the width on a phone. It is a
client component.

```tsx
<BottomSheet
  isOpen={open}
  onOpenChange={setOpen}
  title={t.checkout.paymentMethod}
  footer={
    <Button size="lg" onPress={pay}>
      {t.checkout.pay}
    </Button>
  }
>
  {methods}
</BottomSheet>
```

It keeps clear of the safe areas, and its height is at most 85% of the visual viewport, which shrinks
when the on-screen keyboard opens, so a field and the footer stay above the keyboard. Inside Telegram it
also stays under `--tg-viewport-stable-height`.

## Examples [#examples]

### A choice [#a-choice]

Shown above: a payment method, picked from `OptionCards`, and the payment in the footer.

### Money [#money]

A sheet that moves money passes `isDismissable={false}`: the backdrop, a drag down and Escape close
nothing, the handle is not drawn, and only its own buttons, the close button and the host's Back close
it. While the request runs, `isBusy` closes it by nothing, so its result is seen. After ten seconds busy,
the close button and the host's Back work again, so a stalled request cannot trap the person; the
backdrop, a drag and Escape stay blocked. The request is not cancelled: the sheet only closes, and when
the request settles later it acts on nothing, so report its outcome from your own handlers (a toast).

<Example name="bottom-sheet/money" height="460">
  ```tsx title="bottom-sheet/money.tsx"
  'use client';

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

  const copy = {
    open: 'انتقال موجودی',
    title: 'انتقال ۲۰۰٬۰۰۰ تومان',
    description: 'این مبلغ از کیف پول شما کم و به حساب گیرنده اضافه می‌شود.',
    to: 'گیرنده',
    toValue: 'فروشگاه نمونه',
    fee: 'کارمزد',
    feeValue: 'بدون کارمزد',
    cancel: 'انصراف',
    send: 'انتقال بده',
  };

  // A sheet that moves money is not dismissable: no backdrop tap, drag or Escape, only its buttons.
  // While the request runs it is busy, and nothing closes it.
  export default function Money() {
    const [isOpen, setOpen] = useState(false);
    const [isBusy, setBusy] = useState(false);
    const send = () => {
      setBusy(true);
      setTimeout(() => {
        setBusy(false);
        setOpen(false);
      }, 1500);
    };
    return (
      <>
        <Button variant="secondary" onPress={() => setOpen(true)}>
          {copy.open}
        </Button>
        <BottomSheet
          isOpen={isOpen}
          onOpenChange={setOpen}
          isDismissable={false}
          isBusy={isBusy}
          title={copy.title}
          description={copy.description}
          footer={
            <>
              <Button
                variant="tertiary"
                isDisabled={isBusy}
                onPress={() => setOpen(false)}
              >
                {copy.cancel}
              </Button>
              <Button isPending={isBusy} onPress={send}>
                {copy.send}
              </Button>
            </>
          }
        >
          <KeyValueList
            items={[
              { label: copy.to, value: copy.toValue },
              { label: copy.fee, value: copy.feeValue },
            ]}
          />
        </BottomSheet>
      </>
    );
  }
  ```
</Example>

## Rules [#rules]

* For choices and short forms on a phone. A row's detail is a `SidePanel`; a dialog that suits every
  screen is a `Modal`.
* Pass `isBusy` while a request runs, so its result is seen, and expect a close after ten seconds:
  report the outcome from your own handlers, not from the sheet.

## Accessibility [#accessibility]

It is a modal dialog named by its `title` and described by its `description`; the focus moves into it
and returns to the control that opened it. It closes with Escape, a backdrop tap, a drag down, the close
button or the host's Back. A press on the backdrop is ignored for half a second after the content changes
height, so a tap meant for a button that just moved does not close it. The close button says «بستن»
(`closeLabel` renames it). 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`         | `string`                    | required |                                                                                                                   |
| `description`   | `ReactNode`                 | —        | Under the title; the sheet is described by it.                                                                    |
| `children`      | `ReactNode`                 | required |                                                                                                                   |
| `footer`        | `ReactNode`                 | —        | Action buttons; the sheet closes on Escape, backdrop tap, drag down or the close button.                          |
| `isDismissable` | `boolean`                   | —        | `false` for a flow that moves money: the backdrop, a drag and Escape close nothing; its close button and Back do. |
| `isBusy`        | `boolean`                   | —        | A request is running: nothing closes the sheet, not Back, not Escape, not its close button.                       |
| `closeLabel`    | `string`                    | —        | Names the close button; defaults to «بستن».                                                                       |
| `className`     | `string`                    | —        |                                                                                                                   |

## Related [#related]

`Modal`, `SidePanel` (on a phone it is this sheet) and `OptionCards` (choices that need more than a
word).