Skip to content

Commit ce8c23d

Browse files
authored
Merge pull request shubhamjain#42 from MonkeyDo/error-event
Fix shubhamjain#22 : throw custom error event on SVG load failure
2 parents 2be8b9c + b645e18 commit ce8c23d

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,28 @@ When the SVG has been loaded an event `iconload` is triggered. This can be used
211211
</script>
212212
```
213213

214+
### Using the `iconloaderror` event
215+
When an error occurs during loading of the SVG file, an `iconloaderror` event is triggered, passing the error message as the event's `detail`.
216+
217+
```html
218+
<svg data-src="https://unpkg.com/@mdi/[email protected]/svg/this-svg-does-not-exist.svg"></svg>
219+
220+
<script>
221+
window.addEventListener('iconloaderror', (e) => {
222+
console.error('Failed to load SVG:', e.detail);
223+
});
224+
</script>
225+
```
226+
227+
Similarly to the `iconload` event, `iconloaderror` can also be used with an inline function, which will have access to an `error` argument (the `Error` object that was thrown):
228+
```html
229+
<svg
230+
data-src="https://unpkg.com/@mdi/[email protected]/svg/cog.svg"
231+
oniconloaderror="console.log('Error loading SVG:', error.toString())"></svg>
232+
```
233+
214234
### Using Events in React
215-
React doesn't support custom events out of the box. To circumvent this limitation, you can [refs](https://reactjs.org/docs/refs-and-the-dom.html).
235+
React doesn't support custom events out of the box. To circumvent this limitation, you can use [refs](https://reactjs.org/docs/refs-and-the-dom.html).
216236

217237
```jsx
218238
class MyApp extends React.Component {
@@ -226,7 +246,10 @@ class MyApp extends React.Component {
226246
componentDidMount() {
227247
this.ref.current.addEventListener('iconload', () => {
228248
console.log("Icon Loaded", this.ref.current)
229-
})
249+
});
250+
this.ref.current.addEventListener('iconloaderror', (e) => {
251+
console.error('Failed to load SVG:', e.detail);
252+
});
230253
}
231254
}
232255
```

svg-loader.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ const renderBody = (elem, options, body) => {
196196
// no way to specify the context for execution. So, `this` in the attribute
197197
// will point to `window` instead of the element itself.
198198
//
199-
// Here we are recycling a rarely used GlobalEventHandler 'onloadedmetadata'
199+
// Here we are recycling a rarely used GlobalEventHandler 'onauxclick'
200200
// and offloading the execution to the browser. This is a hack, but because
201201
// the event doesn't bubble, it shouldn't affect anything else in the code.
202202
elem.setAttribute('onauxclick', elem.getAttribute('oniconload'));
@@ -270,6 +270,16 @@ const renderIcon = async (elem) => {
270270
})
271271
.catch((e) => {
272272
console.error(e);
273+
const event = new CustomEvent('iconloaderror', {
274+
bubbles: true,
275+
detail: e.toString(),
276+
});
277+
elem.dispatchEvent(event);
278+
if(elem.getAttribute('oniconloaderror')){
279+
// the oniconloaderror inline function will have access to an `error` argument
280+
const loadErrorFunction = Function("error", elem.getAttribute('oniconloaderror'));
281+
loadErrorFunction(e);
282+
}
273283
})
274284
.finally(() => {
275285
delete requestsInProgress[src];

test/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ <h1>
4646
});
4747
</script>
4848

49+
<div class="heading">Icon load error event (see console)</div>
50+
<svg data-src="https://unpkg.com/@mdi/[email protected]/svg/this-svg-does-not-exist.svg"></svg>
51+
52+
<script>
53+
window.addEventListener('iconloaderror', (e) => {
54+
console.error('Error event fired:', e.detail);
55+
});
56+
</script>
57+
4958
<div class="heading">Icon using doctype declaration</div>
5059
<svg data-src="/icons/cog-with-doctype.svg" fill="green"></svg>
5160

0 commit comments

Comments
 (0)