Update src/components/shared/SvgTextLogo/SvgTextLogo.tsx

This commit is contained in:
2026-03-11 18:33:00 +00:00
parent 9455e1f711
commit 3c5fbf9c4f

View File

@@ -1,51 +1,97 @@
"use client";
import { memo } from "react";
import useSvgTextLogo from "./useSvgTextLogo";
import { cls } from "@/lib/utils";
import React from 'react';
interface SvgTextLogoProps {
logoText: string;
adjustHeightFactor?: number;
verticalAlign?: "top" | "center";
text: string;
fontSize?: number;
fontWeight?: number | string;
letterSpacing?: number;
className?: string;
textClassName?: string;
}
const SvgTextLogo = memo<SvgTextLogoProps>(function SvgTextLogo({
logoText,
adjustHeightFactor,
verticalAlign = "top",
className = "",
}) {
const { svgRef, textRef, viewBox, aspectRatio } = useSvgTextLogo(logoText, false, adjustHeightFactor);
const SvgTextLogo: React.FC<SvgTextLogoProps> = ({
text,
fontSize = 48,
fontWeight = 700,
letterSpacing = 0,
className = '',
textClassName = '',
}) => {
const words = text.split(' ');
let currentX = 0;
const padding = 20;
const lineHeight = fontSize * 1.2;
const maxWidth = 800;
let lines: Array<{ words: string[]; width: number }> = [];
let currentLine: string[] = [];
let currentLineWidth = 0;
// Simple word wrapping
words.forEach((word) => {
const wordWidth = word.length * (fontSize * 0.6);
if (currentLineWidth + wordWidth > maxWidth && currentLine.length > 0) {
lines.push({ words: currentLine, width: currentLineWidth });
currentLine = [word];
currentLineWidth = wordWidth;
} else {
currentLine.push(word);
currentLineWidth += wordWidth + letterSpacing;
}
});
if (currentLine.length > 0) {
lines.push({ words: currentLine, width: currentLineWidth });
}
const svgHeight = lines.length * lineHeight + padding * 2;
const svgWidth = maxWidth + padding * 2;
return (
<svg
ref={svgRef}
viewBox={viewBox}
className={cls("w-full", className)}
style={{ aspectRatio: aspectRatio }}
preserveAspectRatio="none"
role="img"
aria-label={`${logoText} logo`}
width={svgWidth}
height={svgHeight}
className={className}
viewBox={`0 0 ${svgWidth} ${svgHeight}`}
xmlns="http://www.w3.org/2000/svg"
>
<text
ref={textRef}
x="0"
y={verticalAlign === "center" ? "50%" : "0"}
className="font-bold fill-current"
style={{
fontSize: "20px",
letterSpacing: "-0.02em",
dominantBaseline: verticalAlign === "center" ? "middle" : "text-before-edge"
}}
>
{logoText}
</text>
<defs>
<style>{`
.svg-text {
font-size: ${fontSize}px;
font-weight: ${fontWeight};
letter-spacing: ${letterSpacing}px;
fill: currentColor;
}
`}</style>
</defs>
{lines.map((line, lineIndex) => {
const y = padding + (lineIndex + 1) * lineHeight;
let x = padding;
return (
<g key={lineIndex}>
{line.words.map((word, wordIndex) => {
const wordElement = (
<text
key={wordIndex}
x={x}
y={y}
className={`svg-text ${textClassName}`}
dominantBaseline="hanging"
>
{word}
</text>
);
x += word.length * (fontSize * 0.6) + letterSpacing;
return wordElement;
})}
</g>
);
})}
</svg>
);
});
SvgTextLogo.displayName = "SvgTextLogo";
};
export default SvgTextLogo;