Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Voice Selector for Ark UI

A searchable dialog of text-to-speech voices, with a play button and the voice's gender, accent and age on each row. For React on Ark UI.

On this page11 sections

Voice Selector is a dialog where people search your text-to-speech voices and pick the one the assistant speaks with. A row can carry a play button for a sample, plus the voice's gender, accent and age, so people narrow the list before they listen.

It's a Command inside a Dialog, and it plays no audio itself. For three or four voices with nothing to search, a Select takes less room.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Voice Selector anatomy: content, input, item, preview, name, attributes

Usage

import { useListCollection } from "@ark-ui/react/collection";
import { useFilter } from "@ark-ui/react/locale";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import {
  VoiceSelector, VoiceSelectorContent, VoiceSelectorDescription,
  VoiceSelectorEmpty, VoiceSelectorGroup, VoiceSelectorInput,
  VoiceSelectorItem, VoiceSelectorList, VoiceSelectorName,
  VoiceSelectorTrigger,
} from "@/components/ui/voice-selector";
const voices = [
  { description: "Warm and professional", label: "Alloy", value: "alloy" },
  { description: "Clear and articulate", label: "Echo", value: "echo" },
];

export function VoicePicker() {
  const [voice, setVoice] = useState<string>();
  const selected = voices.find((item) => item.value === voice);
  const { contains } = useFilter({ sensitivity: "base" });
  const { collection, filter } = useListCollection({
    filter: contains,
    initialItems: voices,
  });

  return (
    <VoiceSelector onValueChange={setVoice} value={voice}>
      <VoiceSelectorTrigger asChild>
        <Button variant="outline">
          {selected ? selected.label : "Select a voice..."}
        </Button>
      </VoiceSelectorTrigger>
      <VoiceSelectorContent
        collection={collection}
        onInputValueChange={(details) => filter(details.inputValue)}
      >
        <VoiceSelectorInput placeholder="Search voices..." />
        <VoiceSelectorList>
          <VoiceSelectorEmpty>No voices found.</VoiceSelectorEmpty>
          <VoiceSelectorGroup heading="Voices">
            {collection.items.map((item) => (
              <VoiceSelectorItem item={item} key={item.value}>
                <div className="flex flex-col">
                  <VoiceSelectorName>{item.label}</VoiceSelectorName>
                  <VoiceSelectorDescription>
                    {item.description}
                  </VoiceSelectorDescription>
                </div>
              </VoiceSelectorItem>
            ))}
          </VoiceSelectorGroup>
        </VoiceSelectorList>
      </VoiceSelectorContent>
    </VoiceSelector>
  );
}

The voices are a collection, as Command's are. VoiceSelectorContent takes it and reports each change to the search text, which you pass to filter, and each VoiceSelectorItem takes one of its items. Picking a voice sets value and closes the dialog.

Accessibility

The trigger opens a modal dialog with focus in the search field, which takes its name from its placeholder. The dialog's title is read only by screen readers, Voice Selector unless you set title on VoiceSelectorContent. Focus stays in the field while the arrow keys move a highlight through the voices. The list is a listbox, and the picked voice reports aria-selected. Gender and accent are read as words. A play button reads Play preview until you pass the voice's name as its label, which makes it Play Alloy. It sits inside its row, so its label is read as part of the voice's name, and Tab reaches it.

Arrow Down
Highlights the next voice, and stays on the last one.
Arrow Up
Highlights the previous voice, and stays on the first one.
Home
Highlights the first voice.
End
Highlights the last voice.
Enter
In the search field, picks the highlighted voice and closes the dialog. On a play button, plays or pauses its sample without picking the voice.
Space
On a play button, plays or pauses its sample without picking the voice.
Tab
Moves focus from the search field to the play buttons, one row at a time.
Escape
Closes the dialog without a change.

Demos

Attributes

Six voices from OpenAI and ElevenLabs in two groups, with Sage picked at the start. Under each name, VoiceSelectorAttributes lines up a gender symbol, an accent flag, an age and one word on the tone, split by VoiceSelectorBullet. The search matches the voice's name only. Use it when people narrow a long list by trait before they listen.

Preview width

Preview

Onyx, Alloy and Nova each have a VoiceSelectorPreview, labelled with the voice's name, that speaks a line through the browser's speech synthesis at that voice's pitch and rate. The button spins until speech starts, shows pause while it plays and never picks the voice, and closing the dialog stops the sample. Swap the synthesis for an <audio> element and a sample file per voice to play a provider's real voices.

Preview width

Shortcuts

Four voices with VoiceSelectorShortcut hints from ⌥1 to ⌥4, and a key listener that picks a voice with Alt and its number while the dialog is open. The hint is only text, so the listener is yours to write, and the picked voice keeps its check beside the hint. Use shortcuts for a few voices people switch between often.

Preview width

Controlled

An Assistant voice settings row holds open and value in its own state. Its Change button sits outside VoiceSelector and opens the dialog by setting open, and the line under the label shows the picked voice. Control open when the dialog opens from a menu item, a first-run screen or anything else that isn't a trigger.

Preview width

AI SDK

The picked value is the voice the speech model speaks with. Send it with the text to a route that calls generateSpeech, and play the audio that comes back.

import { openai } from "@ai-sdk/openai";
import { generateSpeech } from "ai";

export async function POST(request: Request) {
  const { text, voice } = await request.json();
  const { audio } = await generateSpeech({
    model: openai.speech("gpt-4o-mini-tts"),
    text,
    voice,
  });

  return new Response(audio.uint8Array, {
    headers: { "Content-Type": audio.mediaType },
  });
}

The same route can make each voice's preview. Call it once per voice with a short line and keep the files, rather than paying for a new sample on every press.

Migrating from AI SDK Elements

If you added Voice Selector from AI SDK Elements (opens in a new tab), install this one and change the import from @/components/ai-elements/voice-selector to @/components/ui/voice-selector. Every part keeps its name, and value, defaultValue, onValueChange, open, defaultOpen, onOpenChange and the preview's playing, loading and onPlay work the same. What differs:

  • VoiceSelectorContent's title is a string, and it takes a description for screen readers too.
  • VoiceSelectorAccent knows 35 accents and reads each one's name to screen readers. An accent it doesn't know shows its name as text.
  • The picked voice shows a check, and a preview button doesn't pick the voice with Enter or Space either.
  • VoiceSelectorPreview takes the voice's name as label, for Play Alloy and Pause Alloy, and keeps focus while it loads.

On Ark UI, the Command takes a collection: pass collection and onInputValueChange to VoiceSelectorContent, and give each VoiceSelectorItem an item instead of a value, as the Usage above does.

FAQ

How does the Voice Selector play button play a voice sample?

It doesn't play anything. VoiceSelectorPreview calls onPlay when pressed, and your code plays the sample, setting loading while it loads and playing while it plays, which swap the icon for a spinner or a pause sign. Presses while it loads are ignored.

Give each button the voice's name as label, so screen readers hear Play Alloy rather than the same Play preview on every row. The Preview demo speaks through the browser's speech synthesis. For a provider's real voices, play a stored sample file per voice in an <audio> element, and stop it when the open state turns false, as the demo does.

How do I get the voice someone picks in Voice Selector?

onValueChange receives the picked voice's value as a string, and the dialog closes. Pass value from your state to control it, or defaultValue to start from one voice, and the picked row shows a check.

Store the value, not the label, and send it to your speech model as the voice, as the AI SDK section does.

Which genders and accents can Voice Selector show?

VoiceSelectorGender draws a symbol for six values, male, female, transgender, androgyne, non-binary and intersex, and a small circle when you leave value out. Any other string, such as a provider's own Male, shows the small circle too, and screen readers still hear the word, so map your data to those six when you want the symbols.

VoiceSelectorAccent shows a flag for 35 accents, from american to vietnamese, and an accent it doesn't know as its name in text. Both read the word to screen readers, and children you pass replace the drawing and that word together.

Why do the Voice Selector accent flags show as letters on Windows?

The flags are emoji, and Windows' emoji font has no country flags, so the British flag shows as the two letters GB. macOS, iOS and Android draw the flag itself.

To show flags everywhere, pass your own children to VoiceSelectorAccent, such as a small SVG flag. Children replace the default content, the hidden accent name included, so put the name back as text that screen readers can read.

How do I open Voice Selector from my own button?

Hold open in your state, set it to true from your button and update it from onOpenChange, as the Controlled demo does, and the dialog still reports Escape, a click outside or a pick through onOpenChange. On close, Ark UI returns focus to a VoiceSelectorTrigger, and without one focus falls to the page, because the search field takes focus before the dialog can note your button. Pass VoiceSelector a finalFocusEl function that returns your button to send focus back there.

Can Voice Selector be used with the keyboard?

Yes. The trigger opens the dialog with focus in the search field, typing filters the list, the arrow keys move through the voices, and Enter picks one and closes the dialog. Tab moves on to the play buttons, where Enter or Space plays a sample without picking the voice, and Escape closes the dialog.

The Accessibility section lists every key.

Is this the same as AI SDK Elements' Voice Selector?

It has the same parts and props, so most code only changes its import. The title also takes a description, the accents read their names aloud, and the picked voice shows a check. The Migrating section lists what differs on your stack.

API Reference

VoiceSelector

PropType
open?boolean

Whether the dialog is open, when controlled.

defaultOpen?boolean

Whether the dialog starts open, when uncontrolled.

Default
false
onOpenChange?(open: boolean) → void

Called when the dialog opens or closes.

Parameters

open – boolean

Returns
void
triggerValue?string | null

The controlled active trigger value.

Type
string | null
defaultTriggerValue?string | null

The initial active trigger value when rendered. Use when you don't need to control the active trigger value.

Type
string | null
onTriggerValueChange?(details: TriggerValueChangeDetails) → void

Function to call when the active trigger changes.

Type
(details: TriggerValueChangeDetails) → void
Parameters

details – { value: string | null; triggerElement: HTMLElement | null }

Returns
void
value?string

The picked voice's value, when controlled.

defaultValue?string

The voice picked at first, when uncontrolled.

onValueChange?(value: string) → void

Called with a voice's value when one is picked.

Parameters

value – string

Returns
void
closeOnEscape?boolean

Whether to close the dialog when the escape key is pressed.

Default
true
closeOnInteractOutside?boolean

Whether to close the dialog when the outside is clicked.

Default
true
dir?"ltr" | "rtl"

The document's text/writing direction.

Type
"ltr" | "rtl"
Default
"ltr"
finalFocusEl?() → MaybeElement

Element to receive focus when the dialog is closed.

Returns
MaybeElement
hideMode?"display-none" | "activity"

How to hide content when mounted but not present. - display-none: HTML hidden attribute. Effects stay alive. - activity: React 19 <Activity mode="hidden">. Effects pause. Requires React 19+.

Type
"display-none" | "activity"
Default
"display-none"
id?string

The unique identifier of the machine.

ids?object

The ids of the elements in the dialog. Useful for composition.

Type
{ root?: string; label?: string; control?: string; input?: string; content?: string; trigger?: string; clearTrigger?: string; item?: (id: string, index?: number) → string; positioner?: string; itemGroup?: (id: string | number) → string; itemGroupLabel?: (id: string | number) → string }
immediate?boolean

Whether to synchronize the present change immediately or defer it to the next frame.

initialFocusEl?() → MaybeElement | false

Element to receive focus when the dialog is opened. Return false to skip moving focus, or null to use the default.

Type
() → MaybeElement | false
Returns
MaybeElement | false
lazyMount?boolean

Whether to enable lazy mounting.

Default
false
modal?boolean

Whether to prevent pointer interaction outside the element and hide all content below it.

Default
true
onEnterComplete?VoidFunction

Function called when the animation ends in the open state.

onEscapeKeyDown?(event: KeyboardEvent) → void

Function called when the escape key is pressed.

Parameters

event – { altKey: boolean; charCode: number; ctrlKey: boolean; code: string; getModifierState: (key: string) → boolean; key: string; keyCode: number; locale: string; location: number; metaKey: boolean; repeat: boolean; shiftKey: boolean; which: number }

Returns
void
onExitComplete?VoidFunction

Function called when the animation ends in the closed state.

onFocusOutside?(event: FocusOutsideEvent) → void

Function called when the focus is moved outside the component.

Type
(event: FocusOutsideEvent) → void
Parameters

event – FocusOutsideEvent

Returns
void
onInteractOutside?(event: InteractOutsideEvent) → void

Function called when an interaction happens outside the component.

Type
(event: InteractOutsideEvent) → void
Parameters

event – InteractOutsideEvent

Returns
void
onPointerDownOutside?(event: PointerDownOutsideEvent) → void

Function called when the pointer is pressed down outside the component.

Type
(event: PointerDownOutsideEvent) → void
Parameters

event – PointerDownOutsideEvent

Returns
void
onRequestDismiss?(event: LayerDismissEvent) → void

Function called when this layer is closed due to a parent layer being closed.

Type
(event: LayerDismissEvent) → void
Parameters

event – LayerDismissEvent

Returns
void
persistentElements?Array<() → Element | null>

Returns the persistent elements that: - should not have pointer-events disabled - should not trigger the dismiss event

Type
Array<() → Element | null>
present?boolean

Whether the node is present (controlled by the user).

preventScroll?boolean

Whether to prevent scrolling behind the dialog when it's opened.

Default
true
restoreFocus?boolean

Whether to restore focus to the element that had focus before the dialog was opened.

role?"dialog" | "alertdialog"

The dialog's role.

Type
"dialog" | "alertdialog"
Default
"dialog"
skipAnimationOnMount?boolean

Whether to allow the initial presence animation.

Default
false
trapFocus?boolean

Whether to trap focus inside the dialog when it's opened.

Default
true
unmountOnExit?boolean

Whether to unmount on exit.

Default
false
AttributeType
data-invalid?string

Present when invalid is true.

data-readonly?string

Present when readOnly is true.

VoiceSelectorTrigger

data-slot="voice-selector-trigger"

PropType
value?string

The value that identifies this specific trigger.

asChild?boolean

Use the provided child element as the default rendered element, combining their props and behavior.

AttributeType
data-invalid?string

Present when invalid is true.

data-readonly?string

Present when readOnly is true.

VoiceSelectorInput

data-slot="voice-selector-input"

PropType
size?InputProps["size"]

The size of the input.

Default
"md"
asChild?boolean

Use the provided child element as the default rendered element, combining their props and behavior.

AttributeType
data-invalid?string

Present when invalid is true.

data-readonly?string

Present when readOnly is true.

VoiceSelectorList

data-slot="voice-selector-list"

PropType
asChild?boolean

Use the provided child element as the default rendered element, combining their props and behavior.

className?string

Merged with the part's classes.

AttributeType
data-invalid?string

Present when invalid is true.

data-readonly?string

Present when readOnly is true.

VoiceSelectorEmpty

data-slot="voice-selector-empty"

PropType
asChild?boolean

Use the provided child element as the default rendered element, combining their props and behavior.

AttributeType
data-invalid?string

Present when invalid is true.

data-readonly?string

Present when readOnly is true.

VoiceSelectorGroup

data-slot="voice-selector-group"

PropType
heading?string | React.ReactNode

The heading of the group.

Type
string | React.ReactNode
asChild?boolean

Use the provided child element as the default rendered element, combining their props and behavior.

AttributeType
data-invalid?string

Present when invalid is true.

data-readonly?string

Present when readOnly is true.

VoiceSelectorItem

data-slot="voice-selector-item"

PropType
itemany

The item to render.

persistFocus?boolean

Whether hovering outside should clear the highlighted state.

asChild?boolean

Use the provided child element as the default rendered element, combining their props and behavior.

className?string

Merged with the part's classes.

AttributeType
data-invalid?string

Present when invalid is true.

data-readonly?string

Present when readOnly is true.

VoiceSelectorShortcut

data-slot="voice-selector-shortcut"

PropType
asChild?boolean

Use the provided child element as the default rendered element, combining their props and behavior.

AttributeType
data-invalid?string

Present when invalid is true.

data-readonly?string

Present when readOnly is true.

VoiceSelectorSeparator

data-slot="voice-selector-separator"

AttributeType
data-invalid?string

Present when invalid is true.

data-readonly?string

Present when readOnly is true.

VoiceSelectorGender

<span> data-slot="voice-selector-gender"

PropType
value?VoiceSelectorGenderValue

The voice's gender, drawn as its symbol and read out as its name.

className?string

Merged with the part's classes.

VoiceSelectorAccent

<span> data-slot="voice-selector-accent"

PropType
value?string

The accent, such as british: shown as its country's flag.

className?string

Merged with the part's classes.

VoiceSelectorAge

<span> data-slot="voice-selector-age"

PropType
className?string

Merged with the part's classes.

VoiceSelectorName

<span> data-slot="voice-selector-name"

PropType
className?string

Merged with the part's classes.

VoiceSelectorDescription

<span> data-slot="voice-selector-description"

PropType
className?string

Merged with the part's classes.

VoiceSelectorAttributes

<div> data-slot="voice-selector-attributes"

PropType
className?string

Merged with the part's classes.

VoiceSelectorBullet

<span> data-slot="voice-selector-bullet"

PropType
className?string

Merged with the part's classes.

VoiceSelectorPreview

data-slot="voice-selector-preview"

PropType
clickEffect?boolean

Apply a click effect to the button.

Default
true
isLoading?boolean

Show a loading indicator.

Default
false
label?string

The voice's name, which the button's label ends with: Play Alloy, Pause Alloy.

Default
"preview"
loading?boolean

Shows a spinner while the sample loads. The button keeps focus and ignores presses until it's done.

onPlay?() → void

Called on press. Play or pause the sample here.

Returns
void
pill?boolean

Fully rounded corners.

Default
false
playing?boolean

Shows pause instead of play while the sample plays.

size?"default" | "icon" | "icon-lg" | "icon-md" | "icon-sm" | "icon-xl" | "icon-xs" | "lg" | "md" | "sm" | "xl" | "xs"

The size.

Type
"default" | "icon" | "icon-lg" | "icon-md" | "icon-sm" | "icon-xl" | "icon-xs" | "lg" | "md" | "sm" | "xl" | "xs"
Default
"md"
variant?"default" | "destructive" | "ghost" | "link" | "outline" | "secondary"

The visual style.

Type
"default" | "destructive" | "ghost" | "link" | "outline" | "secondary"
Default
"default"
asChild?boolean

Use the provided child element as the default rendered element, combining their props and behavior.

className?string

Merged with the part's classes.

AttributeType
data-invalid?string

Present when invalid is true.

data-readonly?string

Present when readOnly is true.

useVoiceSelector