Skip to content

Commit

Permalink
feat: add load and error events to cldimage (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
codercatdev committed Mar 11, 2024
1 parent 80342c6 commit d6523a5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
53 changes: 53 additions & 0 deletions docs/src/docs/components/cldimage/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,59 @@ To preserve those transformations, you can apply the `preserveTransformations` p
/>
```

## Events

You can capture both the `on:load` and `on:error` events to take actions when your image loads.

In this working example you will see "Turtle has loaded" in the console.

```svelte
<CldImage
width="960"
height="600"
src={`images/turtle`}
sizes="100vw"
alt="Turtle in the ocean"
on:load={() => console.log("Turtle has loaded.")}
on:error={()=>console.error("Turtle Error")}
/>
```

<CldImage
width="960"
height="600"
src={`images/turtle`}
sizes="100vw"
alt="Turtle in the ocean"
on:load={() => console.log("Turtle has loaded.")}
on:error={()=>console.error("Turtle Error")}
/>

In this broken example you will see "Turtle Error" in the console.

```svelte
<CldImage
width="960"
height="600"
src={`images/turtle-broken`}
sizes="100vw"
alt="Broken Turtle Image"
on:load={() => console.log("Turtle has loaded.")}
on:error={()=>console.error("Turtle Error")}
/>
```

<CldImage
width="960"
height="600"
src={`images/turtle-broken`}
sizes="100vw"
alt="Broken Turtle Image"
on:load={() => console.log("Turtle has loaded.")}
on:error={()=>console.error("Turtle Error")}
/>


## Watch & Learn

<Video
Expand Down
2 changes: 2 additions & 0 deletions svelte-cloudinary/src/lib/components/CldImage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,7 @@
transformer={(loaderOptions) => {
return cloudinaryLoader( { loaderOptions, imageProps, cldOptions: {...cldOptions, width: imageProps.width }, cldConfig: config });
}}
on:load
on:error
/>
{/if}

0 comments on commit d6523a5

Please sign in to comment.