A scrub-bar component from ElevenLabs UI.
From ElevenLabs UIMITSource
1"use client"
2
3import * as React from "react"
4import {
5 createContext,
6 useCallback,
7 useContext,
8 useRef,
9 type ComponentProps,
10 type HTMLAttributes,
11} from "react"
12
13import { cn } from "@/lib/utils"
14import { Progress } from "@/components/ui/progress"
15
16function formatTimestamp(value: number) {
17 if (!Number.isFinite(value) || value < 0) return "0:00"
18 const totalSeconds = Math.floor(value)
19 const minutes = Math.floor(totalSeconds / 60)
20 const seconds = totalSeconds % 60
21 return `${minutes}:${seconds.toString().padStart(2, "0")}`
22}
23
24interface ScrubBarContextValue {
25 duration: number
26 value: number
27 progress: number
28 onScrub?: (time: number) => void
29 onScrubStart?: () => void
30 onScrubEnd?: () => void
31}
32
33const ScrubBarContext = createContext<ScrubBarContextValue | null>(null)
34
35function useScrubBarContext() {
36 const context = useContext(ScrubBarContext)
37 if (!context) {
38 throw new Error("useScrubBarContext must be used within a ScrubBar.Root")
39 }
40 return context
41}
42
43interface ScrubBarContainerProps extends HTMLAttributes<HTMLDivElement> {
44 duration: number
45 value: number
46 onScrub?: (time: number) => void
47 onScrubStart?: () => void
48 onScrubEnd?: () => void
49}
50
51function ScrubBarContainer({
52 duration,
53 value,
54 onScrub,
55 onScrubStart,
56 onScrubEnd,
57 children,
58 className,
59 ...props
60}: ScrubBarContainerProps) {
61 const progress = duration > 0 ? (value / duration) * 100 : 0
62
63 const contextValue: ScrubBarContextValue = {
64 duration,
65 value,
66 progress,
67 onScrub,
68 onScrubStart,
69 onScrubEnd,
70 }
71
72 return (
73 <ScrubBarContext.Provider value={contextValue}>
74 <div
75 data-slot="scrub-bar-root"
76 className={cn("flex w-full items-center", className)}
77 {...props}
78 >
79 {children}
80 </div>
81 </ScrubBarContext.Provider>
82 )
83}
84ScrubBarContainer.displayName = "ScrubBarContainer"
85
86type ScrubBarTrackProps = HTMLAttributes<HTMLDivElement>
87
88function ScrubBarTrack({ className, children, ...props }: ScrubBarTrackProps) {
89 const trackRef = useRef<HTMLDivElement | null>(null)
90 const { duration, onScrub, onScrubStart, onScrubEnd, value } =
91 useScrubBarContext()
92
93 const getTimeFromClientX = useCallback(
94 (clientX: number) => {
95 const track = trackRef.current
96 if (!track || !duration) return null
97 const rect = track.getBoundingClientRect()
98 const ratio = (clientX - rect.left) / rect.width
99 const clamped = Math.min(Math.max(ratio, 0), 1)
100 return duration * clamped
101 },
102 [duration]
103 )
104
105 const handlePointerDown = useCallback(
106 (event: React.PointerEvent<HTMLDivElement>) => {
107 if (!duration) return
108 event.preventDefault()
109 onScrubStart?.()
110 const time = getTimeFromClientX(event.clientX)
111 if (time != null) {
112 onScrub?.(time)
113 }
114
115 const handleMove = (moveEvent: PointerEvent) => {
116 const nextTime = getTimeFromClientX(moveEvent.clientX)
117 if (nextTime != null) {
118 onScrub?.(nextTime)
119 }
120 }
121
122 const handleUp = () => {
123 onScrubEnd?.()
124 window.removeEventListener("pointermove", handleMove)
125 window.removeEventListener("pointerup", handleUp)
126 }
127
128 window.addEventListener("pointermove", handleMove)
129 window.addEventListener("pointerup", handleUp, { once: true })
130 },
131 [duration, getTimeFromClientX, onScrub, onScrubEnd, onScrubStart]
132 )
133
134 const clampedValue = Math.min(Math.max(value, 0), duration || 0)
135
136 return (
137 <div
138 ref={trackRef}
139 data-slot="scrub-bar-track"
140 className={cn(
141 "bg-secondary relative h-2 w-full grow cursor-pointer touch-none rounded-full transition-none select-none",
142 className
143 )}
144 onPointerDown={handlePointerDown}
145 role="slider"
146 aria-valuemin={0}
147 aria-valuemax={duration || 0}
148 aria-valuenow={clampedValue}
149 {...props}
150 >
151 {children}
152 </div>
153 )
154}
155ScrubBarTrack.displayName = "ScrubBarTrack"
156
157type ScrubBarProgressProps = Omit<ComponentProps<typeof Progress>, "value">
158
159function ScrubBarProgress({ className, ...props }: ScrubBarProgressProps) {
160 const { progress } = useScrubBarContext()
161
162 return (
163 <Progress
164 data-slot="scrub-bar-progress"
165 value={progress}
166 className={cn("absolute h-full [&>div]:transition-none", className)}
167 {...props}
168 />
169 )
170}
171ScrubBarProgress.displayName = "ScrubBarProgress"
172
173type ScrubBarThumbProps = HTMLAttributes<HTMLDivElement>
174
175function ScrubBarThumb({ className, children, ...props }: ScrubBarThumbProps) {
176 const { progress } = useScrubBarContext()
177 return (
178 <div
179 data-slot="scrub-bar-thumb"
180 className={cn(
181 "bg-primary absolute top-1/2 block h-4 w-4 -translate-x-1/2 -translate-y-1/2 rounded-full transition-colors disabled:pointer-events-none disabled:opacity-50",
182 className
183 )}
184 style={{ left: `${progress}%` }}
185 {...props}
186 >
187 {children}
188 </div>
189 )
190}
191ScrubBarThumb.displayName = "ScrubBarThumb"
192
193interface ScrubBarTimeLabelProps extends HTMLAttributes<HTMLSpanElement> {
194 time: number
195 format?: (time: number) => string
196}
197
198function ScrubBarTimeLabel({
199 className,
200 time,
201 format = formatTimestamp,
202 ...props
203}: ScrubBarTimeLabelProps) {
204 return (
205 <span
206 data-slot="scrub-bar-time-label"
207 {...props}
208 className={cn("tabular-nums", className)}
209 >
210 {format(time)}
211 </span>
212 )
213}
214ScrubBarTimeLabel.displayName = "ScrubBarTimeLabel"
215
216export {
217 ScrubBarContainer,
218 ScrubBarTrack,
219 ScrubBarProgress,
220 ScrubBarThumb,
221 ScrubBarTimeLabel,
222}
223