PlayButton
A button component for playing and pausing media playback
Anatomy
<PlayButton /><media-play-button></media-play-button>Behavior
PlayButton is a three-state button: play , pause , and replay . When media reaches the end (ended state), clicking restarts playback from the beginning.
Styling
Style with the [data-paused] and [data-ended] attributes to show/hide play/pause/replay icons based on state. For example:
/* Paused (but not ended) */
media-play-button[data-paused]:not([data-ended]) .play-icon { display: inline; }
/* Playing */
media-play-button:not([data-paused]) .pause-icon { display: inline; }
/* Ended */
media-play-button[data-ended] .replay-icon { display: inline; } After first play, the data-started attribute is added and remains present until a new source is loaded. Use this to hide the play button when media hasn’t started yet:
/* Hide play button before first play */
media-play-button:not([data-started]) .play-icon { display: none; }Accessibility
Renders a <button> element with an automatic aria-label that updates based on state: “Play”, “Pause”, or “Replay”. Override with the label prop (accepts a string or function). Keyboard activation: Enter / Space.
Examples
Basic Usage
import { createPlayer, features, PlayButton } from '@videojs/react';
import { Video } from '@videojs/react/video';
import './BasicUsage.css';
const Player = createPlayer({ features: [...features.video] });
export default function BasicUsage() {
return (
<Player.Provider>
<Player.Container className="react-play-button-basic">
<Video
src="https://stream.mux.com/lhnU49l1VGi3zrTAZhDm9LUUxSjpaPW9BL4jY25Kwo4/highest.mp4"
autoPlay
muted
playsInline
/>
<PlayButton
className="react-play-button-basic__button"
render={(props, state) => (
<button {...props}>{state.ended ? 'Replay' : state.paused ? 'Play' : 'Pause'}</button>
)}
/>
</Player.Container>
</Player.Provider>
);
}
.react-play-button-basic {
position: relative;
}
.react-play-button-basic video {
width: 100%;
}
.react-play-button-basic__button {
padding-block: 8px;
position: absolute;
bottom: 10px;
left: 10px;
background: rgba(255, 255, 255, 0.7);
backdrop-filter: blur(10px);
color: black;
border: 1px solid rgba(255, 255, 255, 0.3);
border-radius: 9999px;
padding-inline: 20px;
cursor: pointer;
}
<video-player class="html-play-button-basic">
<video
src="https://stream.mux.com/lhnU49l1VGi3zrTAZhDm9LUUxSjpaPW9BL4jY25Kwo4/highest.mp4"
autoplay
muted
playsinline
></video>
<media-play-button class="html-play-button-basic__button">
<span class="show-when-paused">Play</span>
<span class="show-when-playing">Pause</span>
<span class="show-when-ended">Replay</span>
</media-play-button>
</video-player>
.html-play-button-basic {
position: relative;
}
.html-play-button-basic video {
width: 100%;
}
.html-play-button-basic__button {
padding-block: 8px;
position: absolute;
bottom: 10px;
left: 10px;
background: rgba(255, 255, 255, 0.7);
backdrop-filter: blur(10px);
color: black;
border: 1px solid rgba(255, 255, 255, 0.3);
border-radius: 9999px;
padding-inline: 20px;
cursor: pointer;
}
.html-play-button-basic__button .show-when-paused {
display: none;
}
.html-play-button-basic__button .show-when-playing {
display: none;
}
.html-play-button-basic__button .show-when-ended {
display: none;
}
.html-play-button-basic__button[data-paused]:not([data-ended]) .show-when-paused {
display: inline;
}
.html-play-button-basic__button:not([data-paused]) .show-when-playing {
display: inline;
}
.html-play-button-basic__button[data-ended] .show-when-ended {
display: inline;
}
.html-play-button-basic__button[data-ended] .show-when-paused {
display: none;
}
.html-play-button-basic__button[data-ended] .show-when-playing {
display: none;
}
import '@videojs/html/video/player';
import '@videojs/html/ui/play-button';
API Reference
Props
| Prop | Type | Default | |
|---|---|---|---|
disabled | boolean | false | |
| |||
label | string | function | '' | |
| |||
State
State is accessible via the
render, className, and style props.
State is reflected as data attributes for CSS styling.
| Property | Type | |
|---|---|---|
paused | boolean | |
| ||
ended | boolean | |
| ||
started | boolean | |
| ||
Data attributes
| Attribute | Description |
|---|---|
data-paused | Present when the media is paused. |
data-ended | Present when the media has ended. |
data-started | Present when playback has started. |