SVG sprite generator with automatic TypeScript type generation for React projects.
- Two Icon Variants: Dynamic and Resizable icons
- Automatic Sprite Generation: Combines multiple SVG files into a single optimized sprite
- TypeScript Support: Auto-generated type-safe Icon component with
.tsconfig file support - Optimized: Uses SVGO for SVG optimization
- Easy Integration: Simple CLI commands for initialization and generation
# npm
npm install -D flexisvg
# pnpm
pnpm add -D flexisvgRun the init command to create configuration file and folder structure:
# npm
npx flexisvg init
# pnpm
pnpm dlx flexisvg initThis will create:
flexisvg.config.ts- Configuration filepublic/icons/dynamic/- Dynamic icons folderpublic/icons/resizable/- Resizable icons foldersrc/components/- Output folder for Icon component
Place your SVG files in the appropriate folders:
- dynamic: Icons with customizable size and color (colors converted to
currentColor) - resizable: Icons with customizable size only (keeps original colors)
Run the generate command to create sprite and Icon component:
npx flexisvgThis will generate:
public/icons/sprite.svg- Optimized SVG spritesrc/components/icon/index.tsx- Type-safe React Icon component
The flexisvg.config.ts file allows you to customize the folder structure:
import { SpriteGeneratorConfig } from 'flexisvg';
const config: SpriteGeneratorConfig = {
// Input directories
dynamicDir: 'public/icons/dynamic',
resizableDir: 'public/icons/resizable',
// Output paths
outputSpriteDir: 'public/icons',
outputComponentPath: 'src/components/icon/index.tsx',
};
export default config;| Option | Type | Description |
|---|---|---|
dynamicDir |
string |
Directory for dynamic icons (customizable size and color) |
resizableDir |
string |
Directory for resizable icons (customizable size only) |
outputSpriteDir |
string |
Output directory for sprite.svg |
outputComponentPath |
string |
Output path for Icon component |
- Use case: Icons that need both size and color customization
- Processing:
- Converts all colors to
currentColor(inherits CSScolorproperty) - Removes
fillandstrokeattributes with valuesnone,black, or#000000 - SVG
<symbol>automatically handles size viaviewBox
- Converts all colors to
- Example: UI icons, action buttons
public/icons/dynamic/
├─ home.svg
├─ user.svg
└─ settings.svg
Before processing:
<svg fill="none" viewBox="0 0 24 24">
<path fill="#7C868F" d="M12..." />
</svg>After processing:
<symbol fill="none" viewBox="0 0 24 24" id="home">
<path fill="currentColor" d="M12..." />
</symbol>- Use case: Icons that need size customization but should keep original colors
- Processing: Only converts to
<symbol>format, preserves all original colors - Example: Colored illustrations, multi-color icons, brand logos
public/icons/resizable/
├─ illustration-1.svg
├─ colored-icon.svg
└─ brand-logo.svg
An optimized SVG sprite containing all icons as symbols:
<svg xmlns="http://www.w3.org/2000/svg">
<symbol id="home" viewBox="0 0 24 24">
<!-- SVG content -->
</symbol>
<symbol id="user" viewBox="0 0 24 24">
<!-- SVG content -->
</symbol>
<!-- More symbols... -->
</svg>A type-safe React component with auto-generated types:
// Auto-generated types
export type DynamicIconId = 'home' | 'user' | 'settings';
export type ResizableIconId = 'illustration-1' | 'colored-icon' | 'brand-logo';
export type IconId = DynamicIconId | ResizableIconId;
// Icon component
export const Icon = ({ id, size = 24, ...props }: IconProps) => {
return (
<svg width={size} height={size} {...props}>
<use href={`/sprite.svg#${id}`} />
</svg>
);
};import { Icon } from '@/components/icon';
function App() {
return (
<div>
{/* Default size (24px) */}
<Icon id="home" />
{/* Custom size */}
<Icon id="user" size={32} />
{/* With custom color (for dynamic icons) */}
<Icon id="settings" size={20} style={{ color: 'blue' }} />
{/* With additional props */}
<Icon id="logo" className="my-icon" onClick={handleClick} />
</div>
);
}The generated types provide autocomplete and type checking:
// ✅ Valid - 'home' exists in icon set
<Icon id="home" />
// ❌ TypeScript error - 'nonexistent' is not a valid icon id
<Icon id="nonexistent" />Creates configuration file and folder structure.
npx flexisvg initGenerates sprite and Icon component based on configuration.
npx flexisvgUse a custom configuration file:
npx flexisvg --config path/to/custom.config.jsMIT