codeburn/app/renderer/components/ChartTip.tsx
Resham Joshi 16c41df8eb
Compare periods: lead sentence, three tiles, day bars, top movers (#1446)
* Add per-day cost for both sides to the compare-periods report

* Rebuild the Compare periods screen around a lead sentence, day bars and top movers

* Update the Compare periods doc for the new layout

* Extract the duplicated chart tooltip into one ChartTip component

* Make the Compare periods sentence, tiles, day chart and movers table say what they measure

* Cover the compare lead sentence rules and the percent, date and path formats

* Update the Compare periods doc for the new day chart

* Style the Compare periods Swap button as an outlined pill instead of a solid grey block

* Fix the round-2 findings on the Compare periods chart, range label, Swap button and duration

* Fix single-day range label doubling and lone day bar width in Compare periods

rangeLabel now short-circuits from === to like terseRangeLabel already did.
pcmp-bar uses flex 0 1 50% so a lone bar in a slot takes half the slot
instead of the full slot, matching paired bars.

* Keep a lone day bar the same width as a paired one
2026-09-15 23:12:12 +02:00

36 lines
1.4 KiB
TypeScript

import { useLayoutEffect, useRef, useState, type ReactNode } from 'react'
import { createPortal } from 'react-dom'
/// The app's cursor-following chart tooltip. It renders into `document.body`
/// so no chart has to fight its own overflow, and it clamps to the viewport
/// so a bar at the right or top edge still shows its whole tip.
export function ChartTip({ x, y, children }: { x: number; y: number; children: ReactNode }) {
const tipRef = useRef<HTMLDivElement>(null)
const [position, setPosition] = useState<{ left: number; top: number } | null>(null)
useLayoutEffect(() => {
const width = tipRef.current?.offsetWidth ?? 220
const height = tipRef.current?.offsetHeight ?? 62
const gutter = 8
const cursorGap = 12
let left = x + cursorGap
if (left + width > window.innerWidth - gutter) left = x - width - cursorGap
left = Math.max(gutter, Math.min(left, window.innerWidth - width - gutter))
let top = y - height - cursorGap
if (top < gutter) top = y + cursorGap
top = Math.max(gutter, Math.min(top, window.innerHeight - height - gutter))
setPosition({ left, top })
}, [x, y])
return createPortal(
<div
ref={tipRef}
className={`chart-tip${position ? ' on' : ''}`}
style={{ position: 'fixed', ...(position ?? { left: 0, top: 0 }) }}
role="tooltip"
>
{children}
</div>,
document.body,
)
}