1 parent
7a18682b0e
commit
24fecc90f3
107 files changed
+14935
-1919
No files matched your search
@@ -0,0 +1,82 @@
|
||||
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<HTMLDivElement>(null);
|
||||
const [dragPct, setDragPct] = useState<number | null>(null);
|
||||
// Pointer position (0..100) for the tooltip; null when the pointer is away.
|
||||
const [hoverPct, setHoverPct] = useState<number | null>(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 (
|
||||
<div
|
||||
ref={el}
|
||||
className={
|
||||
'bar' +
|
||||
(props.slim ? ' bar-slim' : '') +
|
||||
(props.className ? ` ${props.className}` : '')
|
||||
}
|
||||
onPointerDown={(e) => {
|
||||
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 && (
|
||||
<div
|
||||
className="bar-buffer"
|
||||
style={{ width: `${Math.min(100, props.bufferPct)}%` }}
|
||||
/>
|
||||
)}
|
||||
<div
|
||||
className="bar-fill"
|
||||
style={{ width: `${dragPct ?? props.pct}%` }}
|
||||
/>
|
||||
{showTip && (
|
||||
<div
|
||||
className="bar-tip"
|
||||
style={{ left: `${tipPct}%` }}
|
||||
>
|
||||
{props.formatValue!((tipPct as number) / 100)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user