|
1 | | -# react-responsive-picture |
2 | | -A future-proof responsive image component that supports latest Picture specification |
| 1 | +## react-responsive-picture |
| 2 | + |
| 3 | +A future-proof responsive image component that supports latest `<picture>` specification. Uses [picturefill](https://github.com/scottjehl/picturefill) for backward compatibility from IE9+. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +`npm install react-responsive-picture` || `yarn add react-responsive-picture` |
| 10 | + |
| 11 | +## How to use |
| 12 | + |
| 13 | +### Code |
| 14 | + |
| 15 | +```jsx |
| 16 | +import { Picture } from 'react-responsive-picture'; |
| 17 | + |
| 18 | +class App extends Component { |
| 19 | + render() { |
| 20 | + return ( |
| 21 | + <Picture |
| 22 | + sources = {[ |
| 23 | + { |
| 24 | + srcSet: "path-to-mobile-image.jpg, path-to-mobile-image@2x.jpg 2x", |
| 25 | + media: "(max-width: 420px)", |
| 26 | + }, |
| 27 | + { |
| 28 | + srcSet: "path-to-desktop-image.jpg 1x, path-to-desktop-image@2x.jpg 2x", |
| 29 | + }, |
| 30 | + { |
| 31 | + srcSet: "path-to-desktop-image.webp", |
| 32 | + type: "image/webp" |
| 33 | + } |
| 34 | + ]} |
| 35 | + /> |
| 36 | + ); |
| 37 | + }; |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +### Props |
| 42 | + |
| 43 | +| Prop | Type | Default | Definition | |
| 44 | +| --- | --- | --- | --- | |
| 45 | +| sources | array | | The array of source objects. Check Sources section for more information. | |
| 46 | +| src | string | (transparent pixel) | Source for standalone/fallback image. To prevent issues in some browsers, by default `src` is set to a 1x1 transparent pixel data image. | |
| 47 | +| sizes | string | | Sizes attribute to be used with `src` for determing best image for user's viewport. | |
| 48 | +| alt | string | | Alternative text for image | |
| 49 | +| className | string | | Any additional CSS classes you might want to use to style the image | |
| 50 | +| style | object \|\| array | | Any additional styles you might want to send to the wrapper. Uses glamor to process it so you can send either objects or arrays. | |
| 51 | + |
| 52 | +## Examples |
| 53 | + |
| 54 | +### Simple image |
| 55 | +Normal `<img>` like behaviour. The same image is displayed on every device/viewport. |
| 56 | + |
| 57 | +```jsx |
| 58 | +<Picture src="path-to-image.jpg" /> |
| 59 | +``` |
| 60 | + |
| 61 | +will render: |
| 62 | + |
| 63 | +```html |
| 64 | +<img srcset="path-to-image.jpg" /> |
| 65 | +``` |
| 66 | + |
| 67 | +### Image with different resolutions |
| 68 | +Different images for specific devices (usually retina). |
| 69 | + |
| 70 | +```jsx |
| 71 | +<Picture src="path-to-image@2x.png 2x, path-to-image.png 1x" /> |
| 72 | +``` |
| 73 | + |
| 74 | +will render: |
| 75 | + |
| 76 | +```html |
| 77 | +<img srcset="path-to-image@2x.png 2x, path-to-image.png 1x" /> |
| 78 | +``` |
| 79 | + |
| 80 | +### Image with sizes |
| 81 | +When you want to let the browser determine the best image for user's current viewport. More information about `size` attribute on this great [blog post](http://ericportis.com/posts/2014/srcset-sizes/). |
| 82 | + |
| 83 | +```jsx |
| 84 | +<Picture |
| 85 | + src="large.jpg 1024w, medium.jpg 640w, small.jpg 320w" |
| 86 | + sizes="(min-width: 36em) 33.3vw, 100vw" |
| 87 | +/> |
| 88 | +``` |
| 89 | + |
| 90 | +will render: |
| 91 | + |
| 92 | +```html |
| 93 | +<img srcset="large.jpg 1024w, medium.jpg 640w, small.jpg 320w" sizes="(min-width: 36em) 33.3vw, 100vw" /> |
| 94 | +``` |
| 95 | + |
| 96 | +### Image with art direction |
| 97 | +When you want to explicitly control which image is displayed at specific viewport sizes. |
| 98 | + |
| 99 | +```jsx |
| 100 | +<Picture |
| 101 | + sources = {[ |
| 102 | + { |
| 103 | + srcSet: "path-to-mobile-image.jpg, path-to-mobile-image@2x.jpg 2x", |
| 104 | + media: "(max-width: 420px)", |
| 105 | + }, |
| 106 | + { |
| 107 | + srcSet: "path-to-desktop-image.jpg 1x, path-to-desktop-image@2x.jpg 2x", |
| 108 | + }, |
| 109 | + { |
| 110 | + srcSet: "path-to-desktop-image.webp", |
| 111 | + type: "image/webp" |
| 112 | + } |
| 113 | + ]} |
| 114 | +/> |
| 115 | +``` |
| 116 | + |
| 117 | +will render: |
| 118 | + |
| 119 | +```html |
| 120 | +<picture> |
| 121 | + <source srcset="path-to-mobile-image.jpg, path-to-mobile-image@2x.jpg 2x" media="(max-width: 420px)"> |
| 122 | + <source srcset="path-to-desktop-image.jpg 1x, path-to-desktop-image@2x.jpg 2x"> |
| 123 | + <source srcset="path-to-desktop-image.webp" type="image/webp"> |
| 124 | + <img srcset="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" /> |
| 125 | +</picture> |
| 126 | +``` |
| 127 | + |
| 128 | +The `sources` prop is where you can determine the behaviour of the `<Picture>` component and which images will show for the specific screens. |
| 129 | + |
| 130 | +For each source you can send an object containing `srcSet`, `media` and `type` although the last two are optional. |
| 131 | + |
| 132 | +### Utilities |
| 133 | + |
| 134 | +There's also a `<FullsizePicture>` component that you can use to display full-size images using the same benefits of `<Picture>` for art direction. |
| 135 | + |
| 136 | +```jsx |
| 137 | +<div style={{ height: 200 }}> |
| 138 | + <FullsizePicture |
| 139 | + sources = {[ |
| 140 | + { |
| 141 | + srcSet: "path-to-mobile-image.jpg, path-to-mobile-image@2x.jpg 2x", |
| 142 | + media: "(max-width: 420px)", |
| 143 | + }, |
| 144 | + { |
| 145 | + srcSet: "path-to-desktop-image.jpg 1x, path-to-desktop-image@2x.jpg 2x", |
| 146 | + }, |
| 147 | + ]} |
| 148 | + /> |
| 149 | +</div> |
| 150 | +``` |
| 151 | + |
| 152 | +It will automatically fill the entire parent element maintaining the original image ratio. Please not that the parent element needs to have a defined height as you would expect for any background image as well. |
| 153 | + |
| 154 | +## Contributing |
| 155 | + |
| 156 | +Please follow our [contributing guidelines](https://github.com/EDITD/react-responsive-picture/blob/master/CONTRIBUTING.md). |
| 157 | + |
| 158 | +## License |
| 159 | + |
| 160 | +[MIT](https://github.com/EDITD/react-responsive-picture/blob/master/LICENSE) |
| 161 | + |
0 commit comments