|
| 1 | +import { FloatingFocusManager } from "@floating-ui/react"; |
| 2 | +import type { PrimitiveProps } from "../../primitives"; |
| 3 | +import { usePopperAria } from "../../primitives/popper/popper-aria-context"; |
| 4 | +import { PopperFloating } from "../../primitives/popper/popper-floating"; |
| 5 | +import { usePopperTransition } from "../../primitives/popper/popper-transtion-context"; |
| 6 | +import { tx } from "../../utils"; |
| 7 | +import { useDialog } from "../dialog/dialog-context"; |
| 8 | +import { useDrawer } from "./drawer-context"; |
| 9 | + |
| 10 | +const placementStyles = { |
| 11 | + start: { |
| 12 | + base: "start-0 top-0 bottom-0", |
| 13 | + open: "translate-x-none", |
| 14 | + close: "-translate-x-full", |
| 15 | + }, |
| 16 | + end: { |
| 17 | + base: "end-0 top-0 bottom-0", |
| 18 | + open: "translate-x-none", |
| 19 | + close: "translate-x-full", |
| 20 | + }, |
| 21 | + top: { |
| 22 | + base: "top-0 left-0 right-0", |
| 23 | + open: "translate-y-none", |
| 24 | + close: "-translate-y-full", |
| 25 | + }, |
| 26 | + bottom: { |
| 27 | + base: "bottom-0 left-0 right-0", |
| 28 | + open: "translate-y-none", |
| 29 | + close: "translate-y-full", |
| 30 | + }, |
| 31 | +}; |
| 32 | + |
| 33 | +export const DrawerContent = (props: PrimitiveProps<"div">) => { |
| 34 | + const { children, className, ...rest } = props; |
| 35 | + |
| 36 | + const { placement } = useDrawer(); |
| 37 | + const { labelId, descriptionId } = usePopperAria(); |
| 38 | + const { context, initialFocus, finalFocus } = useDialog(); |
| 39 | + const { status, mounted, duration } = usePopperTransition(); |
| 40 | + |
| 41 | + if (!mounted) { |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + const drawerStyle = placementStyles[placement]; |
| 46 | + |
| 47 | + return ( |
| 48 | + <div className={"z-55 fixed left-0 top-0 flex h-screen w-screen justify-center"}> |
| 49 | + <FloatingFocusManager context={context} initialFocus={initialFocus} returnFocus={finalFocus}> |
| 50 | + <PopperFloating |
| 51 | + duration={duration} |
| 52 | + className={tx( |
| 53 | + "fixed flex flex-col shadow-md transition-[opacity,translate]", |
| 54 | + drawerStyle.base, |
| 55 | + !className?.split(" ").some((cls) => cls.startsWith("bg-")) && "bg-bg-normal", |
| 56 | + status == "open" ? ["opacity-100", drawerStyle.open] : ["opacity-0", drawerStyle.close], |
| 57 | + className, |
| 58 | + )} |
| 59 | + aria-labelledby={labelId} |
| 60 | + aria-describedby={descriptionId} |
| 61 | + {...rest} |
| 62 | + > |
| 63 | + {children} |
| 64 | + </PopperFloating> |
| 65 | + </FloatingFocusManager> |
| 66 | + </div> |
| 67 | + ); |
| 68 | +}; |
0 commit comments