# Drawer

## Introduction

The Drawer component is a panel that slides in from the edge of the screen, overlaying the main content to present secondary information, navigation, or actions. Built on top of Joy UI's Drawer with Framer Motion animation, it provides smooth entrance and exit transitions out of the box.

Drawer supports three size presets (`sm`, `md`, `lg`) which control the panel width on horizontal anchors and height on vertical anchors. On smaller viewports (below `md` breakpoint), the drawer automatically expands to full width for an optimized mobile experience.

```tsx
<Drawer
  open
  children={<Box sx={{
  width: '100%',
  height: '100%',
  backgroundColor: 'white',
  boxShadow: 'var(--ceed-shadowRing, 0 0 #000),0px 2px 8px -2px rgba(var(--ceed-shadowChannel, 21 21 21) / var(--ceed-shadowOpacity, 0.08)),0px 6px 12px -2px rgba(var(--ceed-shadowChannel, 21 21 21) / var(--ceed-shadowOpacity, 0.08))'
}}>
        asdas
      </Box>}
/>
```

| Field                        | Description | Default |
| ---------------------------- | ----------- | ------- |
| Controls resolved at runtime | —           | —       |

## Usage

```tsx
import { Drawer } from '@ceed/cds';

function MyComponent() {
  const [open, setOpen] = useState(false);

  return (
    <>
      <Button onClick={() => setOpen(true)}>Open Drawer</Button>
      <Drawer open={open} onClose={() => setOpen(false)}>
        <Box sx={{ p: 2, backgroundColor: 'white', height: '100%' }}>
          <Typography level="title-lg">Drawer Content</Typography>
          <Typography level="body-md">Place your navigation, detail view, or form content here.</Typography>
        </Box>
      </Drawer>
    </>
  );
}
```

## Default

The default Drawer displays a panel sliding in from the left with the `md` size preset.

```tsx
<Drawer
  open
  children={<Box sx={{
  width: '100%',
  height: '100%',
  backgroundColor: 'white',
  boxShadow: 'var(--ceed-shadowRing, 0 0 #000),0px 2px 8px -2px rgba(var(--ceed-shadowChannel, 21 21 21) / var(--ceed-shadowOpacity, 0.08)),0px 6px 12px -2px rgba(var(--ceed-shadowChannel, 21 21 21) / var(--ceed-shadowOpacity, 0.08))'
}}>
        asdas
      </Box>}
/>
```

## Common Use Cases

### Navigation Drawer

```tsx
function NavigationDrawer({ open, onClose }) {
  const navItems = [
    { label: 'Home', icon: <HomeIcon />, path: '/' },
    { label: 'Orders', icon: <ListIcon />, path: '/orders' },
    { label: 'Account', icon: <PersonIcon />, path: '/account' },
  ];

  return (
    <Drawer open={open} onClose={onClose} size="sm">
      <Box sx={{ p: 2, backgroundColor: 'white', height: '100%' }}>
        <Typography level="title-lg" sx={{ mb: 2 }}>
          Menu
        </Typography>
        <List>
          {navItems.map((item) => (
            <ListItem key={item.path}>
              <ListItemButton
                onClick={() => {
                  navigate(item.path);
                  onClose();
                }}
              >
                <ListItemDecorator>{item.icon}</ListItemDecorator>
                {item.label}
              </ListItemButton>
            </ListItem>
          ))}
        </List>
      </Box>
    </Drawer>
  );
}
```

### Detail View

```tsx
function DetailDrawer({ open, onClose, selectedItem }) {
  return (
    <Drawer open={open} onClose={onClose} anchor="right" size="lg">
      <Box
        sx={{
          p: 3,
          backgroundColor: 'white',
          height: '100%',
          display: 'flex',
          flexDirection: 'column',
        }}
      >
        <Box sx={{ display: 'flex', justifyContent: 'space-between', mb: 2 }}>
          <Typography level="title-lg">{selectedItem?.name}</Typography>
          <IconButton onClick={onClose}>
            <CloseIcon />
          </IconButton>
        </Box>
        <Divider />
        <Box sx={{ flex: 1, overflow: 'auto', mt: 2 }}>{/* Detail content */}</Box>
      </Box>
    </Drawer>
  );
}
```

### Form Drawer

```tsx
function FormDrawer({ open, onClose, onSubmit }) {
  return (
    <Drawer open={open} onClose={onClose} anchor="right" size="md">
      <Box
        sx={{
          p: 3,
          backgroundColor: 'white',
          height: '100%',
          display: 'flex',
          flexDirection: 'column',
        }}
      >
        <Typography level="title-lg" sx={{ mb: 2 }}>
          Create New Item
        </Typography>
        <Divider />
        <Box sx={{ flex: 1, overflow: 'auto', my: 2 }}>
          <Stack gap={2}>
            <Input label="Name" placeholder="Enter name" />
            <Textarea label="Description" placeholder="Enter description" minRows={3} />
          </Stack>
        </Box>
        <Divider />
        <Stack direction="row" gap={1} sx={{ mt: 2, justifyContent: 'flex-end' }}>
          <Button variant="outlined" color="neutral" onClick={onClose}>
            Cancel
          </Button>
          <Button onClick={onSubmit}>Save</Button>
        </Stack>
      </Box>
    </Drawer>
  );
}
```

## Props and Customization

### Key Props

\| Prop       | Type                                                           | Default                         | Description                                                                |
\| ---------- | -------------------------------------------------------------- | ------------------------------- | -------------------------------------------------------------------------- | ------------------------------------------------------------------------------ |
\| `children` | `ReactNode`                                                    | -                               | Content rendered inside the drawer panel                                   |
\| `open`     | `boolean`                                                      | `false`                         | Controls whether the drawer is visible                                     |
\| `onClose`  | `(event: React.SyntheticEvent                                  | Event, reason: string) => void` | -                                                                          | Called when the drawer requests to close, such as backdrop click or Escape key |
\| `anchor`   | `'left' \| 'right' \| 'top' \| 'bottom'`                       | `'left'`                        | Edge of the screen from which the drawer slides in                         |
\| `size`     | `'sm' \| 'md' \| 'lg'`                                         | `'md'`                          | Preset drawer size used to control width or height depending on the anchor |
\| `variant`  | `'plain' \| 'soft' \| 'outlined' \| 'solid'`                   | `'plain'`                       | Visual style inherited from Joy UI Drawer                                  |
\| `color`    | `'primary' \| 'neutral' \| 'danger' \| 'success' \| 'warning'` | `'neutral'`                     | Color scheme inherited from Joy UI Drawer                                  |
\| `sx`       | `SxProps`                                                      | -                               | Custom styles using the MUI system                                         |

> **Note**: Drawer also accepts Joy UI `DrawerProps` and Framer Motion props because the component is a thin wrapper around Joy UI Drawer with motion support.

## Best Practices

1. **Choose the right size**: Use `sm` (360px) for simple navigation, `md` (600px) for moderate content, and `lg` (900px) for complex detail views or forms.

```tsx
// ✅ Good: Size matches content complexity
<Drawer size="sm">  {/* Simple nav list */}
<Drawer size="md">  {/* Filter panel or form */}
<Drawer size="lg">  {/* Detailed information view */}
```

2. **Provide visible close affordances**: Always include a close button or cancel action inside the Drawer content, in addition to the backdrop click.

```tsx
// ✅ Good: Close button inside the drawer
<Drawer open={open} onClose={onClose}>
  <Box>
    <IconButton onClick={onClose}>
      <CloseIcon />
    </IconButton>
    {/* Content */}
  </Box>
</Drawer>
```

3. **Apply proper background and shadow styling**: The Drawer renders children directly, so you should apply background color and shadow to your content container for visual clarity.

```tsx
// ✅ Good: Styled content container
<Drawer open={open}>
  <Box sx={{ backgroundColor: 'white', height: '100%', boxShadow: 'md', p: 2 }}>{/* Content */}</Box>
</Drawer>
```

4. **Do not use Drawer for confirmations**: For simple yes/no prompts, use Dialog instead. Drawer is designed for richer, scrollable content.

5. **Structure content with header, body, and footer**: Use a flex column layout to keep the header and footer fixed while allowing the body to scroll.

## Accessibility

- The Drawer overlay traps focus within the panel when open, preventing keyboard users from interacting with background content.
- Pressing **Escape** closes the Drawer. **Tab** and **Shift+Tab** cycle through focusable elements within the Drawer content.
- On mobile viewports, the Drawer automatically expands to full width, ensuring touch targets remain accessible and content is not clipped.
- Provide an `aria-label` or visible heading within the Drawer content to describe the panel's purpose for screen reader users.
