# CastButton

Accessible Cast toggle button with state reflection and keyboard support

## Import

```tsx
import { CastButton } from '@videojs/react';
```

## Anatomy

```tsx
<CastButton />
```

## Behavior

Toggles a Google Cast session. Clicking the button while `disconnected` opens the Cast device picker; clicking while `connected` ends the session.

The button derives its presentation from `availability`:

- **`"unsupported"`** — the component returns `null`.
- **`"unavailable"`** — the button remains visible and focusable with `aria-disabled="true"` and `data-disabled`, but does not open the picker. This lets a tooltip explain that no Cast device is reachable.
- **`"available"`** — the button is fully interactive unless the `disabled` prop is set.

Configurable Cast sessions come from the [Google Cast extension](https://videojs.org/docs/framework/react/reference/components/google-cast). Add it to the player next to your media element; without it the button drives the browser’s native Remote Playback API instead, which offers no receiver or load-request configuration. See [Cast to AirPlay and Chromecast](https://videojs.org/docs/framework/react/guides/casting) for the complete setup.

## Styling

Style based on cast and disabled state:

React renders a `<button>` element. Add a `className` to style it:

```css
/* During an active session */
.cast-button[data-cast-state="connected"] {
  color: blue;
}

/* Cast is supported, but no device is reachable */
.cast-button[data-disabled] {
  cursor: not-allowed;
  opacity: 0.5;
}
```

Unsupported buttons are hidden automatically. No availability selector or extra hiding CSS is required.

## Accessibility

Renders a `<button>` with an automatic `aria-label`:

| Cast state | Default label |
| --- | --- |
| `'disconnected'` | “Start casting” |
| `'connecting'` | “Connecting” |
| `'connected'` | “Stop casting” |

Override with the `label` prop. Keyboard activation: Enter / Space.

## Examples

### Basic Usage

**App.tsx**

```tsx
import { CastButton, Container, createPlayer } from '@videojs/react';
import { GoogleCast } from '@videojs/react/extensions/google-cast';
import { HlsJsVideo } from '@videojs/react/media/hlsjs-video';
import { videoFeatures } from '@videojs/react/video';

const { Player } = createPlayer({ features: videoFeatures });

export default function BasicUsage() {
  return (
    <Player>
      <Container className="media-container">
        <HlsJsVideo src="https://stream.mux.com/BV3YZtogl89mg9VcNBhhnHm02Y34zI1nlMuMQfAbl3dM.m3u8" autoPlay muted playsInline loop />
        <GoogleCast />
        <CastButton
          className="media-cast-button"
          render={(props, state) => (
            <button {...props}>{state.connection === 'connected' ? 'Stop casting' : 'Start casting'}</button>
          )}
        />
      </Container>
    </Player>
  );
}
```

**App.css**

```css
.media-container {
  position: relative;
}

.media-container video {
  width: 100%;
}

.media-cast-button {
  position: absolute;
  right: 10px;
  bottom: 10px;
  padding-block: 8px;
  padding-inline: 20px;
  color: black;
  cursor: pointer;
  background: rgba(255, 255, 255, 0.7);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 9999px;
  backdrop-filter: blur(10px);
}

.media-cast-button[data-disabled] {
  cursor: not-allowed;
  opacity: 0.5;
  filter: grayscale(1);
}
```

## API Reference

### Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `disabled` | `boolean` | `false` | Whether the button is disabled. |
| `label` | `{ key: string; text: string } \| string \| ((state: CastButtonState) => Text \| string)` | `''` | Custom label for the button. |

### State

State is accessible via the `render`, `className`, and `style` props.

| Property | Type | Description |
| --- | --- | --- |
| `label` | `{ key: string; text: string } \| string` | |
| `connection` | `'disconnected' \| 'connecting' \| 'connected'` | Current cast connection state (`disconnected`, `connecting`, or `connected`). |
| `availability` | `'available' \| 'unavailable' \| 'unsupported'` | Whether casting is `available` (a device is reachable), `unavailable` (no device), or `unsupported`. |
| `disabled` | `boolean` | Non-interactive but still focusable (mirrors `aria-disabled`). |
| `hidden` | `boolean` | Whether the button is hidden because the feature is unsupported. |

### Data attributes

| Attribute | Type | Description |
| --- | --- | --- |
| `data-cast-state` | `'disconnected' \| 'connecting' \| 'connected'` | Current remote playback connection state. |
| `data-availability` | `'available' \| 'unavailable' \| 'unsupported'` | Whether remote playback can be requested on this platform. |
| `data-disabled` | — | Present when the button is non-interactive (mirrors `aria-disabled`). |
| `data-hidden` | — | Present when the button is hidden because the feature is unsupported. |

---

React documentation: https://videojs.org/docs/framework/react/llms.txt
All documentation: https://videojs.org/llms.txt
