# InsetDrawer

## Introduction

InsetDrawer is a slide-out panel that appears from any edge of the screen, providing a secondary space for navigation, filters, settings, or detailed content without leaving the current page. Unlike a Modal, which overlays the entire viewport, InsetDrawer slides in from a specific edge and pushes or covers only part of the interface.

It is commonly used for mobile navigation menus, filter panels, configuration sidebars, and detail views. The component supports four anchor positions (left, right, top, bottom) and three size presets, making it adaptable to a wide range of layout requirements.

```
<Canvas of={InsetDrawer.Playground} />
```

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

## Usage

```tsx
import { InsetDrawer, DialogTitle, DialogContent, ModalClose, Sheet } from '@ceed/cds';

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

  return (
    <>
      <Button onClick={() => setOpen(true)}>Open Filters</Button>
      <InsetDrawer open={open} onClose={() => setOpen(false)}>
        <Sheet sx={{ p: 2, height: '100%' }}>
          <DialogTitle>Filters</DialogTitle>
          <ModalClose />
          <DialogContent>{/* Filter content */}</DialogContent>
        </Sheet>
      </InsetDrawer>
    </>
  );
}
```

## Anchor Positions

InsetDrawer supports four anchor positions. Use `left` for navigation, `right` for details or filters, `top` for search bars, and `bottom` for mobile action sheets.

```
<Canvas of={InsetDrawer.Anchors} />
```

## Sizes

Three size presets are available: `sm`, `md`, and `lg`. The size controls width for left/right anchors and height for top/bottom anchors.

```
<Canvas of={InsetDrawer.Sizes} />
```

## Navigation Menu

A typical mobile navigation menu pattern using InsetDrawer anchored to the left with a compact size.

```
<Canvas of={InsetDrawer.NavigationMenu} />
```

## Filter Panel

A filter panel that slides in from the right, containing form controls for refining search results.

```
<Canvas of={InsetDrawer.FilterPanel} />
```

## Common Use Cases

### Settings Drawer

```tsx
function SettingsDrawer({ open, onClose }) {
  const [settings, setSettings] = useState({
    notifications: true,
    darkMode: false,
    language: 'en',
  });

  return (
    <InsetDrawer open={open} onClose={onClose} anchor="right" size="sm">
      <Sheet sx={{ height: '100%', p: 2 }}>
        <DialogTitle>Settings</DialogTitle>
        <ModalClose />
        <Divider sx={{ my: 2 }} />
        <DialogContent>
          <Stack gap={3}>
            <FormControl orientation="horizontal">
              <Box sx={{ flex: 1 }}>
                <FormLabel>Notifications</FormLabel>
                <FormHelperText>Receive push notifications</FormHelperText>
              </Box>
              <Switch
                checked={settings.notifications}
                onChange={(e) => setSettings({ ...settings, notifications: e.target.checked })}
              />
            </FormControl>
            <FormControl orientation="horizontal">
              <Box sx={{ flex: 1 }}>
                <FormLabel>Dark Mode</FormLabel>
                <FormHelperText>Use dark color theme</FormHelperText>
              </Box>
              <Switch
                checked={settings.darkMode}
                onChange={(e) => setSettings({ ...settings, darkMode: e.target.checked })}
              />
            </FormControl>
          </Stack>
        </DialogContent>
      </Sheet>
    </InsetDrawer>
  );
}
```

### Detail View Drawer

```tsx
function DetailDrawer({ open, onClose, itemId }) {
  const [data, setData] = useState(null);

  useEffect(() => {
    if (open && itemId) {
      fetchItemDetails(itemId).then(setData);
    }
  }, [open, itemId]);

  return (
    <InsetDrawer open={open} onClose={onClose} anchor="right" size="lg">
      <Sheet sx={{ height: '100%', display: 'flex', flexDirection: 'column' }}>
        <Box sx={{ p: 2, borderBottom: '1px solid', borderColor: 'divider' }}>
          <DialogTitle>Item Details</DialogTitle>
          <ModalClose />
        </Box>
        <DialogContent sx={{ flex: 1, overflow: 'auto', p: 2 }}>
          {data ? <ItemDetails data={data} /> : <Skeleton variant="text" />}
        </DialogContent>
      </Sheet>
    </InsetDrawer>
  );
}
```

### Bottom Sheet for Mobile Actions

```tsx
function ActionSheet({ open, onClose, onAction }) {
  const actions = [
    { id: 'edit', label: 'Edit', icon: <EditIcon /> },
    { id: 'share', label: 'Share', icon: <ShareIcon /> },
    { id: 'delete', label: 'Delete', icon: <DeleteIcon />, color: 'danger' },
  ];

  return (
    <InsetDrawer open={open} onClose={onClose} anchor="bottom">
      <Sheet sx={{ borderRadius: 'lg lg 0 0', p: 2 }}>
        <List>
          {actions.map((action) => (
            <ListItem key={action.id}>
              <ListItemButton
                color={action.color}
                onClick={() => {
                  onAction(action.id);
                  onClose();
                }}
              >
                <ListItemDecorator>{action.icon}</ListItemDecorator>
                {action.label}
              </ListItemButton>
            </ListItem>
          ))}
        </List>
        <Button variant="soft" color="neutral" fullWidth sx={{ mt: 1 }} onClick={onClose}>
          Cancel
        </Button>
      </Sheet>
    </InsetDrawer>
  );
}
```

## Props and Customization

### Key Props

| Prop       | Type                                                           | Default     | Description    |
| ---------- | -------------------------------------------------------------- | ----------- | -------------- |
| `children` | `ReactNode`                                                    | -           | Drawer content |
| `variant`  | `'solid' \| 'soft' \| 'outlined' \| 'plain'`                   | `'plain'`   | Visual style   |
| `color`    | `'primary' \| 'neutral' \| 'danger' \| 'success' \| 'warning'` | `'neutral'` | Color scheme   |
| `sx`       | `SxProps`                                                      | -           | Custom styles  |

> **Note**: InsetDrawer is a styled Sheet component designed for sidebar layouts. It accepts all Joy UI Sheet props.

## Best Practices

1. **Use appropriate anchor positions**: Match the drawer position to the content type for intuitive UX.

```tsx
// ✅ Good: Navigation on the left, details on the right
<InsetDrawer anchor="left">  {/* Navigation menu */}
<InsetDrawer anchor="right"> {/* Detail panel or filters */}
<InsetDrawer anchor="bottom">{/* Mobile action sheet */}
```

2. **Provide clear close affordances**: Always give users multiple ways to close the drawer -- a close button, a cancel button, and backdrop click.

```tsx
// ✅ Good: Multiple close methods
<InsetDrawer open={open} onClose={handleClose}>
  <Sheet>
    <ModalClose />
    <Button onClick={handleClose}>Cancel</Button>
  </Sheet>
</InsetDrawer>
```

3. **Structure content consistently**: Use a header/body/footer layout with clear dividers so users can quickly scan the drawer's content.

```tsx
// ✅ Good: Clear header/body/footer structure
<Sheet sx={{ height: '100%', display: 'flex', flexDirection: 'column' }}>
  <Box sx={{ p: 2, borderBottom: '1px solid', borderColor: 'divider' }}>
    <DialogTitle>Title</DialogTitle>
    <ModalClose />
  </Box>
  <DialogContent sx={{ flex: 1, overflow: 'auto', p: 2 }}>{/* Scrollable content */}</DialogContent>
  <Box sx={{ p: 2, borderTop: '1px solid', borderColor: 'divider' }}>
    <Button>Save</Button>
  </Box>
</Sheet>
```

4. **Do not nest drawers**: Opening a drawer inside another drawer creates a confusing experience.

```tsx
// ❌ Bad: Nested drawers
<InsetDrawer>
  <InsetDrawer>{/* Avoid this pattern */}</InsetDrawer>
</InsetDrawer>
```

5. **Do not use for confirmations**: Use Dialog or Modal for simple confirmation prompts. InsetDrawer is designed for rich, scrollable content.

## Accessibility

- InsetDrawer traps focus within the drawer when open, ensuring keyboard users cannot accidentally interact with content behind it.
- Pressing **Escape** closes the drawer, and **Tab** / **Shift+Tab** cycles through focusable elements inside.
- Use `DialogTitle` with a matching `id` and `aria-labelledby` on the Sheet to provide a descriptive label for screen readers.
- When the drawer closes, focus returns to the element that triggered it, maintaining a predictable navigation flow.
