import React from "react"; import { formatSeconds } from "../format"; export type SeekBarProps = { value: number; max: number; buffer?: number; onChange: (value: number) => void; onMouseMove?: (e: React.MouseEvent) => void; videoId?: string; }; const SeekBar = (props: SeekBarProps) => { const { value, max, onChange } = props; const buffer = props.buffer || 0; const [isScrubbing, setIsScrubbing] = React.useState(false); const [hoverPercentX, setHoverPercentX] = React.useState(0); const [isMouseOver, setIsMouseOver] = React.useState(false); const handleMouseMove = (e: React.MouseEvent) => { const rect = e.currentTarget.getBoundingClientRect(); const percent = Math.min(1, Math.max(0, (e.clientX - rect.x) / rect.width)); setHoverPercentX(percent); if (isScrubbing) onChange(percent * max); props.onMouseMove?.(e); }; return ( <>
{ setIsScrubbing(true); onChange(hoverPercentX * max); }} onMouseUp={() => setIsScrubbing(false)} onMouseMove={handleMouseMove} onMouseEnter={() => setIsMouseOver(true)} onMouseLeave={() => setIsMouseOver(false)} aria-label="Seekbar" aria-valuenow={value} >
{formatSeconds(hoverPercentX * max)}
); }; export default SeekBar;