CraftUI

Hotbar

A row of item slots with one always selected, chosen with the arrow keys, the number keys or a click.

Iron Sword, 90% durability
Iron Pickaxe, 42% durability
Torch, 24
Bread, 12
Empty slot
Oak Planks, 64
Empty slot
Glow Potion
Sea Gem, 3

Installation

npx shadcn@latest add @runqora/craft-hotbar

Usage

import { Hotbar } from "@/components/craft/hotbar";

const [slot, setSlot] = useState(0);

<Hotbar aria-label="Hotbar" items={items} onValueChange={setSlot} value={slot} />;

Examples

Number keys anywhere

With hotkeys, the number keys pick a slot from anywhere on the page, except while typing in a field. Without it, they work while the hotbar has focus.

Iron Sword, 90% durability
Iron Pickaxe, 42% durability
Torch, 24
Bread, 12
Apple, 5

Press 1 to 5 anywhere on this page. Holding: Iron Sword

Props

Hotbar

Prop

Type

Accessibility

  • It's a listbox: Tab reaches the selected slot, and the selection follows the arrow keys, wrapping at the ends. Home and End go to the first and last slot.
  • The number keys pick a slot: 1 to 9, then 0 for the tenth.
  • Page-wide hotkeys take the number keys from the page. Turn them on only where nothing else needs those keys, and they ignore key presses with Alt, Ctrl or Command.
  • The name shown with showName is for sighted players. Screen readers hear the selected slot.

Customization

Items are plain objects from your app. CraftUI never looks them up:

interface Item {
  id: string; // a stable id, for React keys
  name: string; // read out as the slot's label, and the tooltip's title
  icon?: ReactNode; // usually an <img>, or an SVG
  count?: number; // shown from 2
  durability?: number; // 0 to 1, shown as a bar below 1
  description?: ReactNode; // more lines for the tooltip
}

slots sets the number of slots. It defaults to the number of items, or 9.

Dependencies

  • craft-item-slot
  • shadcn's utils, and craft-theme

Source

On this page