You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By default, the `Icon` component expects the package's provided `sprite.svg` file to be located in `/static/sprite.svg` of your app. The `name` prop specifies which icon to render.
20
+
The `name` prop specifies which icon to render.
21
21
22
22
```js
23
23
import { Icon } from'@launchpad-ui/icons';
24
24
25
25
constMyIcon= () =><Icon name="info"/>;
26
26
```
27
27
28
-
### Custom static location
29
-
30
-
A custom path to the sprite can be set via the `IconContext` provider. For example, if importing a static asset returns a resolved URL ([like in Vite](https://vitejs.dev/guide/assets.html#importing-asset-as-url) or [Remix](https://remix.run/docs/en/1.18.1/other-api/asset-imports#asset-url-imports)) you can do the following in your app to load the icons:
31
-
32
-
```js
33
-
import { IconContext } from'@launchpad-ui/icons';
34
-
importiconsfrom'@launchpad-ui/icons/sprite.svg';
35
-
import { createRoot } from'react-dom/client';
36
-
37
-
constdomNode=document.getElementById('root');
38
-
constroot=createRoot(domNode);
39
-
40
-
root.render(
41
-
<IconContext.Provider value={{ path: icons }}>
42
-
<App />
43
-
</IconContext.Provider>
44
-
);
45
-
```
46
-
47
28
### CORS limitation
48
29
49
-
Unfortunately SVG sprites [cannot be accessed cross-domain](https://oreillymedia.github.io/Using_SVG/extras/ch10-cors.html). If you are hosting the sprite file in a CDN or different domain you will have to fetch the file and inject it into the document to access the icons directly.
30
+
Unfortunately SVG sprites [cannot be accessed cross-domain](https://oreillymedia.github.io/Using_SVG/extras/ch10-cors.html). The workaround is to fetch the file and inject it into the document to access the icons directly.
50
31
51
-
First set the `Icon`context path to an empty string to indicate the symbols are available in the DOM:
32
+
Fetch and inject the sprite for `Icon` to render icons correctly:
52
33
53
34
```js
54
-
import { IconContext } from'@launchpad-ui/icons';
55
-
import { createRoot } from'react-dom/client';
35
+
importspritefrom'@launchpad-ui/icons/sprite.svg';
56
36
57
-
constdomNode=document.getElementById('root');
58
-
constroot=createRoot(domNode);
59
-
60
-
root.render(
61
-
<IconContext.Provider value={{ path:'' }}>
62
-
<App />
63
-
</IconContext.Provider>
64
-
);
65
-
```
66
-
67
-
Then fetch and inject the sprite for `Icon` to render icons correctly:
68
-
69
-
```js
70
-
fetch('https://cdn.example.com/sprite.svg')
37
+
fetch(sprite)
71
38
.then(async (response) =>response.text())
72
39
.then((data) => {
73
40
constparser=newDOMParser();
@@ -86,7 +53,7 @@ To minimize latency, you can preload the sprite file accordingly:
0 commit comments