Implemented InlineSvg component to fetch and color SVG logos dynamically.

This commit is contained in:
sha
2025-04-29 12:16:41 +03:00
parent 69f6f30cb3
commit 3d90d9b0da
9 changed files with 380 additions and 34 deletions
+25
View File
@@ -0,0 +1,25 @@
<script>
import { onMount } from 'svelte';
export let path;
export let color;
export let colorConfig = { target: 'path', attribute: 'fill' };
let svgHtml = '';
async function fetchAndColorSvg() {
const res = await fetch(path);
let text = await res.text();
// Parse and update color
const parser = new DOMParser();
const doc = parser.parseFromString(text, 'image/svg+xml');
const targets = doc.querySelectorAll(colorConfig.selector || colorConfig.target);
targets.forEach(el => {
el.setAttribute(colorConfig.attribute, color);
});
svgHtml = doc.documentElement.outerHTML;
}
$: path, color, colorConfig, fetchAndColorSvg();
</script>
{@html svgHtml}