Detent UI

Voice Note

Component

About

A voice note for AI replies: a dot waveform you can scrub, play and pause, 1, 1.5 and 2x speed, and a transcript that lights up word by word as it plays. Works without an audio file. Free and MIT from Detent UI, detentui.com/components/voicenote

Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Voice Note to your project and fits it to your theme.

Code

How Voice Note is used: the demo's code. The component's own source comes with a free account.

Usage.tsx

'use client'

import { useEffect, useRef } from 'react'
import { VoiceNote } from '@/components/ui/voice-note'
import type { SpokenWord, VoiceNoteHandle } from '@/components/ui/voice-note'

const SAID = 'Your refund for order 4821 went out this morning. It’s on its way to the Visa ending 4021, and usually lands in three to five days. I’ve also flagged the carrier: that’s the third damaged box this month.'

const WORDS: SpokenWord[] = (() => {
  let at = 0.25
  return SAID.split(' ').map((word) => {
    const w = { word, at }
    at += 0.24 + word.length * 0.028 + (/[.:]$/.test(word) ? 0.55 : /,$/.test(word) ? 0.28 : 0)
    return w
  })
})()
const DURATION = Math.ceil(WORDS[WORDS.length - 1].at + 0.8)
const HOLD = 3000
const CONTROL = 'button,a,input,textarea,select,label,[role="button"],[role="tab"],[role="option"],[role="slider"],[role="switch"],[contenteditable],[draggable]'

export default function VoiceNoteDemo() {
  const note = useRef<VoiceNoteHandle>(null)
  const box = useRef<HTMLDivElement>(null)
  const held = useRef(false)
  const away = useRef(false)
  const touched = useRef(false)
  const timer = useRef<ReturnType<typeof setTimeout>>(undefined)
  const downAt = useRef<[number, number] | null>(null)

  useEffect(() => {
    const el = box.current
    if (!el) return
    const seen = new IntersectionObserver(([e]) => {
      away.current = !e.isIntersecting
      if (away.current) note.current?.pause()
    })
    seen.observe(el)
    return () => seen.disconnect()
  }, [])

  useEffect(() => {
    let live = true
    const tick = (ms: number) => new Promise<void>((r, x) => setTimeout(() => (live && !touched.current ? r() : x(new Error('stopped'))), ms))
    const beat = async (ms: number) => {
      for (let t = 0; t < ms || held.current || away.current; t += 100) await tick(100)
    }
    const play = async () => {
      for (;;) {
        note.current?.seek(0)
        note.current?.speed(1)
        await beat(1200)
        note.current?.play()
        await beat(3800)
        note.current?.speed(1.5)
        await beat(((DURATION - 3.8) / 1.5) * 1000 + 2400)
        note.current?.pause()
      }
    }
    play().catch(() => {})
    return () => {
      live = false
      clearTimeout(timer.current)
    }
  }, [])

  return (
    <div className="flex min-h-[420px] w-full items-center justify-center bg-background p-6">
      <div
        ref={box}
        className="flex w-[430px] max-w-full flex-col gap-3"
        onPointerEnter={(e) => {
          if (e.pointerType === 'touch') return
          held.current = true
          clearTimeout(timer.current)
          timer.current = setTimeout(() => (held.current = false), HOLD)
        }}
        onPointerLeave={() => {
          clearTimeout(timer.current)
          held.current = false
        }}
        onPointerDownCapture={(e) => {
          if (e.pointerType === 'touch') downAt.current = [e.clientX, e.clientY]
          else touched.current = true
        }}
        onPointerUpCapture={(e) => {
          const at = downAt.current
          if (e.pointerType !== 'touch' || !at) return
          downAt.current = null
          if (Math.hypot(e.clientX - at[0], e.clientY - at[1]) > 10 || (e.target as Element).closest?.(CONTROL)) touched.current = true
        }}
        onKeyDownCapture={() => (touched.current = true)}
      >
        <p className="ml-auto w-fit max-w-[280px] rounded-[20px] rounded-br-[6px] bg-foreground px-3.5 py-2.5 text-[15px] leading-5 tracking-[-0.01em] text-background [.dark_&]:bg-[color-mix(in_oklch,var(--color-foreground)_9%,var(--color-card))] [.dark_&]:text-foreground">
          Did the refund go through?
        </p>
        <div className="flex items-end gap-2">
          <span className="mb-1 flex">
            <Mark size={28} />
          </span>
          <div className="min-w-0 flex-1">
            <VoiceNote ref={note} words={WORDS} duration={DURATION} />
          </div>
        </div>
      </div>
    </div>
  )
}

function Mark({ size = 28 }: { size?: number }) {
  return (
    <svg aria-hidden width={size} height={size} viewBox="0 0 24 24" className="shrink-0">
      <rect width="24" height="24" rx={12} style={{ fill: 'var(--color-foreground)' }} />
      <g transform="translate(12 12) scale(0.54) translate(-12 -12)" style={{ fill: 'var(--color-card)' }}>
        <rect x="3.75" y="3" width="5.5" height="18" rx="1.1" />
        <path d="M11.25 3a9 9 0 0 1 0 18z" />
      </g>
    </svg>
  )
}

Dependencies

More by Detent UI

View all
  • Live List

    Live List

    62
  • Prompt Box

    Prompt Box

    2
  • Diff Review

    Diff Review

    1
  • Baseline Chart

    Baseline Chart

    1
  • Shift

    Shift

    1
  • Rewind

    Rewind

    1
  • Phrase

    Phrase

    1
  • Metric Explainer

    Metric Explainer

    1