Skip to content

Commit 4d4e0aa

Browse files
authored
docs: add img tag onLoad cookbook πŸ“Œ (#7877)
* docs: add img tag onLoad cookbook πŸ“Œ * chore: update description β›„
1 parent dbfdbd1 commit 4d4e0aa

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { component$, useSignal, useVisibleTask$ } from '@builder.io/qwik';
2+
3+
export default component$(() => {
4+
const imgRef = useSignal<HTMLImageElement>();
5+
useVisibleTask$(() => {
6+
imgRef.value!.decode().then(() => {
7+
alert('loaded and ready!');
8+
});
9+
});
10+
11+
return (
12+
<section>
13+
<img ref={imgRef} src="/logos/qwik-logo.svg" height={200} width={200} />
14+
</section>
15+
);
16+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: Cookbook | Detect img tag onLoad
3+
contributors:
4+
- gioboa
5+
---
6+
7+
import CodeSandbox from '../../../../components/code-sandbox/index.tsx';
8+
9+
10+
# Detect img tag onLoad even if it's cached
11+
12+
When using the onLoad$ property on an \<img\> tag in a Qwik component, you might encounter an issue where the onLoad$ event doesn't fire when the image is already cached in the browser.<br/>
13+
In that case the onLoad$ logic is not being triggered.<br/>
14+
This behavior is observed during normal navigation, but not when browser caching is disabled, indicating the cache's impact on the event.<br/>
15+
The key point here is to [get hold of a DOM element](https://qwik.dev/docs/components/overview/#getting-hold-of-dom-element) to detect onLoad correctly.
16+
17+
### Basic example
18+
19+
<CodeSandbox src="/src/routes/demo/cookbook/detect-img-tag-onload" style={{ height: '20em' }}>
20+
```tsx
21+
import { component$, useSignal, useVisibleTask$ } from '@builder.io/qwik';
22+
23+
export default component$(() => {
24+
const imgRef = useSignal<HTMLImageElement>();
25+
useVisibleTask$(() => {
26+
imgRef.value!.decode().then(() => {
27+
alert('loaded and ready!');
28+
});
29+
});
30+
31+
return (
32+
<section>
33+
<img
34+
ref={imgRef}
35+
src="/logos/qwik-logo.svg"
36+
height={200}
37+
width={200}
38+
/>
39+
</section>
40+
);
41+
});
42+
```
43+
</CodeSandbox>

β€Žpackages/docs/src/routes/docs/cookbook/index.mdxβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ Examples:
3232
- [Synchronous Events with State](./sync-events/)
3333
- [Theme Management](./theme-management/)
3434
- [Drag & Drop](./drag&drop/)
35-
- [View Transition](./view-transition/)
35+
- [View Transition](./view-transition/)
36+
- [Detect img tag onLoad](./detect-img-tag-onload/)

β€Žpackages/docs/src/routes/docs/menu.mdβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
- [Theme Management](/docs/cookbook/theme-management/index.mdx)
5555
- [Drag & Drop](/docs/cookbook/drag&drop/index.mdx)
5656
- [View Transition](/docs/cookbook/view-transition/index.mdx)
57+
- [Detect img tag onLoad](/docs/cookbook/detect-img-tag-onload/index.mdx)
5758

5859
## Integrations
5960

0 commit comments

Comments
Β (0)