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

new(text): Add 'shrink-only' option to 'scaleToFit' #1362

Merged
merged 1 commit into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion packages/visx-text/src/hooks/useText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default function useText(props: TextProps): {

if (isNumber(x) && isNumber(y) && isNumber(width) && scaleToFit && wordsByLines.length > 0) {
const lineWidth = wordsByLines[0].width || 1;
const sx = width / lineWidth;
const sx = scaleToFit === 'shrink-only' ? Math.min(width / lineWidth, 1) : width / lineWidth;
const sy = sx;
const originX = x - sx * x;
const originY = y - sy * y;
Expand Down
2 changes: 1 addition & 1 deletion packages/visx-text/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type OwnProps = {
/** className to apply to the SVGText element. */
className?: string;
/** Whether to scale the fontSize to accommodate the specified width. */
scaleToFit?: boolean;
scaleToFit?: boolean | 'shrink-only';
/** Rotational angle of the text. */
angle?: number;
/** Horizontal text anchor. */
Expand Down
32 changes: 32 additions & 0 deletions packages/visx-text/test/Text.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,38 @@ describe('<Text />', () => {
expect(transform).toBe('matrix(1.25, 0, 0, 1.25, 0, 0)');
});

it("Does not scale above 1 when scaleToFit is set to 'shrink-only'", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

thanks for adding tests! looks great 😄

const {
result: {
current: { transform },
},
} = renderHook(() =>
useText({
width: 300,
scaleToFit: 'shrink-only',
style: { fontFamily: 'Courier' },
children: 'This is really long text',
}),
);
expect(transform).toBe('matrix(1, 0, 0, 1, 0, 0)');
});

it("Shrinks long text when scaleToFit is set to 'shrink-only'", () => {
const {
result: {
current: { transform },
},
} = renderHook(() =>
useText({
width: 30,
scaleToFit: 'shrink-only',
style: { fontFamily: 'Courier' },
children: 'This is really long text',
}),
);
expect(transform).toBe('matrix(0.125, 0, 0, 0.125, 0, 0)');
});

it('Applies transform if angle is given', () => {
const { container } = render(
<Text width={300} angle={45} style={{ fontFamily: 'Courier' }}>
Expand Down