87 lines
2.9 KiB
TypeScript
87 lines
2.9 KiB
TypeScript
import * as React from "react"
|
||
|
||
import { cn } from "../lib/utils"
|
||
|
||
export { useElementWidth } from "../hooks/use-element-width"
|
||
|
||
export interface HoverState<T> {
|
||
readonly item: T
|
||
/** Якорь подсказки относительно контейнера графика. */
|
||
readonly x: number
|
||
readonly y: number
|
||
}
|
||
|
||
/** Одна подсказка на весь график: координаты считаются от контейнера с `relative`. */
|
||
export function useChartHover<T>() {
|
||
const [hover, setHover] = React.useState<HoverState<T> | null>(null)
|
||
const bind = React.useCallback((item: T) => {
|
||
const move = (e: React.MouseEvent) => {
|
||
const box = (e.currentTarget as Element).closest("[data-chart-root]")?.getBoundingClientRect()
|
||
if (box) setHover({ item, x: e.clientX - box.left, y: e.clientY - box.top })
|
||
}
|
||
return { onMouseEnter: move, onMouseMove: move, onMouseLeave: () => setHover(null) }
|
||
}, [])
|
||
return { hover, bind, clear: () => setHover(null) }
|
||
}
|
||
|
||
/** Плашка подсказки: справа от курсора, у правого края — слева. */
|
||
export function ChartTooltip({
|
||
x,
|
||
y,
|
||
width,
|
||
className,
|
||
children,
|
||
}: {
|
||
x: number
|
||
y: number
|
||
width: number
|
||
className?: string
|
||
children: React.ReactNode
|
||
}) {
|
||
const flip = x > width * 0.6
|
||
return (
|
||
<div
|
||
role="tooltip"
|
||
className={cn(
|
||
"pointer-events-none absolute z-20 min-w-40 rounded-lg border bg-popover px-3 py-2 text-xs text-popover-foreground shadow-md",
|
||
className,
|
||
)}
|
||
style={{ left: flip ? undefined : x + 14, right: flip ? width - x + 14 : undefined, top: Math.max(0, y - 12) }}
|
||
>
|
||
{children}
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** Строка подсказки: цветная метка, подпись, значение справа. */
|
||
export function TooltipRow({ color, label, value }: { color?: string; label: React.ReactNode; value: React.ReactNode }) {
|
||
return (
|
||
<div className="flex items-center gap-2 py-0.5">
|
||
{color && <span className="size-2 shrink-0 rounded-[3px]" style={{ background: color }} />}
|
||
<span className="text-muted-foreground">{label}</span>
|
||
<span className="ml-auto pl-3 font-medium tabular">{value}</span>
|
||
</div>
|
||
)
|
||
}
|
||
|
||
/** Легенда: всегда при двух и более сериях. */
|
||
export function ChartLegend({
|
||
items,
|
||
className,
|
||
}: {
|
||
items: readonly { label: string; color: string; value?: React.ReactNode }[]
|
||
className?: string
|
||
}) {
|
||
return (
|
||
<ul className={cn("flex flex-wrap items-center gap-x-4 gap-y-1 text-xs text-muted-foreground", className)}>
|
||
{items.map((item) => (
|
||
<li key={item.label} className="flex items-center gap-1.5">
|
||
<span className="size-2.5 rounded-[3px]" style={{ background: item.color }} />
|
||
{item.label}
|
||
{item.value !== undefined && <span className="font-medium text-foreground tabular">{item.value}</span>}
|
||
</li>
|
||
))}
|
||
</ul>
|
||
)
|
||
}
|