import { useTranslate } from "@workspace/i18n" import { Button } from "@workspace/ui/components/button" import { createDrawerHandle, Drawer, DrawerClose, DrawerContent, DrawerDescription, DrawerHeader, DrawerTitle, } from "@workspace/ui/components/drawer" import { XIcon } from "@workspace/ui/components/icon" import { OverlayController, type OverlayRequest } from "./internal/controller" import { renderOverlayContent } from "./internal/render-content" import { useOverlayController } from "./internal/use-controller" import { overlayMessages } from "./messages" import { globalOverlayScope, type OverlayScope } from "./scope" import type { DrawerOptions } from "./types" type DrawerRequestOptions = Omit, "scope"> const drawerChannel = Symbol("overlay.drawer") function getDrawerController(scope: OverlayScope) { return scope.getChannel(drawerChannel, () => { const handle = createDrawerHandle>() return new OverlayController({ handle, isDismissible: (options) => options.dismissible !== false, scope, }) }) } export function openDrawer(options: DrawerOptions) { const { scope = globalOverlayScope, ...drawerOptions } = options return getDrawerController(scope).open( drawerOptions as DrawerRequestOptions, undefined ) } export interface DrawerHostProps { scope?: OverlayScope } export function DrawerHost({ scope }: DrawerHostProps) { const controller = useOverlayController(scope, getDrawerController) const activeOptions = controller.getActive()?.options return ( > disablePointerDismissal={activeOptions?.dismissible === false} handle={controller.handle} modal={activeOptions?.modal} showSwipeHandle={activeOptions?.showSwipeHandle} snapPoints={activeOptions?.snapPoints} swipeDirection={activeOptions?.swipeDirection} onOpenChange={(open, details) => { if (!open) controller.handleCloseRequest(details) }} onOpenChangeComplete={(open) => { if (!open) controller.handleCloseComplete() }} > {({ payload }) => payload ? ( controller.resolve(value)} /> ) : null } ) } function DrawerContentView({ onDismiss, onResolve, options, }: { onDismiss: () => void onResolve: (value: unknown) => void options: DrawerRequestOptions }) { const t = useTranslate() const dismissible = options.dismissible !== false return ( {options.title} {options.description != null && ( {options.description} )}
{renderOverlayContent(options.content, { close: onResolve, dismiss: onDismiss, })}
{dismissible && ( } > )}
) } export type { DrawerOptions } from "./types"