<media-cast-button>

The <media-cast-button> component is used to bring up the Cast menu and select playback on a Chromecast-enabled device and to stop casting once started.

The contents of the <media-cast-button> will update based on the availability of casting support and current casting state.

  • When the media is not currently casting, the enter slot will be shown.
  • When the media is currently casting, the exit slot will be shown.

NOTE: Casting support isn’t available in all browsers. Interacting with these examples may not do anything when support isn’t available.

<media-controller>
  <video
    playsinline muted crossorigin
    slot="media"
    src="https://stream.mux.com/A3VXy02VoUinw01pwyomEO3bHnG4P32xzV7u1j1FSzjNg/low.mp4"
  ></video>
  <media-cast-button></media-cast-button>
</media-controller>

You can modify the contents of the <media-cast-button> component using slots. This is useful if you’d like to use your own custom icons instead of the default ones provided by media-chrome.

Here’s an example of how you can replace the default icons with the words “Cast” and “Exit”.

<media-controller>
  <video
    playsinline muted crossorigin
    slot="media"
    src="https://stream.mux.com/A3VXy02VoUinw01pwyomEO3bHnG4P32xzV7u1j1FSzjNg/low.mp4"
  ></video>
  <media-cast-button>
    <span slot="enter">Cast</span>
    <span slot="exit">Exit</span>
  </media-cast-button>
</media-controller>

Alternatively, if you would like to represent both states using a single element you could use the icon slot instead. This is useful for creating an animated icon that transitions between states. Here’s a basic example that uses CSS to change an element based on the casting state.

.my-icon {
  border-radius: 4px;
  outline: 2px solid #fff;
  padding: 0 6px;
  transition: all .4s;
}

media-cast-button[mediaiscasting] .cast-icon {
  outline: 2px solid green;
  color: green;
}

The <media-cast-button> doesn’t expose any configuration attributes. However, it will be updated with Media UI Attributes any time the availability state changes.

You can use these attributes to style the button. For example, if casting is unavailable, hide the button:

media-cast-button[mediacastunavailable] {
  display: none;
}

Or set the background to blue, if the media is now casting:

media-cast-button[mediaiscasting] {
  --media-control-background: blue;
}
NameDescription
enterAn element shown when the media is not in casting mode and pressing the button will open the Cast menu.
exitAn element shown when the media is in casting mode and pressing the button will trigger exiting casting mode.
iconAn element for representing enter and exit states in a single icon
NameTypeDescription
disabledbooleanThe Boolean disabled attribute makes the element not mutable or focusable.
mediacontrollerstringThe element `id` of the media controller to connect to (if not nested within).

The media UI attributes will be set automatically by the controller if they are connected via nesting or the mediacontroller attribute. Only set these attributes manually if you know what you're doing.

NameTypeDescription
mediacastunavailable(unavailable|unsupported) Set if casting is unavailable.
mediaiscastingboolean Present if the media is casting.
NameDefaultDescription
--media-cast-button-displayinline-flexdisplay property of button.
--media-primary-colorrgb(238 238 238)Default color of text and icon.
--media-secondary-colorrgb(20 20 30 / .7)Default color of button background.
--media-text-colorvar(--media-primary-color, rgb(238 238 238))color of button text.
--media-icon-colorvar(--media-primary-color, rgb(238 238 238))fill color of button icon.
--media-control-displaydisplay property of control.
--media-control-backgroundvar(--media-secondary-color, rgb(20 20 30 / .7))background of control.
--media-control-hover-backgroundrgba(50 50 70 / .7)background of control hover state.
--media-control-padding10pxpadding of control.
--media-control-height24pxline-height of control.
--media-fontvar(--media-font-weight, bold) var(--media-font-size, 14px) / var(--media-text-content-height, var(--media-control-height, 24px)) var(--media-font-family, helvetica neue, segoe ui, roboto, arial, sans-serif)font shorthand property.
--media-font-weightboldfont-weight property.
--media-font-familyhelvetica neue, segoe ui, roboto, arial, sans-seriffont-family property.
--media-font-size14pxfont-size property.
--media-text-content-heightvar(--media-control-height, 24px)line-height of button text.
--media-button-icon-widthwidth of button icon.
--media-button-icon-heightvar(--media-control-height, 24px)height of button icon.
--media-button-icon-transformtransform of button icon.
--media-button-icon-transitiontransition of button icon.