# Item Info

The details of an item: rarity, type, description, stats with +/- against the equipped one, value and action buttons. As a card, a compact tooltip or a panel.

Docs: https://cozy-ui.solo.engineer/docs/components/item-info/

Install: `npx shadcn@latest add @cozy/item-info`

### Usage

```tsx
import { ItemInfo, ItemTooltip } from "@/components/ui/item-info"

<ItemInfo item={item} actions={[{ id: "use", label: "Use", onSelect: use }]} />
```

### Props

| Prop | Type | Default | Description |
|---|---|---|---|
| item | `GameItem` | – | The item to show. |
| compareTo | `GameItem \| null` | – | Shows a delta for each stat with a numeric amount; better: "lower" flips the colour. |
| actions | `{ id, label, onSelect?, variant?, disabled?, shortcut? }[]` | – | Buttons in the footer. |
| variant | `"card" \| "compact" \| "panel"` | `"card"` | Card is 340px wide, compact suits tooltips, panel fills its container. |
| formatValue | `(value, item) => ReactNode` | `"{value} coins"` | How the value prints; return null to hide it. |
| renderStats / children | `fn / ReactNode` | – | Replace the stats list, or add lore, set bonuses or requirements under it. |

### Example

```tsx
import { ItemInfo } from "@/components/ui/item-info";
import { ITEMS } from "./game-data";

export default function ItemInfoDemo() {
  return (
    <div className="flex w-full flex-wrap items-stretch justify-center gap-4">
      <ItemInfo
        item={ITEMS.hat2}
        compareTo={ITEMS.hat}
        formatValue={(v) => `${v} coins`}
        actions={[
          { id: "equip", label: "Equip", variant: "reward", shortcut: "E" },
          { id: "sell", label: "Sell", variant: "secondary" },
          { id: "drop", label: "Drop", variant: "ghost" },
        ]}
      >
        <p className="m-0 text-xs text-text-muted">Compared with your Straw hat.</p>
      </ItemInfo>
      <ItemInfo item={ITEMS.gem} variant="compact" formatValue={(v) => `${v} coins`} />
    </div>
  );
}
```

### Source (components/ui/item-info.tsx)

```tsx
import * as React from "react";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import { GameIcon, RarityBadge, rarityStyle, type GameItem, type ItemStat, type RarityConfig } from "./game-core";

export type ItemAction = {
  id: string;
  label: React.ReactNode;
  onSelect?: (item: GameItem) => void;
  variant?: "default" | "secondary" | "success" | "reward" | "outline" | "ghost" | "destructive";
  disabled?: boolean;
  /** Keyboard hint shown on the button, e.g. "E". */
  shortcut?: string;
};

type ItemInfoProps = Omit<React.ComponentProps<"div">, "children"> & {
  item: GameItem;
  /** Show +/- deltas against this item (e.g. the one currently equipped). */
  compareTo?: GameItem | null;
  actions?: ItemAction[];
  /** "card" (default), "compact" (tooltips) or "panel" (fills its container). */
  variant?: "card" | "compact" | "panel";
  rarities?: Record<string, RarityConfig>;
  /** Formats item.value; default "{value} coins". Return null to hide. */
  formatValue?: (value: number, item: GameItem) => React.ReactNode;
  /** Extra content under the stats (lore, set bonus, requirements). */
  children?: React.ReactNode;
  /** Replace the stats list. */
  renderStats?: (item: GameItem, compareTo?: GameItem | null) => React.ReactNode;
};

function delta(stat: ItemStat, compareTo?: GameItem | null) {
  if (!compareTo || stat.amount == null) return null;
  const other = compareTo.stats?.find((s) => String(s.label) === String(stat.label))?.amount ?? 0;
  const d = stat.amount - other;
  if (d === 0) return null;
  const good = stat.better === "lower" ? d < 0 : d > 0;
  return <span className="cz-iteminfo__delta" data-good={good || undefined}>{d > 0 ? `+${d}` : d}</span>;
}

/** The details of one item: name, rarity, type, description, stats with
 *  optional compare deltas, value and action buttons. */
function ItemInfo({ item, compareTo, actions, variant = "card", rarities, formatValue, renderStats, children, className, style, ...props }: ItemInfoProps) {
  const value = item.value != null ? (formatValue ? formatValue(item.value, item) : `${item.value} coins`) : null;
  return (
    <div data-slot="item-info" data-variant={variant} data-rarity={item.rarity} className={cn("cz-iteminfo", className)} style={{ ...rarityStyle(item.rarity, rarities), ...style }} {...props}>
      <div className="cz-iteminfo__head">
        {item.icon != null && <span className="cz-iteminfo__icon"><GameIcon icon={item.icon} alt="" /></span>}
        <div className="cz-iteminfo__titles">
          <div className="cz-iteminfo__name">{item.name}{item.quantity != null && item.quantity > 1 && <span className="cz-iteminfo__qty"> ×{item.quantity}</span>}</div>
          <div className="cz-iteminfo__meta">
            <RarityBadge rarity={item.rarity} rarities={rarities} />
            {item.type != null && <span>{item.type}</span>}
            {item.level != null && <span>Lv {item.level}</span>}
          </div>
        </div>
      </div>
      {item.description != null && <div className="cz-iteminfo__desc">{item.description}</div>}
      {renderStats ? renderStats(item, compareTo) : item.stats?.length ? (
        <dl className="cz-iteminfo__stats">
          {item.stats.map((s, i) => (
            <div key={i} className="cz-iteminfo__stat"><dt>{s.label}</dt><dd>{s.value}{delta(s, compareTo)}</dd></div>
          ))}
        </dl>
      ) : null}
      {children}
      {(value != null || actions?.length) && (
        <div className="cz-iteminfo__foot">
          {value != null && <span className="cz-iteminfo__value">{value}</span>}
          {actions?.length ? (
            <div className="cz-iteminfo__actions">
              {actions.map((a) => (
                <Button key={a.id} size="sm" variant={a.variant ?? "secondary"} disabled={a.disabled || item.disabled} onClick={() => a.onSelect?.(item)} aria-keyshortcuts={a.shortcut}>
                  {a.label}{a.shortcut && <kbd className="cz-iteminfo__kbd">{a.shortcut}</kbd>}
                </Button>
              ))}
            </div>
          ) : null}
        </div>
      )}
    </div>
  );
}

type ItemTooltipProps = Omit<ItemInfoProps, "variant" | "actions"> & {
  children: React.ReactElement;
  side?: "top" | "right" | "bottom" | "left";
  /** Pass false to render the trigger without a tooltip. */
  enabled?: boolean;
};

/** Wraps a trigger (usually an ItemSlot) and shows compact ItemInfo on hover
 *  and keyboard focus. Needs a TooltipProvider above it. */
function ItemTooltip({ children, side = "right", enabled = true, ...info }: ItemTooltipProps) {
  if (!enabled) return children;
  return (
    <Tooltip>
      <TooltipTrigger asChild>{children}</TooltipTrigger>
      <TooltipContent side={side} className="cz-iteminfo-tip">
        <ItemInfo variant="compact" {...info} />
      </TooltipContent>
    </Tooltip>
  );
}

export { ItemInfo, ItemTooltip };
export type { ItemInfoProps, ItemTooltipProps };
```

npm: `import { ItemInfo, ItemTooltip } from "@0xtrou/cozy-ui"` (after `npm install @0xtrou/cozy-ui` and `import "@0xtrou/cozy-ui/styles.css"`).
