"use client"; import * as React from "react"; import { cn } from "@/lib/utils"; import { useIsMobile } from "@/hooks/use-is-mobile"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Sheet, SheetContent, SheetDescription, SheetFooter, SheetHeader, SheetTitle, } from "@/components/ui/sheet"; export interface ResponsiveSheetProps { /** Controlled open state. */ open: boolean; /** Open-state setter forwarded to the underlying Sheet / Dialog root. */ onOpenChange: (open: boolean) => void; /** * Required accessible title. Rendered inside the sheet / dialog * header so screen-readers always announce the surface, even when * the visual header is empty. */ title: React.ReactNode; /** Optional short description rendered beneath the title. */ description?: React.ReactNode; /** * Hide the visual header (title + description). The title remains * mounted in an `sr-only` block so the accessible name is still * announced. Use when the consumer renders its own header inside * the body. */ hideHeader?: boolean; /** * Footer slot. The Sheet branch sticky-pins the footer to the * bottom of the bottom-sheet so the primary CTA stays reachable * once the body scrolls; the Dialog branch lets the footer flow * normally beneath the body. */ footer?: React.ReactNode; /** Class name applied to the content surface (sheet / dialog). */ className?: string; /** Class name applied to the body wrapper. */ bodyClassName?: string; /** * Forward to the underlying Sheet/Dialog `showCloseButton` * primitive prop. Defaults to `true`; pass `false` when the * consumer renders its own close affordance inside the header. */ showCloseButton?: boolean; /** * Desktop (Dialog branch) content width. The bottom-sheet branch * always spans the viewport, so this prop only affects the `md+` * Dialog. Defaults to `"md"` (448 px) to preserve every existing * consumer; pass `"4xl"` for the wide medication editor's * two-column layout. The class is the only width utility applied — * the prior `cn("sm:max-w-md", className)` ordering let a * consumer-passed width silently collide with the hardcoded * default, so the width now resolves from this single source. */ contentWidth?: "md" | "lg" | "2xl" | "3xl" | "4xl"; children: React.ReactNode; } const CONTENT_WIDTH_CLASS: Record< NonNullable, string > = { md: "sm:max-w-md", lg: "sm:max-w-lg", "2xl": "sm:max-w-2xl", "3xl": "sm:max-w-3xl", "4xl": "sm:max-w-4xl", }; /** * v1.4.27 R3d MB1 — shared mount primitive that flips between a * bottom-anchored `` on narrow viewports and the existing * centred `` on tablet/desktop. Picks the breakpoint via * the `useIsMobile()` hook (Tailwind `md` — 768 px). * * On the Sheet branch we cap the content at `90dvh`, scroll the * body on overflow, and sticky-pin the footer so Save / Cancel * stay reachable when the keyboard pushes the bottom of the sheet * up. The Dialog branch consumes the primitive's `max-h-[calc(100dvh-2rem)]` * default so long forms scroll inside the dialog instead of * spilling off-screen on tablet portrait. * * Consumers swap `` → `` and feed the * existing form markup through `children` / `footer`. The rest of * the form's chrome is unchanged because both branches expose the * same header / body / footer slot shape. */ export function ResponsiveSheet({ open, onOpenChange, title, description, hideHeader = false, footer, className, bodyClassName, showCloseButton = true, contentWidth = "md", children, }: ResponsiveSheetProps) { const isMobile = useIsMobile(); if (isMobile) { return ( {hideHeader ? ( {title} {description ? ( {description} ) : null} ) : ( {title} {description ? ( {description} ) : null} )}
{children}
{footer ? ( {footer} ) : null}
); } return ( {hideHeader ? ( {title} {description ? ( {description} ) : null} ) : ( {title} {description ? ( {description} ) : null} )}
{children}
{footer ? ( {footer} ) : null}
); }