Recipes
Code for a single-slot drop area that copies, a reusable list component, and items that keep their own scroll position.
Short patterns that have no demo of their own. Each is a complete component you can copy.
Copy on drop into a single slot
A palette of colors and a slot that holds one of them. Dropping a color into the slot puts a copy there, with a new id, and the palette keeps all of its colors. A new drop replaces what the slot holds, a drag that only passes over the slot leaves it as it was, and dragging the slot’s color back to the palette clears the slot.
The copy gets a new id because ids must be unique across all zones of a type. The copy on drag demo copies at the start of the drag instead: it puts a copy back into the palette as soon as the drag starts, so the palette has no gap while you drag.
A generic list component
A zone is an attachment, so a list component that you reuse with different item markup takes the markup as a snippet:
Use it with bind:items and an item snippet:
The component owns the #each block and the zone, so the rules of a zone hold: one element per item, keyed by its id. The snippet renders what goes inside each item.
Keep the scroll position of a draggable
An item that scrolls on its own loses its scroll position in a drag: the dragged element is a copy that starts at the top, and an item dropped into another zone is a new element. Keep the position in the item’s data, as every piece of state that should survive a drag, and restore it in two places:
See scrolling for how a drag scrolls the containers around the zones.