# Drag and drop (pyzed.dnd)

> API reference for pyzed.dnd: draggable, droppable and sortable widgets, drag handles, the DragEvent and the move and reorder helpers for drag and drop in Python desktop apps.

Source: https://albakore.github.io/pyzed/reference/dnd/

<!-- Generated by docs/scripts/gen_api.py. Edit docs/scripts/api_docs_en.py. -->

Pick things up, move them and drop them somewhere else: Kanban boards, sortable lists and
grids, form and page builders, Figma-style canvases and node wires. The movement, the
previews, the gaps and the animations run natively; Python hears about the drop. See the
[drag and drop guide](https://albakore.github.io/pyzed/dnd/).

```python
pz.sortable(items=self.tasks, item=self.task_row, id="tasks",
            on_reorder=lambda e: pz.dnd.move(self.tasks, e))
```

## Widgets

### `sortable`

```python
sortable(
    children=None,
    id='list',
    *,
    items=None,
    item=None,
    key=None,
    keys=None,
    data=None,
    group=None,
    accepts=None,
    direction='vertical',
    spacing=8,
    line_spacing=None,
    width=None,
    height=None,
    padding=None,
    align=None,
    handle=False,
    lock_axis=False,
    sort=True,
    pull=True,
    put=True,
    max_items=None,
    combine=False,
    locked=None,
    disabled=False,
    draggable=True,
    can_drop=None,
    placeholder='shadow',
    placeholder_style=True,
    ghost_opacity=0.4,
    over_style=False,
    active_style=False,
    source='hide',
    preview=None,
    lift=1.03,
    shadow=True,
    opacity=1.0,
    radius=6,
    threshold=4,
    delay=0,
    drop_animation=180,
    animation=200,
    from_children=False,
    keyboard=False,
    on_reorder=None,
    on_drop=None,
    on_enter=None,
    on_leave=None,
    on_over=None,
    on_drag_start=None,
    on_drag_end=None,
    theme=None,
)
```

A list (or row, or wrapping grid) whose items can be dragged to a new place.

Give the children with `keys` (stable ids: they keep each item's animation while the
order changes), or `items` with `item(x)` to draw one and `key(x)` to name it (its
`id` by default). Python keeps the order: `on_reorder(event)` says where an item went
(`event.from_index` → `event.index`, both counted in the final list) and
:func:`reorder`/:func:`move` apply it. Until then the other items slide out of the way, a
`placeholder` (`"shadow"`, `"ghost"` or `"none"`) shows the landing place and, when
the order changes, items glide to their new places in `animation` milliseconds (also when
the order changes from code; `draggable=False` keeps only that).

`direction` is `"vertical"`, `"horizontal"` or `"wrap"` (rows that wrap: a grid of
cards), with `spacing`, `line_spacing`, `width`, `height`, `padding` and
`align` (`"start"`, `"center"`, `"end"` across the list).

Several lists: the same `group` lets items move between them (a Kanban board); `accepts`
takes other groups or draggable types (drop a :func:`draggable` from a palette into the
list). `pull=False` keeps items in their list, `pull="clone"` copies them out (the
palette of a form builder), `put=False` takes nothing in, `sort=False` doesn't reorder
inside, `max_items` caps how many it holds, `can_drop(event)` adds your own rule and
`locked` lists keys that can't be dragged. `combine=True` lets an item be dropped onto
another one (`event.combine` is its key): folders, groups, merging.

Items: `handle=True` drags them only from a :func:`drag_handle`; `lock_axis` keeps them
on the list's axis and inside it; `source`, `preview`, `lift`, `shadow`,
`opacity`, `radius`, `threshold`, `delay` and `drop_animation` are as in
:func:`draggable` (`preview="none"` moves the items without a preview; `source="ghost"`
shows the faded item at its landing place, as `placeholder="ghost"`).
`keyboard=True` lets the focused item move with Alt+arrows (or Space, arrows, Space).

Events:
- `on_reorder(event)`: An item was dropped in the list (`on_drop` is the same).
- `on_enter(event)`: A drag it takes came over the list.
- `on_over(event)`: The landing index changed (`event.index`, `event.combine`).
- `on_leave(event)`: The drag left the list.
- `on_drag_start(event)`: One of its items started moving.
- `on_drag_end(event)`: One of its items was dropped (anywhere) or the drag was cancelled.

```python
pz.sortable(items=self.cards["todo"], item=self.card, id="todo", group="cards",
on_reorder=lambda e: pz.dnd.move(self.cards, e))
```

### `draggable`

```python
draggable(
    content,
    id,
    *,
    data=None,
    type='item',
    preview=None,
    handle=False,
    disabled=False,
    axis=None,
    bounds=None,
    grid=None,
    threshold=4,
    delay=0,
    source=None,
    source_opacity=0.35,
    lift=1.03,
    shadow=True,
    opacity=1.0,
    radius=6,
    wire_color=None,
    wire_width=2,
    drop_animation=180,
    from_children=False,
    cursor=True,
    on_drag_start=None,
    on_drag_move=None,
    on_drag_end=None,
    theme=None,
)
```

Lets the pointer pick `content` up and drop it on a :func:`droppable` or :func:`sortable`.

`id` names it (unique in the window) and `data` is what targets get as `event.data`.
`type` (a name or several) is what targets match with `accepts`.

Look while dragged: `preview` is what follows the pointer (by default a copy of
`content`; another widget; `"wire"`, a curve from where it started, for connecting nodes;
or `"none"`), lifted to `lift` times its size with a `shadow` (a color, or `False`)
and `opacity`. `source` is how the original looks meanwhile: `"ghost"` (faded to
`source_opacity`, the default), `"hide"` (gone: the item itself seems to move) or
`"keep"` (the default for wires).
On drop the preview flies to its landing place in `drop_animation` milliseconds.

Limits: `handle=True` starts the drag only from a :func:`drag_handle` inside;
`axis="x"`/`"y"` locks the movement; `bounds="parent"` keeps it inside the closest
target around it (`"window"` inside the window); `grid` (pixels, or `(x, y)`) moves it
in steps; `threshold` is how far the pointer moves before a press becomes a drag and
`delay` (milliseconds) asks for a long press instead (touch screens, lists that scroll).
Presses that a button or input inside takes don't start drags unless `from_children`.
`disabled` turns it off. `cursor=False` keeps the pointer's own cursor.

Events:
- `on_drag_start(event)`: The drag began.
- `on_drag_move(event)`: The pointer moved while dragging (`event.window_x`, `event.dx`...).
- `on_drag_end(event)`: The drag ended: `event.dropped` says if a target took it.

```python
pz.draggable(ui.card(ui.label("Report.pdf")), "file-7", data=file, type="file")
```

### `droppable`

```python
droppable(
    content,
    id,
    *,
    accepts=None,
    can_drop=None,
    disabled=False,
    snap=None,
    guides=False,
    snap_preview=None,
    placeholder='none',
    placeholder_style=True,
    ghost_opacity=0.4,
    over_style=True,
    active_style=False,
    guide_color=None,
    on_drop=None,
    on_enter=None,
    on_leave=None,
    on_over=None,
    theme=None,
)
```

Makes `content` a place where dragged items can be dropped.

`accepts` lists the draggable `type`\ s it takes (`None`: all; `"*"` too) and
`can_drop(event)` decides with any rule of yours (asked once per drag; `event.data` is
the dragged item). `disabled` refuses everything. A refused drag shows a "not allowed"
cursor over it.

Free canvases (Figma-like): the drop event's `item_x`/`item_y` is where the item's
top-left lands inside the target. `snap` (pixels or `(x, y)`) rounds it to a grid and
`guides=True` aligns it with the edges and centers of the other draggables inside (and of
the target), drawing the guides in `guide_color`. With `snap_preview` (on by default
when snapping) the preview itself jumps to that place.

Look: `over_style` while a drag it takes is over it and `active_style` while one is in
progress anywhere (`True`: the theme's look; a color; a dict of `background`,
`border_color`, `border_width`, `radius`; `False`). `placeholder="shadow"` shows
where the item would land (`placeholder_style`) and `"ghost"` a translucent copy.

Events:
- `on_drop(event)`: An item was dropped on it.
- `on_enter(event)`: A drag it takes came over it.
- `on_over(event)`: The landing place changed (`event.item_x`, `event.x`...).
- `on_leave(event)`: The drag left it (or was cancelled).

```python
pz.droppable(trash, "trash", accepts="file", on_drop=lambda e: self.delete(e.data))
```

### `drag_handle`

```python
drag_handle(content, *, cursor=Ellipsis)
```

The part of an item that starts the drag when the draggable or sortable has `handle=True` (the grip of a row, the title of a node). Shows `cursor` (`"grab"` by default) when hovered.

| Parameter | Description |
| --- | --- |
| `content` | What the handle looks like. |
| `cursor` | The cursor shown over it: `"grab"`, `"move"`, `"pointer"`... |

## Events and helpers

### `DragEvent`

```python
class DragEvent
```

What a drag and drop handler receives.

`type` is what happened: `"start"`, `"move"`, `"enter"`, `"over"`, `"leave"`,
`"drop"`, `"end"` or `"cancel"` (and `"check"` for `can_drop`).

About the dragged item: `source` (the draggable's id or the sortable item's key), `data`
(its `data`, or the item of a sortable built from `items`; `item` is the same),
`types`, `from_list`/`from_index` (when it comes from a sortable), `width`/`height`
and `copy` (the source list copies, or Alt is held).

About the target (`None` outside one): `target` (its id), `index` (where it goes in a
sortable, counted without the item itself), `combine` (the key of the item it is dropped
onto, with `combine=True`), `x`/`y` (the pointer inside the target) and
`item_x`/`item_y` (the item's top-left inside the target, snapped and aligned: where to
put it on a free canvas).

Also `window_x`/`window_y` (the pointer in the window), `dx`/`dy` (moved since the
start), `dropped` (for `"end"`) and the modifiers `shift`, `ctrl`, `alt`, `logo`.

### `move`

```python
move(lists, event, *, make=None)
```

Applies a drop to your lists and returns the item that landed.

`lists` maps the sortables' ids to their lists (or is the one list of a single sortable).
The item leaves `lists[event.from_list]` (unless the source copies, `pull="clone"`) and
goes into `lists[event.target]` at `event.index`. Items from a :func:`draggable` (no
`from_list`) or copies are `make(event.data)` when given (a new card from a palette
template), else a deep copy of `event.data`. Drops with `event.combine` are not moved:
handle those yourself (`event.combine` is the key of the item it was dropped onto).

```python
pz.dnd.move({"todo": self.todo, "done": self.done}, event)
```

### `reorder`

```python
reorder(items, from_index, to_index)
```

Moves `items[from_index]` to `to_index` (in place) and returns `items`.

### `current`

```python
current()
```

The drag in progress (a :class:`DragEvent` of type `"current"`), or `None`.

Use it in `view()` to change what is shown while something is dragged: a trash can that
appears, hints over the places that take the item... (the view is built again when a drag
starts and ends if a handler changes something, such as `on_drag_start`).

### `cancel`

```python
cancel()
```

Cancels the drag in progress: the preview flies back and `on_drag_end` reports it.
