wealth/packages/island/src/molecules/sheet/components/SheetPopover.tsx

72 lines
2.1 KiB
TypeScript

'use client';
import { useId, useRef } from 'react';
import { FlexBox } from '../../../atoms/flex-box';
import { Typography } from '../../../atoms/typography';
import { Element } from '../../../primitives/element';
import { cn } from '../../../tools/cn';
import { useDismiss } from '../hooks/use-dismiss';
import { useFlip } from '../hooks/use-flip';
import type { SheetPopoverProps } from '../types';
import { SheetHead } from './SheetHead';
/**
* A desktop popover, 320px, anchored by its caller in a positioned container next to the
* trigger. It hangs under the trigger at the inline end, and flips above or to the inline start
* when it would run off the screen. No scrim and not modal: Escape, the ✕ or a press outside
* closes it.
*/
export function SheetPopover({
open,
title,
icon,
children,
footer,
onClose,
closeLabel,
className,
}: SheetPopoverProps) {
const ref = useRef<HTMLDialogElement>(null);
const titleId = useId();
useDismiss(ref, open, onClose);
useFlip(ref, open);
if (!open) return null;
return (
// A non-modal <dialog open>: in place, no scrim, and the page behind stays usable.
<FlexBox
tag="dialog"
open
ref={ref}
aria-labelledby={titleId}
direction="flex-col"
className={cn(
'absolute start-auto end-0 top-full z-menu m-0 mt-2 w-80 text-text-primary max-w-[calc(100vw-2rem)] rounded-lg border border-border-subtle bg-surface-raised p-4 shadow-2',
'data-above:top-auto data-above:bottom-full data-above:mt-0 data-above:mb-2 data-align-start:start-0 data-align-start:end-auto',
className,
)}
>
{icon && <Element className="mb-3">{icon}</Element>}
<SheetHead
title={title}
titleId={titleId}
onClose={onClose}
closeLabel={closeLabel}
compact
/>
{children != null && (
<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>
);
}