'use client' import type { PropsWithChildren } from 'react' import { useEffect } from 'react' import { Portal } from 'rntwsc/components/portal' import { useIsRtl } from 'rntwsc/i18n/use-is-rtl' import { isWeb } from 'rntwsc/platform' import type { ClassName } from 'rntwsc/tw/class-name' import { Pressable } from 'rntwsc/tw/components/pressable' import { ScrollView } from 'rntwsc/tw/components/scroll-view' import { View } from 'rntwsc/tw/components/view' import type { Variant } from 'rntwsc/tw/cva' import { cva } from 'rntwsc/tw/cva' const drawerCva = cva({ classNames: { backdrop: 'web:fixed absolute inset-0 bg-black/50', panel: 'web:fixed absolute flex flex-col overflow-hidden bg-white shadow-xl dark:bg-gray-900', handleZone: 'flex w-full cursor-grab items-center justify-center pt-3 pb-2 select-none', indicator: 'h-1.5 w-20 rounded-full bg-gray-300 dark:bg-gray-600', content: 'flex-1', }, attributes: { side: { bottom: { panel: 'right-0 bottom-0 left-0 max-h-[90%] rounded-t-2xl', }, left: { panel: 'top-0 bottom-0 left-0 h-full w-80 rounded-r-2xl', }, right: { panel: 'top-0 right-0 bottom-0 h-full w-80 rounded-l-2xl', }, }, }, }) export type DrawerProps = Omit, 'side'> & PropsWithChildren<{ open: boolean onClose: () => void // 'start'/'end' follow reading direction (left/right mirrored in rtl); // 'left'/'right' pin to a physical screen edge regardless of direction side?: 'bottom' | 'left' | 'right' | 'start' | 'end' className?: ClassName contentClassName?: ClassName contentContainerClassName?: ClassName }> export const Drawer = async ({ side = 'bottom', open, onClose, className, contentClassName, contentContainerClassName, children, }: DrawerProps) => { const rtl = await useIsRtl() useEffect(() => { if (!open || !isWeb) { return } const h = (e: KeyboardEvent) => { if (e.key === 'Escape') { onClose() } } window.addEventListener('keydown', h) return () => window.removeEventListener('keydown', h) }, [open, onClose]) if (!open) { return null } const resolvedSide = side === 'start' ? rtl ? 'right' : 'left' : side === 'end' ? rtl ? 'left' : 'right' : side const cn = drawerCva({ side: resolvedSide, }) const showHandle = resolvedSide === 'bottom' return ( {showHandle && ( )} {children} ) }