Skip to content

Commit d6523a5

Browse files
authored
feat: add load and error events to cldimage (#96)
1 parent 80342c6 commit d6523a5

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

docs/src/docs/components/cldimage/usage.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,59 @@ To preserve those transformations, you can apply the `preserveTransformations` p
146146
/>
147147
```
148148

149+
## Events
150+
151+
You can capture both the `on:load` and `on:error` events to take actions when your image loads.
152+
153+
In this working example you will see "Turtle has loaded" in the console.
154+
155+
```svelte
156+
<CldImage
157+
width="960"
158+
height="600"
159+
src={`images/turtle`}
160+
sizes="100vw"
161+
alt="Turtle in the ocean"
162+
on:load={() => console.log("Turtle has loaded.")}
163+
on:error={()=>console.error("Turtle Error")}
164+
/>
165+
```
166+
167+
<CldImage
168+
width="960"
169+
height="600"
170+
src={`images/turtle`}
171+
sizes="100vw"
172+
alt="Turtle in the ocean"
173+
on:load={() => console.log("Turtle has loaded.")}
174+
on:error={()=>console.error("Turtle Error")}
175+
/>
176+
177+
In this broken example you will see "Turtle Error" in the console.
178+
179+
```svelte
180+
<CldImage
181+
width="960"
182+
height="600"
183+
src={`images/turtle-broken`}
184+
sizes="100vw"
185+
alt="Broken Turtle Image"
186+
on:load={() => console.log("Turtle has loaded.")}
187+
on:error={()=>console.error("Turtle Error")}
188+
/>
189+
```
190+
191+
<CldImage
192+
width="960"
193+
height="600"
194+
src={`images/turtle-broken`}
195+
sizes="100vw"
196+
alt="Broken Turtle Image"
197+
on:load={() => console.log("Turtle has loaded.")}
198+
on:error={()=>console.error("Turtle Error")}
199+
/>
200+
201+
149202
## Watch & Learn
150203

151204
<Video

svelte-cloudinary/src/lib/components/CldImage.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,7 @@
7979
transformer={(loaderOptions) => {
8080
return cloudinaryLoader( { loaderOptions, imageProps, cldOptions: {...cldOptions, width: imageProps.width }, cldConfig: config });
8181
}}
82+
on:load
83+
on:error
8284
/>
8385
{/if}

0 commit comments

Comments
 (0)