Skip to content

add textPath prop to Text component #926

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
29 changes: 23 additions & 6 deletions packages/visx-text/src/Text.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import reduceCSSCalc from 'reduce-css-calc';
import getStringWidth from './util/getStringWidth';
import uniqueId from 'lodash/uniqueId';

const SVG_STYLE = { overflow: 'visible' };

Expand Down Expand Up @@ -67,12 +68,15 @@ type OwnProps = {
width?: number;
/** String (or number coercible to one) to be styled and positioned. */
children?: string | number;
/** Path for the text to follow along. */
textPath?: string;
};

export type TextProps = OwnProps & Omit<SVGTextProps, keyof OwnProps>;

type TextState = {
wordsByLines: WordsWithWidth[];
textPathId: string;
};

class Text extends React.Component<TextProps, TextState> {
Expand All @@ -90,6 +94,7 @@ class Text extends React.Component<TextProps, TextState> {

state: TextState = {
wordsByLines: [],
textPathId: ''
};

private wordsWithWidth: WordWithWidth[] = [];
Expand All @@ -98,6 +103,7 @@ class Text extends React.Component<TextProps, TextState> {

componentDidMount() {
this.updateWordsByLines(this.props, true);
this.updateTextPathId(this.props);
}

componentDidUpdate(prevProps: TextProps, prevState: TextState) {
Expand All @@ -111,6 +117,12 @@ class Text extends React.Component<TextProps, TextState> {
this.updateWordsByLines(this.props, needCalculate);
}

updateTextPathId(props: TextProps) {
if(props.textPath) {
this.setState({ textPathId: uniqueId('text-path-') })
}
Copy link
Member

@hshoff hshoff Nov 11, 2020

Choose a reason for hiding this comment

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

I see this is called from componentDidMount(). Ideally we could move this initial state to the constructor() because it doesn't rely on the DOM.

Copy link
Author

Choose a reason for hiding this comment

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

Sorry, it took me so long to fix this. I've moved the whole state initialization into the constructor with the id generation in there as well.

}

updateWordsByLines(props: TextProps, needCalculate: boolean = false) {
// Only perform calculations if using features that require them (multiline, scaleToFit)
if (props.width || props.scaleToFit) {
Expand Down Expand Up @@ -178,10 +190,11 @@ class Text extends React.Component<TextProps, TextState> {
capHeight,
innerRef,
width,
textPath,
...textProps
} = this.props;

const { wordsByLines } = this.state;
const { wordsByLines, textPathId } = this.state;
const { x, y } = textProps;

// Cannot render <text> if x or y is invalid
Expand Down Expand Up @@ -214,15 +227,19 @@ class Text extends React.Component<TextProps, TextState> {
}

const transform = transforms.length > 0 ? transforms.join(' ') : undefined;
const text = wordsByLines.map((line, index) => (
<tspan key={index} x={x} dy={(index === 0 ? startDy : lineHeight)}>
{line.words.join(' ')}
</tspan>
));

return (
<svg ref={innerRef} x={dx} y={dy} fontSize={textProps.fontSize} style={SVG_STYLE}>
{textPath && <path id={textPathId} d={textPath} fill="none" />}
<text transform={transform} {...textProps} textAnchor={textAnchor}>
{wordsByLines.map((line, index) => (
<tspan key={index} x={x} dy={index === 0 ? startDy : lineHeight}>
{line.words.join(' ')}
</tspan>
))}
{textPath ? (<textPath href={`#${textPathId}`}>
{text}
</textPath>) : text}
</text>
</svg>
);
Expand Down
19 changes: 19 additions & 0 deletions packages/visx-text/test/Text.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,23 @@ describe('<Text />', () => {
wrapper.setProps({ verticalAnchor: 'start' });
expect(getVerticalOffset(wrapper)).toBe('0.71em');
});
it('Should render textPath when textPath is passed', () => {
const wrapper = mount<Text>(<Text textPath='M10 10'>Text path test</Text>);

const textPath = wrapper.find('textPath');
const path = wrapper.find('path');

expect(textPath).toHaveLength(1);
expect(path).toHaveLength(1);

expect(textPath.props().href).toEqual(`#${path.props().id}`);
expect(path.props().d).toEqual('M10 10');
});

it('Should not render textPath when textPath is not passed', () => {
const wrapper = mount<Text>(<Text>Text path test</Text>);

expect(wrapper.find('textPath')).toHaveLength(0);
expect(wrapper.find('path')).toHaveLength(0);
});
});