-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tinytag): replace svg to render (#517)
* feat(tinytag): replace svg to render * test(tinytag): update tinyTag snapshots * feat(tinytag): remove default font-family && currentColor in svg * fix(tinytag): compute padding error && change height 15px * feat(tinytag): add useSvgWidth * feat(tinytag): update useSvgWidth hooks && change ref binding dom --------- Co-authored-by: beier <[email protected]>
- Loading branch information
1 parent
76c4034
commit 09c4c77
Showing
6 changed files
with
108 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.data-tag { | ||
color: #D1D1D1; | ||
&:hover { | ||
color: #D56161; | ||
} | ||
} | ||
|
||
.ued-tag { | ||
color: #D56161; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,68 @@ | ||
import React from 'react'; | ||
import React, { useCallback, useEffect, useState } from 'react'; | ||
import classNames from 'classnames'; | ||
|
||
import './style.scss'; | ||
|
||
type UseSvgRef = (element: SVGTextElement) => void; | ||
type UseSvgWidthResult = [UseSvgRef, number, number]; | ||
|
||
interface ITinyTag extends React.HTMLAttributes<HTMLSpanElement> { | ||
value?: string; | ||
color?: string; | ||
value: string; | ||
} | ||
|
||
export default function TinyTag({ color, value, className, ...restProps }: ITinyTag) { | ||
const useSvgWidth = (): UseSvgWidthResult => { | ||
const [element, ref] = useState<SVGTextElement | null>(null); | ||
const [svgWidth, setSvgWidth] = useState(0); | ||
const [rectWidth, setRectWidth] = useState(0); | ||
|
||
const getWidth = useCallback(() => { | ||
if (!element) return; | ||
const paddingWidth = 8; | ||
const textWidth = Math.round(element.getBoundingClientRect()?.width); | ||
const svgWidth = textWidth + paddingWidth; | ||
setSvgWidth(svgWidth); | ||
setRectWidth(Math.max(svgWidth - 1, 0)); | ||
}, [element]); | ||
|
||
useEffect(() => { | ||
getWidth(); | ||
}, [element]); | ||
|
||
return [ref, svgWidth, rectWidth]; | ||
}; | ||
|
||
export default function TinyTag({ value, className, ...restProps }: ITinyTag) { | ||
const [ref, svgWidth, rectWidth] = useSvgWidth(); | ||
return ( | ||
<span | ||
className={classNames('dtc-tinyTag', className)} | ||
style={{ borderColor: color, color }} | ||
{...restProps} | ||
> | ||
{value} | ||
<span className={classNames('dtc-tinyTag', className)} {...restProps}> | ||
<svg | ||
width={svgWidth} | ||
height="15" | ||
viewBox={`0 0 ${svgWidth} 15`} | ||
fill="none" | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
<rect | ||
x="0.5" | ||
y="0.5" | ||
width={rectWidth} | ||
height="14" | ||
rx="1.5" | ||
stroke="currentColor" | ||
strokeLinejoin="bevel" | ||
/> | ||
<text | ||
ref={ref} | ||
fill="currentColor" | ||
xmlSpace="preserve" | ||
fontSize="10" | ||
letterSpacing="0px" | ||
> | ||
<tspan x="4" y="11.1"> | ||
{value} | ||
</tspan> | ||
</text> | ||
</svg> | ||
</span> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |