# Drops and drag-to-reorder

`ixfx-grid-list`, `ixfx-data-grid`, `ixfx-detail-list`, `ixfx-vertical-list` and `ixfx-flat-list` share one drop API. It covers three cases:

1. **Reorder within the list.** The user drags one item, or a multi-selection, to a new position in the same list. The app saves the new order.
2. **Insert at a position.** Something from another panel, or the OS, is dropped *between* two items.
3. **Drop on an item.** Something is dropped onto an item, e.g. to add it to the set or folder that item stands for.

A list can want any combination. A set view wants 1 and 2; a list of sets wants only 3.

**The list never reorders its own items.** The host owns the order, as it owns the selection. The list resolves where a drag would land, draws an indicator, and tells you with `list-reorder` or `list-drop`.

| Component | Items | Gap between items flows | Indicator |
|---|---|---|---|
| `ixfx-grid-list` `grid` | left / right half of a cell | along the row, then wraps | vertical caret between cells |
| `ixfx-grid-list` `row` | left / right half | along the row | vertical caret |
| `ixfx-grid-list` `column`, `ixfx-data-grid`, `ixfx-vertical-list` | top / bottom half | down | horizontal line between rows |
| `ixfx-detail-list` | top / bottom half | down a column, then wraps right | horizontal line within the column |

A `grid` that is narrow enough to have one column behaves as `column`.

## Properties

| Property | Attribute | Type | Default | |
|---|---|---|---|---|
| `itemsDroppable` | `items-droppable` | `boolean` | `false` | Items and empty space accept drops |
| `dropPositions` | `drop-positions` | `'on' \| 'between' \| 'both'` | `'on'` | Which targets a drag can resolve to |
| `itemsReorderable` | `items-reorderable` | `boolean` | `false` | A drag that starts here and ends between its own items is a reorder. Implies `items-draggable`, and `between` targets for that drag |
| `canDrop` | – | `(target, dataTransfer) => boolean` | – | Sync gate on every `dragover`. `false` ⇒ no indicator and no event |
| `canReorder` | – | `(keys, target) => boolean` | – | Sync gate for internal moves |
| `resolveInternalDrag` | – | `(dataTransfer) => string[] \| undefined` | – | See [Host-substituted drags](#host-substituted-drags) |
| `reorderBlocked` (read-only) | `reorder-blocked` | `'sorted' \| 'grouped' \| undefined` | – | Why `between` is refused right now |

`canDrop` is not asked about the `between` target of an internal move: `canReorder` gates that. Ask `canReorder` for things like "only hand-picked items can move" or "nothing may go above a pinned item".

## Targets

```ts
type ListDropTarget =
  | { kind: 'on'; key: string }
  | { kind: 'between'; beforeKey: string | undefined; afterKey: string | undefined; index: number }
  | { kind: 'empty' };
```

For `between`, read the gap as "insert **after** `afterKey` and **before** `beforeKey`":

- `afterKey` is the item before the gap. It is `undefined` at the start of the list (`index` 0).
- `beforeKey` is the item after the gap. It is `undefined` at the end.
- `index` is the insertion index into the **full item order**: the unfiltered `items` / `rows` / children you set, not the visible order.

**Filters.** A filter can hide items between two visible neighbours. The drop goes directly after the visible item before the gap: `afterKey` is that visible item and `beforeKey` is whatever follows it in full order, hidden or not. It is the same rule in every list.

**Wrapping.** Where a row or column wraps, the gap after the last cell of one line is the gap before the first cell of the next. It is reported once (`afterKey` is the last cell of the line) and drawn on the side the pointer is on.

**Empty space.** Past the last item, the target is `between` at the end. In an empty list it is `between` at index 0. With `drop-positions="on"` it is `{ kind: 'empty' }` instead, as it always was.

**`drop-positions="both"`.** The outer quarters of an item (on the flow axis) mean `between`; the middle half means `on`. If `canDrop` refuses one of them, the other takes the whole item.

**Never a target** (no indicator, no event):

- an internal move that changes nothing: the gap is inside the dragged block or right next to it, or the target is a dragged item;
- any `between` while the order isn't meaningful, see below.

### When `between` is refused

| List | While |
|---|---|
| `ixfx-data-grid` | a sort column is active (`'sorted'`), or rows are grouped (`'grouped'`) |
| `ixfx-detail-list` | `sort` is set (`'sorted'`), or `groupBy` is set (`'grouped'`) |
| `ixfx-grid-list` | `groupBy` is set (`'grouped'`) |

`on` drops still work. `reorderBlocked` and the `reorder-blocked` host attribute say why, so you can explain it.

## Events

| Event | Detail | |
|---|---|---|
| `list-drop` | `{ target, dataTransfer }` | A drag from outside was dropped (or an internal drag onto an item) |
| `list-reorder` | `{ keys, target }` | Cancelable. `keys` is the dragged block in display order; `target` is a `between` |

`ixfx-vertical-list`'s `list-drop` also carries `key` and `item` for an `on` target, as before. `ixfx-flat-list` keeps `flat-list-drop` (now with `target`), and the inner `list-reorder` bubbles out of it.

The dragged block follows the existing drag-origin rule: dragging a selected item drags the whole selection; dragging an unselected one drags just that item. Its keys are held by the list, so they survive their cells being recycled or scrolled out of the window.

## Applying a reorder

```ts
import { applyListReorder } from '@ixfx/components';

list.itemsReorderable = true;
list.addEventListener('list-reorder', (e) => {
  items = applyListReorder(items, item => item.id, e.detail);
  save(items);
  list.items = items;            // `rows` for ixfx-data-grid
});
```

`applyListReorder(items, keyOf, detail)` is pure. The moved block keeps its relative order, `target.index` counts positions *before* the block is lifted out (so moving down is not off by one), and a stale detail is re-found from `afterKey` / `beforeKey`. An unresolvable detail returns an unchanged copy.

Selection and cursor stay the same across a reorder: the same keys remain selected and the cursor stays on the same key when you hand back the permuted items. The moved cells slide to their new places.

## Behaviour while dragging

- **Virtualisation.** Targets are worked out from layout geometry, not from rendered elements, so a gap whose neighbours are scrolled out can still be targeted. The cell a native drag started from stays rendered however far the list scrolls (the browser delivers `dragend` to it).
- **Edge auto-scroll.** While a drag is over the list, the list scrolls along its scroll axis when the pointer nears the edge (vertical for `grid` / `column`, `data-grid` and `vertical-list`; horizontal for `row` and `detail-list`), faster the longer it is held. After each scroll the indicator is worked out again for the pointer that hasn't moved.
- **`ixfx-vertical-list`** keeps its `list-drag-over` event: a listener that cancels it accepts an external drop. A reorder between rows needs no listener.

## Host-substituted drags

A host that cancels the browser drag in `list-native-drag-start` and starts an OS drag in its place (so a file can be dragged to the desktop) leaves the browser seeing a foreign drag when it comes back. Set `resolveInternalDrag` so a drop inside the list is still a `list-reorder`:

```ts
list.resolveInternalDrag = dt =>
  dt.types.includes('application/x-myapp-keys') ? currentDragKeys : undefined;
```

It runs on every `dragover` and on `drop`. `dataTransfer` data is not readable during `dragover`, so answer from your own state. For the ordinary case (the browser drag is not cancelled) the list remembers the drag started here.

## Theming

| | |
|---|---|
| `part="drop-indicator"` | the caret / line |
| `--list-drop-indicator-color` | default `--accent` |
| `--list-drop-indicator-size` | thickness, default `2px` |
| `--list-drop-target-bg`, `--list-drop-target-outline` | the item an `on` target is over (`data-drop-target`) |

## Item tags

`tagBehaviour` takes `reorderable: false` to keep tagged items from being moved. They are left out of a dragged block, so the rest of a multi-selection still moves, and a gap next to them can still be targeted. See [Item tags](./user-item-tags.md).

## Not covered

- Keyboard reordering: bind Move up / down / to top in your app and reassign `items`.
- Moving between two lists as one gesture: to the target list that is an external drop carrying your payload.
- `ixfx-multicol-list`.
