Skip to content

Commit 73ee5e5

Browse files
committed
README: 'Build your own wrapper' — honest framework examples
Shows a tiny reusable component for React/Vue/Svelte instead of raw onLoad callbacks. Same one line of logic, proper component pattern.
1 parent fdefc26 commit 73ee5e5

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

README.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,38 +93,47 @@ document.addEventListener('DOMContentLoaded', () => {
9393

9494
This approach avoids inline `onload` attributes, which is better for strict [Content Security Policy](#security) setups.
9595

96-
### React
96+
### Build your own wrapper
9797

98+
SVGInject is framework-agnostic. Wrap it in a tiny component for your framework — it's just one line of logic:
99+
100+
**React:**
98101
```jsx
99102
import { SVGInject } from '@iconfu/svg-inject';
100103

101-
function Icon({ src, alt }) {
102-
return <img src={src} alt={alt} onLoad={(e) => SVGInject(e.currentTarget)} />;
104+
function SvgImg({ src, ...props }) {
105+
return <img src={src} {...props} onLoad={(e) => SVGInject(e.currentTarget)} />;
103106
}
104-
```
105107

106-
### Vue
108+
// <SvgImg src="icon.svg" className="icon" alt="Settings" />
109+
```
107110

111+
**Vue:**
108112
```vue
113+
<!-- SvgImg.vue -->
109114
<template>
110-
<img :src="src" @load="inject" />
115+
<img :src="src" v-bind="$attrs" @load="(e) => SVGInject(e.currentTarget)" />
111116
</template>
112117
113118
<script setup>
114119
import { SVGInject } from '@iconfu/svg-inject';
115-
const inject = (e) => SVGInject(e.currentTarget);
120+
defineProps(['src']);
116121
</script>
117-
```
118122
119-
### Svelte
123+
<!-- <SvgImg src="icon.svg" class="icon" alt="Settings" /> -->
124+
```
120125

126+
**Svelte:**
121127
```svelte
128+
<!-- SvgImg.svelte -->
122129
<script>
123130
import { SVGInject } from '@iconfu/svg-inject';
131+
let { src, ...rest } = $props();
124132
</script>
125133
126-
<!-- Svelte 5 syntax. For Svelte 4, use on:load instead of onload -->
127-
<img src="icon.svg" onload={(e) => SVGInject(e.currentTarget)} />
134+
<img {src} {...rest} onload={(e) => SVGInject(e.currentTarget)} />
135+
136+
<!-- <SvgImg src="icon.svg" class="icon" alt="Settings" /> -->
128137
```
129138

130139

0 commit comments

Comments
 (0)