You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- autoSizes accepts { updateOnResize: true } and returns a disposer; an <img> argument walks to <source data-sizes="auto"> siblings in the same call.
- triggerLoad is one-shot again - TriggerLoadOptions.updateSizesOnResize removed.
- lazyLoad keeps updateSizesOnResize as a thin wrapper that delegates to autoSizes.
- Drop resizeObserverCache; one disposer per call.
- Vue/Nuxt switch to watchEffect((onCleanup) => …) per Vue 3.5+ idiom.
- Fix stale picture-callback note in usage.md and stale triggerLoad reference in migration.md.
Copy file name to clipboardExpand all lines: docs/api/auto-sizes.md
+37-2Lines changed: 37 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,34 @@
1
1
# `autoSizes`
2
2
3
-
The `autoSizes` function calculates and sets the `sizes` attribute based on the current display width of image or source elements when `data-sizes="auto"` is present.
3
+
The `autoSizes` function resolves `data-sizes="auto"` to a numeric pixel width based on the rendered display size. It works on standalone `<img>`elements, on `<source>` siblings inside a `<picture>`, or both.
4
4
5
-
The calculation uses the element's rendered width (`element.offsetWidth`) to determine the appropriate value for the `sizes` attribute, enabling browsers to select the optimal image from a `srcset`.
5
+
When called with an `<img>` inside a `<picture>`, `autoSizes` walks to every `<source data-sizes="auto">` sibling and resolves them in the same call. `<source>` elements have no layout box of their own, so the rendered `<img>` width is used instead.
6
6
7
7
To lazy load images, refer to the [`lazyLoad`](/api/lazy-load) method.
8
8
9
+
## Options
10
+
11
+
| Option | Type | Default | Description |
12
+
| --- | --- | --- | --- |
13
+
|`updateOnResize`|`boolean`|`false`| Install a debounced `ResizeObserver` that re-resolves `data-sizes="auto"` on viewport changes. The returned disposer disconnects it. |
14
+
15
+
## Return Value
16
+
17
+
`autoSizes` returns a disposer. When called with `updateOnResize: false` (or no options), the disposer is a no-op. With `updateOnResize: true`, calling it disconnects every `ResizeObserver` created by that call:
For most users, calling [`lazyLoad`](/api/lazy-load) with `updateSizesOnResize: true` is the simpler path – it delegates to `autoSizes` internally and bundles the disposer into the same cleanup callback.
Copy file name to clipboardExpand all lines: docs/api/lazy-load.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,7 @@ Options can be passed to the function to customize its behavior:
30
30
|`hash`|`boolean \| string`|`true`| Whether to use a hash for generating a blurry placeholder. Can be `true` (auto-detect from `data-blurhash`/`data-thumbhash`), `false` (disabled), or a hash string. |
31
31
|`hashType`|`'blurhash' \| 'thumbhash'`|`'blurhash'`| The type of hash to use. Ignored when `hash` is boolean (auto-detected from data attributes). |
32
32
|`placeholderSize`|`number`|`32`| The size of the longer edge for BlurHash decoding. Ignored for ThumbHash. |
33
-
|`updateSizesOnResize`|`boolean`|`false`|Whether to update the `sizes` attribute on resize events using a debounced ResizeObserver. Useful for responsive layouts where image display size changes. |
33
+
|`updateSizesOnResize`|`boolean`|`false`|Re-resolve `data-sizes="auto"`on viewport resize – applies to both `<img>` and `<source>` siblings inside a `<picture>`. Internally delegates to [`autoSizes`](/api/auto-sizes); the returned cleanup disconnects every observer. |
34
34
|`onImageLoad`|`(image: HTMLImageElement) => void`| - | Callback invoked when an image loads successfully. |
35
35
|`onImageError`|`(image: HTMLImageElement, error: Event) => void`| - | Callback invoked when an image fails to load. |
Copy file name to clipboardExpand all lines: docs/api/trigger-load.md
+16-3Lines changed: 16 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,30 @@
1
1
# `triggerLoad`
2
2
3
-
The `triggerLoad` function programmatically loads an image by updating its attributes from data attributes. It handles both standalone `<img>` elements and images within`<picture>` elements.
3
+
The `triggerLoad` function programmatically loads an image by swapping its data attributes to standard ones. It handles both standalone `<img>` elements and images inside`<picture>` elements.
4
4
5
5
The function performs the following operations:
6
6
7
-
1. If the image is inside a `<picture>` element, updates all `<source>` elements by converting their `data-srcset` and `data-sizes` attributes to standard attributes synchronously. Callbacks fire after the browser resolves a source on the visible `<img>`.
7
+
1. If the image is inside a `<picture>` element, swaps `data-srcset` to `srcset` on every `<source>` and resolves `data-sizes="auto"`to a numeric pixel width before the swap so the browser sees a final value at source-selection time. Callbacks fire after the browser resolves a source on the visible `<img>`.
8
8
2. For standalone `<img>` elements, preloads the image in a temporary element to ensure proper loading.
9
-
3. Calculates the `sizes` attribute if `data-sizes="auto"` is set.
9
+
3. Calculates the `sizes` attribute on the temporary element if `data-sizes="auto"` is set on the visible `<img>`.
10
10
4. Swaps `data-src` and `data-srcset` to their standard counterparts.
11
11
5. Invokes the optional `onImageLoad` callback when loading completes, or `onImageError` if loading fails. On failure, a synthetic `error` event also fires on the visible `<img>`.
12
12
13
13
`triggerLoad` returns a disposer that detaches its listeners and, for standalone images, aborts the in-flight network fetch. Calling it after the load has already completed is a no-op.
14
14
15
+
::: tip
16
+
`triggerLoad` is one-shot – it does not install any `ResizeObserver`. For ongoing source-size tracking on responsive `<picture>` layouts, pair it with [`autoSizes`](/api/auto-sizes) and `{ updateOnResize: true }`:
Copy file name to clipboardExpand all lines: docs/guide/migration.md
+30Lines changed: 30 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,6 +25,21 @@ The deprecated `loadImage` alias is gone. Replace every call site with `triggerL
25
25
+ // Optional: dispose() to cancel before the load completes
26
26
```
27
27
28
+
### `autoSizes` Owns Ongoing Size Tracking
29
+
30
+
`triggerLoad` is one-shot again – it no longer accepts `updateSizesOnResize`. Ongoing re-resolution of `data-sizes="auto"` lives on [`autoSizes`](/api/auto-sizes), which now accepts `{ updateOnResize: true }` and returns a disposer:
For the common case, [`lazyLoad`](/api/lazy-load) keeps `updateSizesOnResize` and delegates to `autoSizes` internally – no caller change needed.
40
+
41
+
`autoSizes` itself now always returns a function. With no options, the returned disposer is a no-op; with `updateOnResize: true`, it disconnects every `ResizeObserver` created by the call. Passing an `<img>` inside a `<picture>` walks to every `<source data-sizes="auto">` sibling in the same call, replacing the previous need to invoke `autoSizes` separately on each source.
42
+
28
43
### `isLazyLoadingSupported` Removal
29
44
30
45
Native `loading="lazy"` is Baseline Widely Available; the feature-detect was used internally to fall back to immediate swap, and that branch is gone. If you imported the helper directly, replace it with:
@@ -93,6 +108,21 @@ Result: you can safely re-invoke `lazyLoad()` after inserting images into the DO
93
108
94
109
unlazy now warns in development when the LCP element is still configured for lazy loading. See the [Core Web Vitals guide](/guide/core-web-vitals#dev-mode-lcp-warning).
95
110
111
+
### `sources` Prop Across Adapters
112
+
113
+
The `sources` prop is no longer Nuxt-only. Every adapter now renders a `<picture>` when you pass an array of `UnLazySource` objects:
Each entry becomes a `<source>` child with `type`, `media`, `width`, `height`, and `data-sizes="auto"` support. Pass `updateSizesOnResize: true` to [`lazyLoad`](/api/lazy-load) (or `{ updateOnResize: true }` directly to [`autoSizes`](/api/auto-sizes)) to re-resolve `<source data-sizes="auto">` siblings on viewport resize.
125
+
96
126
### Vue / Nuxt: `@image-load` and `@image-error` Emits
97
127
98
128
The Vue and Nuxt adapters now expose a symmetric pair of emits aligned with the core option names:
Copy file name to clipboardExpand all lines: docs/guide/usage.md
+14-9Lines changed: 14 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -95,17 +95,26 @@ The automatic sizes calculation uses the display width of the image.
95
95
96
96
When calling [`lazyLoad`](/api/lazy-load), the library automatically calculates the `sizes` attribute for all images with `data-sizes="auto"`.
97
97
98
-
Alternatively, use the [`autoSizes`](/api/auto-sizes) function to calculate the `sizes` attribute without lazy loading.
99
-
100
-
To do so, import the [`autoSizes`](/api/auto-sizes) function from the library and call it:
98
+
Alternatively, use the [`autoSizes`](/api/auto-sizes) function to calculate the `sizes` attribute without lazy loading. Call it with an `<img>` inside a `<picture>` and it walks to every `<source data-sizes="auto">` sibling in the same call:
101
99
102
100
```ts
103
101
import { autoSizes } from'unlazy'
104
102
105
-
//Automatically calculate the sizes attribute for all `img[data-sizes="auto"], source[data-sizes="auto"]` images, without lazy loading them
103
+
//One-shot resolve for every matching element in the document
106
104
autoSizes()
107
105
```
108
106
107
+
For responsive layouts where the display width changes – fluid containers, breakpoint switches, orientation changes – pass `{ updateOnResize: true }` to keep `sizes` synced. `autoSizes` returns a disposer – call it when you no longer need the observer:
The same behavior is available on [`lazyLoad`](/api/lazy-load) via `updateSizesOnResize: true`, which delegates to `autoSizes` internally and bundles the disposer into the same cleanup callback.
117
+
109
118
## Custom Selectors
110
119
111
120
You can customize the CSS selectors to target specific images by passing a CSS selector, a DOM element, a list of DOM elements, or an array of DOM elements to [`lazyLoad`](/api/lazy-load) and [`autoSizes`](/api/auto-sizes).
@@ -170,8 +179,4 @@ unlazy fully supports the `<picture>` element for art direction and format selec
170
179
</picture>
171
180
```
172
181
173
-
When the image loads, unlazy automatically swaps `data-srcset` to `srcset` on all `<source>` elements within the `<picture>`.
174
-
175
-
::: info
176
-
For `<picture>` elements, the `onImageLoad` and `onImageError` callbacks passed to `lazyLoad` aren't invoked, as the browser handles source selection internally.
177
-
:::
182
+
When the image loads, unlazy automatically swaps `data-srcset` to `srcset` on all `<source>` elements within the `<picture>`. The `onImageLoad` and `onImageError` callbacks fire once the browser resolves a source on the visible `<img>`.
0 commit comments