wealth/packages/island/src/molecules/sheet/Sheet.tsx

98 lines
2.6 KiB
TypeScript

import { useId } from 'react';
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 { SheetHead } from './components/SheetHead';
import { SheetPopover } from './components/SheetPopover';
import type { SheetProps } from './types';
import { sheetLayerVariants, sheetPanelVariants } from './variants';
/**
* One surface in three placements: `bottom` for every mobile dialog, `side` for desktop detail
* panels, `popover` for desktop share and support. Render the same content in the mobile and
* desktop placement. Bottom and side panels take focus when they open; focus returns to the
* trigger when they close.
*/
export function Sheet({
placement = 'bottom',
open,
title,
icon,
children,
footer,
onClose,
onExited,
closeLabel,
dismissible = true,
alert,
popoverWidth,
className,
}: SheetProps) {
const titleId = useId();
if (placement === 'popover') {
return (
<SheetPopover
open={open}
title={title}
icon={icon}
footer={footer}
onClose={onClose}
closeLabel={closeLabel}
width={popoverWidth}
className={className}
>
{children}
</SheetPopover>
);
}
return (
<Dialog
open={open}
onClose={onClose}
onExited={onExited}
dismissible={dismissible}
alert={alert}
labelledBy={titleId}
className={sheetLayerVariants({ placement })}
>
<FlexBox
data-dialog-panel=""
tabIndex={-1}
direction="flex-col"
className={cn(sheetPanelVariants({ placement }), className)}
>
{placement === 'bottom' && (
<Element
tag="span"
aria-hidden
className="mx-auto mb-2 h-1 w-9 flex-none rounded-full bg-border-strong"
/>
)}
{icon && <Element className="mb-3">{icon}</Element>}
<SheetHead
title={title}
titleId={titleId}
onClose={onClose}
closeLabel={dismissible ? closeLabel : undefined}
/>
{children != null && (
// As Modal's body, so the same content reads the same on mobile and desktop.
<Typography tag="div" variant="body-md" className="[&>p]:mb-3">
{children}
</Typography>
)}
{footer && (
<FlexBox direction="flex-col" gap="gap-2" className="mt-5">
{footer}
</FlexBox>
)}
</FlexBox>
</Dialog>
);
}