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

This commit is contained in:
2026-03-12 04:18:49 +00:00
parent 158e77f210
commit c4426bceb2

View File

@@ -1,51 +1,61 @@
"use client";
import { memo } from "react";
import useSvgTextLogo from "./useSvgTextLogo";
import { cls } from "@/lib/utils";
import React, { useEffect, useRef } from 'react';
interface SvgTextLogoProps {
logoText: string;
adjustHeightFactor?: number;
verticalAlign?: "top" | "center";
text?: string;
className?: string;
textClassName?: string;
fontSize?: number;
fontWeight?: number | string;
letterSpacing?: number;
dominantBaseline?: 'auto' | 'text-top' | 'hanging' | 'middle' | 'central' | 'text-bottom' | 'ideographic' | 'mathematical' | 'inherit';
}
const SvgTextLogo = memo<SvgTextLogoProps>(function SvgTextLogo({
logoText,
adjustHeightFactor,
verticalAlign = "top",
className = "",
}) {
const { svgRef, textRef, viewBox, aspectRatio } = useSvgTextLogo(logoText, false, adjustHeightFactor);
export const SvgTextLogo: React.FC<SvgTextLogoProps> = ({
text = 'Logo',
className = '',
textClassName = '',
fontSize = 32,
fontWeight = 700,
letterSpacing = 0,
dominantBaseline = 'central',
}) => {
const svgRef = useRef<SVGSVGElement>(null);
useEffect(() => {
if (svgRef.current) {
const textElement = svgRef.current.querySelector('text');
if (textElement) {
const bbox = textElement.getBBox();
svgRef.current.setAttribute('viewBox', `${bbox.x - 10} ${bbox.y - 10} ${bbox.width + 20} ${bbox.height + 20}`);
svgRef.current.setAttribute('width', String(bbox.width + 20));
svgRef.current.setAttribute('height', String(bbox.height + 20));
}
}
}, [text]);
return (
<svg
ref={svgRef}
viewBox={viewBox}
className={cls("w-full", className)}
style={{ aspectRatio: aspectRatio }}
preserveAspectRatio="none"
role="img"
aria-label={`${logoText} logo`}
className={className}
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 200 60"
width="200"
height="60"
>
<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="100"
y="30"
textAnchor="middle"
dominantBaseline={dominantBaseline}
fontSize={fontSize}
fontWeight={fontWeight}
letterSpacing={letterSpacing}
className={textClassName}
>
{logoText}
{text}
</text>
</svg>
);
});
};
SvgTextLogo.displayName = "SvgTextLogo";
export default SvgTextLogo;
export default SvgTextLogo;