react-player-controls
This is a minimal set of modular, tested and hopefully useful React components for composing media player interfaces. It is designed for you to compose media player controls yourself using a small and easy-to-learn API.
👴 Check the old README for version 0.5.x, which contained more pre-baked components, if you are still on that version. 👵
From a library point of view, creating and providing components like <Player />
or <ProgressBar />
tends to result in abstractions with tons of props, often preventing arbitrary customisation, whilst providing little real value. These abstractions prove especially hindering when it comes to styling child elements. Therefor, instead of shipping these composite components, there is a collection of recipies that you can more or less copy-paste right into your project. Along with these plain components are a few boilerplate sets of styles in different forms that you can use if you want.
You can see the base components in action on the examples page.
⚠️ NOTE: This library does not deal with actual media in any way, only the UI. ⚠️
Table of contents
Installation
npm i react-player-controls
Usage
// ES2015+ import // Using CommonJSconst Slider Direction =
API
<Button />
<Button />
is basically a simple HTML button
.
<Button onClick= > Click me</Button>
Prop name | Default value | Description |
---|---|---|
onClick |
- | Required. A callback function that is invoked when the button is clicked. |
isEnabled |
true |
Whether the button is enabled. Setting this to false will set the disabled attribute on the button element to true . |
className |
null |
A string to set as the HTML class attribute |
style |
{} |
Styles to set on the button element. |
children |
null |
Child elements. |
Direction
An enum describing a slider's active axis.
Key | Value |
---|---|
HORIZONTAL |
"HORIZONTAL" |
VERTICAL |
"VERTICAL" |
<FormattedTime />
<FormattedTime />
translates a number of seconds into the player-friendly format of m:ss
, or h:mm:ss
if the total time is one hour or higher.
// This will render -1:01:02<FormattedTime numSeconds=-3662 />
Prop name | Default value | Description |
---|---|---|
numSeconds |
0 |
A number of seconds, positive or negative |
className |
null |
A string to set as the HTML class attribute |
style |
{} |
Styles to set on the wrapping span element. |
<PlayerIcon />
<PlayerIcon />
is not really a component in itself, but a container of a number of icon components.
<PlayerIconPlay /><PlayerIconPause /><PlayerIconPrevious /><PlayerIconNext /><PlayerIconSoundOn /><PlayerIconSoundOff />
Any props passed to a <PlayerIcon.* />
component will be passed onto the underlying svg
element.
<Slider />
The <Slider />
helps you build things like volume controls and progress bars. Slightly counterintuitively, it does not take a value
prop, but expects you to keep track of this yourself and render whatever you want inside it.
What this component actually does is that it renders an element inside itself, on top of its children, which listens to mouse events and invokes change and intent callbacks with relative, normalised values based on those events.
<Slider direction=DirectionHORIZONTAL isEnabled onIntent= console onIntentStart= console onIntentEnd= console onChange= console onChangeStart= console onChangeEnd= console> /* Here we render whatever we want. Nothings is rendered by default. */</Slider>
Prop name | Default value | Description |
---|---|---|
direction |
Direction.HORIZONTAL |
The slider's direction |
isEnabled |
true |
Whether the slider is interactable |
onIntent |
() => {} |
A function that is invoked with the relative, normalised value at which the user is hovering (when not dragging). |
onIntentStart |
() => {} |
A function this is invoked with the relative, normalised value at which the user started hovering the slider (when not dragging). |
onIntentEnd |
() => {} |
A function this is invoked when the mouse left the slider area (when not dragging). |
onChange |
() => {} |
A function that is invoked with the latest relative, normalised value that the user has set by either clicking or dragging. |
onChangeStart |
() => {} |
A function that is invoked with the relative, normalised value at which the user started changing the slider's value. |
onChangeEnd |
() => {} |
A function that is invoked with the relative, normalised value at which the user stopped changing the slider's value. When the component unmounts, this function will be invoked with a value of null . |
children |
null |
Child elements. |
className |
null |
A string to set as the HTML class attribute. |
style |
{} |
Styles to set on the wrapping div element. |
overlayZIndex |
10 | The z-index of the invisible overlay that captures mouse events |
Recipies
Styled buttons with icons
// A base component that has base styles applied to itconst PlayerButton = <Button style= appearance: 'none' outline: 'none' border: 'none' borderRadius: 3 background: 'white' color: 'blue' '&:hover': 'color': 'lightblue' ...style ...props > children </Button> // Compose buttons with matching icons. Use whatever icon library// you want. If you don't have any particular logic for each of the// buttons, you might not need this abstraction.const PlayButton = <Button ...props><PlayerIconPlay /></Button>const PauseButton = <Button ...props><PlayerIconPause /></Button>const PreviousButton = <Button ...props><PlayerIconPrevious /></Button>const NextButton = <Button ...props><PlayerIconNext /></Button>
Styled slider
const WHITE_SMOKE = '#eee'const GRAY = '#878c88'const GREEN = '#72d687' // A colored bar that will represent the current valueconst SliderBar = <div style=Object /> // A handle to indicate the current valueconst SliderHandle = <div style=Object /> // A composite progress bar componentconst ProgressBar = <Slider isEnabled=isEnabled direction=direction onChange=/* store value somehow */ style= width: direction === DirectionHORIZONTAL ? 200 : 8 height: direction === DirectionHORIZONTAL ? 8 : 130 borderRadius: 4 background: WHITE_SMOKE transition: direction === DirectionHORIZONTAL ? 'width 0.1s' : 'height 0.1s' cursor: isEnabled === true ? 'pointer' : 'default' ...props > <SliderBar direction=direction value=value style= background: isEnabled ? GREEN : GRAY /> <SliderHandle direction=direction value=value style= background: isEnabled ? GREEN : GRAY /> </Slider> // Now use <ProgressBar /> somewhere<ProgressBar isEnabled direction=DirectionHORIZONTAL value=currentTime / currentSongduration onChange= />
Playback controls
const PlaybackControls = isPlaying onPlaybackChange hasPrevious onPrevious hasNext onNext <div> <Button disabled=hasPrevious === false onClick=onPrevious> <IconPrevious /> </Button> <Button onClick= > isPlaying ? <IconPause /> : <IconPlay /> </Button> <Button disabled=hasNext === false onClick=onNext> <IconNext /> </Button> </div> // Use PlaybackControls in a player context<PlaybackControls isPlaying=playerisPlaying onPlaybackChange= player hasPrevious=songs > 0 hasNext=songs < songslength - 1 onPrevious=player onNext=player/>
Progress bar with buffer
const Bar = <div style= height: 6 width: '100%' ...style > children </div> const ProgressBarWithBuffer = amountBuffered ...props <Slider direction=DirectionHORIZONTAL ...props > /* Background bar */ <Bar style= background: 'gray' width: '100%' /> /* Buffer bar */ <Bar style= background: 'silver' width: `%` /> /* Playtime bar */ <Bar style= background: 'blue' width: `%` /> </Slider> // Use buffer bar somewhere<ProgressBarWithBuffer amountBuffered=secondsBuffered / duration /* callback props etc *//>
Progress bar that shows the target time on hover
// Create a basic bar that represents timeconst TimeBar = <div style= height: 6 width: '100%' background: 'gray' > children </div> // Create a tooltip that will show the timeconst TimeTooltip = <div style= display: 'inline-block' position: 'absolute' bottom: '100%' transform: 'translateX(-50%)' padding: 8 borderRadius: 3 background: 'darkblue' color: 'white' fontSize: 12 fontWeight: 'bold' lineHeight: 16 textAlign: 'center' ...style > <FormattedTime numSeconds=numSeconds /> </div> // Create a component to keep track of user interactionsComponent static propTypes = duration: PropTypesnumberisRequired { superprops thisstate = // This will be a normalised value between 0 and 1, // or null when not hovered hoverValue: null thishandleIntent = thishandleIntent thishandleIntentEnd = thishandleIntentEnd } { this } { // Note that this might not be invoked if the user ends // a control change with the mouse outside of the slider // element, so you might want to do this inside a // onChangeEnd callback too. this } { const duration = thisprops const hoverValue = thisstate return <Slider direction=DirectionHORIZONTAL style= position: 'relative' onIntent=thishandleIntent onIntentEnd=thishandleIntentEnd > <TimeBar /> hoverValue !== null && <TimeTooltip numSeconds=hoverValue * duration style= left: `%` /> </Slider> } // Let's use it somewhere<BarWithTimeOnHover duration=videoduration />
Base CSS styles (as seen on the docs page)
/* Root slider component */ /* Bars – can be progress. value, buffer or whatever */ /* Slider handle */
Contribute
Contributions are very welcome, no matter your experience! Please submit a PR and we'll take it from there.