Inventory

A bag of item slots with drag and drop (mouse, pen and touch), keyboard moving, stack merging, locked slots, category tabs, search, sort and item tooltips. Controlled or uncontrolled.

View as Markdown

Controlled bag with 24 slots (the last 6 locked), tabs, search and sort. The selected slot drives an ItemInfo next to it. Drag a stack of rice seeds onto the other stack to merge them.

Backpack
12 / 18

Arrow keys move between slots. Space picks an item up and drops it. Enter uses it.

Giant pumpkin ×2
RareCrop · Autumn
Wins ribbons at the harvest fair.
Weight
38 kg
95 coins

Installation

npx shadcn@latest add @cozy/inventory

Usage

import { Inventory, type InventorySlots } from "@/components/ui/inventory"

const [items, setItems] = useState<InventorySlots>(bag)
<Inventory items={items} onItemsChange={(next) => setItems(next)} slots={24} />

Keyboard: arrows move between slots, Space picks an item up and drops it, Escape cancels, Enter calls onActivate. Moves are announced to screen readers.

GameItem: { id, name, icon, quantity, maxStack, stackKey, rarity, category, type, description, stats, value, level, meter, corner, locked, disabled, tags, data }. Put anything your game needs in data.

applyMove(items, from, to, stacking) is exported, so a server or game loop can apply the same rules.

Examples

Hotbar

Eight slots, no header. corner shows the hotkey, meter draws durability or cooldown. Enter or double-click calls onActivate.

Arrow keys move between slots. Space picks an item up and drops it. Enter uses it.

API

Everything else is passed through to the underlying element or Radix primitive.

PropTypeDefaultDescription
items / defaultItems(GameItem | null)[]–Slot contents in order; null is empty. Use items + onItemsChange for controlled, defaultItems for uncontrolled.
onItemsChange(items, change) => void–New slots after a move, merge or sort. change is { type: "move" | "merge" | "sort", from?, to?, moved?, sortId? }.
slotsnumberitems.lengthTotal slots; pads with empty ones.
lockedFromnumber–Slots from this index on are locked (bag upgrades).
columns / slotSizenumberfill / 64Fixed column count and tile size in px.
selected / defaultSelected / onSelectnumber | null–Selected slot index, controlled or not.
onActivate(item, index) => void–Double-click or Enter: use, equip or eat.
canMove(from, to, items) => boolean–Return false to refuse a move, e.g. quest items or a full slot type.
movable / stackingbooleantrueTurn off moving, or merging of stacks with the same stackKey (or name) up to maxStack.
categories{ id, label, icon?, match? }[]–Filter tabs. Items match on item.category unless you pass match. Moving pauses while filtered.
search / sortsboolean | string / { id, label, compare }[]–A search box, and sort buttons that reorder the bag.
tooltip / formatValue / raritiesboolean / fn / RecordtrueItem tooltips, how values print, and custom rarity labels and colours.
title / showCapacity / headerEnd / placeholder / emptyTextReactNode–Header, used/total count, extra controls, empty-slot content and the no-match text.