Is it possible to change the color of an SVG image dynamically without using inline SVG tags? I want to create a code that allows users to specify the source for the SVG tag and a hexadecimal color value, enabling them to customize any SVG image with their preferred colors.
The idea is to have a component where users can input the SVG source and color, and the code will automatically adjust the fill colors of the SVG based on these parameters. Any ideas on how to achieve this would be greatly appreciated. Thank you!
Here is an example of how the user interaction might look:
import Frame from './Frame.svg';
...
<Icon src={Frame} color='purple' />
This is my suggested UI component code:
import React from 'react';
import styled from 'styled-components';
const Icon = ({ src, color }) => {
const StyledSVGIcon = styled(src)`
width: 67px;
height: 72px;
fill: ${color};
`;
return <StyledSVGIcon />
};
Sample content of Frame.svg file:
<svg width="73" height="71" viewBox="0 0 73 71" fill="none" xmlns="http://www.w3.org/2000/svg">
<g clip-path="url(#clip0)">
<path d="M47.0139 71H40.8997C39.8807 71 39.541 70.332 39.541 69.664V63.6518C39.541 63.3178 39.8807 62.6498 39.8807 62.3157L64.3376 38.267C65.0169 37.599 65.6963 37.599 66.3756 38.267L72.4898 44.2792C73.1692 44.9472 73.1692 45.6152 72.4898 46.2833L48.033 70.332C47.6933 70.666 47.0139 71 47.0139 71ZM42.2584 67.9939H46.3346L69.0931 45.6152L65.0169 41.6071L42.2584 63.9858V67.9939Z" fill="#4B286D"/>
...
</g>
<defs>
<clipPath id="clip0">
<rect width="73" height="71" fill="white"/>
</clipPath>
</defs>
</svg>