-
Notifications
You must be signed in to change notification settings - Fork 715
/
Text.tsx
41 lines (36 loc) · 1.04 KB
/
Text.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import React from 'react';
import useText from './hooks/useText';
import { TextProps } from './types';
export { TextProps } from './types';
const SVG_STYLE = { overflow: 'visible' };
export default function Text(props: TextProps) {
const {
dx = 0,
dy = 0,
textAnchor = 'start',
innerRef,
verticalAnchor,
angle,
lineHeight = '1em',
// eslint-disable-next-line @typescript-eslint/no-unused-vars
scaleToFit = false,
capHeight,
width,
...textProps
} = props;
const { x = 0, fontSize } = textProps;
const { wordsByLines, startDy, transform } = useText(props);
return (
<svg ref={innerRef} x={dx} y={dy} fontSize={fontSize} style={SVG_STYLE}>
{wordsByLines.length > 0 ? (
<text transform={transform} {...textProps} textAnchor={textAnchor}>
{wordsByLines.map((line, index) => (
<tspan key={index} x={x} dy={index === 0 ? startDy : lineHeight}>
{line.words.join(' ')}
</tspan>
))}
</text>
) : null}
</svg>
);
}