Skip to content
Open
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
11 changes: 7 additions & 4 deletions lib/Picture.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const React = require('react')
const h = React.createElement

const Picture = React.forwardRef(function Picture({ src, sizes, breakpoints, ...imgProps }, ref) {

if (!src) {
return null
}
Expand Down Expand Up @@ -49,7 +50,7 @@ const Picture = React.forwardRef(function Picture({ src, sizes, breakpoints, ...
return h(
'picture',
{},
flattendSrc.map(({ img, sizes, breakpoints, maxWidth }, i) =>
flattendSrc.map(({ img, sizes, breakpoints, media }, i) =>
h(
React.Fragment,
{ key: i },
Expand All @@ -59,15 +60,15 @@ const Picture = React.forwardRef(function Picture({ src, sizes, breakpoints, ...
type: 'image/webp',
srcSet: img.webpSrcSet,
sizes: makeSizes(img, sizes, breakpoints),
...(maxWidth ? { media: `(max-width: ${maxWidth}px)` } : {}),
media,
}),
img.srcSet &&
img.srcSet.length > 0 &&
h('source', {
type: img.type,
srcSet: img.srcSet,
sizes: makeSizes(img, sizes, breakpoints),
...(maxWidth ? { media: `(max-width: ${maxWidth}px)` } : {}),
media,
}),
),
),
Expand All @@ -86,11 +87,13 @@ function flattenSrc(src, sizes, breakpoints) {
continue
}

const breakpoint = typeof breakpoints[i] === 'number' ? `(max-width: ${breakpoints[i]}px)` : breakpoints[i]

result.push({
img: src[i],
sizes: sizes[i],
breakpoints: src.length === 1 ? breakpoints : [],
maxWidth: i === src.length - 1 ? null : breakpoints[i],
media: i === src.length - 1 ? null : breakpoint,
})
}

Expand Down