Compare commits

...

3 Commits

Author SHA1 Message Date
Lukas 6336dec38a Add condition tooltips with 5.5e descriptions
CI / check (push) Successful in 1m22s
CI / build-image (push) Successful in 19s
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 22:48:23 +01:00
Lukas 9def2d7c24 Fix condition picker clipping out of viewport
CI / check (push) Successful in 1m17s
CI / build-image (push) Successful in 27s
Render condition picker via React portal with fixed positioning so it
is no longer clipped by the overflow-y-auto combatant list container.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 22:34:15 +01:00
Lukas f729e37689 Replace book icon with name-click stat block toggle and pencil rename
Name click now opens/collapses the stat block panel; a hover-visible
pencil icon next to the name handles renaming. Removes the standalone
book icon for a cleaner, more intuitive combatant row.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 22:29:56 +01:00
6 changed files with 255 additions and 90 deletions
@@ -4,6 +4,7 @@ import "@testing-library/jest-dom/vitest";
import { CONDITION_DEFINITIONS, type ConditionId } from "@initiative/domain"; import { CONDITION_DEFINITIONS, type ConditionId } from "@initiative/domain";
import { cleanup, render, screen } from "@testing-library/react"; import { cleanup, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event"; import userEvent from "@testing-library/user-event";
import { createRef, type RefObject } from "react";
import { afterEach, describe, expect, it, vi } from "vitest"; import { afterEach, describe, expect, it, vi } from "vitest";
import { ConditionPicker } from "../condition-picker"; import { ConditionPicker } from "../condition-picker";
@@ -18,8 +19,13 @@ function renderPicker(
) { ) {
const onToggle = overrides.onToggle ?? vi.fn(); const onToggle = overrides.onToggle ?? vi.fn();
const onClose = overrides.onClose ?? vi.fn(); const onClose = overrides.onClose ?? vi.fn();
const anchorRef = createRef<HTMLElement>() as RefObject<HTMLElement>;
const anchor = document.createElement("div");
document.body.appendChild(anchor);
(anchorRef as { current: HTMLElement }).current = anchor;
const result = render( const result = render(
<ConditionPicker <ConditionPicker
anchorRef={anchorRef}
activeConditions={overrides.activeConditions ?? []} activeConditions={overrides.activeConditions ?? []}
onToggle={onToggle} onToggle={onToggle}
onClose={onClose} onClose={onClose}
+38 -17
View File
@@ -6,7 +6,7 @@ import {
type PlayerIcon, type PlayerIcon,
type RollMode, type RollMode,
} from "@initiative/domain"; } from "@initiative/domain";
import { Book, BookOpen, Brain, X } from "lucide-react"; import { Brain, Pencil, X } from "lucide-react";
import { type Ref, useCallback, useEffect, useRef, useState } from "react"; import { type Ref, useCallback, useEffect, useRef, useState } from "react";
import { useEncounterContext } from "../contexts/encounter-context.js"; import { useEncounterContext } from "../contexts/encounter-context.js";
import { useInitiativeRollsContext } from "../contexts/initiative-rolls-context.js"; import { useInitiativeRollsContext } from "../contexts/initiative-rolls-context.js";
@@ -47,11 +47,13 @@ function EditableName({
combatantId, combatantId,
onRename, onRename,
color, color,
onToggleStatBlock,
}: Readonly<{ }: Readonly<{
name: string; name: string;
combatantId: CombatantId; combatantId: CombatantId;
onRename: (id: CombatantId, newName: string) => void; onRename: (id: CombatantId, newName: string) => void;
color?: string; color?: string;
onToggleStatBlock?: () => void;
}>) { }>) {
const [editing, setEditing] = useState(false); const [editing, setEditing] = useState(false);
const [draft, setDraft] = useState(name); const [draft, setDraft] = useState(name);
@@ -89,14 +91,31 @@ function EditableName({
} }
return ( return (
<>
<button <button
type="button" type="button"
onClick={startEditing} onClick={onToggleStatBlock}
className="cursor-text truncate text-left text-foreground text-sm transition-colors hover:text-hover-neutral" disabled={!onToggleStatBlock}
className={cn(
"truncate text-left text-sm transition-colors",
onToggleStatBlock
? "cursor-pointer text-foreground hover:text-hover-neutral"
: "cursor-default text-foreground",
)}
style={color ? { color } : undefined} style={color ? { color } : undefined}
> >
{name} {name}
</button> </button>
<button
type="button"
onClick={startEditing}
title="Rename"
aria-label="Rename"
className="inline-flex shrink-0 items-center rounded p-0.5 text-muted-foreground opacity-0 pointer-coarse:opacity-100 transition-colors transition-opacity hover:bg-hover-neutral-bg hover:text-hover-neutral focus:opacity-100 group-hover:opacity-100"
>
<Pencil size={14} />
</button>
</>
); );
} }
@@ -428,14 +447,22 @@ export function CombatantRow({
toggleCondition, toggleCondition,
toggleConcentration, toggleConcentration,
} = useEncounterContext(); } = useEncounterContext();
const { selectedCreatureId, showCreature } = useSidePanelContext(); const { selectedCreatureId, showCreature, toggleCollapse } =
useSidePanelContext();
const { handleRollInitiative } = useInitiativeRollsContext(); const { handleRollInitiative } = useInitiativeRollsContext();
// Derive what was previously conditional props // Derive what was previously conditional props
const isStatBlockOpen = combatant.creatureId === selectedCreatureId; const isStatBlockOpen = combatant.creatureId === selectedCreatureId;
const { creatureId } = combatant; const { creatureId } = combatant;
const onShowStatBlock = creatureId const hasStatBlock = !!creatureId;
? () => showCreature(creatureId) const onToggleStatBlock = hasStatBlock
? () => {
if (isStatBlockOpen) {
toggleCollapse();
} else {
showCreature(creatureId);
}
}
: undefined; : undefined;
const onRollInitiative = combatant.creatureId const onRollInitiative = combatant.creatureId
? handleRollInitiative ? handleRollInitiative
@@ -445,6 +472,7 @@ export function CombatantRow({
const status = deriveHpStatus(currentHp, maxHp); const status = deriveHpStatus(currentHp, maxHp);
const dimmed = status === "unconscious"; const dimmed = status === "unconscious";
const [pickerOpen, setPickerOpen] = useState(false); const [pickerOpen, setPickerOpen] = useState(false);
const conditionAnchorRef = useRef<HTMLDivElement>(null);
const prevHpRef = useRef(currentHp); const prevHpRef = useRef(currentHp);
const [isPulsing, setIsPulsing] = useState(false); const [isPulsing, setIsPulsing] = useState(false);
@@ -517,17 +545,6 @@ export function CombatantRow({
dimmed && "opacity-50", dimmed && "opacity-50",
)} )}
> >
{!!onShowStatBlock && (
<button
type="button"
onClick={onShowStatBlock}
title="View stat block"
aria-label="View stat block"
className="shrink-0 text-foreground transition-colors hover:text-hover-neutral"
>
{isStatBlockOpen ? <BookOpen size={16} /> : <Book size={16} />}
</button>
)}
{!!combatant.icon && {!!combatant.icon &&
!!combatant.color && !!combatant.color &&
(() => { (() => {
@@ -549,14 +566,18 @@ export function CombatantRow({
combatantId={id} combatantId={id}
onRename={editCombatant} onRename={editCombatant}
color={pcColor} color={pcColor}
onToggleStatBlock={onToggleStatBlock}
/> />
<div ref={conditionAnchorRef}>
<ConditionTags <ConditionTags
conditions={combatant.conditions} conditions={combatant.conditions}
onRemove={(conditionId) => toggleCondition(id, conditionId)} onRemove={(conditionId) => toggleCondition(id, conditionId)}
onOpenPicker={() => setPickerOpen((prev) => !prev)} onOpenPicker={() => setPickerOpen((prev) => !prev)}
/> />
</div>
{!!pickerOpen && ( {!!pickerOpen && (
<ConditionPicker <ConditionPicker
anchorRef={conditionAnchorRef}
activeConditions={combatant.conditions} activeConditions={combatant.conditions}
onToggle={(conditionId) => toggleCondition(id, conditionId)} onToggle={(conditionId) => toggleCondition(id, conditionId)}
onClose={() => setPickerOpen(false)} onClose={() => setPickerOpen(false)}
+41 -23
View File
@@ -18,7 +18,9 @@ import {
ZapOff, ZapOff,
} from "lucide-react"; } from "lucide-react";
import { useEffect, useLayoutEffect, useRef, useState } from "react"; import { useEffect, useLayoutEffect, useRef, useState } from "react";
import { createPortal } from "react-dom";
import { cn } from "../lib/utils"; import { cn } from "../lib/utils";
import { Tooltip } from "./ui/tooltip.js";
const ICON_MAP: Record<string, LucideIcon> = { const ICON_MAP: Record<string, LucideIcon> = {
EyeOff, EyeOff,
@@ -52,34 +54,45 @@ const COLOR_CLASSES: Record<string, string> = {
}; };
interface ConditionPickerProps { interface ConditionPickerProps {
anchorRef: React.RefObject<HTMLElement | null>;
activeConditions: readonly ConditionId[] | undefined; activeConditions: readonly ConditionId[] | undefined;
onToggle: (conditionId: ConditionId) => void; onToggle: (conditionId: ConditionId) => void;
onClose: () => void; onClose: () => void;
} }
export function ConditionPicker({ export function ConditionPicker({
anchorRef,
activeConditions, activeConditions,
onToggle, onToggle,
onClose, onClose,
}: Readonly<ConditionPickerProps>) { }: Readonly<ConditionPickerProps>) {
const ref = useRef<HTMLDivElement>(null); const ref = useRef<HTMLDivElement>(null);
const [flipped, setFlipped] = useState(false); const [pos, setPos] = useState<{
const [maxHeight, setMaxHeight] = useState<number | undefined>(undefined); top: number;
left: number;
maxHeight: number;
} | null>(null);
useLayoutEffect(() => { useLayoutEffect(() => {
const anchor = anchorRef.current;
const el = ref.current; const el = ref.current;
if (!el) return; if (!anchor || !el) return;
const rect = el.getBoundingClientRect();
const spaceBelow = window.innerHeight - rect.top; const anchorRect = anchor.getBoundingClientRect();
const spaceAbove = rect.bottom; const menuHeight = el.scrollHeight;
const shouldFlip = const pad = 8;
rect.bottom > window.innerHeight && spaceAbove > spaceBelow;
setFlipped(shouldFlip); const spaceBelow = window.innerHeight - anchorRect.bottom - pad;
const available = shouldFlip ? spaceAbove : spaceBelow; const spaceAbove = anchorRect.top - pad;
if (rect.height > available) { const openBelow = spaceBelow >= menuHeight || spaceBelow >= spaceAbove;
setMaxHeight(available - 16);
} const top = openBelow
}, []); ? anchorRect.bottom + 4
: Math.max(pad, anchorRect.top - Math.min(menuHeight, spaceAbove) - 4);
const maxHeight = openBelow ? spaceBelow : Math.min(menuHeight, spaceAbove);
setPos({ top, left: anchorRect.left, maxHeight });
}, [anchorRef]);
useEffect(() => { useEffect(() => {
function handleClickOutside(e: MouseEvent) { function handleClickOutside(e: MouseEvent) {
@@ -93,14 +106,15 @@ export function ConditionPicker({
const active = new Set(activeConditions ?? []); const active = new Set(activeConditions ?? []);
return ( return createPortal(
<div <div
ref={ref} ref={ref}
className={cn( className="card-glow fixed z-50 w-fit overflow-y-auto rounded-lg border border-border bg-background p-1"
"card-glow absolute left-0 z-10 w-fit overflow-y-auto rounded-lg border border-border bg-background p-1", style={
flipped ? "bottom-full mb-1" : "top-full mt-1", pos
)} ? { top: pos.top, left: pos.left, maxHeight: pos.maxHeight }
style={maxHeight ? { maxHeight } : undefined} : { visibility: "hidden" as const }
}
> >
{CONDITION_DEFINITIONS.map((def) => { {CONDITION_DEFINITIONS.map((def) => {
const Icon = ICON_MAP[def.iconName]; const Icon = ICON_MAP[def.iconName];
@@ -108,8 +122,8 @@ export function ConditionPicker({
const isActive = active.has(def.id); const isActive = active.has(def.id);
const colorClass = COLOR_CLASSES[def.color] ?? "text-muted-foreground"; const colorClass = COLOR_CLASSES[def.color] ?? "text-muted-foreground";
return ( return (
<Tooltip key={def.id} content={def.description} className="block">
<button <button
key={def.id}
type="button" type="button"
className={cn( className={cn(
"flex w-full items-center gap-2 rounded px-2 py-1 text-sm transition-colors hover:bg-hover-neutral-bg", "flex w-full items-center gap-2 rounded px-2 py-1 text-sm transition-colors hover:bg-hover-neutral-bg",
@@ -122,13 +136,17 @@ export function ConditionPicker({
className={isActive ? colorClass : "text-muted-foreground"} className={isActive ? colorClass : "text-muted-foreground"}
/> />
<span <span
className={isActive ? "text-foreground" : "text-muted-foreground"} className={
isActive ? "text-foreground" : "text-muted-foreground"
}
> >
{def.label} {def.label}
</span> </span>
</button> </button>
</Tooltip>
); );
})} })}
</div> </div>,
document.body,
); );
} }
+3 -2
View File
@@ -19,6 +19,7 @@ import {
ZapOff, ZapOff,
} from "lucide-react"; } from "lucide-react";
import { cn } from "../lib/utils.js"; import { cn } from "../lib/utils.js";
import { Tooltip } from "./ui/tooltip.js";
const ICON_MAP: Record<string, LucideIcon> = { const ICON_MAP: Record<string, LucideIcon> = {
EyeOff, EyeOff,
@@ -71,10 +72,9 @@ export function ConditionTags({
if (!Icon) return null; if (!Icon) return null;
const colorClass = COLOR_CLASSES[def.color] ?? "text-muted-foreground"; const colorClass = COLOR_CLASSES[def.color] ?? "text-muted-foreground";
return ( return (
<Tooltip key={condId} content={`${def.label}: ${def.description}`}>
<button <button
key={condId}
type="button" type="button"
title={def.label}
aria-label={`Remove ${def.label}`} aria-label={`Remove ${def.label}`}
className={cn( className={cn(
"inline-flex items-center rounded p-0.5 transition-colors hover:bg-hover-neutral-bg", "inline-flex items-center rounded p-0.5 transition-colors hover:bg-hover-neutral-bg",
@@ -87,6 +87,7 @@ export function ConditionTags({
> >
<Icon size={14} /> <Icon size={14} />
</button> </button>
</Tooltip>
); );
})} })}
<button <button
+55
View File
@@ -0,0 +1,55 @@
import { type ReactNode, useRef, useState } from "react";
import { createPortal } from "react-dom";
interface TooltipProps {
content: string;
children: ReactNode;
className?: string;
}
export function Tooltip({
content,
children,
className,
}: Readonly<TooltipProps>) {
const ref = useRef<HTMLSpanElement>(null);
const [pos, setPos] = useState<{ top: number; left: number } | null>(null);
function show() {
const el = ref.current;
if (!el) return;
const rect = el.getBoundingClientRect();
setPos({
top: rect.top - 4,
left: rect.left + rect.width / 2,
});
}
function hide() {
setPos(null);
}
return (
<>
<span
ref={ref}
onPointerEnter={show}
onPointerLeave={hide}
className={className ?? "inline-flex"}
>
{children}
</span>
{pos !== null &&
createPortal(
<div
role="tooltip"
className="pointer-events-none fixed z-[60] max-w-64 -translate-x-1/2 -translate-y-full rounded-md border border-border bg-background px-2.5 py-1.5 text-foreground text-xs leading-snug shadow-lg"
style={{ top: pos.top, left: pos.left }}
>
{content}
</div>,
document.body,
)}
</>
);
}
+71 -7
View File
@@ -18,63 +18,127 @@ export type ConditionId =
export interface ConditionDefinition { export interface ConditionDefinition {
readonly id: ConditionId; readonly id: ConditionId;
readonly label: string; readonly label: string;
readonly description: string;
readonly iconName: string; readonly iconName: string;
readonly color: string; readonly color: string;
} }
export const CONDITION_DEFINITIONS: readonly ConditionDefinition[] = [ export const CONDITION_DEFINITIONS: readonly ConditionDefinition[] = [
{ id: "blinded", label: "Blinded", iconName: "EyeOff", color: "neutral" }, {
{ id: "charmed", label: "Charmed", iconName: "Heart", color: "pink" }, id: "blinded",
{ id: "deafened", label: "Deafened", iconName: "EarOff", color: "neutral" }, label: "Blinded",
description:
"Can't see. Auto-fail sight checks. Attacks have Disadvantage; attacks against have Advantage.",
iconName: "EyeOff",
color: "neutral",
},
{
id: "charmed",
label: "Charmed",
description:
"Can't attack or target the charmer with harmful abilities. Charmer has Advantage on social checks.",
iconName: "Heart",
color: "pink",
},
{
id: "deafened",
label: "Deafened",
description: "Can't hear. Auto-fail hearing checks.",
iconName: "EarOff",
color: "neutral",
},
{ {
id: "exhaustion", id: "exhaustion",
label: "Exhaustion", label: "Exhaustion",
description:
"Subtract exhaustion level from D20 Tests and Spell save DCs. Speed reduced by 5 ft. \u00D7 level. Removed by long rest (1 level) or death at 10 levels.",
iconName: "BatteryLow", iconName: "BatteryLow",
color: "amber", color: "amber",
}, },
{ {
id: "frightened", id: "frightened",
label: "Frightened", label: "Frightened",
description:
"Disadvantage on ability checks and attacks while source of fear is in line of sight. Can't willingly move closer to the source.",
iconName: "Siren", iconName: "Siren",
color: "orange", color: "orange",
}, },
{ id: "grappled", label: "Grappled", iconName: "Hand", color: "neutral" }, {
id: "grappled",
label: "Grappled",
description:
"Speed is 0 and can't benefit from bonuses to speed. Ends if grappler is Incapacitated or moved out of reach.",
iconName: "Hand",
color: "neutral",
},
{ {
id: "incapacitated", id: "incapacitated",
label: "Incapacitated", label: "Incapacitated",
description:
"Can't take Actions, Bonus Actions, or Reactions. Concentration is broken.",
iconName: "Ban", iconName: "Ban",
color: "gray", color: "gray",
}, },
{ {
id: "invisible", id: "invisible",
label: "Invisible", label: "Invisible",
description:
"Impossible to see without magic or special sense. Heavily Obscured. Attacks have Advantage; attacks against have Disadvantage.",
iconName: "Ghost", iconName: "Ghost",
color: "violet", color: "violet",
}, },
{ {
id: "paralyzed", id: "paralyzed",
label: "Paralyzed", label: "Paralyzed",
description:
"Incapacitated. Can't move or speak. Auto-fail Str/Dex saves. Attacks against have Advantage. Hits within 5 ft. are critical.",
iconName: "ZapOff", iconName: "ZapOff",
color: "yellow", color: "yellow",
}, },
{ {
id: "petrified", id: "petrified",
label: "Petrified", label: "Petrified",
description:
"Turned to stone. Weight \u00D710. Incapacitated. Can't move or speak. Attacks against have Advantage. Auto-fail Str/Dex saves. Resistant to all damage. Immune to poison and disease.",
iconName: "Gem", iconName: "Gem",
color: "slate", color: "slate",
}, },
{ id: "poisoned", label: "Poisoned", iconName: "Droplet", color: "green" }, {
{ id: "prone", label: "Prone", iconName: "ArrowDown", color: "neutral" }, id: "poisoned",
label: "Poisoned",
description: "Disadvantage on attack rolls and ability checks.",
iconName: "Droplet",
color: "green",
},
{
id: "prone",
label: "Prone",
description:
"Can only crawl (costs extra movement). Disadvantage on attacks. Attacks within 5 ft. have Advantage; ranged attacks have Disadvantage. Standing up costs half movement.",
iconName: "ArrowDown",
color: "neutral",
},
{ {
id: "restrained", id: "restrained",
label: "Restrained", label: "Restrained",
description:
"Speed is 0. Attacks have Disadvantage. Attacks against have Advantage. Disadvantage on Dex saves.",
iconName: "Link", iconName: "Link",
color: "neutral", color: "neutral",
}, },
{ id: "stunned", label: "Stunned", iconName: "Sparkles", color: "yellow" }, {
id: "stunned",
label: "Stunned",
description:
"Incapacitated. Can't move. Can speak only falteringly. Auto-fail Str/Dex saves. Attacks against have Advantage.",
iconName: "Sparkles",
color: "yellow",
},
{ {
id: "unconscious", id: "unconscious",
label: "Unconscious", label: "Unconscious",
description:
"Incapacitated. Can't move or speak. Unaware of surroundings. Drops held items, falls Prone. Auto-fail Str/Dex saves. Attacks against have Advantage. Hits within 5 ft. are critical.",
iconName: "Moon", iconName: "Moon",
color: "indigo", color: "indigo",
}, },