A voice-button component from ElevenLabs UI.
From ElevenLabs UIMITSource
1"use client"
2
3import * as React from "react"
4import { CheckIcon, XIcon } from "lucide-react"
5
6import { cn } from "@/lib/utils"
7import { Button } from "@/components/ui/button"
8import { LiveWaveform } from "@/components/ui/live-waveform"
9
10export type VoiceButtonState =
11 | "idle"
12 | "recording"
13 | "processing"
14 | "success"
15 | "error"
16
17export interface VoiceButtonProps
18 extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, "onError"> {
19 /**
20 * Current state of the voice button
21 * @default "idle"
22 */
23 state?: VoiceButtonState
24
25 /**
26 * Callback when button is clicked
27 */
28 onPress?: () => void
29
30 /**
31 * Content to display on the left side (label)
32 * Can be a string or ReactNode for custom components
33 */
34 label?: React.ReactNode
35
36 /**
37 * Content to display on the right side (e.g., keyboard shortcut)
38 * Can be a string or ReactNode for custom components
39 * @example "⌥Space" or <kbd>⌘K</kbd>
40 */
41 trailing?: React.ReactNode
42
43 /**
44 * Icon to display in the center when idle (for icon size buttons)
45 */
46 icon?: React.ReactNode
47
48 /**
49 * Custom variant for the button
50 * @default "outline"
51 */
52 variant?:
53 | "default"
54 | "destructive"
55 | "outline"
56 | "secondary"
57 | "ghost"
58 | "link"
59
60 /**
61 * Size of the button
62 * @default "default"
63 */
64 size?: "default" | "sm" | "lg" | "icon"
65
66 /**
67 * Custom className for the button
68 */
69 className?: string
70
71 /**
72 * Custom className for the waveform container
73 */
74 waveformClassName?: string
75
76 /**
77 * Duration in ms to show success/error states
78 * @default 1500
79 */
80 feedbackDuration?: number
81
82 /**
83 * Disable the button
84 */
85 disabled?: boolean
86}
87
88export const VoiceButton = React.forwardRef<
89 HTMLButtonElement,
90 VoiceButtonProps
91>(
92 (
93 {
94 state = "idle",
95 onPress,
96 label,
97 trailing,
98 icon,
99 variant = "outline",
100 size = "default",
101 className,
102 waveformClassName,
103 feedbackDuration = 1500,
104 disabled,
105 onClick,
106 ...props
107 },
108 ref
109 ) => {
110 const [showFeedback, setShowFeedback] = React.useState(false)
111
112 React.useEffect(() => {
113 if (state === "success" || state === "error") {
114 setShowFeedback(true)
115 const timeout = setTimeout(
116 () => setShowFeedback(false),
117 feedbackDuration
118 )
119 return () => clearTimeout(timeout)
120 } else {
121 // Reset feedback when state changes away from success/error
122 setShowFeedback(false)
123 }
124 }, [state, feedbackDuration])
125
126 const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
127 onClick?.(e)
128 onPress?.()
129 }
130
131 const isRecording = state === "recording"
132 const isProcessing = state === "processing"
133 const isSuccess = state === "success"
134 const isError = state === "error"
135
136 const buttonVariant = variant
137 const isDisabled = disabled || isProcessing
138
139 const displayLabel = label
140
141 const shouldShowWaveform = isRecording || isProcessing || showFeedback
142 const shouldShowTrailing = !shouldShowWaveform && trailing
143
144 return (
145 <Button
146 ref={ref}
147 type="button"
148 variant={buttonVariant}
149 size={size}
150 onClick={handleClick}
151 disabled={isDisabled}
152 className={cn(
153 "gap-2 transition-all duration-200",
154 size === "icon" && "relative",
155 className
156 )}
157 aria-label={"Voice Button"}
158 {...props}
159 >
160 {size !== "icon" && displayLabel && (
161 <span className="inline-flex shrink-0 items-center justify-start">
162 {displayLabel}
163 </span>
164 )}
165
166 <div
167 className={cn(
168 "relative box-content flex shrink-0 items-center justify-center overflow-hidden transition-all duration-300",
169 size === "icon"
170 ? "absolute inset-0 rounded-sm border-0"
171 : "h-5 w-24 rounded-sm border",
172 isRecording
173 ? "bg-primary/10 dark:bg-primary/5"
174 : size === "icon"
175 ? "bg-muted/50 border-0"
176 : "border-border bg-muted/50",
177 waveformClassName
178 )}
179 >
180 {shouldShowWaveform && (
181 <LiveWaveform
182 active={isRecording}
183 processing={isProcessing || isSuccess}
184 barWidth={2}
185 barGap={1}
186 barRadius={4}
187 fadeEdges={false}
188 sensitivity={1.8}
189 smoothingTimeConstant={0.85}
190 height={20}
191 mode="static"
192 className="animate-in fade-in absolute inset-0 h-full w-full duration-300"
193 />
194 )}
195
196 {shouldShowTrailing && (
197 <div className="animate-in fade-in absolute inset-0 flex items-center justify-center duration-300">
198 {typeof trailing === "string" ? (
199 <span className="text-muted-foreground px-1.5 font-mono text-[10px] font-medium select-none">
200 {trailing}
201 </span>
202 ) : (
203 trailing
204 )}
205 </div>
206 )}
207
208 {!shouldShowWaveform &&
209 !shouldShowTrailing &&
210 icon &&
211 size === "icon" && (
212 <div className="animate-in fade-in absolute inset-0 flex items-center justify-center duration-300">
213 {icon}
214 </div>
215 )}
216
217 {isSuccess && showFeedback && (
218 <div className="animate-in fade-in bg-background/80 absolute inset-0 flex items-center justify-center duration-300">
219 <span className="text-primary text-[10px] font-medium">
220 <CheckIcon className="size-3.5" />
221 </span>
222 </div>
223 )}
224
225 {/* Error Icon */}
226 {isError && showFeedback && (
227 <div className="animate-in fade-in bg-background/80 absolute inset-0 flex items-center justify-center duration-300">
228 <span className="text-destructive text-[10px] font-medium">
229 <XIcon className="size-3.5" />
230 </span>
231 </div>
232 )}
233 </div>
234 </Button>
235 )
236 }
237)
238
239VoiceButton.displayName = "VoiceButton"
240