aiellie

Search

Find a component, template or page

GitHub
Components

Waveform

Live bars that follow an audio stream, newest on the right, so a microphone can be seen to hear you. They hold still under reduced motion.

Open full screen
fig. 01 · open it full screen from the corner

Installation

npx shadcn@latest add "@aiellie/waveform"

shadcn needs the @aiellie registry in components.json first. npx aiellie add waveform adds it on its first run, then installs the same way.

Waveform shows that a microphone is hearing something: a row of thin bars that rise with the level of the audio and scroll along as it changes. Give it a MediaStream; while there is none, the bars rest low. It is the meter inside the Dictate button.

Getting started

  1. Get a stream

    Ask for the microphone with getUserMedia and keep the stream in state. Stop its tracks when you're done, or the browser keeps showing the microphone in use.

    components/recorder.tsx
    "use client"
    
    import * as React from "react"
    
    import { Waveform } from "@/components/aiellie/waveform"
    
    export function Recorder() {
      const [stream, setStream] = React.useState<MediaStream | null>(null)
    
      React.useEffect(
        () => () => stream?.getTracks().forEach((track) => track.stop()),
        [stream]
      )
    
      return (
        <div className="flex items-center gap-3">
          <button
            onClick={async () =>
              setStream(await navigator.mediaDevices.getUserMedia({ audio: true }))
            }
          >
            Record
          </button>
          <Waveform stream={stream} />
        </div>
      )
    }

Anatomy

<div data-slot="waveform" aria-hidden>
  <span /> {/* one per bar, scaled to the level it heard */}
</div>

The bars are 2px wide and 14px tall at most, in currentColor, which is the live color unless you set another. Every 50ms the newest level enters at the end and the oldest leaves the start, so the row reads as recent history. It's decorative and hidden from screen readers: say what is happening in words beside it.

With reduced motion the bars stay at rest. Nothing is recorded; the audio only passes through an analyser.

Examples

From the microphone

Press Listen and allow the microphone.

Color and length

bars sets how many there are, and a text color class recolors them.

<Waveform stream={stream} bars={48} className="text-muted-foreground" />

API reference

Waveform

PropTypeDefaultDescription
streamMediaStream | nullrequiredThe audio to follow, usually from getUserMedia. The bars rest while it is null.
barsnumber24How many bars to draw.
classNamestringMerged onto the root. Its text color colors the bars.

All other div props are forwarded to the root.