# Drag handle

> Configure the gutter width, grip height, grip renderer, and drag preview.

`drag_handle()` adds a grip in the left gutter. Dragging the grip moves the block or the selected range of sibling blocks on drop.

## Try it

Drag the grip beside “Collect feedback” to move the whole quote. To try the count badge, click “Draft the outline”, press **Shift + Down Arrow** twice, and drag the quote’s grip.

[Drag handle example](https://gpdoc.dev/docs/plugins/behavior/drag-handle/#drag-handle-example)

## Install

```sh
cargo add gpdoc-plugin-drag-handle
```

## Example

Pass `block_grip` to `with_view` to display the built-in grip in the gutter and during a drag.

```rust
use gpdoc::EditorConfig;
use gpdoc_plugin_drag_handle::{block_grip, drag_handle};
use gpdoc_plugin_paragraph::paragraph;


let config = EditorConfig::builder()
    .plugin(paragraph())
    .plugin(drag_handle().with_view(block_grip))
    .build()
    .expect("configure editor");
```

## Compose the drag handle

The `with_view` renderer receives a `DragHandleSlot`. Use `slot.part` to give the grip a different appearance during a drag. The slot also provides the editor entity and `before`, the document position immediately before the block that owns the grip.

### Anatomy

```text
drag_handle().with_view(...)
├── DragHandlePart::Grip     In the left gutter
└── DragHandlePart::Preview  Beside the dragged block content
```

### Show a label while dragging

Replace `block_grip` in the example above with `render_handle`. The gutter shows a 14 px grip. When you drag it, “Move” appears beside the block copy.

```rust
use gpdoc_plugin_drag_handle::{DragHandlePart, DragHandleSlot};
use gpui::{App, Div, Window, div, prelude::*, px};


fn render_handle(slot: DragHandleSlot, _: &mut Window, _: &mut App) -> Div {
    match slot.part {
        DragHandlePart::Grip => div().text_size(px(14.)).child("⠿"),
        DragHandlePart::Preview => div().child("Move"),
    }
}
```

## Settings

Apply these settings to `drag_handle()` before calling `with_view`, which returns the finished `ViewPlugin`.

### `gutter_width`

Sets the width of the left gutter and the area where a grip drag can start. Defaults to `px(20.)`.

```rust
let handle = drag_handle().gutter_width(gpui::px(28.));
```

### `handle_height`

Sets the height of the grip’s drag target and the minimum height of each top-level block. Defaults to `px(24.)`.

```rust
let handle = drag_handle().handle_height(gpui::px(32.));
```

### `with_preview`

Sets the renderer for the dragged block content. By default, the block copy follows the pointer at 55% opacity. The renderer receives a `DragPreviewSlot` with three fields.

* `content` holds the full-opacity copy at the original size.
* `range` is the document range captured at drag start, with an exclusive end.
* `block_count` counts the sibling blocks being moved. A quote containing two paragraphs counts as one when dragged as a whole.

```rust
use gpui::{div, prelude::*};


let handle = drag_handle().with_preview(|slot, _, _| div().opacity(0.75).child(slot.content));
```

## Preview placement

### `DragPreviewPlacement::over_blocks`

Aligns the preview with the top-left corner of the dragged block range at drag start. Returning a plain GPUI element from `with_preview` uses this placement by default.

### `DragPreviewPlacement::beside_handle`

Aligns the preview with the top-right corner of the grip being dragged. Use it for a count badge that follows the grabbed grip even when the selection starts several blocks above it.

```rust
use gpdoc_plugin_drag_handle::DragPreviewPlacement;
use gpui::{div, prelude::*};


let handle = drag_handle().with_preview(|slot, _, _| {
    if slot.block_count == 1 {
        return DragPreviewPlacement::over_blocks(div().opacity(0.55).child(slot.content));
    }
    let badge = div().whitespace_nowrap().child(format!("{} blocks", slot.block_count));
    DragPreviewPlacement::beside_handle(badge)
});
```

## Source code

[Drag handle implementation](/source/plugins/drag-handle/src/lib.rs.txt)