# Box selection

> Drag a rectangle from the margin outside a block to select whole blocks.

## Try it

Press in the left margin beside “Draft the outline”. Hold the mouse button and drag diagonally across “Review the draft”. Both paragraphs highlight as the rectangle crosses them. Release to keep the blocks selected.

[Box selection example](https://gpdoc.dev/docs/plugins/behavior/box-selection/#box-selection-example)

Start the drag outside all blocks. Inside a paragraph, even the blank space after the last character uses normal text selection. The box selects whole top-level blocks, including an entire list or table when it intersects one.

## Install

```sh
cargo add gpdoc-plugin-box-selection
```

## Example

```rust
use gpdoc_plugin_box_selection::{box_selection, selection_box};
use gpdoc_plugin_paragraph::paragraph;


let config = gpdoc::EditorConfig::builder()
    .plugin(paragraph())
    .plugin(box_selection().with_view(selection_box))
    .build()
    .expect("configure editor");
```

Use `EditorSurface::layout` to leave space between the editable document and the edge of the surface. A drag starting in this space can select blocks. The [page layout example](/docs/quickstart/#place-the-document-in-a-page) centers the document and leaves a gutter on both sides.

For keyboard-based block selection, also register [Node range](/docs/plugins/behavior/node-range/).

## Compose the selection rectangle

### Anatomy

```text
box_selection().with_view(...)  Tracks the drag and selects intersecting blocks
└── bounds                      Position and size of the rectangle in the window
    └── selection_box(...)      Fills the rectangle with the default overlay
```

### Add a label to the rectangle

Replace `selection_box` in the example with this renderer. It keeps the default rectangle and adds a label while the drag is in progress.

```rust
use gpui::{App, Bounds, Div, Pixels, Window, prelude::*};


fn labeled_box(bounds: Bounds<Pixels>, window: &mut Window, cx: &mut App) -> Div {
    selection_box(bounds, window, cx).child("Select blocks")
}
```

Release the mouse to hide the rectangle and keep the intersecting blocks selected.

## Source code

[box-selection/src/lib.rs](/source/plugins/box-selection/src/lib.rs.txt)