|
1 | 1 | # uc-image
|
2 | 2 |
|
| 3 | +We often see browser loads tons of offscreen images when we load a webpage. This contributes to higher data-usage and slower page load times. The modern browsers like Chrome and Firefox supports native lazy loading out-of-the-box. |
3 | 4 |
|
| 5 | +The `uc-image` component can be used to defer loading the images on all browsers, including the legacy ones. It checks for the [native lazy loading](https://caniuse.com/loading-lazy-attr) support. It fallbacks to [IntersectionObserver](https://caniuse.com/intersectionobserver) when native lazy loading is not supported by the browser. The IntersectionObserver API is pollyfilled for the Jurassic Era browsers like `IE11`. |
| 6 | + |
| 7 | +The component internally uses `picture` element. You can supply `source` nodes for the `Art Direction` as a slotted content. We haven't polyfilled the `picture` element so it will always fallback on to the `img` tag inside it on legacy browsers. |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +### Minimum required markup |
| 12 | + |
| 13 | +```html |
| 14 | +<uc-image src="https://placekitten.com/400/400"></uc-image> |
| 15 | +``` |
| 16 | + |
| 17 | +### Other optional attributes |
| 18 | + |
| 19 | +```html |
| 20 | +<uc-image src="https://placekitten.com/400/400" width="400" height="400" alt="A cute kitten" class="lazy-image"></uc-image> |
| 21 | +``` |
| 22 | + |
| 23 | +### Example of `img[srcset]` |
| 24 | + |
| 25 | +```html |
| 26 | +<uc-image src="https://developer.mozilla.org/static/img/favicon72.png" srcset="https://developer.mozilla.org/static/img/favicon144.png 2x" alt="MDN logo" width="410" height="410"></uc-image> |
| 27 | +``` |
| 28 | + |
| 29 | +### Example of `img[sizes]` |
| 30 | + |
| 31 | +```html |
| 32 | +<uc-image src="https://media.prod.mdn.mozit.cloud/attachments/2019/08/29/16864/c77b83ecdb4ce4a6507da42aa7e8585e/clock-demo-200px.png" srcset="https://media.prod.mdn.mozit.cloud/attachments/2019/08/29/16864/c77b83ecdb4ce4a6507da42aa7e8585e/clock-demo-200px.png 200w, https://media.prod.mdn.mozit.cloud/attachments/2019/07/15/16797/4c169938d5f923bfa4db5ee937f24ebe/clock-demo-400px.png 400w" sizes="(max-width: 600px) 200px, 50vw" alt="Clock"> |
| 33 | +</uc-image> |
| 34 | +``` |
| 35 | + |
| 36 | +### Source nodes for Art Direction |
| 37 | + |
| 38 | +Here, the `source` node will be rendered as a slotted content of the `picture` element. The attributes set on the `uc-image` tag will be applied to the `img` tag inside the `picture` element. |
| 39 | + |
| 40 | +```html |
| 41 | +<uc-image src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/painted-hand-298-332.jpg" width="415" height="415"> |
| 42 | + <source srcset="https://interactive-examples.mdn.mozilla.net/media/cc0-images/surfer-240-200.jpg" media="(min-width: 800px)"> |
| 43 | +</uc-image> |
| 44 | +``` |
4 | 45 |
|
5 | 46 | <!-- Auto Generated Below -->
|
6 | 47 |
|
7 | 48 |
|
8 | 49 | ## Properties
|
9 | 50 |
|
10 |
| -| Property | Attribute | Description | Type | Default | |
11 |
| -| ---------------------- | --------- | --------------------------------------------------------- | -------------------- | ----------- | |
12 |
| -| `as` | `as` | Render component as 'img' or 'picture'. Default is 'img'. | `"img" \| "picture"` | `'img'` | |
13 |
| -| `dataAlt` | `alt` | Text for image's alt attribute. | `string` | `undefined` | |
14 |
| -| `dataClass` | `class` | CSS classes to be added on the root (picture) element. | `string` | `undefined` | |
15 |
| -| `dataHeight` | `height` | Image height. | `number \| string` | `undefined` | |
16 |
| -| `dataSizes` | `sizes` | | `string` | `undefined` | |
17 |
| -| `dataSrc` _(required)_ | `src` | Image src. | `string` | `undefined` | |
18 |
| -| `dataSrcset` | `srcset` | | `string` | `undefined` | |
19 |
| -| `dataWidth` | `width` | | `number \| string` | `undefined` | |
| 51 | +| Property | Attribute | Description | Type | Default | |
| 52 | +| ---------------------- | --------- | ------------------------------------------------------------------------- | ------------------ | ----------- | |
| 53 | +| `dataAlt` | `alt` | Image alt text. | `string` | `undefined` | |
| 54 | +| `dataClass` | `class` | CSS class(es) to be added on the <img> tag inside the <picture> element. | `string` | `undefined` | |
| 55 | +| `dataHeight` | `height` | Image height. | `number \| string` | `undefined` | |
| 56 | +| `dataSizes` | `sizes` | sizes for img tag. | `string` | `undefined` | |
| 57 | +| `dataSrc` _(required)_ | `src` | Image URL. The value of this prop will be set on img tag's src attribute. | `string` | `undefined` | |
| 58 | +| `dataSrcset` | `srcset` | srcset for img tag. | `string` | `undefined` | |
| 59 | +| `dataWidth` | `width` | Image width. | `number \| string` | `undefined` | |
20 | 60 |
|
21 | 61 |
|
22 | 62 | ----------------------------------------------
|
|
0 commit comments