Fix condition picker clipping out of viewport
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>
This commit is contained in:
@@ -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}
|
||||||
|
|||||||
@@ -472,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);
|
||||||
@@ -567,13 +568,16 @@ export function CombatantRow({
|
|||||||
color={pcColor}
|
color={pcColor}
|
||||||
onToggleStatBlock={onToggleStatBlock}
|
onToggleStatBlock={onToggleStatBlock}
|
||||||
/>
|
/>
|
||||||
<ConditionTags
|
<div ref={conditionAnchorRef}>
|
||||||
conditions={combatant.conditions}
|
<ConditionTags
|
||||||
onRemove={(conditionId) => toggleCondition(id, conditionId)}
|
conditions={combatant.conditions}
|
||||||
onOpenPicker={() => setPickerOpen((prev) => !prev)}
|
onRemove={(conditionId) => toggleCondition(id, conditionId)}
|
||||||
/>
|
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)}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ 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";
|
||||||
|
|
||||||
const ICON_MAP: Record<string, LucideIcon> = {
|
const ICON_MAP: Record<string, LucideIcon> = {
|
||||||
@@ -52,34 +53,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 +105,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];
|
||||||
@@ -129,6 +142,7 @@ export function ConditionPicker({
|
|||||||
</button>
|
</button>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
</div>
|
</div>,
|
||||||
|
document.body,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user