Skip to content

Made use of React.lazy and Suspense #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"presets": ["env", "react"],
"plugins": ["transform-class-properties", "transform-object-rest-spread"]
"plugins": ["transform-class-properties", "transform-object-rest-spread", "syntax-dynamic-import"]
}
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"author": "Sara Vieira",
"main": "dist/index.js",
"dependencies": {
"react": "^16.4.0",
"react-dom": "^16.4.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-scripts": "1.1.1",
"react-youtube": "^7.6.0",
"remcalc": "^1.0.10",
"styled-is": "^1.1.3"
Expand All @@ -27,8 +28,8 @@
},
"peerDependencies": {
"prop-types": "^15.6.1",
"react": "^16.4.0",
"react-dom": "^16.4.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-scripts": "1.1.1",
"styled-components": "^3.3.2"
},
Expand Down
25 changes: 6 additions & 19 deletions src/Components/Player.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import React, { Component } from "react"
import React, { Component, Suspense, lazy } from "react"
import styled from "styled-components"
import YouTube from "react-youtube"
import is, { isNot } from "styled-is"
import remcalc from "remcalc"
import PropTypes from "prop-types"
const YouTube = lazy(() => import('./Youtube'));

import Play from "./Play"

Expand Down Expand Up @@ -31,20 +30,6 @@ const VideoWrapper = styled.section`
`};
`

const Iframe = styled(YouTube)`
position: relative;
z-index: 3;
border: none;
transition: all 200ms ease;
height: 100%;
${is("cinemaMode")`
height: ${remcalc(600)};
@media (max-width: ${remcalc(768)}) {
height: auto;
}
`};
`

const Thumbnail = styled.img`
display: block;
width: 100%;
Expand Down Expand Up @@ -101,9 +86,10 @@ class Player extends Component {
<VideoWrapper {...props} style={styles}>
<Video>
{showVideo ? (
<Iframe
<Suspense fallback={<div>Loading...</div>}>
<YouTube
videoId={id}
id={`a-${id} do-not-delete-this-hack`}
id={`${id}`}
onReady={e => e.target.playVideo()}
onPlay={onPlay}
onPause={onPause}
Expand All @@ -120,6 +106,7 @@ class Player extends Component {
...playerVars
}}
/>
</Suspense>
) : (
<Image>
<Play onClick={this.showVideo} aria-label="Play Video" />
Expand Down
91 changes: 91 additions & 0 deletions src/Components/Youtube.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import React, { Component } from "react"
import styled from "styled-components";
import is from "styled-is";
import remcalc from "remcalc";
import ReactYouTube from "react-youtube"
import PropTypes from "prop-types"

const Iframe = styled(ReactYouTube)`
position: relative;
z-index: 3;
border: none;
transition: all 200ms ease;
height: 100%;
${is("cinemaMode")`
height: ${remcalc(600)};
@media (max-width: ${remcalc(768)}) {
height: auto;
}
`};
`;

class YouTube extends Component {

render () {

const {
id,
onPlay,
onPause,
onEnd,
onError,
onStateChange,
onPlaybackRateChange,
onPlaybackQualityChange,
playerVars,
noCookies,
...props
} = this.props

return (
<Iframe
videoId={id}
id={id}
onReady={e => e.target.playVideo()}
onPlay={onPlay}
onPause={onPause}
onEnd={onEnd}
onError={onError}
onStateChange={onStateChange}
onPlaybackRateChange={onPlaybackRateChange}
onPlaybackQualityChange={onPlaybackQualityChange}
opts={{
width: "100%",
host: noCookies
? "https://www.youtube-nocookie.com"
: "https://www.youtube.com",
...playerVars
}}
/>
)

}
}

export default YouTube

YouTube.propTypes = {
/** ID of the youtube video to play . */
id: PropTypes.string.isRequired,
/** .function to run when the video starts Playing */
onPlay: PropTypes.func,
/** .Function that runs when the video is paused */
onPause: PropTypes.func,
/** . Functinn that runs on the end of the video */
onEnd: PropTypes.func,
/** .Function that runs when the video encounters an error */
onError: PropTypes.func,
/** .Function that runs when the video changes state like from playing to paused */
onStateChange: PropTypes.func,
/** .Function that runs when the video encounters changes playback rater */
onPlaybackRateChange: PropTypes.func,
/** .Function that runs when the video changes quality */
onPlaybackQualityChange: PropTypes.func,
/** https://developers.google.com/youtube/player_parameters */
playerVars: PropTypes.object,
/** .Styles to apply over the wrappr */
styles: PropTypes.object,
/** .if set to true will change the host to "https://www.youtube-nocookie.com" */
noCookies: PropTypes.bool,
/** .Size of the thumbnail we get from youtube */
};