diff --git a/.babelrc b/.babelrc deleted file mode 100644 index 2f01e1d..0000000 --- a/.babelrc +++ /dev/null @@ -1,3 +0,0 @@ -{ - "presets": ["env"] -} \ No newline at end of file diff --git a/.gitignore b/.gitignore index e20b037..3c3629e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1 @@ -lib/index.js node_modules -*.log \ No newline at end of file diff --git a/.npmignore b/.npmignore index 5c457d7..701eeab 100644 --- a/.npmignore +++ b/.npmignore @@ -1 +1,2 @@ -docs \ No newline at end of file +docs +test diff --git a/lib/.gitkeep b/lib/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/package.json b/package.json index 4900690..19ae4d1 100644 --- a/package.json +++ b/package.json @@ -2,25 +2,22 @@ "name": "react-responsive-embed", "version": "2.0.0", "description": "Embed iframes responsively", - "main": "lib/index.js", + "main": "src/index.js", "scripts": { "test": "ava", - "prepublish": "npm run build", - "build": "babel src -d lib", "docs": "static-react docs/components/Page.js > docs/index.html" }, "author": "oilzilla ", "license": "MIT", "devDependencies": { - "ava": "^0.17.0", - "babel-cli": "^6.22.2", - "babel-preset-env": "^1.1.8", - "react-addons-test-utils": "^15.0", + "ava": "^0.22", "react-dom": "^15.0", - "static-react": "^3.2.0" + "react": "^15.0", + "static-react": "^3.2" }, "peerDependencies": { - "react": "^15.0" + "prop-types": "^15.0 ||^16.0", + "react": "^15.0 ||^16.0" }, "engines": { "node": ">=6.0" diff --git a/src/index.js b/src/index.js index 750aa46..f168590 100644 --- a/src/index.js +++ b/src/index.js @@ -1,58 +1,59 @@ -const React = require('react') -const PropTypes = React.PropTypes -const div = React.createElement.bind(React, 'div') -const iframe = React.createElement.bind(React, 'iframe') +const React = require('react'); +const PropTypes = require('prop-types'); +const div = React.createElement.bind(React, 'div'); +const iframe = React.createElement.bind(React, 'iframe'); const divStyle = { - position: 'relative', - height: 0, - overflow: 'hidden', - maxWidth: '100%' -} + position: 'relative', + height: 0, + overflow: 'hidden', + maxWidth: '100%' +}; const iframeStyle = { - position: 'absolute', - top: 0, - left: 0, - width: '100%', - height: '100%' -} + position: 'absolute', + top: 0, + left: 0, + width: '100%', + height: '100%' +}; /* * Turn `16:9` into `9 / 16` into `56.25%` * Turn `4:3` into `3 / 4` into `75%` */ const ratioToPercent = (ratio) => { - const [w, h] = ratio.split(':').map((num) => Number(num)) - return `${(h / w) * 100}%` -} + const [w, h] = ratio.split(':').map((num) => Number(num)); + + return `${h / w * 100}%`; +}; /* * Usage: */ const ResponsiveEmbed = (props) => { - const paddingBottom = ratioToPercent(props.ratio) - const style = Object.assign({}, divStyle, {paddingBottom}) - const iframeProps = Object.assign({frameBorder: 0}, props, {style: iframeStyle}) - return div({style}, - iframe(iframeProps) - ) -} + const ratio = props.ratio; + delete props.ratio; + + const paddingBottom = ratioToPercent(ratio); + const style = Object.assign({}, divStyle, {paddingBottom}); + const iframeProps = Object.assign({frameBorder: 0}, props, {style: iframeStyle}); + + return div({style}, iframe(iframeProps)); +}; ResponsiveEmbed.defaultProps = { - src: 'https://www.youtube.com/embed/dQw4w9WgXcQ', - ratio: '16:9' -} + src: 'https://www.youtube.com/embed/dQw4w9WgXcQ', + ratio: '16:9' +}; ResponsiveEmbed.propTypes = { - src: PropTypes.string, - ratio: (props, propName, componentName) => { - if (!/\d+:\d+/.test(props[propName])) { - return new Error( - 'Invalid ratio supplied to ResponsiveEmbed. Expected a string like "16:9" or any 2 numbers seperated by a colon' - ) + src: PropTypes.string, + ratio: (props, propName) => { + if (!/\d+:\d+/.test(props[propName])) { + return new Error('Invalid ratio supplied to ResponsiveEmbed. Expected a string like "16:9" or any 2 numbers seperated by a colon'); + } } - } -} +}; -module.exports = ResponsiveEmbed +module.exports = ResponsiveEmbed; diff --git a/test/index.test.js b/test/index.test.js index bce9458..41c7a59 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,6 +1,6 @@ const test = require('ava') const React = require('react') -const ReactTestUtils = require('react-addons-test-utils') +const ReactTestUtils = require('react-dom/test-utils') const ResponsiveEmbed = require('../src/index') test('ResponsiveEmbed renders OK', (t) => { @@ -46,4 +46,4 @@ test('ResponsiveEmbed renders with default props', (t) => { t.is(root.props.style.paddingBottom, '56.25%', 'Aspect ratio of 16x9 is manifest as a paddingBottom style of 56.25%') t.is(child.type, 'iframe', 'Child element is an iframe') t.is(child.props.src, 'https://www.youtube.com/embed/dQw4w9WgXcQ', 'Default `src` is used') -}) \ No newline at end of file +})