# Push to talk

A radio's push-to-talk key: it listens only while held, and letting go stops it. Nothing records by accident. For voice input to agents, walkie-talkie chat and dictation.

- Category: Instruments
- Docs: https://bevel-ui.vercel.app/components/push-to-talk
- Import from: `@/components/ui/push-to-talk`

## Install

```bash
npx shadcn@latest add @bevel/push-to-talk
```

npm dependencies (installed by the CLI): `cn`.

## Anatomy

```tsx
<PushToTalk talking={talking} onTalkingChange={setTalking}>
  <PushToTalkKey />
  <PushToTalkStatus />
</PushToTalk>
```

## Parts

| Part | Renders | Description |
| --- | --- | --- |
| `PushToTalk` | div | talking, onTalkingChange(talking), disabled. The radio body and speaker grille. |
| `PushToTalkKey` | button (aria-pressed) | Pressed only while held, with the pointer or Space. Releasing, leaving the window or losing focus stops it; Enter does nothing, since it can't be held. |
| `PushToTalkStatus` | p + status | The on-air lamp and its words: idle and live, “Not listening” and “Listening” by default. |

## Example: Voice notes to an agent

Hold the key, or Space, to record a note. Let go to send it.

```tsx
"use client"

import * as React from "react"
import { PushToTalk, PushToTalkKey, PushToTalkStatus } from "@/components/ui/push-to-talk"

/** Talk to the agent only while the key is held. Hold Space, or press and hold with the pointer. */
export function PushToTalkAgent() {
  const [talking, setTalking] = React.useState(false)
  const [seconds, setSeconds] = React.useState(0)
  const [clips, setClips] = React.useState<number[]>([])

  React.useEffect(() => {
    if (!talking) return
    const started = Date.now()
    const timer = setInterval(() => setSeconds((Date.now() - started) / 1000), 100)
    return () => {
      clearInterval(timer)
      setClips((c) => [...c.slice(-2), (Date.now() - started) / 1000])
      setSeconds(0)
    }
  }, [talking])

  return (
    <div className="flex flex-col items-center gap-4">
      <PushToTalk talking={talking} onTalkingChange={setTalking}>
        <PushToTalkKey />
        <PushToTalkStatus />
        {/* Shown, not announced: a live region that ticks would talk over you. */}
        <span aria-hidden className="h-4 font-mono text-xs tabular-nums opacity-70">
          {talking ? `${seconds.toFixed(1)}s` : ""}
        </span>
      </PushToTalk>
      <ul aria-label="Sent" className="flex flex-col items-center gap-1 text-sm text-muted-foreground">
        {clips.map((c, i) => (
          <li key={i}>Sent a {c.toFixed(1)}s voice note</li>
        ))}
      </ul>
    </div>
  )
}
```
