import { useRef, useState } from 'react'; // Pointer-drag on a bar: the fill tracks the pointer while dragging and // onCommit fires once on release (a plain click is a zero-length drag). // Port of app.js makeDraggableBar. // // When `formatValue` is given, a tooltip bubble follows the pointer on hover // and drag showing the value at that position (item 14) — e.g. a seek time or // a volume percentage. export function Bar(props: { pct: number; // 0..100, authoritative fill when not dragging bufferPct?: number; // 0..100, second fill layer behind the progress slim?: boolean; className?: string; onCommit?: (fraction: number) => void; onDragStart?: () => boolean; // return false to refuse the drag live?: boolean; // live drag feedback: onCommit also fires on move formatValue?: (fraction: number) => string; // tooltip text at a fraction }) { const el = useRef(null); const [dragPct, setDragPct] = useState(null); // Pointer position (0..100) for the tooltip; null when the pointer is away. const [hoverPct, setHoverPct] = useState(null); const fraction = (e: React.PointerEvent) => { const r = el.current!.getBoundingClientRect(); return Math.min(1, Math.max(0, (e.clientX - r.left) / r.width)); }; const tipPct = dragPct ?? hoverPct; const showTip = props.formatValue && tipPct != null; return (
{ if (props.onDragStart && !props.onDragStart()) return; e.currentTarget.setPointerCapture(e.pointerId); const f = fraction(e); setDragPct(f * 100); if (props.live) props.onCommit?.(f); }} onPointerMove={(e) => { const f = fraction(e); setHoverPct(f * 100); if (dragPct == null) return; setDragPct(f * 100); if (props.live) props.onCommit?.(f); }} onPointerLeave={() => setHoverPct(null)} onPointerUp={(e) => { if (dragPct == null) return; setDragPct(null); if (!props.live) props.onCommit?.(fraction(e)); }} > {props.bufferPct != null && (
)}
{showTip && (
{props.formatValue!((tipPct as number) / 100)}
)}
); }