Skip to content
SortableExamples

Vanilla JS — Copy items

comins-sortable@0.1.6Vanilla JS

Create copies with unique IDs in Vanilla JS 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 the source with createSortable, then register the destination on that same instance. copyElement returns a detached element with a new data-sortable-id. Do not append it yourself; let the package validate and insert it.

copyElement?: (source: Element, context: CopyItemContext) => Element;

The copy must be detached and match the destination item selector. The destination’s getItemId must return the new ID; the default resolver reads data-sortable-id. cloneNode does not copy separately registered application listeners or state.

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

Drag an item from the catalog to the board more than once.
Catalog2
Note
Chart
Copies0

Ready to copy