> ## Documentation Index
> Fetch the complete documentation index at: https://docs.beyondwords.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Download Button

> Downloads the current item's audio as an MP3.

export const DownloadButtonDemo = () => {
  const MOCK_CONTENT = [{
    id: "demo-1",
    title: "How AI voices are changing publishing",
    imageUrl: "/images/player-ui/demo-cover.svg",
    sourceUrl: "https://beyondwords.io",
    sourceId: "demo",
    adsEnabled: false,
    duration: 124,
    audio: [{
      id: "audio-1",
      url: "/images/player-ui/demo-silent.mp3",
      contentType: "audio/mpeg",
      duration: 124
    }],
    video: [],
    summarization: {
      audio: [],
      video: []
    },
    segments: []
  }];
  const newEvent = props => ({
    id: crypto.randomUUID(),
    createdAt: new Date().toISOString(),
    status: "pending",
    initiatedBy: "user",
    ...props
  });
  const [target, setTarget] = useState(null);
  const [player, setPlayer] = useState(null);
  const [content, setContent] = useState([]);
  const [contentIndex, setContentIndex] = useState(0);
  useEffect(() => {
    if (!target) return;
    let instance;
    const init = () => {
      instance = new window.BeyondWords.Player({
        target,
        content: MOCK_CONTENT,
        widgetStyle: "none",
        showUserInterface: false,
        analyticsConsent: "none"
      });
      const sync = () => {
        setContent(instance.content);
        setContentIndex(instance.contentIndex);
      };
      instance.addEventListener("<any>", sync);
      sync();
      setPlayer(instance);
    };
    if (window.BeyondWords) init(); else window.addEventListener("BeyondWordsReady", init);
    return () => {
      window.removeEventListener("BeyondWordsReady", init);
      if (instance) instance.destroy();
    };
  }, [target]);
  return <div className="not-prose flex w-full items-center justify-center rounded-lg border p-10" style={{
    minHeight: "16rem",
    fontFamily: 'ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif'
  }}>
      <div ref={setTarget} style={{
    display: "none"
  }} />
      {player && <button type="button" aria-label="Download audio" onClick={() => {
    const audioIndex = content[contentIndex]?.audio?.findIndex(a => a.url?.endsWith(".mp3"));
    if (typeof audioIndex !== "number" || audioIndex === -1) return;
    player.onEvent(newEvent({
      type: "PressedDownload",
      description: "The download button was pressed.",
      contentIndex,
      audioIndex
    }));
  }}>
          <svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path d="M12 3.9v9.9m0 0 4.2-4.2M12 13.8 7.8 9.6" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
            <path d="M4.4 15.7v2a2.4 2.4 0 0 0 2.4 2.4h10.4a2.4 2.4 0 0 0 2.4-2.4v-2" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
          </svg>
        </button>}
    </div>;
};

<Tabs>
  <Tab title="Preview">
    <DownloadButtonDemo />
  </Tab>

  <Tab title="Code">
    ```jsx DownloadButton.jsx theme={null}
    "use client";

    import { newEvent } from "@/lib/newEvent";
    import { DownloadIcon } from "@/registry/react/Icons";
    import { usePlayer, usePlayerSelector } from "@/registry/react/PlayerProvider";

    export function DownloadButton() {
      const player = usePlayer();
      const content = usePlayerSelector((p) => p.content);
      const contentIndex = usePlayerSelector((p) => p.contentIndex);
      return (
        <button
          type="button"
          aria-label="Download audio"
          onClick={() => {
            const audioIndex = content[contentIndex]?.audio?.findIndex((a) => a.url?.endsWith(".mp3"));
            if (typeof audioIndex !== "number" || audioIndex === -1) return;
            player.onEvent(
              newEvent({
                type: "PressedDownload",
                description: "The download button was pressed.",
                contentIndex,
                audioIndex,
              }),
            );
          }}
        >
          <DownloadIcon size={20} />
        </button>
      );
    }
    ```
  </Tab>
</Tabs>

## Installation

<CodeGroup>
  ```bash npm theme={null}
  npx @beyondwords/cli components add download-button
  ```

  ```bash pnpm theme={null}
  pnpm dlx @beyondwords/cli components add download-button
  ```

  ```bash yarn theme={null}
  yarn dlx @beyondwords/cli components add download-button
  ```

  ```bash bun theme={null}
  bunx --bun @beyondwords/cli components add download-button
  ```
</CodeGroup>

## Usage

```tsx theme={null}
import { PlayerProvider } from '@/registry/react/PlayerProvider';
import { DownloadButton } from '@/registry/react/DownloadButton';

export function Player() {
  return (
    <PlayerProvider args={{ projectId: 9504, contentId: "ba00ef51-03ac-4a85-8b98-e45c957e8ef8", showUserInterface: false }}>
      <DownloadButton />
    </PlayerProvider>
  );
}
```

On click the button finds the current item's MP3 audio and emits a
`PressedDownload` event (built with [`newEvent`](/player-ui/react/new-event))
carrying the `contentIndex` and `audioIndex`, on the surrounding
[`PlayerProvider`](/player-ui/react/player-provider). If the current item has no MP3,
the click is a no-op.

## Reference

|                       |                                                                 |
| --------------------- | --------------------------------------------------------------- |
| **Props**             | None                                                            |
| **Player state read** | `content`, `contentIndex`                                       |
| **Events emitted**    | `PressedDownload`                                               |
| **Requires**          | a [`PlayerProvider`](/player-ui/react/player-provider) ancestor |
