Standard Toolkit
Tooling@accelint/smeegle

buildReactFiles

Generate React SVG components from SVG files with type-safe props and customizable fill colors.

Usage

import { buildReactFiles } from '@accelint/smeegl/build-react-files';
import { findSprites } from '@accelint/smeegl/utils/find-sprites';

// First find sprites
const findResult = await findSprites('**/*.svg', './icons', process.cwd(), tmpDir);

// Generate React components
buildReactFiles(
  findResult,
  './icons',           // Input directory
  './components/svg'   // Output directory
);

Reference

function buildReactFiles(
  findSpriteResult: FindSpritesResult,
  inputPath: string,
  outputPath: string
): void

Parameters

ParameterTypeDescription
findSpriteResultFindSpritesResultResult from findSprites containing sprite metadata.
inputPathstringOriginal input directory path, used for determining component naming prefixes.
outputPathstringDirectory where React component files will be written. Created if it doesn't exist.

Returns

Returns void. Components are written to the file system synchronously.

Type Parameters

type FindSpritesResult = Result<FindSpritesOutput, ErrorPath>;

Examples

Example: Generate components from directory

import { buildReactFiles } from '@accelint/smeegl/build-react-files';
import { findSprites } from '@accelint/smeegl/utils/find-sprites';
import { makeTempDirectory } from '@accelint/smeegl/utils/make-temp-directory';

const tmpDir = await makeTempDirectory();
const findResult = await findSprites('', './assets/icons', process.cwd(), tmpDir);

buildReactFiles(
  findResult,
  './assets/icons',
  './src/components/icons'
);

// Output:
// - SvgArrow.tsx
// - SvgArrowDown.tsx
// - index.ts (barrel export)

Example: Component structure

Generated components follow this pattern:

// SvgArrow.tsx
import type { SVGProps } from 'react';

interface Props {
  title?: string;
  titleId?: string;
}

export const SvgArrow = ({
  title,
  titleId,
  ...props
}: SVGProps<SVGSVGElement> & Props) => {
  const { fill } = props;
  const fillColor = fill ?? '#FF69B4';
  const hasTitle = typeof title === 'string' && Boolean(title.trim());

  return (
    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none">
      {hasTitle && <title id={titleId}>{title}</title>}
      <path d="M19.9997 4L..." fill={fillColor} />
    </svg>
  );
};

Example: Using generated components

import { SvgArrow, SvgArrowDown } from './components/icons';

function App() {
  return (
    <div>
      <SvgArrow fill="#3b82f6" title="Navigate forward" />
      <SvgArrowDown fill="currentColor" />
    </div>
  );
}

Example: Nested directory structure

import { buildReactFiles } from '@accelint/smeegl/build-react-files';

// Input structure:
//   ./icons/
//     ui/arrow.svg
//     ui/chevron.svg
//     social/github.svg

buildReactFiles(findResult, './icons', './components');

// Output:
// - SvgUiArrow.tsx
// - SvgUiChevron.tsx
// - SvgSocialGithub.tsx
// - index.ts

Good to know: The function assumes SVG files have a single fill color set to #FF69B4 or any non-none value, which will be replaced with the component's fill prop. SVG attributes are automatically converted from kebab-case to camelCase.

Generated Files

Component Files

Each SVG generates a TypeScript React component with:

  • TypeScript strict mode support
  • SVG props inheritance
  • Optional title and titleId for accessibility
  • Customizable fill color (defaults to #FF69B4)

index.ts

Barrel export file:

export { SvgArrow } from './arrow';
export { SvgArrowDown } from './arrow-down';

Component Naming

Components are named using PascalCase with "Svg" prefix:

  • arrow.svg → SvgArrow
  • arrow-down.svg → SvgArrowDown
  • ui/arrow.svg → SvgUiArrow

Parent directory names are included when present to ensure unique component names.

On this page