2026-07-11 09:26:43 +03:00
# React + Vite
[← Back to home ](../../README.md )
A quick guide to installing and using SVG sprites in a React and Vite project.
The result is a typed React component and a separate cacheable SVG asset.
## 1. Install the package
```bash
2026-07-11 18:23:41 +03:00
npm install --save-dev @gromlab/svg -sprites
2026-07-11 09:26:43 +03:00
```
## 2. Create the sprite directory
```text
src/ui/file-manager/svg-sprite/
├── icons/
│ ├── check.svg
│ └── folder.svg
2026-07-13 20:07:42 +03:00
├── index.ts
2026-07-11 09:26:43 +03:00
└── svg-sprite.config.ts
```
Place the source SVG files in `icons/` .
## 3. Add the configuration
```ts
// src/ui/file-manager/svg-sprite/svg-sprite.config.ts
2026-07-13 20:07:42 +03:00
import { defineSpriteConfig } from '@gromlab/svg -sprites'
2026-07-11 09:26:43 +03:00
2026-07-13 20:07:42 +03:00
export default defineSpriteConfig({
mode: 'react@vite ',
2026-07-11 09:26:43 +03:00
name: 'file-manager',
description: 'File manager icons',
})
```
2026-07-13 20:07:42 +03:00
The root barrel is application-owned and explicitly re-exports the generated API:
```ts
// src/ui/file-manager/svg-sprite/index.ts
export * from './.svg-sprite'
```
2026-07-11 09:26:43 +03:00
By default, SVG files are loaded from `./icons` . You can add shared icons from other directories through `inputFiles` : the directory and file list are combined into a single sprite.
2026-07-13 20:07:42 +03:00
The complete list of options is available under [Unified configuration ](reference.md#unified-configuration ).
2026-07-11 09:26:43 +03:00
## 4. Add generation to package.json
```json
{
"scripts": {
2026-07-13 20:07:42 +03:00
"sprite:file-manager": "svg-sprites src/ui/file-manager/svg-sprite/svg-sprite.config.ts",
2026-07-11 09:26:43 +03:00
"predev": "npm run sprite:file-manager",
"prebuild": "npm run sprite:file-manager",
"pretypecheck": "npm run sprite:file-manager"
}
}
```
Generated files are excluded from Git, so generation must run before `dev` , `build` , and `typecheck` .
First run:
```bash
npm run sprite:file-manager
```
## 5. Use the component
The name `file-manager` is converted to `FileManagerIcon` :
```tsx
import { FileManagerIcon } from './svg-sprite'
export const OpenFolderButton = () => (
< button type = "button" >
< FileManagerIcon icon = "folder" width = {24} height = {24} / >
Open
< / button >
)
```
TypeScript checks the `icon` value against the file names:
```tsx
< FileManagerIcon icon = "check" / > // valid
< FileManagerIcon icon = "unknown" / > // TypeScript error
```
2026-07-11 23:20:39 +03:00
Types, rendering methods, and color controls are described in the [technical reference ](reference.md#react-component-and-typescript ).
2026-07-11 09:26:43 +03:00
Vite emits the sprite as a separate file named like `assets/sprite-<hash>.svg` . SVG path data is not included in JavaScript.
## 6. Add a debug page
After integrating the icons, you can display all React sprites with `SpriteViewer` :
```tsx
import { SpriteViewer } from '@gromlab/svg -sprites/react'
import type { SpriteManifestModule } from '@gromlab/svg -sprites/react'
const sources = import.meta.glob< SpriteManifestModule > (
2026-07-13 20:07:42 +03:00
'/src/**/svg-sprite/.svg-sprite/svg-sprite.manifest.js',
2026-07-11 09:26:43 +03:00
)
export const IconsDebugPage = () => (
< SpriteViewer sources = {sources} title = "Project icons" / >
)
```
2026-07-13 20:07:42 +03:00
Vite automatically finds the generated manifest for each React sprite. The `import.meta.glob` pattern must be a string literal, and generation must run before Vite starts.
2026-07-11 09:26:43 +03:00
Only include the Viewer on a debug route or in an internal tool.
## Troubleshooting
2026-07-13 20:07:42 +03:00
- Missing `.svg-sprite/index.js` : run `npm run sprite:file-manager` .
- The Viewer cannot find the sprite: check the glob path to `.svg-sprite/svg-sprite.manifest.js` .
2026-07-11 09:26:43 +03:00
- `Refusing to overwrite a user file` error: there is a user file at a generated path.
- The icon does not change color: use `color` or `--icon-color-N` .