feat: add item tooltips to match history

This commit is contained in:
dahyeon committed 2026-06-26 23:07:06 -07:00
1 parent 38c2d1f1b4
commit 68afaf0cb8
8 files changed
+477 -49

No files matched your search

+204
View File
@@ -0,0 +1,204 @@
import type { CSSProperties, RefObject } from "react";
import { useEffect, useId, useRef, useState } from "react";
import { createPortal } from "react-dom";
import type { DdragonManifest } from "../api/types";
import {
itemDetails,
itemDisplayName,
itemIconUrl,
safeDdragonDescriptionHtml,
} from "../lib/ddragon";
type TooltipPosition = {
left: number;
top: number;
width: number;
placement: "top" | "bottom";
};
type ItemIconProps = {
itemId: number;
manifest: DdragonManifest | null | undefined;
className?: string;
imageClassName?: string;
trinket?: boolean;
};
const VIEWPORT_MARGIN = 8;
const TOOLTIP_GAP = 8;
const TOOLTIP_WIDTH = 320;
export function ItemIcon({
itemId,
manifest,
className = "h-6 w-6",
imageClassName = "scale-[1.18]",
trinket = false,
}: ItemIconProps) {
const anchorRef = useRef<HTMLSpanElement>(null);
const tooltipId = useId();
const [open, setOpen] = useState(false);
const icon = itemIconUrl(manifest, itemId);
const hasItem = itemId > 0;
const itemName = itemDisplayName(manifest, itemId) ?? `아이템 ${itemId}`;
const slotTone = trinket
? "border-primary/25 bg-base-content/[0.04]"
: "border-base-content/5 bg-base-content/8";
return (
<span
ref={anchorRef}
className={`relative inline-flex shrink-0 ${className}`}
role={hasItem ? "img" : undefined}
tabIndex={hasItem ? 0 : undefined}
aria-label={hasItem ? itemName : "빈 아이템 슬롯"}
aria-describedby={hasItem && open ? tooltipId : undefined}
onBlur={() => setOpen(false)}
onFocus={() => hasItem && setOpen(true)}
onMouseEnter={() => hasItem && setOpen(true)}
onMouseLeave={() => setOpen(false)}
>
<span
className={`block h-full w-full overflow-hidden border ${slotTone}`}
aria-hidden="true"
>
{icon ? (
<img
src={icon}
alt=""
className={`h-full w-full object-cover ${imageClassName}`}
loading="lazy"
/>
) : null}
</span>
{hasItem && open ? (
<ItemTooltipPortal
id={tooltipId}
anchorRef={anchorRef}
itemId={itemId}
manifest={manifest}
/>
) : null}
</span>
);
}
function ItemTooltipPortal({
id,
anchorRef,
itemId,
manifest,
}: {
id: string;
anchorRef: RefObject<HTMLElement | null>;
itemId: number;
manifest: DdragonManifest | null | undefined;
}) {
const [position, setPosition] = useState<TooltipPosition | null>(null);
useEffect(() => {
function updatePosition() {
const anchor = anchorRef.current;
if (!anchor) return;
const rect = anchor.getBoundingClientRect();
const width = Math.min(
TOOLTIP_WIDTH,
window.innerWidth - VIEWPORT_MARGIN * 2,
);
const left = clamp(
rect.left + rect.width / 2 - width / 2,
VIEWPORT_MARGIN,
window.innerWidth - width - VIEWPORT_MARGIN,
);
const placement = rect.top > 260 ? "top" : "bottom";
setPosition({
left,
top:
placement === "top"
? rect.top - TOOLTIP_GAP
: rect.bottom + TOOLTIP_GAP,
width,
placement,
});
}
updatePosition();
window.addEventListener("resize", updatePosition);
window.addEventListener("scroll", updatePosition, true);
return () => {
window.removeEventListener("resize", updatePosition);
window.removeEventListener("scroll", updatePosition, true);
};
}, [anchorRef]);
if (typeof document === "undefined" || !position) return null;
const style: CSSProperties = {
left: position.left,
top: position.top,
width: position.width,
transform:
position.placement === "top" ? "translateY(-100%)" : undefined,
};
return createPortal(
<div
id={id}
role="tooltip"
style={style}
className="pointer-events-none fixed z-[1000] border border-base-content/20 bg-black px-3 py-3 text-xs leading-relaxed text-white shadow-2xl"
>
<ItemTooltipContent itemId={itemId} manifest={manifest} />
</div>,
document.body,
);
}
function ItemTooltipContent({
itemId,
manifest,
}: {
itemId: number;
manifest: DdragonManifest | null | undefined;
}) {
const item = itemDetails(manifest, itemId);
const descriptionHtml = item?.description
? safeDdragonDescriptionHtml(item.description)
: "";
return (
<div className="space-y-2">
<div className="font-black text-[#f0d878]">
{item?.name ?? `아이템 ${itemId}`}
</div>
{item?.plaintext ? (
<div className="font-bold text-white">{item.plaintext}</div>
) : null}
{descriptionHtml ? (
<div
className="space-y-1 text-white/80 [&_br]:block [&_br]:content-['']"
dangerouslySetInnerHTML={{ __html: descriptionHtml }}
/>
) : (
<div className="text-white/55">
아이템 상세 정보를 불러오는 중입니다.
</div>
)}
{item?.gold.total ? (
<div className="border-t border-white/10 pt-2 font-bold text-white">
가격:
<span className="text-[#f0d878]">{item.gold.total}</span>{" "}
<span className="text-[#f0d878]">
({item.gold.base || item.gold.total})
</span>
</div>
) : null}
</div>
);
}
function clamp(value: number, min: number, max: number) {
if (max < min) return min;
return Math.min(Math.max(value, min), max);
}