---
title: "Drawer · UI SDK — StyleX Blocks"
description: "Slide-in panels anchored to any viewport edge, with header, body, and footer slots."
url: "https://stylexblocks.com/components/ui-sdk/drawer/"
agent:
  instructions: "Use this search API to find relevant components and blocks across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://stylexblocks.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Drawer

Slide-in panels anchored to any viewport edge, with header, body, and footer slots.

- Behavior engine: UI SDK
- Also available for: [Ark UI](https://stylexblocks.com/components/ark-ui/drawer.md), [Base UI](https://stylexblocks.com/components/base-ui/drawer.md)
- Category: Layout
- Variants: start, end, top, bottom
- Sizes: xs, sm, md, lg, xl
- Used in blocks: [Filterable Table](https://stylexblocks.com/block/ui-sdk/filterable-table.md), [Grouped Table](https://stylexblocks.com/block/ui-sdk/grouped-table.md), [Incident Console](https://stylexblocks.com/block/ui-sdk/incident-console.md), [Kanban Board](https://stylexblocks.com/block/ui-sdk/kanban-board.md), [Messaging Shell](https://stylexblocks.com/block/ui-sdk/messaging-shell.md), [Shell Nav](https://stylexblocks.com/block/ui-sdk/shell-nav.md), [Side Nav Shell](https://stylexblocks.com/block/ui-sdk/shell-side-nav.md), [Top Nav Shell](https://stylexblocks.com/block/ui-sdk/shell-top-nav.md), [Tree Table](https://stylexblocks.com/block/ui-sdk/tree-table.md)
- HTML page: https://stylexblocks.com/components/ui-sdk/drawer/

## Install

Configure the project once (sets the behavior engine and import aliases):

```sh
npx stylexblocks init --style ui-sdk
```

Then add the item as source you own:

```sh
npx stylexblocks add drawer
```

Equivalent: `npx shadcn add @stylexblocks/drawer`. Registry item JSON: https://stylexblocks.com/r/ui-sdk/drawer.json

## Usage

```tsx
import { Drawer } from "#components/ui/ui-sdk/drawer.tsx";
import { Button } from "#components/ui/ui-sdk/button.tsx";

// UI SDK's drawer wearing the drawer slot recipe: placement picks the edge
// the sheet slides in from, and it can be dragged back to dismiss. The
// engine renders its own viewport, so there is no Portal or Positioner.
export function Example() {
  return (
    <Drawer.Root placement="end" size="xs">
      <Drawer.Trigger render={<Button variant="surface" />}>
        Open drawer
      </Drawer.Trigger>
      <Drawer.Backdrop />
      <Drawer.Content>
        <Drawer.Header>
          <Drawer.Title>Filters</Drawer.Title>
          <Drawer.Close aria-label="Close">×</Drawer.Close>
        </Drawer.Header>
        <Drawer.Body>
          <Drawer.Description>
            Narrow results by status, owner, or date range.
          </Drawer.Description>
        </Drawer.Body>
        <Drawer.Footer>
          <Drawer.Close render={<Button variant="solid" />}>Apply</Drawer.Close>
        </Drawer.Footer>
      </Drawer.Content>
    </Drawer.Root>
  );
}
```

## Source

`components/ui/ui-sdk/drawer.tsx`

```tsx
import * as stylex from "@stylexjs/stylex";
import { useContext } from "react";
import {
  Drawer as UiSdkDrawer,
  DrawerBackdrop,
  DrawerClose,
  DrawerContent,
  DrawerDescription,
  DrawerTitle,
  DrawerTrigger,
} from "@ui-sdk/react/drawer";
import { createSlotRecipeContext } from "#lib/ui-sdk/create-slot-recipe-context.tsx";
import {
  defineSlotRecipe,
  fromMap,
  resolveSlotStyles,
  type StyleOverrideProps,
} from "#lib/recipe-props.ts";
import { semanticRadii } from "@stylexblocks/ui/semantic-tokens/radii.stylex.ts";
import { semanticShadows } from "@stylexblocks/ui/semantic-tokens/shadows.stylex.ts";
import { durations } from '@stylexblocks/ui/tokens/durations.stylex.ts'
import { fontSizes } from '@stylexblocks/ui/tokens/font-sizes.stylex.ts'
import { fontWeights } from '@stylexblocks/ui/tokens/font-weights.stylex.ts'
import { lineHeights } from '@stylexblocks/ui/tokens/line-heights.stylex.ts'
import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'
import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'
import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'
import { zIndices } from '@stylexblocks/ui/tokens/z-indices.stylex.ts'

const drawerBackdrop = stylex.create({
  base: {
    position: 'fixed',
    insetInlineStart: 0,
    top: 0,
    width: '100vw',
    height: '100dvh',
    zIndex: zIndices.layer4,
    animationName: {
      default: null,
      ':is([data-open], [data-state="open"])': 'fade-in',
      ':is([data-closed], [data-state="closed"])': 'fade-out',
    },
    animationDuration: {
      default: null,
      ':is([data-open], [data-state="open"])': durations.moderate,
      ':is([data-closed], [data-state="closed"])': durations.faster,
    },
  },
})

const drawerPositioner = stylex.create({
  base: {
    '--drawer-z-index': zIndices.layer4,
    display: 'flex',
    width: '100vw',
    height: '100dvh',
    position: 'fixed',
    padding: spacing._2,
    insetInlineStart: 0,
    top: 0,
    zIndex: 'calc(var(--drawer-z-index) + var(--layer-index, 0))',
  },
})

const drawerPositionerPlacements = stylex.create({
  start: {
    justifyContent: 'flex-start',
  },
  end: {
    justifyContent: 'flex-end',
  },
  top: {
    alignItems: 'flex-start',
  },
  bottom: {
    alignItems: 'flex-end',
  },
})

const drawerPositionerAttacheds = stylex.create({
  true: {
    padding: 0,
  },
})

const drawerContent = stylex.create({
  base: {
    display: 'flex',
    flexDirection: 'column',
    position: 'relative',
    width: '100%',
    outline: 'none',
    zIndex: 'calc(var(--drawer-z-index) + var(--layer-index, 0))',
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
    maxHeight: '100dvh',
    color: 'inherit',
    animationDuration: {
      default: null,
      ':is([data-open], [data-state="open"])': durations.moderate,
      ':is([data-closed], [data-state="closed"])': durations.faster,
    },
  },
})

const drawerContentSizes = stylex.create({
  xs: {
    maxWidth: sizes.xs,
  },
  sm: {
    maxWidth: sizes.md,
  },
  md: {
    maxWidth: sizes.lg,
  },
  lg: {
    maxWidth: '2xl',
  },
  xl: {
    maxWidth: '4xl',
  },
  full: {
    maxWidth: '100vw',
    height: '100dvh',
  },
})

const drawerContentPlacements = stylex.create({
  start: {
    animationName: {
      default: null,
      ':is([data-open], [data-state="open"])': 'slide-from-left-full',
      ':is([data-closed], [data-state="closed"])': 'slide-to-left-full',
    },
  },
  end: {
    animationName: {
      default: null,
      ':is([data-open], [data-state="open"])': 'slide-from-right-full',
      ':is([data-closed], [data-state="closed"])': 'slide-to-right-full',
    },
  },
  top: {
    maxWidth: '100%',
    animationName: {
      default: null,
      ':is([data-open], [data-state="open"])': 'slide-from-top-full',
      ':is([data-closed], [data-state="closed"])': 'slide-to-top-full',
    },
  },
  bottom: {
    maxWidth: '100%',
    animationName: {
      default: null,
      ':is([data-open], [data-state="open"])': 'slide-from-bottom-full',
      ':is([data-closed], [data-state="closed"])': 'slide-to-bottom-full',
    },
  },
})

const drawerContentAttacheds = stylex.create({
  true: {
    borderRadius: 0,
  },
})

const drawerHeader = stylex.create({
  base: {
    display: 'flex',
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
    flex: 0,
    paddingInline: spacing._6,
    paddingBlock: spacing._4,
  },
})

const drawerBody = stylex.create({
  base: {
    paddingInline: spacing._6,
    paddingBlock: spacing._2,
    flex: 1,
    overflow: 'auto',
  },
})

const drawerFooter = stylex.create({
  base: {
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'flex-end',
    gap: spacing._3,
    paddingInline: spacing._6,
    paddingTop: spacing._2,
    paddingBottom: spacing._4,
  },
})

const drawerTitle = stylex.create({
  base: {
    fontSize: fontSizes.lg,
    lineHeight: lineHeights.lg,
    fontWeight: fontWeights.medium,
  },
})

const drawerDescription = stylex.create({
  base: {
    color: semanticColors.fgMuted,
  },
})

const drawerCloseTrigger = stylex.create({
  base: {
    position: 'absolute',
    top: spacing._4,
    right: spacing._4,
  },
})

export type DrawerSize = keyof typeof drawerContentSizes

export type DrawerPlacement = keyof typeof drawerPositionerPlacements

export type DrawerAttached = keyof typeof drawerPositionerAttacheds

// defineSlotRecipe validates the shape at compile time: default values must
// exist in the axis maps, and slot keys must be `styles` or a declared axis.
// Axis maps are looked up by pluralized axis name (`placement` → `placements`).
const drawerRecipe = defineSlotRecipe({
  slots: {
    backdrop: { styles: drawerBackdrop },
    positioner: {
      styles: drawerPositioner,
      placements: drawerPositionerPlacements,
    },
    content: {
      styles: drawerContent,
      sizes: drawerContentSizes,
      placements: drawerContentPlacements,
    },
    header: { styles: drawerHeader },
    body: { styles: drawerBody },
    footer: { styles: drawerFooter },
    title: { styles: drawerTitle },
    description: { styles: drawerDescription },
    closeTrigger: { styles: drawerCloseTrigger },
  },
  defaultVariants: { size: "xs", placement: "end" },
});

// Local chrome the recipe leaves to the composition layer: the backdrop's
// fixed fill + wash and the panel's surface (plus real max-widths for the
// `lg`/`xl` steps, whose generated values are Chakra size names).
// UI SDK's drawer slides the sheet with `translate` and drives the
// backdrop's opacity itself, so the recipe's slide/fade keyframes are
// switched off (they would fight the engine's motion).
const chrome = stylex.create({
  backdrop: {
    position: "fixed",
    inset: 0,
    backgroundColor: semanticColors.bgBackdrop,
    animationName: "none",
  },
  content: {
    backgroundColor: semanticColors.bgPanel,
    borderWidth: "1px",
    borderStyle: "solid",
    borderColor: semanticColors.border,
    borderRadius: semanticRadii.panel,
    boxShadow: semanticShadows.overlay,
    animationName: "none",
  },
});

// UI SDK lays the sheet out in its own full-screen viewport (a flex row for
// side drawers, a column for top/bottom). These do the recipe positioner's
// job on the content: the 0.5rem gap from the edges (the far side stays
// `auto` so the sheet hugs its edge), full width for top/bottom sheets.
const sheet = stylex.create({
  base: {
    marginBlock: spacing._2,
    marginLeft: {
      default: spacing._2,
      ':is([data-side="right"])': "auto",
    },
    marginRight: {
      default: spacing._2,
      ':is([data-side="left"])': "auto",
    },
    width: {
      default: "100%",
      ':is([data-side="top"], [data-side="bottom"])': "auto",
    },
  },
});

// UI SDK's Title renders an `h2` and Description a `p`: drop UA margins.
const text = stylex.create({
  reset: { margin: 0 },
});

const contentSizeFix = stylex.create({
  lg: { maxWidth: "42rem" },
  xl: { maxWidth: "56rem" },
});

const { withRootProvider, withContext, VariantContext } =
  createSlotRecipeContext(drawerRecipe);

const ProviderRoot = withRootProvider(UiSdkDrawer, "DrawerRoot");

/**
 * Root resolves the recipe variants and hands the drawer its `side` from
 * `placement` (`start`/`end` follow the writing direction), unless a
 * `side` is passed explicitly.
 */
function Root(allProps: React.ComponentProps<typeof ProviderRoot>) {
  const { placement, side, ...rest } = allProps as React.ComponentProps<
    typeof ProviderRoot
  > & { side?: React.ComponentProps<typeof UiSdkDrawer>["side"] };
  return (
    <ProviderRoot
      placement={placement}
      side={side ?? placement ?? drawerRecipe.defaultVariants.placement}
      {...rest}
    />
  );
}
Root.displayName = "DrawerRoot";

const ContentElement = DrawerContent as React.ElementType;

/**
 * UI SDK has no positioner part: its own viewport places the sheet, so
 * Content carries the recipe's content slot plus the positioner's edge gap.
 */
function Content(
  allProps: Omit<React.ComponentProps<typeof DrawerContent>, "style"> & StyleOverrideProps,
) {
  const { palette, style, className, ...rest } = allProps as Record<string, unknown>;
  const v = useContext(VariantContext);
  // Resolved outside the stylex.props spread: StyleX drops a create() whose
  // only use sits inside the cast spread, leaving a dangling reference.
  const sizeFix = fromMap(contentSizeFix, v.size);
  const layout = sheet.base;
  const sx = stylex.props(
    ...([
      palette,
      ...resolveSlotStyles(drawerRecipe.slots.content, v),
      chrome.content,
      sizeFix,
      layout,
      style,
    ] as unknown as ReadonlyArray<never>),
  );
  return (
    <ContentElement
      {...rest}
      className={
        typeof className === "string" && className.length > 0
          ? [sx.className, className].filter(Boolean).join(" ")
          : sx.className
      }
      style={sx.style}
    />
  );
}
Content.displayName = "DrawerContent";

/**
 * Drawer — UI SDK's drawer wearing the drawer slot recipe: the sheet slides
 * in from `placement`'s edge, can be dragged back to dismiss, traps focus and
 * makes the page inert. There is no Portal or Positioner: the engine renders
 * its own full-screen viewport above the page. `<Drawer.Root size
 * placement>` resolves the recipe once; every part inherits the variants
 * through context and composes via `render`.
 */
export const Drawer = {
  Root,
  Trigger: DrawerTrigger,
  Backdrop: withContext(
    DrawerBackdrop,
    (v) => [...resolveSlotStyles(drawerRecipe.slots.backdrop, v), chrome.backdrop],
    "DrawerBackdrop",
  ),
  Content,
  Header: withContext("div", "header", "DrawerHeader"),
  Body: withContext("div", "body", "DrawerBody"),
  Footer: withContext("div", "footer", "DrawerFooter"),
  Title: withContext(
    DrawerTitle,
    (v) => [...resolveSlotStyles(drawerRecipe.slots.title, v), text.reset],
    "DrawerTitle",
  ),
  Description: withContext(
    DrawerDescription,
    (v) => [...resolveSlotStyles(drawerRecipe.slots.description, v), text.reset],
    "DrawerDescription",
  ),
  Close: withContext(DrawerClose, "closeTrigger", "DrawerClose"),
};
```
