Skip to content
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

Automatically set srcSet attributes #4

Merged
merged 1 commit into from
Nov 17, 2015
Merged
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
22 changes: 18 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export default class ReactImgix extends Component {
fluid: PropTypes.bool,
children: PropTypes.any,
customParams: PropTypes.object,
entropy: PropTypes.bool
entropy: PropTypes.bool,
generateSrcSet: PropTypes.bool
}
static defaultProps = {
precision: 100,
Expand All @@ -45,7 +46,8 @@ export default class ReactImgix extends Component {
faces: true,
fit: 'crop',
entropy: false,
auto: ['format']
auto: ['format'],
generateSrcSet: true
}
state = {
width: null,
Expand All @@ -64,6 +66,7 @@ export default class ReactImgix extends Component {

render () {
let src = ''
let srcSet = ''
let component = this.props.component

let width = this._findSizeForDimension('width')
Expand All @@ -78,14 +81,19 @@ export default class ReactImgix extends Component {
if (this.props.fit) fit = this.props.fit

if (this.state.mounted || this.props.aggresiveLoad) {
src = processImage(this.props.src, {
const srcOptions = {
auto: this.props.auto,
...this.props.customParams,
crop,
fit,
width,
height
})
}

src = processImage(this.props.src, srcOptions)
const dpr2 = processImage(this.props.src, {...srcOptions, dpr: 2})
const dpr3 = processImage(this.props.src, {...srcOptions, dpr: 3})
srcSet = `${dpr2} 2x, ${dpr3} 3x`
}

let childProps = {...this.props}
Expand All @@ -100,10 +108,16 @@ export default class ReactImgix extends Component {
backgroundSize: 'cover'
}
delete childProps.src
delete childProps.srcSet
} else {
if (!this.props.component) {
component = 'img'
}

if (component === 'img' && this.props.generateSrcSet) {
childProps.srcSet = srcSet
}

childProps.src = src
}
return React.createElement(component,
Expand Down
13 changes: 13 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,17 @@ describe('image props', () => {
// it.skip('custom props', () => {
// expect(vdom.props.src).to.include('auto=format,enhance')
// })
it('generateSrcSet prop', () => {
tree = sd.shallowRender(
<Imgix
src={src}
aggresiveLoad
generateSrcSet
/>
)
vdom = tree.getRenderOutput()

expect(vdom.props.srcSet).toInclude('dpr=2')
expect(vdom.props.srcSet).toInclude('dpr=3')
})
})