From fff1e0b395497324e4d2b1e986190d201bf40a4e Mon Sep 17 00:00:00 2001 From: sHa Date: Fri, 20 Jun 2025 00:02:36 +0300 Subject: [PATCH] Refactor SVG handling for responsive scaling and improve styling attributes --- public/global.css | 4 +- src/components/CardFull.svelte | 33 ++++------------- src/components/Grid.svelte | 41 --------------------- src/components/InlineSvg.svelte | 65 ++++++++++++++++++++++++++++++++- 4 files changed, 72 insertions(+), 71 deletions(-) delete mode 100644 src/components/Grid.svelte diff --git a/public/global.css b/public/global.css index 144293f..8e87861 100644 --- a/public/global.css +++ b/public/global.css @@ -284,12 +284,12 @@ div.logo-image img { } .logo-image svg { - width: 100%; + /* width: 100%; height: 100%; max-width: 100%; max-height: 100%; display: block; - object-fit: contain; + object-fit: contain; */ color: var(--color-default-fill); } diff --git a/src/components/CardFull.svelte b/src/components/CardFull.svelte index abef2af..e591c3f 100644 --- a/src/components/CardFull.svelte +++ b/src/components/CardFull.svelte @@ -6,6 +6,7 @@ import { getDefaultLogoColor, getThemeColor } from "../utils/colorTheme.js"; import { fetchSvgSource } from "../utils/svgSource.js"; import { collections } from '../collections.js'; + import { get } from "svelte/store"; export let show = false; export let logo = null; @@ -101,28 +102,6 @@ window.scrollTo(0, 0); } - // Svelte action to remove width/height from SVGs for responsive scaling - function removeSvgSize(node) { - function cleanSvg() { - const svgs = node.querySelectorAll("svg"); - svgs.forEach((svg) => { - svg.removeAttribute("width"); - svg.removeAttribute("height"); - svg.style.width = "100%"; - svg.style.height = "100%"; - }); - } - cleanSvg(); - // In case SVG is loaded async (e.g. InlineSvg), observe for changes - const observer = new MutationObserver(cleanSvg); - observer.observe(node, { childList: true, subtree: true }); - return { - destroy() { - observer.disconnect(); - }, - }; - } - // Capitalize first letter of a string function capitalizeFirst(str) { if (!str) return ""; @@ -158,7 +137,7 @@
-
+
{#if isSvgLogo(logo)} - import CardSquare from './CardMiddle.svelte'; - - export let logos = []; - export let onCopy; - export let onDownload; - export let setSearchQuery; - export let allLogos = []; - - export let theme; - - -
- {#each logos as logo} - - {:else} -

No logos found matching your search criteria.

- {/each} -
- - diff --git a/src/components/InlineSvg.svelte b/src/components/InlineSvg.svelte index 1d06ee8..5a41162 100644 --- a/src/components/InlineSvg.svelte +++ b/src/components/InlineSvg.svelte @@ -26,17 +26,73 @@ const doc = parser.parseFromString(text, "image/svg+xml"); const svg = doc.documentElement; + // Fix SVG dimensions here too + const viewBox = svg.getAttribute('viewBox'); + if (viewBox) { + // Remove any existing style and dimension attributes + svg.removeAttribute('style'); + svg.removeAttribute('width'); + svg.removeAttribute('height'); + + // Set percentage dimensions to allow scaling + svg.setAttribute('width', '100%'); + svg.setAttribute('height', '100%'); + // svg.setAttribute('preserveAspectRatio', 'none'); + + // Ensure viewBox is preserved for proper clipping + svg.setAttribute('viewBox', viewBox); + } else { + svg.removeAttribute('style'); + svg.setAttribute('width', '100%'); + svg.setAttribute('height', '100%'); + } + // Set currentColor on SVG element if no fill is specified if (!svg.hasAttribute("fill")) { svg.setAttribute("fill", "currentColor"); } svgHtml = doc.documentElement.outerHTML; + svgSource = svgHtml; return; } // Parse and update color only if user selected const parser = new DOMParser(); const doc = parser.parseFromString(text, "image/svg+xml"); + + // Add proper SVG clipping first (before color processing) + const svg = doc.documentElement; + + // Fix SVG dimensions based on viewBox + const viewBox = svg.getAttribute('viewBox'); + if (viewBox) { + // Remove any existing style attribute and fixed dimensions + svg.removeAttribute('style'); + svg.removeAttribute('width'); + svg.removeAttribute('height'); + + // Set percentage dimensions to allow scaling + svg.setAttribute('width', '100%'); + svg.setAttribute('height', '100%'); + + // Ensure viewBox is preserved for proper clipping + svg.setAttribute('viewBox', viewBox); + + // Add preserveAspectRatio to ensure proper scaling + if (!svg.hasAttribute('preserveAspectRatio')) { + svg.setAttribute('preserveAspectRatio', 'xMidYMid meet'); + } + } else { + // If no viewBox, remove style and set percentage dimensions + svg.removeAttribute('style'); + svg.setAttribute('width', '100%'); + svg.setAttribute('height', '100%'); + } + + // Ensure proper overflow handling + svg.setAttribute('overflow', 'visible'); // Let CSS handle the clipping + + // Now process color changes // 1. Parse