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

This commit is contained in:
2026-03-11 14:25:28 +00:00
parent 533f15f087
commit 34dfd2bb6e

View File

@@ -1,51 +1,59 @@
"use client";
import { memo } from "react";
import useSvgTextLogo from "./useSvgTextLogo";
import { cls } from "@/lib/utils";
import React, { useMemo } from "react";
interface SvgTextLogoProps {
logoText: string;
adjustHeightFactor?: number;
verticalAlign?: "top" | "center";
text: string;
fontSize?: number;
fontFamily?: string;
fontWeight?: string | number;
fill?: string;
className?: 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 = 24,
fontFamily = "Arial", fontWeight = "bold", fill = "currentColor", className = ""}) => {
const textMetrics = useMemo(() => {
const canvas = typeof document !== "undefined" ? document.createElement("canvas") : null;
if (!canvas) return { width: text.length * fontSize * 0.5, height: fontSize };
const ctx = canvas.getContext("2d");
if (!ctx) return { width: text.length * fontSize * 0.5, height: fontSize };
ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
const metrics = ctx.measureText(text);
return {
width: metrics.width,
height: fontSize,
};
}, [text, fontSize, fontFamily, fontWeight]);
const padding = 8;
const svgWidth = textMetrics.width + padding * 2;
const svgHeight = textMetrics.height + 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}
viewBox={`0 0 ${svgWidth} ${svgHeight}`}
xmlns="http://www.w3.org/2000/svg"
className={className}
>
<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"
}}
x={padding}
y={padding + fontSize * 0.75}
fontSize={fontSize}
fontFamily={fontFamily}
fontWeight={fontWeight}
fill={fill}
dominantBaseline="middle"
>
{logoText}
{text}
</text>
</svg>
);
});
};
SvgTextLogo.displayName = "SvgTextLogo";
export default SvgTextLogo;
export default SvgTextLogo;