aiellie

Search

Find a component, template or page

GitHub
Components

Mic Menu

Which microphone to use, from the inputs the browser lists, kept current as devices come and go.

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

Installation

npx shadcn@latest add "@aiellie/mic-menu"

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

Mic Menu picks which microphone to listen with, from the inputs the browser lists. It updates when a headset is plugged in or taken out.

Until a page has been allowed the microphone once, browsers hide the inputs' names. The menu numbers them instead and offers a row that asks for access, after which the real names appear.

Getting started

  1. Keep the choice

    components/voice-controls.tsx
    "use client"
    
    import * as React from "react"
    
    import { MicMenu } from "@/components/aiellie/mic-menu"
    
    export function VoiceControls() {
      const [device, setDevice] = React.useState("")
      return <MicMenu value={device} onValueChange={setDevice} />
    }
  2. Listen on it

    Ask for that input when you open the microphone. An empty value means the system default.

    lib/listen.ts
    export function listen(deviceId: string) {
      return navigator.mediaDevices.getUserMedia({
        audio: deviceId ? { deviceId: { exact: deviceId } } : true,
      })
    }

Anatomy

<button data-slot="mic-menu" /> {/* tooltip icon button */}
<div data-slot="menu-content"> {/* in a portal */}
  <div data-slot="menu-radio-group">{/* a row per input */}</div>
  {/* "Allow access to see names", while they're hidden */}
</div>

The button's tooltip names the chosen input. useMicDevices is exported too, for a picker of your own.

Examples

Your own picker

useMicDevices gives the list, whether it has names yet, and a way to ask for them.

components/mic-select.tsx
"use client"

import { useMicDevices } from "@/components/aiellie/mic-menu"

export function MicSelect() {
  const { devices, named, requestNames } = useMicDevices()
  return (
    <>
      <select>
        {devices.map((device) => (
          <option key={device.deviceId} value={device.deviceId}>
            {device.label}
          </option>
        ))}
      </select>
      {named ? null : <button onClick={requestNames}>Show names</button>}
    </>
  )
}

API reference

MicMenu

PropTypeDefaultDescription
valuestringrequiredThe chosen deviceId, or "" for the system default.
onValueChange(deviceId: string) => voidrequired
labelstring"Microphone"Heads the menu and is read out on the button.
side, align"top", "start"Where the menu opens.

All other TooltipIconButton props are forwarded.

useMicDevices

FieldTypeDefaultDescription
devices{ deviceId: string; label: string }[]The inputs.
namedbooleanWhether the browser has shown their names.
requestNames() => Promise<void>Opens the microphone for a moment so the names appear.