Skip to content
SortableExamples

Svelte — Copy items

comins-sortable@0.1.6Svelte

Create copies with unique IDs in Svelte while retaining the source, and verify cancellation and reverse rejection.

  1. Drag the Note handle from the catalog into the empty board. Both source items remain and one copy appears.
  2. Copy the same Note again. The board contains two copies with distinct IDs.
  3. Dragging a copy back into the catalog is refused; this example permits delivery only from catalog to board.
  4. Start a source drag and cancel with Escape. List membership remains unchanged.
  5. Reset restores the source order and clears the board. The ID sequence is not reused.

Dragging uses pointer input. Inspect the onAfterDrag status and displayed IDs to verify the result.

Area Setting Meaning
Source group: { name: "templates", pull: "copy", put: false } Copies out and refuses incoming items from other areas.
Destination group: { name: "templates", pull: false } Accepts the same group and does not export items.
Both Different areaId values Distinguishes area identity in the shared scope.

pull: "copy" retains source items during cross-area delivery. It does not automatically disable reordering within that same area. The source remains visible during a drag while preview and placeholder provide separate feedback.

Create a scope from comins-sortable/svelte and attach use:sortable to both elements. Reactive options pass items and commit onItemsChange. Keep DOM identity with {#each ... (item.id)} and destroy the owned scope when the component is removed.

type CopyItem<T> = (item: T, context: CopyItemContext) => T;

Return a new object and a new ID. The example uses spread because items are shallow objects. Add an application-specific cloning policy if nested objects must be edited independently.

Check onAfterDrag.status === "dropped"; a list update notification alone does not confirm completion. Distinguish Escape, outside drops, and policy rejection from a committed copy. Missing or duplicate IDs and malformed copy results are not valid copies.

The ID counter is local to one demo instance. Persistence or multi-client use needs an application-wide collision-free identity policy. Alt/Option copying, custom previews, and exhaustive malformed-input testing are separate scenarios.

Copy guide · Core lifecycle · Vanilla instance · Transfer with this adapter