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

This commit is contained in:
2026-03-11 14:14:58 +00:00
parent 1d529b48b4
commit 034fa5835a

View File

@@ -1,51 +1,65 @@
"use client";
import { memo } from "react";
import useSvgTextLogo from "./useSvgTextLogo";
import { cls } from "@/lib/utils";
import React, { useState, useEffect } from 'react';
interface SvgTextLogoProps {
logoText: string;
adjustHeightFactor?: number;
verticalAlign?: "top" | "center";
text: string;
className?: string;
fontSize?: number;
fontWeight?: number | string;
fontFamily?: string;
fill?: string;
textAnchor?: 'start' | 'middle' | 'end';
dominantBaseline?: 'auto' | 'text-bottom' | 'alphabetic' | 'ideographic' | 'middle' | 'central' | 'mathematical' | 'hanging';
}
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,
className = '',
fontSize = 48,
fontWeight = 'bold',
fontFamily = 'system-ui, -apple-system, sans-serif',
fill = 'currentColor',
textAnchor = 'middle',
dominantBaseline = 'middle',
}) => {
const [dimensions, setDimensions] = useState({ width: 100, height: 100 });
const svgRef = React.useRef<SVGSVGElement>(null);
useEffect(() => {
if (svgRef.current) {
const textElement = svgRef.current.querySelector('text');
if (textElement) {
const bbox = (textElement as SVGTextElement).getBBox();
setDimensions({
width: Math.ceil(bbox.width) + 20,
height: Math.ceil(bbox.height) + 20,
});
}
}
}, [text, fontSize, fontWeight, fontFamily]);
return (
<svg
ref={svgRef}
viewBox={viewBox}
className={cls("w-full", className)}
style={{ aspectRatio: aspectRatio }}
preserveAspectRatio="none"
role="img"
aria-label={`${logoText} logo`}
width={dimensions.width}
height={dimensions.height}
viewBox={`0 0 ${dimensions.width} ${dimensions.height}`}
className={className}
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"
}}
x={dimensions.width / 2}
y={dimensions.height / 2}
fontSize={fontSize}
fontWeight={fontWeight}
fontFamily={fontFamily}
fill={fill}
textAnchor={textAnchor}
dominantBaseline={dominantBaseline}
>
{logoText}
{text}
</text>
</svg>
);
});
};
SvgTextLogo.displayName = "SvgTextLogo";
export default SvgTextLogo;
export default SvgTextLogo;