> ## Documentation Index
> Fetch the complete documentation index at: https://dnd-grid.blode.md/llms.txt
> Use this file to discover all available pages before exploring further.

# Composition

Render state-aware item content with useDndGridItemState.

<div className="not-prose my-6 rounded-lg border border-zinc-200/70 bg-white/70 shadow-sm dark:border-white/10 dark:bg-white/5">
  <iframe
    title="Composition preview"
    src="https://blode.co/dnd-grid/examples/composition-example?embed=1"
    className="h-[640px] w-full"
    loading="lazy"
  />
</div>

[View source on GitHub](https://github.com/mblode/dnd-grid/blob/main/apps/web/examples/dnd-grid-composition-example.tsx)

## Installation

### CLI

```bash
npx shadcn@latest add https://blode.co/dnd-grid/r/composition-example.json
```
### Manual

```bash
npm install @dnd-grid/react
```

```css
@import "@dnd-grid/react/styles.css";
```

```tsx title="components/dnd-grid-composition-example.tsx"
"use client";

import { DndGrid, type Layout, useDndGridItemState } from "@dnd-grid/react";
import { useState } from "react";

const initialLayout: Layout = [
  { id: "c1", x: 0, y: 0, w: 3, h: 2 },
  { id: "c2", x: 3, y: 0, w: 3, h: 2 },
  { id: "c3", x: 6, y: 0, w: 3, h: 2 },
  { id: "c4", x: 9, y: 0, w: 3, h: 2 },
];

function StateAwareContent() {
  const { item, state } = useDndGridItemState();
  let label = item.id;
  if (state.dragging) {
    label = "Dragging";
  } else if (state.resizing) {
    label = "Resizing";
  }

  return (
    <div className="grid-item">
      <span>{label}</span>
      {state.dragging && (
        <span className="block text-xs text-zinc-500">
          {item.x},{item.y}
        </span>
      )}
    </div>
  );
}

export function CompositionExample() {
  const [layout, setLayout] = useState<Layout>(initialLayout);

  return (
    <div className="space-y-4">
      <div className="text-xs text-zinc-500">
        Items use useDndGridItemState() to render state-aware content.
      </div>
      <DndGrid
        cols={12}
        layout={layout}
        onLayoutChange={setLayout}
        rowHeight={50}
      >
        {layout.map((item) => (
          <StateAwareContent key={item.id} />
        ))}
      </DndGrid>
    </div>
  );
}
```

## Usage

```tsx
import { CompositionExample } from "@/components/dnd-grid-composition-example";

export default function Page() {
  return <CompositionExample />;
}
```