Mic Selector

A composable dropdown component for selecting audio input devices with permission handling and device change detection.

The MicSelector component provides a flexible and composable interface for selecting microphone input devices. Built on shadcn-vue's Command and Popover components, it features automatic device detection, permission handling, dynamic device list updates, and intelligent device name parsing.

Installation

AI Elements Vue
shadcn-vue CLI
npx ai-elements-vue@latest add mic-selector

Manual Installation

Copy and paste the following code into your project.

MicSelector.vue
MicSelectorTrigger.vue
MicSelectorContent.vue
MicSelectorInput.vue
MicSelectorList.vue
MicSelectorEmpty.vue
MicSelectorItem.vue
MicSelectorLabel.vue
MicSelectorValue.vue
context.ts
useAudioDevices.ts
index.ts
<script setup lang="ts">
import { Popover } from '@repo/shadcn-vue/components/ui/popover'
import { useVModel } from '@vueuse/core'
import { computed, provide, ref, watch } from 'vue'
import { MicSelectorKey } from './context'
import { useAudioDevices } from './useAudioDevices'

type PopoverProps = InstanceType<typeof Popover>['$props']

interface Props extends /* @vue-ignore */ PopoverProps {
  value?: string
  defaultValue?: string
  open?: boolean
  defaultOpen?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  open: undefined,
  defaultOpen: false,
})

const emit = defineEmits<{
  (e: 'update:value', value: string | undefined): void
  (e: 'update:open', open: boolean): void
  (e: 'valueChange', value: string | undefined): void
  (e: 'openChange', open: boolean): void
}>()

const value = useVModel(props, 'value', emit, {
  defaultValue: props.defaultValue,
  passive: (props.value === undefined) as any,
})

const open = useVModel(props, 'open', emit, {
  defaultValue: props.defaultOpen,
  passive: (props.open === undefined) as any,
})

const forwardedProps = computed(() => {
  const { value, defaultValue, open, defaultOpen, ...rest } = props
  return rest
})

const width = ref(200)

const { devices, hasPermission, loadDevices, loading } = useAudioDevices()

watch([open, hasPermission, loading], ([newOpen, newHasPermission, newLoading]) => {
  if (newOpen && !newHasPermission && !newLoading) {
    loadDevices()
  }
})

function setValue(newValue: string | undefined) {
  value.value = newValue
  emit('valueChange', newValue)
}

function setOpen(newOpen: boolean) {
  open.value = newOpen
  emit('openChange', newOpen)
}

function setWidth(newWidth: number) {
  width.value = newWidth
}

provide(MicSelectorKey, {
  devices,
  value,
  setValue,
  open,
  setOpen,
  width,
  setWidth,
})
</script>

<template>
  <Popover
    v-bind="forwardedProps"
    :open="open"
    @update:open="setOpen"
  >
    <slot />
  </Popover>
</template>

Features

  • Fully composable architecture with granular control components
  • Automatic audio input device enumeration
  • Permission-based device name display
  • Real-time device change detection via devicechange events
  • Intelligent device label parsing with ID extraction
  • Controlled and uncontrolled component patterns
  • Responsive width matching between trigger and content
  • Built on shadcn-vue Command and Popover components
  • Full TypeScript support with proper types for all components

Props

<MicSelector />

Root Popover component that provides context for all child components.

v-model:valuestring
The selected device ID (controlled).
defaultValuestring
The default selected device ID (uncontrolled).
v-model:openboolean
The open state of the popover (controlled).
defaultOpenboolean
The default open state (uncontrolled).
...propsPopoverProps
Any other props are spread to the Popover component.

<MicSelectorTrigger />

Button that opens the microphone selector popover. Automatically tracks its width to match the popover content.

...propsButtonProps
Any other props are spread to the Button component.

<MicSelectorValue />

Displays the currently selected microphone name or a placeholder.

...propsHTMLAttributes
Any other props are spread to the span element.

<MicSelectorContent />

Container for the Command component, rendered inside the popover.

popoverOptionsPopoverContentProps
Props to pass to the underlying PopoverContent component.
...propsCommandProps
Any other props are spread to the Command component.

<MicSelectorInput />

Search input for filtering microphones.

...propsCommandInputProps
Any other props are spread to the CommandInput component.

<MicSelectorList />

Wrapper for the list of microphone items. Uses scoped slots to provide access to device data.

v-slot{ devices: MediaDeviceInfo[] }
Scoped slot that receives the array of available devices.
...propsCommandListProps
Any other props are spread to the CommandList component.

<MicSelectorEmpty />

Message shown when no microphones match the search.

...propsCommandEmptyProps
Any other props are spread to the CommandEmpty component.

<MicSelectorItem />

Selectable item representing a microphone.

valuestring
The device ID for this item.
...propsCommandItemProps
Any other props are spread to the CommandItem component.

<MicSelectorLabel />

Displays a formatted microphone label with intelligent device ID parsing. Automatically extracts and styles device IDs in the format (XXXX:XXXX).

deviceMediaDeviceInfo
The MediaDeviceInfo object for the device.
...propsHTMLAttributes
Any other props are spread to the span element.

Emits

<MicSelector />

update:valuestring | undefined
Emitted when the selected device changes (for v-model).
valueChangestring | undefined
Callback emitted when the selected device changes.
update:openboolean
Emitted when the open state changes (for v-model).
openChangeboolean
Callback emitted when the open state changes.

Composables

useAudioDevices()

A custom composable for managing audio input devices. This composable is used internally by the MicSelector component but can also be used independently.

<script setup lang="ts">
import { useAudioDevices } from '@repo/elements/mic-selector'

const { devices, loading, error, hasPermission, loadDevices } = useAudioDevices()
</script>

<template>
  <div>
    <p v-if="loading">
      Loading devices...
    </p>
    <p v-else-if="error">
      Error: {{ error }}
    </p>
    <div v-for="device in devices" :key="device.deviceId">
      {{ device.label }}
    </div>
    <button v-if="!hasPermission" @click="loadDevices">
      Grant Permission
    </button>
  </div>
</template>

Return Value

devicesRef<MediaDeviceInfo[]>
Array of available audio input devices.
loadingRef<boolean>
Whether devices are currently being loaded.
errorRef<string | null>
Error message if device loading failed.
hasPermissionRef<boolean>
Whether microphone permission has been granted.
loadDevices() => Promise<void>
Function to request microphone permission and load device names.

Behavior

Permission Handling

The component implements a two-stage permission approach:

  1. Without Permission: Initially loads devices without requesting permission. Device labels may show as generic names (e.g., "Microphone 1").
  2. With Permission: When the popover is opened and permission hasn't been granted, automatically requests microphone access and displays actual device names.

Device Label Parsing

The MicSelectorLabel component intelligently parses device names that include hardware IDs in the format (XXXX:XXXX). It splits the label into the device name and ID, styling the ID with muted text for better readability.

For example: "MacBook Pro Microphone (1a2b:3c4d)" becomes:

  • Device name: "MacBook Pro Microphone"
  • Device ID: "(1a2b:3c4d)" (styled with muted color)

Width Synchronization

The MicSelectorTrigger uses a ResizeObserver to track its width and automatically synchronizes it with the MicSelectorContent popover width for a cohesive appearance.

Device Change Detection

The component listens for devicechange events (e.g., plugging/unplugging microphones) and automatically updates the device list in real-time.

Accessibility

  • Uses semantic HTML with proper ARIA attributes via shadcn-vue components
  • Full keyboard navigation support through Command component
  • Screen reader friendly with proper labels and roles
  • Searchable device list for quick selection

Notes

  • Requires a secure context (HTTPS or localhost) for microphone access
  • Browser may prompt user for microphone permission on first open
  • Device labels are only fully descriptive after permission is granted
  • Component handles cleanup of temporary media streams during permission requests
  • Uses VueUse's useVModel for flexible controlled/uncontrolled patterns