wealth/packages/island/src/molecules/modal/Modal.tsx

112 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useId } from 'react';
import { Button } from '../../atoms/button';
import { FlexBox } from '../../atoms/flex-box';
import { Typography } from '../../atoms/typography';
import { Dialog } from '../../atoms/dialog';
import { Element } from '../../primitives/element';
import { cn } from '../../tools/cn';
import type { ModalProps } from './types';
import { modalPanelVariants } from './variants';
/**
* A centred dialog for short, blocking decisions on desktop, at most 420px wide. On mobile the
* same content is always a bottom Sheet, never a Modal. The panel takes focus when it opens, and
* focus returns to the trigger when it closes.
*
* `size="wide"` is the desktop form dialog (C4 ۷.۳, «پست جدید»): 640px, a header row with the
* title and `headerAction`, then the caller's body and footer bar, edge to edge.
*/
export function Modal({
open,
title,
children,
actions,
icon,
size = 'default',
headerAction,
onClose,
onExited,
closeLabel,
dismissible = true,
alert,
className,
}: ModalProps) {
const titleId = useId();
return (
<Dialog
open={open}
onClose={onClose}
onExited={onExited}
dismissible={dismissible}
alert={alert}
labelledBy={titleId}
className="items-center justify-center p-4"
>
{size === 'wide' ? (
<FlexBox
data-dialog-panel=""
tabIndex={-1}
direction="flex-col"
className={cn(modalPanelVariants({ size }), className)}
>
<FlexBox
tag="header"
alignItems="items-center"
gap="gap-3"
className="flex-none border-b border-border-subtle py-2.5 ps-6 pe-4"
>
<Typography
tag="h2"
id={titleId}
variant="heading-sm"
className="min-w-0 flex-1"
>
{title}
</Typography>
{headerAction}
</FlexBox>
{children}
</FlexBox>
) : (
<Element
data-dialog-panel=""
tabIndex={-1}
className={cn(modalPanelVariants({ size }), className)}
>
{closeLabel && dismissible && (
<Button
variant="ghost"
icon="close"
label={closeLabel}
onClick={onClose}
className="absolute end-2 top-2"
/>
)}
{icon && <Element className="mb-3">{icon}</Element>}
<Typography
tag="h2"
id={titleId}
variant="heading-sm"
className="mb-2 pe-10"
>
{title}
</Typography>
{children != null && (
// Plain text reads as body-md; paragraphs keep space-3 between them.
<Typography tag="div" variant="body-md" className="[&>p]:mb-3">
{children}
</Typography>
)}
{actions && (
<FlexBox direction="flex-col" gap="gap-2" className="mt-6">
{actions}
</FlexBox>
)}
</Element>
)}
</Dialog>
);
}