Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tinytag): replace svg to render #517

Merged
merged 6 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/tinyTag/__tests__/__snapshots__/tinyTag.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,36 @@ exports[`test StatusTag should match snapshot 1`] = `
<span
class="dtc-tinyTag dtc-test"
>
完成
<svg
fill="none"
height="16"
viewBox="0 0 9 16"
xmlns="http://www.w3.org/2000/svg"
>
<rect
height="14"
rx="1.5"
stroke="#1D78FF"
stroke-linejoin="bevel"
width="8"
x="0.5"
y="1"
/>
<text
fill="#1D78FF"
font-family="PingFang SC"
font-size="10"
letter-spacing="0px"
xml:space="preserve"
>
<tspan
x="4"
y="11.6"
>
完成
</tspan>
</text>
</svg>
</span>
</DocumentFragment>
`;
58 changes: 50 additions & 8 deletions src/tinyTag/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useRef, useState } from 'react';
import classNames from 'classnames';
LuckyFBB marked this conversation as resolved.
Show resolved Hide resolved

import './style.scss';
Expand All @@ -8,14 +8,56 @@ interface ITinyTag extends React.HTMLAttributes<HTMLSpanElement> {
color?: string;
}

export default function TinyTag({ color, value, className, ...restProps }: ITinyTag) {
const DEFAULT_COLOR = '#1D78FF';

export default function TinyTag({
color = DEFAULT_COLOR,
value,
LuckyFBB marked this conversation as resolved.
Show resolved Hide resolved
className,
...restProps
}: ITinyTag) {
const domRef = useRef<HTMLDivElement>(null);
const [width, setWidth] = useState(0);
shiqiWang0 marked this conversation as resolved.
Show resolved Hide resolved

const getTextWidth = () => {
const text = domRef?.current?.getElementsByTagName('text')?.[0];
const paddingWidth = 9;
if (!text) return;
const widthSvg = text.getBoundingClientRect()?.width + paddingWidth;
setWidth(widthSvg);
};
useEffect(() => {
getTextWidth();
}, []);
return (
<span
className={classNames('dtc-tinyTag', className)}
style={{ borderColor: color, color }}
{...restProps}
>
{value}
<span ref={domRef} className={classNames('dtc-tinyTag', className)} {...restProps}>
<svg
height="16"
viewBox={`0 0 ${width} 16`}
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<rect
x="0.5"
y="1"
width={width - 1}
height="14"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里加个判断吧?width初始值为0,在第一次渲染时,rect的width变成了 -1,会出现错误
Error: attribute width: A negative value is not valid. ("-1")

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 会提取到 hook 里面, 处理掉

rx="1.5"
stroke={color}
strokeLinejoin="bevel"
/>
<text
fill={color}
xmlSpace="preserve"
fontFamily="PingFang SC"
fontSize="10"
shiqiWang0 marked this conversation as resolved.
Show resolved Hide resolved
letterSpacing="0px"
>
<tspan x="4" y="11.6">
{value}
</tspan>
</text>
</svg>
</span>
);
}
15 changes: 2 additions & 13 deletions src/tinyTag/style.scss
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
$primaryColor: #1D78FF;
$factor: 0.83;

.dtc-tinyTag {
border-radius: 2px;
border: 1px solid $primaryColor;
font-weight: 400;
color: $primaryColor;
line-height: calc(15px / $factor);
padding: 0 calc(4px / $factor);
font-size: 12px;
display: inline-block;
transform: scale($factor);
user-select: none;
white-space: nowrap;
display: inline-block;
transform: translateY(3px);
}
Loading