Item connects one rendered entry to the surrounding SnapSort Container. Import the component from the React binding and object types from the package root.

import { Item } from "@snap-engine/snapsort/react";

<Item itemId={task.id}>{task.title}</Item>;

For collection design and complete interfaces, see Basic Item and Item Recipes.

Component Properties

interface ItemProps extends Omit<HTMLAttributes<HTMLDivElement>, "children"> {
  children: ReactNode;
  className?: string;
  item?: SnapSortItem | null;
  itemId: string;
  metadata?: ItemMetadata;
  selected?: boolean;
}

children

Type: ReactNode · Required

The content 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, React keys, and callback events. It must be non-empty, unique within one SnapSort root, and unchanged while the component is mounted. Remount with another key to use a new ID.

{
  tasks.map((task) => (
    <Item key={task.id} itemId={task.id}>
      {task.title}
    </Item>
  ));
}

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.

import { Item as SnapSortItem } from "@snap-engine/snapsort";

const existingItem = new SnapSortItem(engine, container, {
  itemId: task.id,
});

<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.

ref

Type: Ref<SnapSortItem>

Receives the component-owned or adopted core object. Supplying a ref does not change object ownership.

import { useRef } from "react";
import type { Item as SnapSortItem } from "@snap-engine/snapsort";

const itemRef = useRef<SnapSortItem | null>(null);

<Item ref={itemRef} itemId={task.id}>
  {task.title}
</Item>;

console.log(itemRef.current?.getIndexAndContainer());

className, style, and HTML attributes

Type: standard React HTMLAttributes<HTMLDivElement>

The component renders a div. className is merged with snapsort-item; style, events, ARIA attributes, and custom data-* attributes are forwarded. User style values are merged after the stock Item styles.

<Item
  itemId={task.id}
  className="task-card"
  style={{ "--task-accent": "tomato" } as React.CSSProperties}
  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 a React ref. 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 React 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 React context.

interface HandleProps extends Omit<HTMLAttributes<HTMLDivElement>, "children"> {
  children: ReactNode;
  className?: string;
}

children

Type: ReactNode · 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>

ref

Type: Ref<HTMLDivElement>

Receives the Handle’s rendered div, not the core Item.

const handleRef = useRef<HTMLDivElement | null>(null);

<Handle ref={handleRef}>Drag</Handle>;

className, style, and HTML attributes

className is 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

Rendering an Item from a collection

Extract repeated markup into a component when every entry shares the same content and controls. Use the stable application ID as both React’s key and the itemId property.

function TaskItem({ task }: { task: Task }) {
  return (
    <Item key={task.id} itemId={task.id}>
      <article>
        <Handle className="drag-handle">Drag</Handle>
        <span>{task.label}</span>
      </article>
    </Item>
  );
}

In a mixed RenderTree loop, ghost entries render Ghost, entries with a childTree render a nested Container, and ordinary leaf entries render Item. Keep all three branches in the same keyed sequence.

Application-owned item lifetime

Adding or deleting an ordinary application value is a React-state operation. 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.

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 a ref when they only need an imperative handle. The creating code remains responsible for destroying an adopted object after the component releases it.