Skip to content

Commit f7da11f

Browse files
feat(components): immediate prop
1 parent cdbc69a commit f7da11f

File tree

11 files changed

+81
-6
lines changed

11 files changed

+81
-6
lines changed

docs/api/lazy-load.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ interface UnLazyLoadOptions {
6262
*/
6363
placeholderSize?: number
6464

65+
/**
66+
* Controls whether the image should be loaded immediately, or only when it
67+
* enters the viewport.
68+
*
69+
* @remarks
70+
* This option is intended primarily for internal usage in frontend framework
71+
* components that need to control when single images should be loaded.
72+
*
73+
* @default false
74+
*/
75+
immediate?: boolean
76+
6577
/**
6678
* A callback function to run when an image is loaded.
6779
*/

docs/integrations/nuxt.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ The `UnLazyImage` component accepts the following props:
8383
| `blurhash` | String | A BlurHash string representing the blurry placeholder image. |
8484
| `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. |
8585
| `placeholderRatio` | Number | Aspect ratio (width / height) of the decoded BlurHash image. Only applies to SSR-decoded placeholder images from a BlurHash string. |
86+
| `immediate` | A flag to indicate whether the image should be loaded immediately. |
8687
| `ssr` | Boolean | Whether the ThumbHash or BlurHash should be decoded on the server. Overrides the global module configuration if set. |
8788

8889
## Examples

docs/integrations/react.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ The `UnLazyImage` component accepts the following props:
5050
| `blurhash` | String | A BlurHash string representing the blurry placeholder image. |
5151
| `thumbhash` | String | A ThumbHash string representing the blurry placeholder image. |
5252
| `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. |
53+
| `immediate` | A flag to indicate whether the image should be loaded immediately. |
5354

5455
## Examples
5556

docs/integrations/solid.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ The `UnLazyImage` component accepts the following props:
5050
| `blurhash` | String | A BlurHash string representing the blurry placeholder image. |
5151
| `thumbhash` | String | A ThumbHash string representing the blurry placeholder image. |
5252
| `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. |
53+
| `immediate` | A flag to indicate whether the image should be loaded immediately. |
5354

5455
## Examples
5556

docs/integrations/vue.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ The `UnLazyImage` component accepts the following props:
6565
| `blurhash` | String | A BlurHash string representing the blurry placeholder image. |
6666
| `thumbhash` | String | A ThumbHash string representing the blurry placeholder image. |
6767
| `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. |
68+
| `immediate` | A flag to indicate whether the image should be loaded immediately. |
6869

6970
## Examples
7071

packages/core/src/lazyLoad.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export function lazyLoad<T extends HTMLImageElement>(
1818
hash = true,
1919
hashType = 'blurhash',
2020
placeholderSize = DEFAULT_PLACEHOLDER_SIZE,
21+
immediate = false,
2122
onImageLoad,
2223
}: UnLazyLoadOptions = {},
2324
) {
@@ -27,6 +28,14 @@ export function lazyLoad<T extends HTMLImageElement>(
2728
// Calculate the image's `sizes` attribute if `data-sizes="auto"` is set
2829
updateSizesAttribute(image)
2930

31+
// Load the image right away if `immediate` is set to `true`
32+
if (immediate) {
33+
updatePictureSources(image)
34+
updateImageSrcset(image)
35+
updateImageSrc(image)
36+
continue
37+
}
38+
3039
// Generate the blurry placeholder from a Blurhash or ThumbHash string if applicable
3140
if (__ENABLE_HASH_DECODING__ && hash) {
3241
const { blurhash, thumbhash } = image.dataset
@@ -50,7 +59,7 @@ export function lazyLoad<T extends HTMLImageElement>(
5059
if (isCrawler || !isLazyLoadingSupported) {
5160
updatePictureSources(image)
5261
updateImageSrcset(image)
53-
onImageLoad?.(image)
62+
updateImageSrc(image)
5463
continue
5564
}
5665

packages/core/src/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ export interface UnLazyLoadOptions {
3535
*/
3636
placeholderSize?: number
3737

38+
/**
39+
* Controls whether the image should be loaded immediately, or only when it
40+
* enters the viewport.
41+
*
42+
* @remarks
43+
* This option is intended primarily for internal usage in frontend framework
44+
* components that need to control when single images should be loaded.
45+
*
46+
* @default false
47+
*/
48+
immediate?: boolean
49+
3850
/**
3951
* A callback function to run when an image is loaded.
4052
*/

packages/nuxt/src/runtime/components/UnLazyImage.vue

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import { onBeforeUnmount, onMounted, ref, useRuntimeConfig, watchEffect } from '
88
const props = withDefaults(
99
defineProps<{
1010
src?: ImgHTMLAttributes['src']
11-
/** A flag to indicate whether the sizes attribute should be automatically calculated. */
11+
/**
12+
* A flag to indicate whether the sizes attribute should be automatically calculated.
13+
* @default false
14+
*/
1215
autoSizes?: boolean
1316
/** A BlurHash string representing the blurry placeholder image. */
1417
blurhash?: string
@@ -18,6 +21,11 @@ const props = withDefaults(
1821
placeholderSize?: number
1922
/** Aspect ratio (width / height) of the decoded BlurHash image. Only applies to SSR-decoded placeholder images from a BlurHash string. */
2023
placeholderRatio?: number
24+
/**
25+
* A flag to indicate whether the image should be loaded immediately.
26+
* @default false
27+
*/
28+
immediate?: boolean
2129
/** Whether the ThumbHash or BlurHash should be decoded on the server. Overrides the global module configuration if set. */
2230
ssr?: boolean
2331
}>(),
@@ -28,6 +36,7 @@ const props = withDefaults(
2836
thumbhash: undefined,
2937
placeholderSize: undefined,
3038
placeholderRatio: undefined,
39+
immediate: false,
3140
ssr: undefined,
3241
},
3342
)
@@ -60,6 +69,7 @@ onMounted(() => {
6069
hash: props.thumbhash || props.blurhash,
6170
hashType: props.thumbhash ? 'thumbhash' : 'blurhash',
6271
placeholderSize: props.placeholderSize || unlazy.placeholderSize,
72+
immediate: props.immediate,
6373
})
6474
})
6575
})

packages/react/src/index.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,28 @@ import type { UnLazyLoadOptions } from 'unlazy'
66
interface Props
77
extends ImgHTMLAttributes<HTMLImageElement>,
88
Pick<UnLazyLoadOptions, 'placeholderSize'> {
9-
/** A flag to indicate whether the sizes attribute should be automatically calculated. */
9+
/**
10+
* A flag to indicate whether the sizes attribute should be automatically calculated.
11+
* @default false
12+
*/
1013
autoSizes?: boolean
1114
/** A BlurHash string representing the blurry placeholder image. */
1215
blurhash?: string
1316
/** A ThumbHash string representing the blurry placeholder image. */
1417
thumbhash?: string
18+
/**
19+
* A flag to indicate whether the image should be loaded immediately.
20+
* @default false
21+
*/
22+
immediate?: boolean
1523
}
1624

1725
export function UnLazyImage({
1826
autoSizes,
1927
blurhash,
2028
thumbhash,
2129
placeholderSize,
30+
immediate,
2231
...rest
2332
}: Props) {
2433
const target = useRef<HTMLImageElement | null>(null)
@@ -29,12 +38,13 @@ export function UnLazyImage({
2938
hash: thumbhash || blurhash,
3039
hashType: thumbhash ? 'thumbhash' : 'blurhash',
3140
placeholderSize,
41+
immediate,
3242
})
3343
return () => {
3444
cleanup()
3545
}
3646
}
37-
}, [thumbhash, blurhash, placeholderSize])
47+
}, [blurhash, thumbhash, placeholderSize, immediate])
3848

3949
return (
4050
<img

packages/solid/src/index.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,20 @@ import type { UnLazyLoadOptions } from 'unlazy'
88
interface Props
99
extends JSX.ImgHTMLAttributes<HTMLImageElement>,
1010
Pick<UnLazyLoadOptions, 'placeholderSize'> {
11-
/** A flag to indicate whether the sizes attribute should be automatically calculated. */
11+
/**
12+
* A flag to indicate whether the sizes attribute should be automatically calculated.
13+
* @default false
14+
*/
1215
autoSizes?: boolean
1316
/** A BlurHash string representing the blurry placeholder image. */
1417
blurhash?: string
1518
/** A ThumbHash string representing the blurry placeholder image. */
1619
thumbhash?: string
20+
/**
21+
* A flag to indicate whether the image should be loaded immediately.
22+
* @default false
23+
*/
24+
immediate?: boolean
1725
}
1826

1927
export function UnLazyImage(props: Props) {
@@ -28,6 +36,7 @@ export function UnLazyImage(props: Props) {
2836
hash: props.thumbhash || props.blurhash,
2937
hashType: props.thumbhash ? 'thumbhash' : 'blurhash',
3038
placeholderSize: props.placeholderSize,
39+
immediate: props.immediate,
3140
})
3241

3342
onCleanup(() => {

0 commit comments

Comments
 (0)