Item
On this page
Item connects one rendered entry to the surrounding SnapSort Container.
Import the component from the Svelte binding and the object types from the
package root.
<script lang="ts">
import { Item } from "@snap-engine/snapsort/svelte";
</script>
<Item itemId={task.id}>{task.title}</Item> For collection design and complete interfaces, see Basic Item and Item Recipes.
Component Properties
type ItemProps = Omit<HTMLAttributes<HTMLDivElement>, "children"> & {
children: Snippet;
className?: string;
item?: SnapSortItem | null;
itemId: string;
metadata?: ItemMetadata;
selected?: boolean;
}; children
Type: Snippet · Required
The snippet rendered inside the Item’s managed div. SnapSort does not impose
a content structure.
<Item itemId={task.id}>
<strong>{task.title}</strong>
<span>{task.assignee}</span>
</Item> itemId
Type: string · Required · Construction-only
The stable identity used by moves, removals, ghosts, and callback events. It must be non-empty, unique within one SnapSort root, and unchanged while the component is mounted. Remount with a new Svelte key to use another ID.
{#each tasks as task (task.id)}
<Item itemId={task.id}>{task.title}</Item>
{/each} Pass identity through this property, not metadata.itemId; the latter is
rejected.
metadata
Type: ItemMetadata (Readonly<Record<string, unknown>>) · Default: {}
Application data copied into drag snapshots and exposed as itemMetadata and itemsMetadata in callback events. Replacing the object updates the core Item;
an active drag continues using its frozen shallow snapshot.
<Item
itemId={task.id}
metadata={{ kind: "task", projectId: task.projectId }}
>
{task.title}
</Item> SnapSort reads this value but never mutates it.
selected
Type: boolean · Default: false
Consumer-owned selection state. Starting a drag on a selected Item includes the other selected Items in the same tree; starting on an unselected Item drags only that Item.
<Item itemId={task.id} selected={selectedIds.has(task.id)}>
{task.title}
</Item> SnapSort supplies no selection controls, styling, keyboard behavior, or ARIA state.
item
Type: SnapSortItem | null · Default: null · Advanced
Adopts an existing core Item instead of creating one. The object must use the
same Engine, already belong to the surrounding Container, and have the same itemId. The object and ID cannot change after mount. The component binds its
element but does not reparent, destroy, or otherwise take ownership of an
adopted object.
<script lang="ts">
import { Item as SnapSortItem } from "@snap-engine/snapsort";
import { Item } from "@snap-engine/snapsort/svelte";
const existingItem = new SnapSortItem(engine, container, {
itemId: task.id,
});
</script>
<Item item={existingItem} itemId={task.id}>{task.title}</Item> Omitted metadata and selected properties preserve an adopted Item’s current
values; supplied values override them.
bind:item
Type: SnapSortItem | null
Returns the component-owned or adopted core object. The value becomes available
when the component initializes and is null after teardown. Binding does not
change object ownership.
<script lang="ts">
import type { Item as SnapSortItem } from "@snap-engine/snapsort";
let item = $state<SnapSortItem | null>(null);
</script>
<Item itemId={task.id} bind:item>{task.title}</Item>
<button onclick={() => console.log(item?.getIndexAndContainer())}>
Log location
</button> class, className, style, and HTML attributes
Type: standard Svelte HTMLAttributes<HTMLDivElement>
The component renders a div. Native class and the className alias are
merged with snapsort-item; style, events, ARIA attributes, and custom data-* attributes are forwarded.
<Item
itemId={task.id}
class:urgent={task.priority === "high"}
className="task-card"
style="--task-accent: tomato;"
aria-label={`Task: ${task.title}`}
onclick={() => selectTask(task.id)}
>
{task.title}
</Item> SnapSort owns the final data-snapsort-item-id attribute.
Core Item Object
Access the object through bind:item. The entries below cover the supported
SnapSort-specific surface; inherited SnapEngine object APIs are documented in Objects.
item.itemId
Type: ItemId · Read-only
Returns the construction identity supplied through the component or new Item(...).
console.log(item.itemId); item.metadata
Type: ItemMetadata · Read/write
Gets or replaces the application metadata associated with the Item. Prefer the component property while Svelte owns the object.
item.metadata = { kind: "task", projectId: "alpha" }; item.locked
Type: boolean · Read/write · Default for Item: false
Controls whether this object can start a drag.
item.locked = true; item.selected
Type: boolean · Read/write
Gets or sets the consumer-owned multi-drag selection flag. Prefer the component property for declarative selection state.
item.selected = selectedIds.has(item.itemId); item.container
Type: Container · Read-only
Returns the direct owning Container. Detached Items do not have a usable container value.
const owner = item.container; item.rootContainer
Type: Container · Read-only for consumers
Returns the root of the Item’s current SnapSort tree.
const root = item.rootContainer; item.depth
Type: number · Read-only
Returns the Item’s current depth in the SnapSort object tree. A root Container
has depth 0; each Item or nested Container level adds one.
console.log(item.depth); item.isGhost
Type: boolean · Read-only
Reports whether the object is a transient engine-owned ghost Item. A normal Item component exposes false.
if (!item.isGhost) console.log(item.itemId); item.dragSnapshot
Type: ItemSnapshot<Item> | null · Read-only
The frozen geometry and child-state snapshot used for the active drag, or null outside that lifecycle.
const startRect = item.dragSnapshot?.box; item.dragPointerPosition
Type: { x: number; y: number } | null · Read-only
Returns the active pointer position in world coordinates when this Item has a drag snapshot.
const pointer = item.dragPointerPosition; item.dragPositionX
Type: number · Read-only
Returns the Item’s projected world-space left edge during a drag. Outside an active drag, it falls back to the object’s current world X position.
const left = item.dragPositionX; item.dragPositionY
Type: number · Read-only
Returns the Item’s projected world-space top edge during a drag. Outside an active drag, it falls back to the object’s current world Y position.
const top = item.dragPositionY; item.getIndexAndContainer()
getIndexAndContainer(): { index: number; container: Container | null } Returns the current direct Container and zero-based index. A detached Item
returns { index: -1, container: null }.
const { container, index } = item.getIndexAndContainer(); Engine-owned ghost creation/state methods, internal drag mutation methods, and
inherited ElementObject methods are intentionally outside this reference.
Handle
Handle makes one descendant element the Item’s drag input. It renders a div and registers it with the nearest Item through Svelte context.
type HandleProps = Omit<HTMLAttributes<HTMLDivElement>, "children"> & {
children: Snippet;
className?: string;
}; children
Type: Snippet · Required
The visible grip, label, icon, or other content rendered inside the handle.
<Item itemId={task.id}>
<Handle aria-label={`Drag ${task.title}`}>⋮⋮</Handle>
{task.title}
</Item> class, className, style, and HTML attributes
Native classes and className are merged with snapsort-handle. Styles,
events, ARIA attributes, and other div properties are forwarded.
<Handle className="drag-grip" style="cursor: grab;">Drag</Handle> Use one Handle per Item. The core object has one input-alias slot, so a later Handle replaces the earlier drag target.
Appendix
Application-owned item lifetime
Adding or deleting an ordinary application value is a Svelte-state operation.
Render each value from a keyed loop and let reconciliation mount or unmount its Item. That synchronization does not emit onItemInsert or onItemRemove;
those callbacks report operations initiated by SnapSort’s core API.
{#each tasks as task (task.id)}
<Item itemId={task.id}>{task.title}</Item>
{/each} Metadata, geometry, and selection
Metadata is application data, not geometry. Insertion-marker geometry is
carried by GhostState and rendered through Ghost or insertionMarkerRect; hover and swap geometry is customized through the
owner Container’s getItemHitbox callback.
Selection is similarly application-owned. Standard DOM events can update the
selected ID set, and the selected property projects that state into
SnapSort. Add normal keyboard behavior and ARIA state when the Item itself
acts as a control.
Adopting a core Item
Adoption is intended for integrations that create the core object separately.
Most components should omit item and use bind:item when they only need an
imperative handle. The creating code remains responsible for destroying an
adopted object after the component releases it.
See Adopting a Core Item for the complete component-and-helper pattern.