{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "cart-drawer",
  "title": "Cart Drawer",
  "description": "Review items, quantities, discounts, totals, and checkout actions.",
  "dependencies": [
    "class-variance-authority",
    "lucide-react"
  ],
  "registryDependencies": [
    "button",
    "sheet",
    "https://uaiblocks.dev/r/uai-theme.json",
    "https://uaiblocks.dev/r/cart-item.json",
    "https://uaiblocks.dev/r/coupon-field.json",
    "https://uaiblocks.dev/r/price-summary.json",
    "https://uaiblocks.dev/r/quantity-picker.json",
    "https://uaiblocks.dev/r/uai-utils.json"
  ],
  "files": [
    {
      "path": "src/registry/uai/blocks/cart-drawer.tsx",
      "content": "\"use client\";\n\nimport { cva } from \"class-variance-authority\";\nimport { ShoppingBag, X } from \"lucide-react\";\nimport {\n  type ComponentProps,\n  createContext,\n  type ReactNode,\n  type RefObject,\n  useContext,\n  useEffect,\n  useLayoutEffect,\n  useRef,\n  useState,\n} from \"react\";\nimport { Button } from \"@/components/ui/button\";\nimport { Sheet, SheetContent, SheetTitle, SheetTrigger } from \"@/components/ui/sheet\";\nimport { CartItem, type CartItemProps, type CartItemVariant } from \"@/components/ui/uai/cart-item\";\nimport {\n  CouponField,\n  type CouponFieldProps,\n  type CouponFieldVariant,\n} from \"@/components/ui/uai/coupon-field\";\nimport {\n  PriceSummary,\n  type PriceSummaryProps,\n  type PriceSummaryVariant,\n} from \"@/components/ui/uai/price-summary\";\nimport { QuantityPicker, type QuantityPickerProps } from \"@/components/ui/uai/quantity-picker\";\nimport { cn } from \"@/lib/uai-utils\";\n\nexport const CART_DRAWER_VARIANTS = [\"side\", \"sheet\", \"compact\"] as const;\nexport type CartDrawerVariant = (typeof CART_DRAWER_VARIANTS)[number];\nexport type CartDrawerProps = {\n  variant?: CartDrawerVariant;\n  open?: boolean;\n  defaultOpen?: boolean;\n  onOpenChange?: (open: boolean) => void;\n  children?: ReactNode;\n};\n\ntype DrawerContext = {\n  variant: CartDrawerVariant;\n  open: boolean;\n  setOpen: (open: boolean) => void;\n  triggerRef: RefObject<HTMLButtonElement | null>;\n  closeRef: RefObject<HTMLButtonElement | null>;\n};\nconst Context = createContext<DrawerContext | null>(null);\nfunction useDrawer(part: string) {\n  const context = useContext(Context);\n  if (!context) throw new Error(`${part} must be used within CartDrawer`);\n  return context;\n}\n\nconst useIsomorphicLayoutEffect = typeof window === \"undefined\" ? useEffect : useLayoutEffect;\n\n// Ticks an element in place when its text changes, so a new count reads as an update.\nfunction useValueTick<T extends HTMLElement>() {\n  const ref = useRef<T>(null);\n  const previous = useRef<string | null>(null);\n  useIsomorphicLayoutEffect(() => {\n    const element = ref.current;\n    const text = element?.textContent ?? null;\n    const changed = previous.current !== null && text !== null && previous.current !== text;\n    previous.current = text;\n    if (!changed || !element || typeof element.animate !== \"function\") return;\n    if (window.matchMedia?.(\"(prefers-reduced-motion: reduce)\").matches) return;\n    element.animate(\n      [\n        { opacity: 0.4, transform: \"translateY(3px)\" },\n        { opacity: 1, transform: \"none\" },\n      ],\n      { duration: 200, easing: \"cubic-bezier(0.23, 1, 0.32, 1)\" },\n    );\n  });\n  return ref;\n}\n\nconst buttonBase =\n  \"py-0 transition-[background-color,color,filter,scale] duration-[120ms,120ms,120ms,140ms] ease-[ease-out,ease-out,ease-out,cubic-bezier(0.23,1,0.32,1)] focus-visible:ring-0 focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-ring active:scale-[0.97] motion-reduce:transition-none motion-reduce:active:scale-100\";\nconst secondaryButton =\n  \"cursor-pointer rounded-full border-0 bg-secondary text-[13px] font-medium text-foreground hover:bg-[color-mix(in_oklab,var(--secondary)_85%,var(--foreground))]\";\n\nconst itemVariants: Record<CartDrawerVariant, CartItemVariant> = {\n  side: \"plain\",\n  sheet: \"plain\",\n  compact: \"compact\",\n};\nconst couponVariants: Record<CartDrawerVariant, CouponFieldVariant> = {\n  side: \"rounded\",\n  sheet: \"pill\",\n  compact: \"compact\",\n};\nconst summaryVariants: Record<CartDrawerVariant, PriceSummaryVariant> = {\n  side: \"plain\",\n  sheet: \"plain\",\n  compact: \"compact\",\n};\n\n/** Cart review in a modal drawer: items, quantities, discounts, totals, and checkout actions. */\nexport function CartDrawer({\n  variant = \"side\",\n  open,\n  defaultOpen = false,\n  onOpenChange,\n  children,\n}: CartDrawerProps) {\n  const [internal, setInternal] = useState(defaultOpen);\n  const triggerRef = useRef<HTMLButtonElement>(null);\n  const closeRef = useRef<HTMLButtonElement>(null);\n  const current = open ?? internal;\n  const setOpen = (next: boolean) => {\n    if (next === current) return;\n    if (open === undefined) setInternal(next);\n    onOpenChange?.(next);\n  };\n  return (\n    <Context.Provider value={{ variant, open: current, setOpen, triggerRef, closeRef }}>\n      <Sheet open={current} onOpenChange={setOpen}>\n        <div data-slot=\"cart-drawer\" data-variant={variant} className=\"contents\">\n          {children}\n        </div>\n      </Sheet>\n    </Context.Provider>\n  );\n}\n\nexport type CartDrawerTriggerProps = ComponentProps<\"button\"> & {\n  /** Item count shown in the badge and included in the accessible name. */\n  count?: number;\n};\n\n/** Opens the drawer. The item count is part of the button's accessible name. */\nexport function CartDrawerTrigger({\n  count,\n  children = \"Cart\",\n  onClick,\n  className,\n  ...props\n}: CartDrawerTriggerProps) {\n  const context = useDrawer(\"CartDrawerTrigger\");\n  const countRef = useValueTick<HTMLSpanElement>();\n  return (\n    <SheetTrigger asChild onClick={onClick}>\n      <Button\n        type=\"button\"\n        variant=\"secondary\"\n        data-slot=\"cart-drawer-trigger\"\n        className={cn(\n          buttonBase,\n          secondaryButton,\n          \"inline-flex h-8 items-center gap-2 pr-1.5 pl-3 has-[>svg]:pr-1.5 has-[>svg]:pl-3\",\n          className,\n        )}\n        {...props}\n        ref={context.triggerRef}\n      >\n        <ShoppingBag\n          className=\"size-3.5 text-muted-foreground\"\n          size={14}\n          strokeWidth={1.75}\n          aria-hidden=\"true\"\n        />\n        {children}\n        {count === undefined ? null : (\n          <span\n            ref={countRef}\n            className=\"inline-grid h-5 min-w-5 place-items-center rounded-full bg-primary px-1.5 text-[11.5px] font-medium text-primary-foreground tabular-nums\"\n          >\n            {count}\n            <span className=\"sr-only\">{count === 1 ? \" item\" : \" items\"}</span>\n          </span>\n        )}\n      </Button>\n    </SheetTrigger>\n  );\n}\n\nconst cartDrawerContentVariants = cva(\n  \"box-border max-w-full gap-0 overflow-hidden border-0 bg-popover p-0 text-[13px]/[18px] text-popover-foreground ease-[cubic-bezier(0.23,1,0.32,1)] data-[state=closed]:duration-220 data-[state=open]:duration-220 data-[state=closed]:fade-out-0 data-closed:fade-out-0 data-[state=open]:fade-in-0 data-open:fade-in-0 sm:max-w-full motion-reduce:data-[state=closed]:animate-none motion-reduce:data-closed:animate-none motion-reduce:data-[state=open]:animate-none motion-reduce:data-open:animate-none\",\n  {\n    variants: {\n      variant: {\n        side: \"h-full max-h-dvh w-[min(100%,420px)] rounded-none shadow-[0_0_0_1px_var(--border),-12px_0_32px_-16px_oklch(0_0_0/0.24)] data-[state=closed]:slide-out-to-right-6 data-closed:slide-out-to-right-6 data-[state=open]:slide-in-from-right-6 data-open:slide-in-from-right-6\",\n        sheet:\n          \"mx-auto h-auto max-h-[min(88dvh,720px)] w-[min(100%,640px)] rounded-t-3xl rounded-b-none shadow-[0_0_0_1px_var(--border),0_-12px_32px_-16px_oklch(0_0_0/0.24)] data-[state=closed]:slide-out-to-bottom-6 data-closed:slide-out-to-bottom-6 data-[state=open]:slide-in-from-bottom-6 data-open:slide-in-from-bottom-6\",\n        compact:\n          \"h-full max-h-dvh w-[min(100%,360px)] rounded-none shadow-[0_0_0_1px_var(--border),-12px_0_32px_-16px_oklch(0_0_0/0.24)] data-[state=closed]:slide-out-to-right-6 data-closed:slide-out-to-right-6 data-[state=open]:slide-in-from-right-6 data-open:slide-in-from-right-6\",\n      },\n    },\n  },\n);\nconst cartDrawerSides = { side: \"right\", sheet: \"bottom\", compact: \"right\" } as const;\n\n/**\n * The drawer: a modal Sheet. Focus moves to the close button (or an element marked autofocus)\n * when it opens and returns to the trigger when it closes. Escape and the overlay close it.\n */\nexport function CartDrawerContent({\n  className,\n  children,\n  ...props\n}: Omit<ComponentProps<typeof SheetContent>, \"side\" | \"showCloseButton\">) {\n  const context = useDrawer(\"CartDrawerContent\");\n  const { closeRef, variant } = context;\n  return (\n    <SheetContent\n      aria-describedby={undefined}\n      data-slot=\"cart-drawer-content\"\n      side={cartDrawerSides[variant]}\n      showCloseButton={false}\n      className={cn(cartDrawerContentVariants({ variant }), className)}\n      {...props}\n      data-variant={variant}\n    >\n      {children}\n      <InitialFocus fallback={closeRef} />\n    </SheetContent>\n  );\n}\n\n/**\n * Moves focus into the drawer as it mounts: an element marked `autofocus` or `data-autofocus`,\n * else the close button. It runs before the primitive's own initial focus, which Radix and Base UI\n * both skip when focus is already inside, so neither needs primitive-specific props.\n */\nfunction InitialFocus({ fallback }: { fallback: React.RefObject<HTMLElement | null> }) {\n  useLayoutEffect(() => {\n    const content = fallback.current?.closest<HTMLElement>(\"[role=dialog], [role=alertdialog]\");\n    const preferred = content?.querySelector<HTMLElement>(\"[autofocus], [data-autofocus]\");\n    (preferred ?? fallback.current)?.focus();\n  }, [fallback]);\n  return null;\n}\n\nexport function CartDrawerHeader({ children, className, ...props }: ComponentProps<\"header\">) {\n  const context = useDrawer(\"CartDrawerHeader\");\n  return (\n    <header\n      data-slot=\"cart-drawer-header\"\n      className={cn(\n        \"flex flex-none items-center justify-between gap-3\",\n        context.variant === \"compact\" ? \"py-2.5 pr-1.5 pl-3.5\" : \"py-4 pr-3 pl-5\",\n        className,\n      )}\n      {...props}\n    >\n      {children}\n    </header>\n  );\n}\n\nexport function CartDrawerTitle({ className, ...props }: ComponentProps<\"h2\">) {\n  useDrawer(\"CartDrawerTitle\");\n  return (\n    <SheetTitle\n      data-slot=\"cart-drawer-title\"\n      className={cn(\"m-0 text-[15px]/5 font-semibold tracking-[-0.01em]\", className)}\n      {...props}\n    />\n  );\n}\n\n/** Closes the drawer. Receives focus when the drawer opens. */\nexport function CartDrawerClose({\n  \"aria-label\": ariaLabel = \"Close cart\",\n  onClick,\n  className,\n  ...props\n}: ComponentProps<\"button\">) {\n  const context = useDrawer(\"CartDrawerClose\");\n  return (\n    <Button\n      type=\"button\"\n      variant=\"ghost\"\n      size=\"icon\"\n      aria-label={ariaLabel}\n      data-slot=\"cart-drawer-close\"\n      className={cn(\n        buttonBase,\n        \"grid size-7 flex-none cursor-pointer place-items-center rounded-lg border-0 bg-transparent text-muted-foreground hover:bg-accent hover:text-foreground dark:hover:bg-accent\",\n        className,\n      )}\n      {...props}\n      ref={context.closeRef}\n      onClick={(event) => {\n        onClick?.(event);\n        if (!event.defaultPrevented) context.setOpen(false);\n      }}\n    >\n      <X size={16} strokeWidth={1.75} aria-hidden=\"true\" />\n    </Button>\n  );\n}\n\n/** Scrollable region that holds the cart lines and the empty state. */\nexport function CartDrawerBody({ className, ...props }: ComponentProps<\"div\">) {\n  const context = useDrawer(\"CartDrawerBody\");\n  return (\n    <div\n      data-slot=\"cart-drawer-body\"\n      className={cn(\n        \"grid min-h-0 flex-[1_1_auto] content-start gap-4 overflow-y-auto overscroll-contain\",\n        context.variant === \"compact\" ? \"p-3.5\" : \"p-5\",\n        className,\n      )}\n      {...props}\n    />\n  );\n}\n\nexport function CartDrawerItems({\n  \"aria-label\": ariaLabel = \"Items in your cart\",\n  className,\n  ...props\n}: ComponentProps<\"ul\">) {\n  const context = useDrawer(\"CartDrawerItems\");\n  return (\n    <ul\n      aria-label={ariaLabel}\n      data-slot=\"cart-drawer-items\"\n      className={cn(\n        \"m-0 grid list-none p-0\",\n        context.variant === \"compact\" ? \"gap-2\" : \"gap-4.5\",\n        className,\n      )}\n      {...props}\n    />\n  );\n}\n\n/** One cart line. Compose CartItem parts inside; the block picks the cart item style. */\nexport function CartDrawerItem(props: Omit<CartItemProps, \"variant\">) {\n  const context = useDrawer(\"CartDrawerItem\");\n  return (\n    <li\n      data-slot=\"cart-drawer-item\"\n      // The line collapses while its remove action is in flight.\n      className=\"grid min-w-0 grid-rows-[1fr] transition-[grid-template-rows,opacity] duration-240 ease-out-quint has-[[data-slot=cart-item-remove][aria-busy=true]]:grid-rows-[0fr] has-[[data-slot=cart-item-remove][aria-busy=true]]:opacity-0 motion-reduce:transition-none\"\n    >\n      <div className=\"min-h-0 has-[[data-slot=cart-item-remove][aria-busy=true]]:overflow-hidden\">\n        <CartItem {...props} variant={itemVariants[context.variant]} />\n      </div>\n    </li>\n  );\n}\n\n/** Quantity control for a cart line. Compose QuantityPicker parts inside. */\nexport function CartDrawerQuantity(props: Omit<QuantityPickerProps, \"variant\">) {\n  useDrawer(\"CartDrawerQuantity\");\n  return <QuantityPicker {...props} variant=\"compact\" />;\n}\n\n/** Pinned footer with the discount field, totals, and checkout actions. */\nexport function CartDrawerFooter({ className, ...props }: ComponentProps<\"footer\">) {\n  const context = useDrawer(\"CartDrawerFooter\");\n  const compact = context.variant === \"compact\";\n  return (\n    <footer\n      data-slot=\"cart-drawer-footer\"\n      className={cn(\n        \"grid flex-none border-t bg-[color-mix(in_oklab,var(--background)_35%,var(--card))]\",\n        compact\n          ? \"gap-2.5 p-3.5 pb-[max(14px,env(safe-area-inset-bottom))]\"\n          : \"gap-3.5 p-5 pb-[max(20px,env(safe-area-inset-bottom))]\",\n        className,\n      )}\n      {...props}\n    />\n  );\n}\n\n/** Discount code. Compose CouponField parts inside; the block picks the field style. */\nexport function CartDrawerDiscount(props: Omit<CouponFieldProps, \"variant\">) {\n  const context = useDrawer(\"CartDrawerDiscount\");\n  return <CouponField {...props} variant={couponVariants[context.variant]} />;\n}\n\n/** Totals. Compose PriceSummary parts inside; the block picks the summary style. */\nexport function CartDrawerSummary(props: Omit<PriceSummaryProps, \"variant\">) {\n  const context = useDrawer(\"CartDrawerSummary\");\n  return <PriceSummary {...props} variant={summaryVariants[context.variant]} />;\n}\n\nexport function CartDrawerActions({ className, ...props }: ComponentProps<\"div\">) {\n  return <div data-slot=\"cart-drawer-actions\" className={cn(\"grid gap-2\", className)} {...props} />;\n}\n\n/** The primary checkout link, filled with the accent color. */\nexport function CartDrawerCheckout({ className, ...props }: ComponentProps<\"a\">) {\n  const context = useDrawer(\"CartDrawerCheckout\");\n  return (\n    <Button\n      asChild\n      className={cn(\n        buttonBase,\n        \"inline-flex items-center justify-center rounded-full bg-primary px-4 text-[13px] font-medium text-primary-foreground no-underline hover:bg-primary hover:brightness-108 has-[>svg]:px-4\",\n        context.variant === \"compact\" ? \"h-8\" : \"h-10\",\n        className,\n      )}\n    >\n      <a data-slot=\"cart-drawer-checkout\" {...props} />\n    </Button>\n  );\n}\n\n/** Closes the drawer and returns the shopper to the page. */\nexport function CartDrawerContinue({\n  children = \"Continue shopping\",\n  onClick,\n  className,\n  ...props\n}: ComponentProps<\"button\">) {\n  const context = useDrawer(\"CartDrawerContinue\");\n  return (\n    <Button\n      type=\"button\"\n      variant=\"secondary\"\n      data-slot=\"cart-drawer-continue\"\n      className={cn(\n        buttonBase,\n        secondaryButton,\n        \"px-4\",\n        context.variant === \"compact\" ? \"h-8\" : \"h-10\",\n        className,\n      )}\n      {...props}\n      onClick={(event) => {\n        onClick?.(event);\n        if (!event.defaultPrevented) context.setOpen(false);\n      }}\n    >\n      {children}\n    </Button>\n  );\n}\n",
      "type": "registry:component",
      "target": "@components/uai/cart-drawer.tsx"
    }
  ],
  "categories": [
    "commerce",
    "cart"
  ],
  "type": "registry:block"
}