import { Theme } from "@radix-ui/themes" import { cva } from "class-variance-authority" import clsx from "clsx" import type * as React from "react" import { useMemo, useRef } from "react" import { Drawer as DrawerPrimitive } from "vaul" import { DrawerContext, useDrawerContext } from "./drawer.context" import { useSwipeToClose } from "./drawer.hook" import type { DrawerDirection } from "./drawer.type" const drawerContentVariants = cva( "group/drawer-content bg-background fixed z-50 flex h-auto flex-col", { variants: { direction: { top: [ "data-[vaul-drawer-direction=top]:inset-x-0", "data-[vaul-drawer-direction=top]:top-0", "data-[vaul-drawer-direction=top]:mb-24", "data-[vaul-drawer-direction=top]:max-h-[80vh]", "data-[vaul-drawer-direction=top]:rounded-b-lg", ], bottom: [ "data-[vaul-drawer-direction=bottom]:inset-x-0", "data-[vaul-drawer-direction=bottom]:bottom-0", "data-[vaul-drawer-direction=bottom]:mt-24", "data-[vaul-drawer-direction=bottom]:max-h-[80vh]", "data-[vaul-drawer-direction=bottom]:rounded-t-lg", ], right: [ "data-[vaul-drawer-direction=right]:inset-y-0", "data-[vaul-drawer-direction=right]:right-0", ], left: [ "data-[vaul-drawer-direction=left]:inset-y-0", "data-[vaul-drawer-direction=left]:left-0", ], }, width: { sm: ["data-[vaul-drawer-direction=right]:w-80", "data-[vaul-drawer-direction=left]:w-80"], md: ["data-[vaul-drawer-direction=right]:w-96", "data-[vaul-drawer-direction=left]:w-96"], lg: [ "data-[vaul-drawer-direction=right]:w-[28rem]", "data-[vaul-drawer-direction=left]:w-[28rem]", ], xl: [ "data-[vaul-drawer-direction=right]:w-[32rem]", "data-[vaul-drawer-direction=left]:w-[32rem]", ], "2xl": [ "data-[vaul-drawer-direction=right]:w-[36rem]", "data-[vaul-drawer-direction=left]:w-[36rem]", ], full: [ "data-[vaul-drawer-direction=right]:w-full", "data-[vaul-drawer-direction=left]:w-full", ], auto: [ "data-[vaul-drawer-direction=right]:w-3/4 data-[vaul-drawer-direction=right]:sm:max-w-sm", "data-[vaul-drawer-direction=left]:w-3/4 data-[vaul-drawer-direction=left]:sm:max-w-sm", ], }, }, defaultVariants: { direction: "bottom", width: "auto", }, }, ) type DrawerProps = React.ComponentProps & { direction?: DrawerDirection } type DrawerContentProps = React.ComponentProps & { width?: "sm" | "md" | "lg" | "xl" | "2xl" | "full" | "auto" onOpenChange?: (open: boolean) => void } function Drawer({ direction = "bottom", open, onOpenChange, ...props }: DrawerProps) { useSwipeToClose({ isOpen: open ?? false, direction, onClose: () => onOpenChange?.(false), threshold: 50, }) const contextValue = useMemo( () => ({ direction, isOpen: open ?? false, onOpenChange: onOpenChange || (() => {}), }), [direction, open, onOpenChange], ) return ( {props.children} ) } function DrawerTrigger({ ...props }: React.ComponentProps) { return } function DrawerPortal({ ...props }: Readonly>) { return } function DrawerClose({ ...props }: React.ComponentProps) { return } function DrawerOverlay({ className, ...props }: React.ComponentProps) { return ( ) } function DrawerContent({ className, children, width = "auto", onOpenChange, ...props }: DrawerContentProps) { const contentRef = useRef(null) const { direction } = useDrawerContext() return (
{children} ) } function DrawerHeader({ className, ...props }: React.ComponentProps<"div">) { return (
) } function DrawerFooter({ className, ...props }: React.ComponentProps<"div">) { return (
) } function DrawerTitle({ className, ...props }: React.ComponentProps) { return ( ) } function DrawerDescription({ className, ...props }: React.ComponentProps) { return ( ) } export { Drawer, DrawerClose, DrawerContent, DrawerDescription, DrawerFooter, DrawerHeader, DrawerOverlay, DrawerPortal, DrawerTitle, DrawerTrigger, }