Modal
A dialog for a form or a detail, centred on a desktop and a sheet along the bottom of a phone.
'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>
</>
);
}Import
import { Modal } from '@nyxon/nasim';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.
<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
A detail
Shown above. Open it at the 390 px width to see the phone sheet.
A short message
presentation="dialog" keeps it a centred card on every screen, with a margin on a phone.
'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>
</>
);
}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.FormDialogasks 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 aModal.
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
| 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
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).