UNPKG

1.4 kBJavaScriptView Raw
1import React from 'react'
2import PropTypes from 'prop-types'
3
4import Sizes from './sizes.base'
5import SourceSet from './sourceSet.base'
6
7const InlineImage = ({ alt, src, sizes: inSizes, srcSet: inSources, lazyLoad, ...props }) => {
8 let srcSet = undefined
9 if (inSources) {
10 srcSet = new SourceSet(inSources).toString()
11 }
12 let sizesStr = undefined
13 if (inSizes) {
14 sizesStr = new Sizes(inSizes).toString()
15 }
16 if (!lazyLoad) {
17 return (
18 <img
19 alt={alt}
20 src={src}
21 srcSet={srcSet}
22 sizes={sizesStr}
23 {...props} />
24 )
25 } else {
26 return (
27 <img
28 alt={alt}
29 data-src={src}
30 srcSet={srcSet}
31 sizes={sizesStr}
32 {...props} />
33 )
34 }
35}
36
37InlineImage.defaultProps = {
38 alt: ''
39}
40
41InlineImage.propTypes = {
42 alt: PropTypes.string.isRequired,
43 lazyLoad: PropTypes.string,
44 src: PropTypes.string.isRequired,
45 sizes: PropTypes.object,
46 srcSet: PropTypes.oneOfType([
47 (props, propName, componentName) => {
48 if (props[propName] && !props['sizes']) {
49 console.warn(
50 `You have provided ${propName}, but not defined the sizes prop, this will cause your InlineImages for be sized to 100vw if a \`width\` css property is not set.`
51 )
52 }
53 },
54 PropTypes.object
55 ])
56}
57
58/** @component */
59export default InlineImage