Skip to content

Commit

Permalink
Support custom wrappers (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanforce authored and cookpete committed Feb 22, 2018
1 parent 10ff5cc commit 23c9faf
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Prop | Description | Default
`style` | Add [inline styles](https://facebook.github.io/react/tips/inline-styles.html) to the root element | `{}`
`progressInterval` | The time between `onProgress` callbacks, in milliseconds | `1000`
`playsinline` | Applies the `playsinline` attribute where supported | `false`
`wrapper` | Element or component to use as the container element | `div`
`config` | Override options for the various players, see [config prop](#config-prop)

#### Callback props
Expand Down Expand Up @@ -167,7 +168,7 @@ class ResponsivePlayer extends Component {
render () {
return (
<div className='player-wrapper'>
<ReactPlayer
<ReactPlayer
className='react-player'
url='https://www.youtube.com/watch?v=ysz5S6PUM-U'
width='100%'
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export interface ReactPlayerProps {
style?: Object;
progressInterval?: number;
playsinline?: boolean;
wrapper?: any;
config?: Config;
soundcloudConfig?: SoundCloudConfig;
youtubeConfig?: YouTubeConfig;
Expand Down
6 changes: 3 additions & 3 deletions src/ReactPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ export default class ReactPlayer extends Component {
return 0
}
render () {
const { url, style, width, height } = this.props
const { url, style, width, height, wrapper: Wrapper } = this.props
const otherProps = omit(this.props, SUPPORTED_PROPS, DEPRECATED_CONFIG_PROPS)
const activePlayer = this.renderActivePlayer(url)
const preloadPlayers = renderPreloadPlayers(url, this.config)
const players = [ activePlayer, ...preloadPlayers ].sort(this.sortPlayers)
return (
<div ref={this.wrapperRef} style={{ ...style, width, height }} {...otherProps}>
<Wrapper ref={this.wrapperRef} style={{ ...style, width, height }} {...otherProps}>
{players}
</div>
</Wrapper>
)
}
}
2 changes: 2 additions & 0 deletions src/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const propTypes = {
style: object,
progressInterval: number,
playsinline: bool,
wrapper: oneOfType([ string, func ]),
config: shape({
soundcloud: shape({
options: object
Expand Down Expand Up @@ -70,6 +71,7 @@ export const defaultProps = {
style: {},
progressInterval: 1000,
playsinline: false,
wrapper: 'div',
config: {
soundcloud: {
options: {
Expand Down
28 changes: 28 additions & 0 deletions test/specs/ReactPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,32 @@ describe('ReactPlayer', () => {
expect(ReactPlayer.canPlay('file.txt')).to.be.false
expect(ReactPlayer.canPlay([ 'http://example.com', 'file.txt' ])).to.be.false
})

describe('wrapper prop', () => {
it('defaults wrapper to a div', () => {
renderPlayer({
url: 'http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4'
})
expect(player.wrapper).to.be.a('HTMLDivElement')
})

it('supports custom wrapper elements', () => {
renderPlayer({
wrapper: 'p',
url: 'http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4'
})
expect(player.wrapper).to.be.a('HTMLParagraphElement')
})

it('supports custom wrapper components', () => {
const CustomWrapper = ({ children }) => <div id='test-hook' data-fake-attribute='woah'>{children}</div>
renderPlayer({
wrapper: CustomWrapper,
url: 'http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4'
})
const el = document.getElementById('test-hook')
expect(el.dataset.fakeAttribute).to.equal('woah')
expect(el.querySelectorAll('video').length).to.equal(1)
})
})
})

0 comments on commit 23c9faf

Please sign in to comment.