Bin
2025-12-17 1d710f844b65d9bfdf986a71a3b924cd70598a41
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import type * as React from "react";
import {
  Sheet,
  SheetContent,
  SheetDescription,
  SheetHeader,
  SheetTitle,
  SheetFooter,
  SheetTrigger,
  SheetClose,
} from "../../shad/components/ui/sheet";
import { cn } from "@humansignal/shad/utils";
import { cnm } from "../../utils/utils";
 
export interface DrawerProps {
  /**
   * Whether the drawer is open
   */
  open?: boolean;
  /**
   * Callback when the drawer open state changes
   */
  onOpenChange?: (open: boolean) => void;
  /**
   * Side from which the drawer slides in
   * @default "right"
   */
  side?: "top" | "right" | "bottom" | "left";
  /**
   * Drawer title
   * If not provided, a hidden "Drawer" title will be rendered for accessibility
   */
  title?: React.ReactNode;
  /**
   * Drawer description
   */
  description?: React.ReactNode;
  /**
   * Footer content (e.g., action buttons)
   */
  footer?: React.ReactNode;
  /**
   * Main content of the drawer
   */
  children: React.ReactNode;
  /**
   * Additional CSS classes for the drawer content
   */
  className?: string;
  /**
   * Additional CSS classes for the drawer header
   */
  headerClassName?: string;
  /**
   * Additional CSS classes for the drawer footer
   */
  footerClassName?: string;
  /**
   * Additional CSS classes for the drawer body wrapper
   */
  bodyClassName?: string;
  /**
   * Whether to show the close button
   * @default true
   */
  showCloseButton?: boolean;
  /**
   * Whether to close the drawer when clicking outside (on the overlay)
   * @default true
   */
  closeOnClickOutside?: boolean;
  /**
   * Custom CSS classes for the SheetContent wrapper
   * This allows full control over the drawer content container styling
   * For left/right sides, default width is "w-1/4" if not specified
   */
  contentClassName?: string;
  /**
   * Custom data-testid for the drawer content
   * @default "drawer"
   */
  dataTestId?: string;
  /**
   * Callback fired when the drawer opens to handle auto-focus behavior
   * Set to `(e) => e.preventDefault()` to prevent auto-focus on the first focusable element
   */
  onOpenAutoFocus?: (event: Event) => void;
}
 
/**
 * Drawer component
 *
 * A slide-out panel component that appears from the edge of the screen.
 * Built on top of Radix UI Dialog primitives and styled with Tailwind CSS.
 *
 * @example
 * ```tsx
 * <Drawer
 *   open={isOpen}
 *   onOpenChange={setIsOpen}
 *   title="User Details"
 *   description="View and edit user information"
 * >
 *   <div>Content here</div>
 * </Drawer>
 * ```
 */
export const Drawer = ({
  open,
  onOpenChange,
  side = "right",
  title,
  description,
  footer,
  children,
  className,
  headerClassName,
  footerClassName,
  bodyClassName,
  showCloseButton = true,
  closeOnClickOutside = true,
  contentClassName,
  dataTestId = "drawer",
  onOpenAutoFocus,
}: DrawerProps) => {
  const defaultWidth = side === "left" || side === "right" ? "w-1/4" : undefined;
  const computedContentClassName = cnm(
    // Apply default width for left/right sides if contentClassName is not provided
    contentClassName ? undefined : defaultWidth,
    contentClassName, // This can override default width via twMerge
    className,
  );
 
  return (
    <Sheet open={open} onOpenChange={onOpenChange} modal={true}>
      <SheetContent
        side={side}
        className={computedContentClassName}
        showCloseButton={closeOnClickOutside === false ? true : showCloseButton}
        closeOnClickOutside={closeOnClickOutside}
        data-slot="drawer-content"
        data-testid={dataTestId}
        onOpenAutoFocus={onOpenAutoFocus}
      >
        <SheetHeader
          className={cn(
            "p-base border-b border-neutral-border",
            headerClassName,
            !title && !description ? "sr-only !m-0 !p-0 !space-y-0 h-0 overflow-hidden" : undefined,
          )}
          data-slot="drawer-header"
          data-testid="drawer-header"
        >
          <SheetTitle data-slot="drawer-title" data-testid="drawer-title" className={cn(!title && "sr-only")}>
            {title || "Drawer"}
          </SheetTitle>
          {description && (
            <SheetDescription data-slot="drawer-description" data-testid="drawer-description">
              {description}
            </SheetDescription>
          )}
        </SheetHeader>
        <div
          className={cn("flex-1 min-h-0 overflow-y-auto", bodyClassName)}
          data-slot="drawer-body"
          data-testid="drawer-body"
        >
          {children}
        </div>
        {footer && (
          <SheetFooter
            className={cn("p-base border-t border-neutral-border", footerClassName)}
            data-slot="drawer-footer"
            data-testid="drawer-footer"
          >
            {footer}
          </SheetFooter>
        )}
      </SheetContent>
    </Sheet>
  );
};
 
export { SheetTrigger as DrawerTrigger, SheetClose as DrawerClose, Sheet };