replaceCurrentColor

Fetch an SVG and replace its currentColor references with a given color, returning the recolored markup as a string.

Usage

replaceCurrentColor fetches an SVG file and replaces all currentColor references with a specified color.

Note
  • Requires a browser environment (DOMParser and XMLSerializer)
  • Uses fetch to retrieve the SVG file
import { replaceCurrentColor } from '@movk/core'

// Fetch and replace currentColor in an SVG
try {
  const svgContent = await replaceCurrentColor('/icons/star.svg', '#ff0000')
  // Create a Blob and display
  const blob = new Blob([svgContent], { type: 'image/svg+xml' })
  const url = URL.createObjectURL(blob)

  const img = document.createElement('img')
  img.src = url
  document.body.appendChild(img)
}
catch (error) {
  console.error('SVG processing failed:', error)
}

// Fetch SVG content without replacing colors
const originalSvg = await replaceCurrentColor('/icons/star.svg')

API

replaceCurrentColor(path, color?)

Replace currentColor references in an SVG file.

Parameters

path
string required
The path to the SVG file.
color
string
The replacement color value. If omitted, the original SVG is returned unchanged.

Returns

returns
Promise<string>
The processed SVG markup as a string.

Changelog

No recent changes
Copyright © 2024 - 2026 YiXuan - MIT License