# Character Info

A character sheet: portrait with level, name and title, XP, bars for health, energy or mood, status chips, attributes with gear bonuses and equipment slots. A compact variant fits a HUD or party list.

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

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

### Usage

```tsx
import { CharacterInfo } from "@/components/ui/character-info"

<CharacterInfo character={{ name: "Mai", level: 12, bars: [{ id: "hp", label: "Health", value: 82, max: 100 }] }} />
```

### Props

| Prop | Type | Default | Description |
|---|---|---|---|
| character | `Character` | – | { name, title?, portrait?, level?, xp?, rarity?, bars?, attributes?, status?, equipment?, description? }. |
| variant | `"full" \| "compact"` | `"full"` | Compact shows portrait, name, title, XP and bars only. |
| onEquipmentSelect / selectedEquipment | `(slot) => void / string` | – | Click on an equipment slot, and highlight one (e.g. where a selected item would go). |
| bars[].tone | `"success" \| "info" \| "reward" \| "primary" \| "danger" \| CSS colour` | `"success"` | Bar colour. |
| attributes[].bonus | `number` | – | Shown as +n in green or -n in red next to the value. |
| actions / children / renderPortrait | `ReactNode / fn` | – | Buttons under the card, extra sections, or your own portrait (a sprite, a 3D canvas). |
| slotSize / tooltip / rarities | `number / boolean / Record` | `56 / true` | Equipment tile size, item tooltips and custom rarities. |

### Example

```tsx
import { Button } from "@/components/ui/button";
import { CharacterInfo } from "@/components/ui/character-info";
import { MAI } from "./game-data";

export default function CharacterInfoDemo() {
  return (
    <CharacterInfo
      character={MAI}
      onEquipmentSelect={(slot) => console.log("open", slot.label)}
      actions={<><Button size="sm" variant="reward">Visit farm</Button><Button size="sm" variant="secondary">Send gift</Button></>}
    />
  );
}
```

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

```tsx
import * as React from "react";
import { cn } from "@/lib/utils";
import { TooltipProvider } from "@/components/ui/tooltip";
import { GameIcon, ItemSlot, RarityBadge, StatBar, type GameItem, type Rarity, type RarityConfig } from "./game-core";
import { ItemTooltip } from "./item-info";

export type CharacterBar = { id: string; label: React.ReactNode; value: number; max: number; tone?: string; icon?: React.ReactNode; valueText?: React.ReactNode };
export type CharacterAttribute = { id: string; label: React.ReactNode; value: React.ReactNode; icon?: React.ReactNode; hint?: React.ReactNode; /** e.g. +2 from gear */ bonus?: number };
export type CharacterStatus = { id: string; label: React.ReactNode; tone?: "default" | "primary" | "secondary" | "success" | "info" | "accent" | "destructive"; icon?: React.ReactNode };
export type EquipmentSlot = { id: string; label: string; item?: GameItem | null; /** Faint icon when empty. */ placeholder?: React.ReactNode; locked?: boolean };

export type Character = {
  name: React.ReactNode;
  /** "Rice farmer", "Village chief"… */
  title?: React.ReactNode;
  /** A React node or an image URL. */
  portrait?: React.ReactNode;
  level?: number;
  xp?: { value: number; max: number };
  rarity?: Rarity;
  bars?: CharacterBar[];
  attributes?: CharacterAttribute[];
  status?: CharacterStatus[];
  equipment?: EquipmentSlot[];
  description?: React.ReactNode;
};

type CharacterInfoProps = Omit<React.ComponentProps<"section">, "onSelect"> & {
  character: Character;
  /** "full" (default) shows everything; "compact" is a HUD card with portrait, name, level and bars. */
  variant?: "full" | "compact";
  /** Click or Enter on an equipment slot. */
  onEquipmentSelect?: (slot: EquipmentSlot) => void;
  /** Highlight one equipment slot (e.g. the drop target from an inventory). */
  selectedEquipment?: string | null;
  rarities?: Record<string, RarityConfig>;
  /** Buttons or links under the card. */
  actions?: React.ReactNode;
  /** Equipment tile size in px. Default 56. */
  slotSize?: number;
  /** Show item tooltips on equipment. Default true. */
  tooltip?: boolean;
  /** Replace the portrait area. */
  renderPortrait?: (character: Character) => React.ReactNode;
  /** Extra content at the end (skills, quests, relationships). */
  children?: React.ReactNode;
};

/** A character sheet: portrait, name, title, level and XP, bars (health,
 *  energy, mood), attributes, status chips and equipment slots. */
function CharacterInfo({
  character: c, variant = "full", onEquipmentSelect, selectedEquipment, rarities, actions, slotSize = 56, tooltip = true,
  renderPortrait, children, className, ...props
}: CharacterInfoProps) {
  const compact = variant === "compact";
  const titleId = React.useId();
  return (
    <TooltipProvider delayDuration={250}>
      <section data-slot="character-info" data-variant={variant} aria-labelledby={titleId} className={cn("cz-character", className)} {...props}>
        <div className="cz-character__top">
          <div className="cz-character__portrait" data-rarity={c.rarity}>
            {renderPortrait ? renderPortrait(c) : <GameIcon icon={c.portrait} alt="" />}
            {c.level != null && <span className="cz-character__level" aria-label={`Level ${c.level}`}>{c.level}</span>}
          </div>
          <div className="cz-character__id">
            <h3 id={titleId} className="cz-character__name">{c.name}</h3>
            {(c.title != null || c.rarity) && (
              <div className="cz-character__title">{c.title}<RarityBadge rarity={c.rarity} rarities={rarities} /></div>
            )}
            {c.xp && <StatBar className="cz-character__xp" label="XP" value={c.xp.value} max={c.xp.max} tone="reward" />}
          </div>
        </div>

        {c.bars?.length ? (
          <div className="cz-character__bars">
            {c.bars.map((b) => <StatBar key={b.id} label={b.label} value={b.value} max={b.max} tone={b.tone ?? "success"} icon={b.icon} valueText={b.valueText} />)}
          </div>
        ) : null}

        {!compact && c.status?.length ? (
          <ul className="cz-character__status" aria-label="Status">
            {c.status.map((s) => <li key={s.id} className={cn("cz-badge", s.tone && s.tone !== "default" && `cz-badge--${s.tone}`)}>{s.icon}{s.label}</li>)}
          </ul>
        ) : null}

        {!compact && c.description != null && <div className="cz-character__desc">{c.description}</div>}

        {!compact && c.attributes?.length ? (
          <dl className="cz-character__attrs">
            {c.attributes.map((a) => (
              <div key={a.id} className="cz-character__attr" title={typeof a.hint === "string" ? a.hint : undefined}>
                <dt>{a.icon}{a.label}</dt>
                <dd>{a.value}{a.bonus ? <span className="cz-character__bonus" data-good={a.bonus > 0 || undefined}>{a.bonus > 0 ? `+${a.bonus}` : a.bonus}</span> : null}</dd>
              </div>
            ))}
          </dl>
        ) : null}

        {!compact && c.equipment?.length ? (
          <div className="cz-character__equip" role="group" aria-label="Equipment" style={{ "--slot": `${slotSize}px` } as React.CSSProperties}>
            {c.equipment.map((slot) => {
              const tile = (
                <ItemSlot
                  item={slot.item}
                  locked={slot.locked}
                  placeholder={slot.placeholder}
                  rarities={rarities}
                  selected={selectedEquipment === slot.id}
                  aria-label={`${slot.label}: ${slot.item ? slot.item.name : "empty"}`}
                  onClick={() => onEquipmentSelect?.(slot)}
                />
              );
              return (
                <div key={slot.id} className="cz-character__equip-slot" data-equip-slot={slot.id}>
                  {slot.item && tooltip ? <ItemTooltip item={slot.item} rarities={rarities}>{tile}</ItemTooltip> : tile}
                  <span className="cz-character__equip-label">{slot.label}</span>
                </div>
              );
            })}
          </div>
        ) : null}

        {children}
        {actions != null && <div className="cz-character__actions">{actions}</div>}
      </section>
    </TooltipProvider>
  );
}

export { CharacterInfo };
export type { CharacterInfoProps };
```

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