# PaneGrid

> 各格怎么排：左右并排的栏、上下分开的一栏、折成标签栏的一格、最大化的一格，大小以 px 为单位，由调用方保存。

Source: https://raft-design.botiverse.build/zh/components/pane-grid. Translated from https://raft-design.botiverse.build/components/pane-grid.md; where the two differ, the English page is right.

PaneGrid 负责排列各格：左右并排的栏，或者嵌套一个 `orientation="vertical"` 的 PaneGrid，把一栏上下分开。一格可以折成它的标签栏，也可以最大化一格。它建在 [Resizable](https://raft-design.botiverse.build/zh/components/resizable.md) 上；大小是调用方保存的 px，网格只报告有人拖动的接缝（[标签页与分栏](https://raft-design.botiverse.build/zh/patterns/tabs-and-panes.md#panes-and-columns)）。

```tsx
import { useCallback, useMemo, useState } from "react";
import { Globe } from "lucide-react";
import { PaneGrid, Tab, TabBar } from "@botiverse/raft-ui/wip";
import type { PaneGridCell } from "@botiverse/raft-ui/wip";

function keepFront() {}

/** A pane folded to its tab bar: only its tabs show. */
const FOLDED_PANE = (
  <TabBar label="Tabs" value="vite" onValueChange={keepFront}>
    <Tab value="vite" source="site" icon={<Globe />} title="vite.dev" pageTitle="Getting started" />
  </TabBar>
);

const SPLIT: PaneGridCell[] = [
  {
    id: "top",
    size: 180,
    min: 60,
    content: (
      <div className="flex size-full items-center justify-center bg-layer-canvas text-callout">
        Launch notes
      </div>
    ),
  },
  { id: "folded", size: 44, fixed: true, content: FOLDED_PANE },
];

/**
 * The conversation and two columns of panes; the first column is split top
 * and bottom, its lower pane folded to its tab bar. Drag a seam, or focus it
 * and use the arrow keys; the sizes are the caller's, in px.
 */
export function PaneGridPreviewContent() {
  const [sizes, setSizes] = useState<Record<string, number>>({ chat: 280, left: 260, right: 240 });
  const resize = useCallback(
    (next: Record<string, number>) => setSizes((current) => ({ ...current, ...next })),
    [],
  );
  const cells = useMemo<PaneGridCell[]>(
    () => [
      {
        id: "chat",
        size: sizes.chat!,
        min: 160,
        content: (
          <div className="flex size-full items-center justify-center bg-layer-canvas text-callout">
            #launch-week
          </div>
        ),
      },
      {
        id: "left",
        size: sizes.left!,
        min: 160,
        content: <PaneGrid orientation="vertical" layoutKey="left-split" cells={SPLIT} />,
      },
      {
        id: "right",
        size: sizes.right!,
        min: 160,
        content: (
          <div className="flex size-full items-center justify-center bg-layer-canvas text-callout">
            #25 Ship the tab bar
          </div>
        ),
      },
    ],
    [sizes],
  );
  return (
    <div className="h-64 w-full max-w-3xl overflow-hidden rounded-container border border-line-hairline">
      <PaneGrid layoutKey="chat-left-right" onResize={resize} cells={cells} />
    </div>
  );
}
```

## 何时使用
| 场景 | 使用 |
| --- | --- |
| 对话旁边带标签页的各格 | PaneGrid，每个单元格里一个 [Pane](https://raft-design.botiverse.build/zh/components/pane.md) |
| App 自己的、可以拖动分界的两栏 | [Resizable](https://raft-design.botiverse.build/zh/components/resizable.md) |

## 结构
- 网格：由 `cells` 组成的 `ResizableGroup`，每条接缝上一个 `ResizableHandle`。
- 单元格：一个 `id`、保存的 `size`（px）、`min` 和 `max`，以及它的 `content`。`fixed` 把它折成标签栏：固定在它的大小，两边都没有拖动条。`data` 给它加上 `data-*` 属性，供 App 做命中检测。

## 行为
- 只有有人拖动接缝时，`onResize` 才收到每个单元格的新大小（px），是窄窗口限制之前请求的大小。网格自己做的布局（比如窗口变窄时）从不改变保存的大小。
- 结构变化时（加了一格、折起一格、窗口宽度变化）`layoutKey` 跟着变：网格按保存的大小重新布局。
- `maximized` 让一个单元格占满网格，其他单元格藏起来；带 `keepWhenMaximized` 的单元格按它的大小留在旁边。最大化的单元格的内容拿到新的宽度。
- 最大化、还原和新的 `layoutKey` 会重新布局网格，单元格的内容会重新挂载。要保留下来的东西放在网格外面：外壳把页面从固定图层叠到各格上，标签页和草稿放在它自己的状态里（[标签页与分栏](https://raft-design.botiverse.build/zh/patterns/tabs-and-panes.md#moving-never-reloads)）。
- 单元格的最小值：一栏 160px，折起的一格是标签栏的高度：内容比它自己的最小宽度还窄时被裁切，而不是被压扁。

## 无障碍
- 每条接缝是一个可以获得焦点的分隔条：方向键调整大小。用 `messages.resize` 给它命名（默认 Resize）。

## 规则
- 最多 4 栏，上下分开的每一格至少 200px 高。
- 只保存人拖动过的大小。

## API

### PaneGrid

How panes are laid out: columns side by side, a column split top and bottom, and one pane maximized. Built on `ResizableGroup`, `ResizablePanel` and `ResizableHandle`: a seam is a focusable separator the arrow keys move. Sizes are px kept by the caller; the grid only reports a dragged seam.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `cells` (required) | `PaneGridCell[]` |  |  |
| `layoutKey` (required) | `string` |  | Changes when the cells' structure changes (a pane added or folded, the window's width): the grid lays out again from the saved sizes. |
| `maximized` | `string` | `null` | One cell fills the grid; the others are hidden. Maximizing and restoring lay the grid out anew, so the cells' content mounts again: keep what must live on (a page, a draft) outside the grid, as the shell's fixed layer does. |
| `messages` | `Partial<PaneGridMessages>` |  |  |
| `onResize` | `(sizes: Record<string, number>) => void` |  | A seam was dragged: each cell's new size in px. Never called for a layout the grid made itself. |
| `orientation` | `"vertical" \| "horizontal"` | `"horizontal"` | Columns side by side, or one column split top to bottom (a vertical PaneGrid inside a cell). |
