Skip to content

🎥 Make videos programmatically with React

generic renderSvg method

Jonny Burgerc8a57a3688c7
diff --git a/packages/shapes/src/rect.tsx b/packages/shapes/src/rect.tsx
index 2e89920f25..1979a3ca5f 100644
--- a/packages/shapes/src/rect.tsx
+++ b/packages/shapes/src/rect.tsx
@@ -1,5 +1,6 @@
-import React from 'react';
+import type React from 'react';
import {makeRect} from './make-rect';
+import {renderSvg} from './render-svg';
export type SquareProps = {
width: number;
@@ -9,22 +10,14 @@ export type SquareProps = {
};
export const Rect: React.FC<SquareProps> = ({width, height, fill, style}) => {
- return (
- <svg
- width={width}
- height={height}
- viewBox={`0 0 ${width} ${height}`}
- xmlns="http://www.w3.org/2000/svg"
- style={style}
- data-shape-type="rect"
- >
- <path
- d={makeRect({
- width,
- height,
- })}
- fill={fill}
- />
- </svg>
- );
+ return renderSvg({
+ fill,
+ height,
+ path: makeRect({
+ width,
+ height,
+ }),
+ width,
+ style,
+ });
};
diff --git a/packages/shapes/src/render-svg.tsx b/packages/shapes/src/render-svg.tsx
new file mode 100644
index 0000000000..89665064bb
--- /dev/null
+++ b/packages/shapes/src/render-svg.tsx
@@ -0,0 +1,28 @@
+import React from 'react';
+
+export const renderSvg = ({
+ width,
+ height,
+ path,
+ fill,
+ style,
+}: {
+ width: number;
+ height: number;
+ path: string;
+ fill?: string;
+ style?: React.CSSProperties;
+}) => {
+ return (
+ <svg
+ width={width}
+ height={height}
+ viewBox={`0 0 ${width} ${height}`}
+ xmlns="http://www.w3.org/2000/svg"
+ style={style}
+ data-shape-type="triangle"
+ >
+ <path d={path} fill={fill} />
+ </svg>
+ );
+};
diff --git a/packages/shapes/src/triangle.tsx b/packages/shapes/src/triangle.tsx
index f0db1ef3cb..1c2c424bef 100644
--- a/packages/shapes/src/triangle.tsx
+++ b/packages/shapes/src/triangle.tsx
@@ -1,38 +1,30 @@
-import React from 'react';
+import type React from 'react';
+import type {MakeTriangleProps} from './make-triangle';
import {makeTriangle} from './make-triangle';
+import {renderSvg} from './render-svg';
-export type TriangleProps = {
- width: number;
- height: number;
+export type TriangleProps = MakeTriangleProps & {
fill?: string;
- direction: 'right' | 'left' | 'top' | 'bottom';
style?: React.CSSProperties;
};
export const Triangle: React.FC<TriangleProps> = ({
+ // TODO: Do not use width and height, but use length of a side
width,
height,
fill,
direction,
style,
}) => {
- return (
- <svg
- width={width}
- height={height}
- viewBox={`0 0 ${width} ${height}`}
- xmlns="http://www.w3.org/2000/svg"
- style={style}
- data-shape-type="triangle"
- >
- <path
- d={makeTriangle({
- width,
- height,
- direction,
- })}
- fill={fill}
- />
- </svg>
- );
+ return renderSvg({
+ fill,
+ height,
+ path: makeTriangle({
+ width,
+ height,
+ direction,
+ }),
+ style,
+ width,
+ });
};