Skip to content

Swap thresholds

The swap zone is the part of an item that, when dragged over, makes the dragged item swap places with it. SwapThreshold sets how much of the item (from the centre out) is an active swap zone - the rest is a "dead zone" that does not trigger a swap. InvertSwap moves the active zones to the item's edges instead, giving a "sort between items" feel, and InvertedSwapThreshold sets their size. The shaded bands show the active zones as you move the sliders. This mirrors the SortableJS thresholds demo.

Vertical list
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Grid
1
2
3
4
5
6
7
8
9

List: Item 1, Item 2, Item 3, Item 4, Item 5, Item 6

Grid: 1, 2, 3, 4, 5, 6, 7, 8, 9

Try it

  • Set SwapThreshold to 1: the whole item is the zone, and the dragged item swaps as soon as the pointer enters the next one. Set it to 0.2: the pointer has to reach the thin band in the middle, and the edges around it do nothing.
  • Tick InvertSwap: the zones move to the edges, and entering the top or bottom part of an item puts the dragged one before or after it - a "drop between the items" feel. InvertedSwapThreshold sizes the two edge bands together.
  • Swap an item past the next one and move the pointer back a little: the area the dragged item has just left - its shadow - swaps it back. That rule, ported from SortableJS, is what keeps a short item and a tall one from trading places back and forth while the pointer sits still.
  • The grid is measured left to right (Direction="Horizontal"), so its zones run across each square.

Direction

Direction is the axis the swap zones are measured along. BortableDirection.Auto, the default, reads it from the layout of the first two items: side by side is horizontal, anything else vertical. With a single item in view it reads the list instead: a flex row, or a grid of several columns, is horizontal, a flex column vertical. Set it yourself when the layout says otherwise - in a grid, whose first two items sit side by side although the list also runs down. The hit test itself is two-dimensional, so a grid sorts in every direction whatever Direction says; Direction only decides which way the zones span.

An item that enters a list it is not in yet always uses the inverted zones, whatever InvertSwap says, so it can be dropped before the first item as easily as after the last.

Parameters on this page

ParameterDefaultWhat it does
SwapThreshold1How much of an item, from 0 to 1 and measured from its centre, is the active zone.
InvertSwapfalseMoves the active zones to the item's edges.
InvertedSwapThreshold0The size of the inverted zones, from 0 to 1; 0 uses SwapThreshold.
DirectionAutoThe axis the zones are measured along: Auto, Horizontal or Vertical.
StyleThe container's inline style - here, the two custom properties this page's stylesheet draws the zones with.
An unhandled error has occurred. Reload 🗙