Skip to content

Commit 8acfc8a

Browse files
feat(components)!: src an srcSet props for lazy loading
1 parent 43f26bd commit 8acfc8a

File tree

17 files changed

+134
-78
lines changed

17 files changed

+134
-78
lines changed

docs/integrations/nuxt.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,46 +78,50 @@ The `UnLazyImage` component accepts the following props:
7878

7979
| Prop | Type | Description |
8080
| --- | --- | --- |
81+
| `src` | String | Image source URL to be lazy-loaded. |
82+
| `srcSet` | String | Image source set to be lazy-loaded. |
8183
| `autoSizes` | Boolean | A flag to indicate whether the sizes attribute should be automatically calculated. |
8284
| `thumbhash` | String | A ThumbHash string representing the blurry placeholder image. |
8385
| `blurhash` | String | A BlurHash string representing the blurry placeholder image. |
86+
| `placeholderSrc` | String | Optional image source URL for a custom placeholder image. Will be ignored if a BlurHash or ThumbHash is provided. |
8487
| `placeholderSize` | Number | The size of the longer edge (width or height) of the BlurHash image to be decoded, depending on the aspect ratio. This option only applies when the `blurhash` prop is used. |
8588
| `placeholderRatio` | Number | Aspect ratio (width / height) of the decoded BlurHash image. Only applies to SSR-decoded placeholder images from a BlurHash string. |
8689
| `lazyLoad` | Boolean | A flag to indicate whether the image should be lazy-loaded (default) or deferred until this prop is set to `true`. Note: Placeholder images from hashes will still be decoded. |
8790
| `ssr` | Boolean | Whether the ThumbHash or BlurHash should be decoded on the server. Overrides the global module configuration if set. |
8891

8992
## Examples
9093

91-
::: info
92-
In each example, the `sizes` attribute is automatically calculated given the `auto-sizes` prop.
93-
:::
94-
9594
```html
9695
<!-- Inlined placeholder image as data URI -->
9796
<UnLazyImage
98-
src="data:image/svg+xml, ..."
99-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
97+
placeholder-src="data:image/svg+xml, ..."
98+
src-set="image-320w.jpg 320w, image-640w.jpg 640w"
10099
auto-sizes
101100
/>
102101
```
103102

103+
::: tip
104+
In every `srcSet` example, the `sizes` attribute is automatically calculated given the `auto-sizes` prop.
105+
:::
106+
104107
### BlurHash
105108

106109
::: code-group
107110
```html [SSR-decoded BlurHash]
108111
<UnLazyImage
109112
:blurhash="blurhash"
110113
:blurhash-ratio="2"
111-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
114+
src-set="image-320w.jpg 320w, image-640w.jpg 640w"
112115
width="640"
113116
height="320"
114117
/>
115118
```
116119
```html [Client-side decoded BlurHash]
117120
<UnLazyImage
118-
:blurhash="blurhash"
119121
:ssr="false"
120-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
122+
:blurhash="blurhash"
123+
src-set="image-320w.jpg 320w, image-640w.jpg 640w"
124+
auto-sizes
121125
width="640"
122126
height="320"
123127
/>
@@ -130,16 +134,18 @@ In each example, the `sizes` attribute is automatically calculated given the `au
130134
```html [SSR-decoded ThumbHash]
131135
<UnLazyImage
132136
:thumbhash="thumbhash"
133-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
137+
src-set="image-320w.jpg 320w, image-640w.jpg 640w"
138+
auto-sizes
134139
width="640"
135140
height="320"
136141
/>
137142
```
138143
```html [Client-side decoded ThumbHash]
139144
<UnLazyImage
140-
:thumbhash="thumbhash"
141145
:ssr="false"
142-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
146+
:thumbhash="thumbhash"
147+
src-set="image-320w.jpg 320w, image-640w.jpg 640w"
148+
auto-sizes
143149
width="640"
144150
height="320"
145151
/>

docs/integrations/react.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ import { UnLazyImage } from '@unlazy/react'
2626
export default function MyComponent() {
2727
return (
2828
<UnLazyImage
29+
blurhash="LKO2:N%2Tw=w]~RBVZRi};RPxuwH"
30+
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
2931
autoSizes
30-
src="data:image/svg+xml, ..."
31-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
3232
/>
3333
)
3434
}
@@ -46,43 +46,46 @@ The `UnLazyImage` component accepts the following props:
4646

4747
| Prop | Type | Description |
4848
| --- | --- | --- |
49+
| `src` | String | Image source URL to be lazy-loaded. |
50+
| `srcSet` | String | Image source set to be lazy-loaded. |
4951
| `autoSizes` | Boolean | A flag to indicate whether the sizes attribute should be automatically calculated. |
5052
| `blurhash` | String | A BlurHash string representing the blurry placeholder image. |
5153
| `thumbhash` | String | A ThumbHash string representing the blurry placeholder image. |
54+
| `placeholderSrc` | String | Optional image source URL for a custom placeholder image. Will be ignored if a BlurHash or ThumbHash is provided. |
5255
| `placeholderSize` | Number | The size of the longer edge (width or height) of the BlurHash image to be decoded, depending on the aspect ratio. This option only applies when the `blurhash` prop is used. |
5356

5457
## Examples
5558

56-
::: info
57-
In each example, the `sizes` attribute is automatically calculated given the `auto-sizes` prop.
58-
:::
59-
6059
::: code-group
6160
```tsx [BlurHash]
6261
return (
6362
<UnLazyImage
6463
blurhash="LKO2:N%2Tw=w]~RBVZRi};RPxuwH"
64+
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
6565
autoSizes
66-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
6766
/>
6867
)
6968
```
7069
```tsx [ThumbHash]
7170
return (
7271
<UnLazyImage
7372
thumbhash="1QcSHQRnh493V4dIh4eXh1h4kJUI"
73+
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
7474
autoSizes
75-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
7675
/>
7776
)
7877
```
7978
```tsx [Inlined placeholder image]
8079
return (
8180
<UnLazyImage
81+
placeholderSrc="data:image/svg+xml, ..."
82+
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
8283
autoSizes
83-
src="data:image/svg+xml, ..."
84-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
8584
/>
8685
)
8786
```
8887
:::
88+
89+
::: tip
90+
In each example, the `sizes` attribute is automatically calculated given the `auto-sizes` prop.
91+
:::

docs/integrations/solid.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ import { UnLazyImage } from '@unlazy/solid'
2626
export default function MyComponent() {
2727
return (
2828
<UnLazyImage
29+
blurhash="LKO2:N%2Tw=w]~RBVZRi};RPxuwH"
30+
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
2931
autoSizes
30-
src="data:image/svg+xml, ..."
31-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
3232
/>
3333
)
3434
}
@@ -46,43 +46,46 @@ The `UnLazyImage` component accepts the following props:
4646

4747
| Prop | Type | Description |
4848
| --- | --- | --- |
49+
| `src` | String | Image source URL to be lazy-loaded. |
50+
| `srcSet` | String | Image source set to be lazy-loaded. |
4951
| `autoSizes` | Boolean | A flag to indicate whether the sizes attribute should be automatically calculated. |
5052
| `blurhash` | String | A BlurHash string representing the blurry placeholder image. |
5153
| `thumbhash` | String | A ThumbHash string representing the blurry placeholder image. |
54+
| `placeholderSrc` | String | Optional image source URL for a custom placeholder image. Will be ignored if a BlurHash or ThumbHash is provided. |
5255
| `placeholderSize` | Number | The size of the longer edge (width or height) of the BlurHash image to be decoded, depending on the aspect ratio. This option only applies when the `blurhash` prop is used. |
5356

5457
## Examples
5558

56-
::: info
57-
In each example, the `sizes` attribute is automatically calculated given the `auto-sizes` prop.
58-
:::
59-
6059
::: code-group
6160
```tsx [BlurHash]
6261
return (
6362
<UnLazyImage
6463
blurhash="LKO2:N%2Tw=w]~RBVZRi};RPxuwH"
64+
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
6565
autoSizes
66-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
6766
/>
6867
)
6968
```
7069
```tsx [ThumbHash]
7170
return (
7271
<UnLazyImage
7372
thumbhash="1QcSHQRnh493V4dIh4eXh1h4kJUI"
73+
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
7474
autoSizes
75-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
7675
/>
7776
)
7877
```
7978
```tsx [Inlined placeholder image]
8079
return (
8180
<UnLazyImage
81+
placeholderSrc="data:image/svg+xml, ..."
82+
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
8283
autoSizes
83-
src="data:image/svg+xml, ..."
84-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
8584
/>
8685
)
8786
```
8887
:::
88+
89+
::: tip
90+
In each example, the `sizes` attribute is automatically calculated given the `auto-sizes` prop.
91+
:::

docs/integrations/svelte.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ Import the `UnLazyImage` component in your component file:
2626
</script>
2727
2828
<UnLazyImage
29+
blurhash="LKO2:N%2Tw=w]~RBVZRi};RPxuwH"
30+
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
2931
autoSizes
30-
src="data:image/svg+xml, ..."
31-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
3232
/>
3333
```
3434

@@ -44,37 +44,40 @@ The `UnLazyImage` component accepts the following props:
4444

4545
| Prop | Type | Description |
4646
| --- | --- | --- |
47+
| `src` | String | Image source URL to be lazy-loaded. |
48+
| `srcSet` | String | Image source set to be lazy-loaded. |
4749
| `autoSizes` | Boolean | A flag to indicate whether the sizes attribute should be automatically calculated. |
4850
| `blurhash` | String | A BlurHash string representing the blurry placeholder image. |
4951
| `thumbhash` | String | A ThumbHash string representing the blurry placeholder image. |
52+
| `placeholderSrc` | String | Optional image source URL for a custom placeholder image. Will be ignored if a BlurHash or ThumbHash is provided. |
5053
| `placeholderSize` | Number | The size of the longer edge (width or height) of the BlurHash image to be decoded, depending on the aspect ratio. This option only applies when the `blurhash` prop is used. |
5154

5255
## Examples
5356

54-
::: info
55-
In each example, the `sizes` attribute is automatically calculated given the `auto-sizes` prop.
56-
:::
57-
5857
::: code-group
5958
```svelte [BlurHash]
6059
<UnLazyImage
6160
blurhash="LKO2:N%2Tw=w]~RBVZRi};RPxuwH"
61+
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
6262
autoSizes
63-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
6463
/>
6564
```
6665
```svelte [ThumbHash]
6766
<UnLazyImage
6867
thumbhash="1QcSHQRnh493V4dIh4eXh1h4kJUI"
68+
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
6969
autoSizes
70-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
7170
/>
7271
```
7372
```svelte [Inlined placeholder image]
7473
<UnLazyImage
74+
placeholderSrc="data:image/svg+xml, ..."
75+
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
7576
autoSizes
76-
src="data:image/svg+xml, ..."
77-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
7877
/>
7978
```
8079
:::
80+
81+
::: tip
82+
In each example, the `sizes` attribute is automatically calculated given the `auto-sizes` prop.
83+
:::

docs/integrations/vue.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import { UnLazyImage } from '@unlazy/vue/components'
2929
3030
<template>
3131
<UnLazyImage
32-
src="data:image/svg+xml, ..."
33-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
32+
blurhash="LKO2:N%2Tw=w]~RBVZRi};RPxuwH"
33+
src-set="image-320w.jpg 320w, image-640w.jpg 640w"
3434
auto-sizes
3535
/>
3636
</template>
@@ -64,34 +64,35 @@ The `UnLazyImage` component accepts the following props:
6464
| `autoSizes` | Boolean | A flag to indicate whether the sizes attribute should be automatically calculated. |
6565
| `blurhash` | String | A BlurHash string representing the blurry placeholder image. |
6666
| `thumbhash` | String | A ThumbHash string representing the blurry placeholder image. |
67+
| `placeholderSrc` | String | Optional image source URL for a custom placeholder image. Will be ignored if a BlurHash or ThumbHash is provided. |
6768
| `placeholderSize` | Number | The size of the longer edge (width or height) of the BlurHash image to be decoded, depending on the aspect ratio. This option only applies when the `blurhash` prop is used. |
6869

6970
## Examples
7071

71-
::: info
72-
In each example, the `sizes` attribute is automatically calculated given the `auto-sizes` prop.
73-
:::
74-
7572
::: code-group
76-
```vue [BlurHash]
73+
```html [BlurHash]
7774
<UnLazyImage
78-
:blurhash="blurhash"
79-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
75+
blurhash="LKO2:N%2Tw=w]~RBVZRi};RPxuwH"
76+
src-set="image-320w.jpg 320w, image-640w.jpg 640w"
8077
auto-sizes
8178
/>
8279
```
83-
```vue [ThumbHash]
80+
```html [ThumbHash]
8481
<UnLazyImage
85-
:thumbhash="thumbhash"
86-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
82+
thumbhash="1QcSHQRnh493V4dIh4eXh1h4kJUI"
83+
src-set="image-320w.jpg 320w, image-640w.jpg 640w"
8784
auto-sizes
8885
/>
8986
```
90-
```vue [Inlined placeholder image]
87+
```html [Inlined placeholder image]
9188
<UnLazyImage
92-
src="data:image/svg+xml, ..."
93-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
89+
placeholder-src="data:image/svg+xml, ..."
90+
src-set="image-320w.jpg 320w, image-640w.jpg 640w"
9491
auto-sizes
9592
/>
9693
```
9794
:::
95+
96+
::: tip
97+
In each example, the `sizes` attribute is automatically calculated given the `auto-sizes` prop.
98+
:::

packages/react/playground/App.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ function App() {
55
<>
66
<UnLazyImage
77
blurhash="LKO2:N%2Tw=w]~RBVZRi};RPxuwH"
8+
srcSet="image-320w.jpg 320w, image-640w.jpg 640w"
89
autoSizes={true}
9-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
1010
width="640"
1111
height="640"
1212
/>
1313
<UnLazyImage
1414
thumbhash="1QcSHQRnh493V4dIh4eXh1h4kJUI"
15-
autoSizes={true}
16-
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
15+
src="/images/sunrise-evan-wallace.jpg"
1716
width="480"
1817
height="640"
1918
/>
2.48 KB
Loading

packages/react/src/index.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import type { UnLazyLoadOptions } from 'unlazy'
66
interface Props
77
extends ImgHTMLAttributes<HTMLImageElement>,
88
Pick<UnLazyLoadOptions, 'placeholderSize'> {
9+
/** Image source URL to be lazy-loaded. */
10+
src?: ImgHTMLAttributes<HTMLImageElement>['src']
11+
/** Image source set to be lazy-loaded. */
12+
srcSet?: ImgHTMLAttributes<HTMLImageElement>['srcSet']
913
/**
1014
* A flag to indicate whether the sizes attribute should be automatically calculated.
1115
* @default false
@@ -15,12 +19,17 @@ interface Props
1519
blurhash?: string
1620
/** A ThumbHash string representing the blurry placeholder image. */
1721
thumbhash?: string
22+
/** Optional image source URL for a custom placeholder image. Will be ignored if a BlurHash or ThumbHash is provided. */
23+
placeholderSrc?: string
1824
}
1925

2026
export function UnLazyImage({
27+
src,
28+
srcSet,
2129
autoSizes,
2230
blurhash,
2331
thumbhash,
32+
placeholderSrc,
2433
placeholderSize,
2534
...rest
2635
}: Props) {
@@ -37,11 +46,14 @@ export function UnLazyImage({
3746
cleanup()
3847
}
3948
}
40-
}, [blurhash, thumbhash, placeholderSize])
49+
}, [src, srcSet, autoSizes, blurhash, thumbhash, placeholderSrc, placeholderSize])
4150

4251
return (
4352
<img
4453
ref={target}
54+
src={placeholderSrc}
55+
data-src={src}
56+
data-srcset={srcSet}
4557
data-sizes={autoSizes ? 'auto' : undefined}
4658
loading="lazy"
4759
{...rest}

0 commit comments

Comments
 (0)