Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/primitive-svg-props.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"victory-core": minor
---

The primitives `Circle`, `Line`, `Path`, `Rect`, `Text` and `TSpan` accept their element's SVG attributes in TypeScript, such as `<Line x1={25} y1={270} />`, `<Path d="..." />` and `<Circle cx={1} cy={2} r={3} />` (#3122). These worked at runtime but failed to type-check. Where a Victory prop shares a name with an SVG attribute (`origin`, `role`, `style`, `id`, `tabIndex`, `rx`, ...), Victory's type is kept; `name`, which the primitives don't write to the element, stays out. `VictoryPrimitiveShapeProps` takes an optional element type (`VictoryPrimitiveShapeProps<SVGLineElement>`). New exported types: `TSpanProps` (the props of `TSpan`) and `VictoryPrimitiveSvgProps`.
2 changes: 2 additions & 0 deletions packages/victory-core/src/exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import {
StringOrNumberOrList,
Style,
TSpan,
TSpanProps,
Text,
TextAnchorType,
TextProps,
Expand Down Expand Up @@ -125,6 +126,7 @@ import {
VictoryPortal,
VictoryPortalProps,
VictoryPrimitiveShapeProps,
VictoryPrimitiveSvgProps,
VictorySingleLabelableProps,
VictoryStringCallback,
VictoryStringOrNumberCallback,
Expand Down
37 changes: 19 additions & 18 deletions packages/victory-core/src/victory-primitives/circle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ import { evaluateProp } from "../victory-util/helpers";
import { getSvgProps } from "./svg-props";
import { VictoryPrimitiveShapeProps } from "./types";

export const Circle = forwardRef<SVGCircleElement, VictoryPrimitiveShapeProps>(
(props, ref) => {
const { desc, id, tabIndex, ...rest } = props;
export const Circle = forwardRef<
SVGCircleElement,
VictoryPrimitiveShapeProps<SVGCircleElement>
>((props, ref) => {
const { desc, id, tabIndex, ...rest } = props;

const svgProps: React.SVGProps<SVGCircleElement> = {
vectorEffect: "non-scaling-stroke",
id: evaluateProp(id, props)?.toString(),
tabIndex: evaluateProp(tabIndex, props),
...getSvgProps(rest),
};
return desc ? (
<circle {...svgProps} ref={ref}>
<desc>{desc}</desc>
</circle>
) : (
<circle {...svgProps} ref={ref} />
);
},
);
const svgProps: React.SVGProps<SVGCircleElement> = {
vectorEffect: "non-scaling-stroke",
id: evaluateProp(id, props)?.toString(),
tabIndex: evaluateProp(tabIndex, props),
...getSvgProps(rest),
};
return desc ? (
<circle {...svgProps} ref={ref}>
<desc>{desc}</desc>
</circle>
) : (
<circle {...svgProps} ref={ref} />
);
});
6 changes: 4 additions & 2 deletions packages/victory-core/src/victory-primitives/line.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import { render } from "@testing-library/react";
import { SVGWrapper } from "../../../../test/helpers";

import { Line } from "./line";

Expand All @@ -12,8 +13,9 @@ describe("victory-primitives/line", () => {
};

it("should render a line element with the correct coordinates", () => {
// @ts-expect-error "baseProps has no properties in common with VictoryPrimitiveShapeProps"
const { container } = render(<Line {...baseProps} />, { wrapper: "svg" });
const { container } = render(<Line {...baseProps} />, {
wrapper: SVGWrapper,
});
expect(container.querySelector("line")).toMatchInlineSnapshot(`
<line
vector-effect="non-scaling-stroke"
Expand Down
37 changes: 19 additions & 18 deletions packages/victory-core/src/victory-primitives/line.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ import { evaluateProp } from "../victory-util/helpers";
import { getSvgProps } from "./svg-props";
import { VictoryPrimitiveShapeProps } from "./types";

export const Line = forwardRef<SVGLineElement, VictoryPrimitiveShapeProps>(
(props, ref) => {
const { desc, id, tabIndex, ...rest } = props;
export const Line = forwardRef<
SVGLineElement,
VictoryPrimitiveShapeProps<SVGLineElement>
>((props, ref) => {
const { desc, id, tabIndex, ...rest } = props;

const svgProps: React.SVGProps<SVGLineElement> = {
vectorEffect: "non-scaling-stroke",
id: evaluateProp(id, props)?.toString(),
tabIndex: evaluateProp(tabIndex, props),
...getSvgProps(rest),
};
const svgProps: React.SVGProps<SVGLineElement> = {
vectorEffect: "non-scaling-stroke",
id: evaluateProp(id, props)?.toString(),
tabIndex: evaluateProp(tabIndex, props),
...getSvgProps(rest),
};

return desc ? (
<line {...svgProps} ref={ref}>
<desc>{desc}</desc>
</line>
) : (
<line {...svgProps} ref={ref} />
);
},
);
return desc ? (
<line {...svgProps} ref={ref}>
<desc>{desc}</desc>
</line>
) : (
<line {...svgProps} ref={ref} />
);
});
35 changes: 18 additions & 17 deletions packages/victory-core/src/victory-primitives/path.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ import { evaluateProp } from "../victory-util/helpers";
import { getSvgProps } from "./svg-props";
import { VictoryPrimitiveShapeProps } from "./types";

export const Path = forwardRef<SVGPathElement, VictoryPrimitiveShapeProps>(
(props, ref) => {
const { desc, id, tabIndex, ...rest } = props;
export const Path = forwardRef<
SVGPathElement,
VictoryPrimitiveShapeProps<SVGPathElement>
>((props, ref) => {
const { desc, id, tabIndex, ...rest } = props;

const svgProps: React.SVGProps<SVGPathElement> = {
id: evaluateProp(id, props)?.toString(),
tabIndex: evaluateProp(tabIndex, props),
...getSvgProps(rest),
};
const svgProps: React.SVGProps<SVGPathElement> = {
id: evaluateProp(id, props)?.toString(),
tabIndex: evaluateProp(tabIndex, props),
...getSvgProps(rest),
};

return desc ? (
<path {...svgProps} ref={ref}>
<desc>{desc}</desc>
</path>
) : (
<path {...svgProps} ref={ref} />
);
},
);
return desc ? (
<path {...svgProps} ref={ref}>
<desc>{desc}</desc>
</path>
) : (
<path {...svgProps} ref={ref} />
);
});
37 changes: 19 additions & 18 deletions packages/victory-core/src/victory-primitives/rect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ import { evaluateProp } from "../victory-util/helpers";
import { getSvgProps } from "./svg-props";
import { VictoryPrimitiveShapeProps } from "./types";

export const Rect = forwardRef<SVGRectElement, VictoryPrimitiveShapeProps>(
(props, ref) => {
const { desc, id, tabIndex, ...rest } = props;
export const Rect = forwardRef<
SVGRectElement,
VictoryPrimitiveShapeProps<SVGRectElement>
>((props, ref) => {
const { desc, id, tabIndex, ...rest } = props;

const svgProps: React.SVGProps<SVGRectElement> = {
vectorEffect: "non-scaling-stroke",
id: evaluateProp(id, props)?.toString(),
tabIndex: evaluateProp(tabIndex, props),
...getSvgProps(rest),
};
const svgProps: React.SVGProps<SVGRectElement> = {
vectorEffect: "non-scaling-stroke",
id: evaluateProp(id, props)?.toString(),
tabIndex: evaluateProp(tabIndex, props),
...getSvgProps(rest),
};

return desc ? (
<rect {...svgProps} ref={ref}>
<desc>{desc}</desc>
</rect>
) : (
<rect {...svgProps} ref={ref} />
);
},
);
return desc ? (
<rect {...svgProps} ref={ref}>
<desc>{desc}</desc>
</rect>
) : (
<rect {...svgProps} ref={ref} />
);
});
66 changes: 66 additions & 0 deletions packages/victory-core/src/victory-primitives/svg-props.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,70 @@ describe("victory-primitives/svg-props", () => {
error.mockRestore();
},
);

// Also a type test: the primitives accept their element's SVG attributes
// (#3122).
it("passes SVG attributes through to the element", () => {
const { container } = render(
<svg>
<Circle cx={1} cy={2} r={3} />
<Line x1={1} x2={2} y1={3} y2={4} strokeDasharray="4,2" />
<Path d="M0,0L1,1" fillOpacity={0.5} />
<Rect x={1} y={2} width={3} height={4} rx={1} />
<Text x={1} y={2} dy="0.5em" textAnchor="middle">
<TSpan x={1} dx={2} fontSize={12}>
label
</TSpan>
</Text>
</svg>,
);
const attributes = (tag: string) =>
Object.fromEntries(
[...container.querySelector(tag)!.attributes].map((a) => [
a.name,
a.value,
]),
);

expect(attributes("circle")).toMatchObject({ cx: "1", cy: "2", r: "3" });
expect(attributes("line")).toMatchObject({
x1: "1",
x2: "2",
y1: "3",
y2: "4",
"stroke-dasharray": "4,2",
});
expect(attributes("path")).toMatchObject({
d: "M0,0L1,1",
"fill-opacity": "0.5",
});
expect(attributes("rect")).toMatchObject({
x: "1",
y: "2",
width: "3",
height: "4",
rx: "1",
});
expect(attributes("text")).toMatchObject({
x: "1",
y: "2",
dy: "0.5em",
"text-anchor": "middle",
});
expect(attributes("tspan")).toMatchObject({
x: "1",
dx: "2",
"font-size": "12",
});
});

it("types only what reaches the element", () => {
// `getSvgProps` drops `name`, so the types leave it out too.
// @ts-expect-error `name` is an SVG attribute, but never written
expect(<Line name="x" />).toBeDefined();
// @ts-expect-error not an SVG attribute
expect(<Line notAnAttribute={1} />).toBeDefined();
// @ts-expect-error Victory's own type wins: `rx` is a number
expect(<Rect rx="2" />).toBeDefined();
});
});
7 changes: 4 additions & 3 deletions packages/victory-core/src/victory-primitives/svg-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ const NON_SVG_PROPS = [
] as const;
const nonSvgProps = new Set<string>(NON_SVG_PROPS);

/** Props `getSvgProps` drops: never written to the primitive's element. */
export type NonSvgProp = (typeof NON_SVG_PROPS)[number];

/**
* Drops props that aren't SVG attributes, so primitives can spread the rest
* onto their element.
*/
export function getSvgProps<T extends object>(
props: T,
): Omit<T, (typeof NON_SVG_PROPS)[number]> {
export function getSvgProps<T extends object>(props: T): Omit<T, NonSvgProp> {
const svgProps = {} as T;
for (const key in props) {
if (!nonSvgProps.has(key)) {
Expand Down
8 changes: 7 additions & 1 deletion packages/victory-core/src/victory-primitives/text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ import React from "react";
import { evaluateProp } from "../victory-util/helpers";
import { VictoryCommonPrimitiveProps } from "../victory-util/common-props";
import { getSvgProps } from "./svg-props";
import { VictoryPrimitiveSvgProps } from "./types";

export interface TextProps extends VictoryCommonPrimitiveProps {
interface TextOwnProps extends VictoryCommonPrimitiveProps {
children?: React.ReactNode;
desc?: string;
title?: string;
}

export interface TextProps
extends
TextOwnProps,
VictoryPrimitiveSvgProps<SVGTextElement, TextOwnProps> {}

export const Text = (props: TextProps) => {
const { children, desc, id, tabIndex, title, ...rest } = props;

Expand Down
8 changes: 7 additions & 1 deletion packages/victory-core/src/victory-primitives/tspan.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import React from "react";
import { evaluateProp } from "../victory-util/helpers";
import { VictoryCommonPrimitiveProps } from "../victory-util/common-props";
import { getSvgProps } from "./svg-props";
import { VictoryPrimitiveSvgProps } from "./types";

export const TSpan = (props: VictoryCommonPrimitiveProps) => {
export interface TSpanProps
extends
VictoryCommonPrimitiveProps,
VictoryPrimitiveSvgProps<SVGTSpanElement, VictoryCommonPrimitiveProps> {}

export const TSpan = (props: TSpanProps) => {
/* eslint-disable-next-line @typescript-eslint/no-unused-vars --
* a <tspan> can't contain a <desc>
*/
Expand Down
21 changes: 20 additions & 1 deletion packages/victory-core/src/victory-primitives/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type React from "react";
import { VictoryCommonPrimitiveProps } from "../victory-util/common-props";
import type { NonSvgProp } from "./svg-props";

export type ScatterSymbolType =
| "circle"
Expand All @@ -11,8 +13,25 @@ export type ScatterSymbolType =
| "triangleDown"
| "triangleUp";

export interface VictoryPrimitiveShapeProps extends VictoryCommonPrimitiveProps {
/**
* The SVG attributes of a primitive's element (`x1`, `d`, `cx`, ...), less
* `ref`, the props the primitives don't write to the element (`name`, ...),
* and the props Victory types itself (`TOwnProps`), whose types win.
*/
export type VictoryPrimitiveSvgProps<
TElement extends SVGElement,
TOwnProps = object,
> = Omit<React.SVGProps<TElement>, keyof TOwnProps | NonSvgProp | "ref">;

interface VictoryPrimitiveShapeOwnProps extends VictoryCommonPrimitiveProps {
desc?: string;
rx?: number;
ry?: number;
}

export interface VictoryPrimitiveShapeProps<
TElement extends SVGElement = SVGElement,
>
extends
VictoryPrimitiveShapeOwnProps,
VictoryPrimitiveSvgProps<TElement, VictoryPrimitiveShapeOwnProps> {}
1 change: 1 addition & 0 deletions packages/victory/src/victory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
SliceProps,
Style,
TSpan,
TSpanProps,
Text,
TextProps,
TextSize,
Expand Down
Loading