Skip to content

Instantly share code, notes, and snippets.

@keyurparalkar
Created March 4, 2024 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keyurparalkar/6824590ee3edfafc33f22e3247cc19a8 to your computer and use it in GitHub Desktop.
Save keyurparalkar/6824590ee3edfafc33f22e3247cc19a8 to your computer and use it in GitHub Desktop.
A component that converts the JSON object into html div elements
import styled from "styled-components";
import { ComponentType } from "./constants";
export type Tree = {
name: string;
id: string;
style: {
top: number;
left: number;
width: number;
height: number;
backgroundColor: string;
borderRadius?: string;
};
type: ComponentType;
children?: Tree[];
};
type DrawComponentProps = {
tree: Tree;
};
/**
* Canvas
* ---- Container
* ---- Divs
* ---- Divs
*
* ---- Container
* ---- Divs
* ---- Divs
*/
const StyledCanvas = styled.div`
position: relative;
border: 2px solid white;
`;
const DrawComponent = (props: DrawComponentProps) => {
const { tree } = props;
if (tree.type !== ComponentType.CONTAINER) {
return <div style={{ position: "absolute", ...tree.style }}></div>;
}
return (
<StyledCanvas style={{ position: "relative", ...tree.style }}>
{tree.children &&
tree.children.map((child) => (
<DrawComponent key={`${child.name}-${child.id}`} tree={child} />
))}
</StyledCanvas>
);
};
const Canvas = (props: DrawComponentProps) => {
const { tree } = props;
return <DrawComponent tree={tree} />;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment