Giter VIP home page Giter VIP logo

Comments (1)

rajatdhoot123 avatar rajatdhoot123 commented on September 22, 2024
"use client";
import { Editor, Frame, Element, useEditor, useNode } from "@craftjs/core";

import RenderNode from "./render_node";

import { COMPONENTS_ARRAY } from "../constants__/floater";

const Container = ({ background, padding, children, ...props }) => {
  const {
    connectors: { connect, drag },
  } = useNode();
  return (
    <div
      {...props}
      ref={(ref) => connect(drag(ref))}
      style={{
        margin: "5px 0",
        background,
        padding: `${padding}px`,
        zIndex: 9999,
      }}
    >
      {children}
    </div>
  );
};

const SideBar = () => {
  const { connectors } = useEditor();

  return (
    <div className="space-y-6 p-5">
      {COMPONENTS_ARRAY.map(({ name, components }) => (
        <div className="p-2" key={name}>
          <div>{name}</div>
          <ul className="flex flex-col">
            {components.map((comp, index) => (
              <button
                key={index}
                ref={(ref) => connectors.create(ref, comp)}
              >{`Varient ${index + 1}`}</button>
            ))}
          </ul>
        </div>
      ))}
    </div>
  );
};

const all_components = COMPONENTS_ARRAY.reduce((acc, currentComp) => {
  return {
    ...acc,
    ...currentComp.components.reduce((acc, currentVarient, index) => {
      return {
        ...acc,
        [`${currentComp.name}${index + 1}`]: currentVarient,
      };
    }, {}),
  };
}, {});

const App = () => {
  return (
    <div className="w-full flex page-container">
      <Editor
        onRender={RenderNode}
        resolver={{
          Container,
          ...all_components,
        }}
      >
        <div className="w-3/12">
          <SideBar />
        </div>
        <div className="w-full min-h-screen">
          <Frame className="h-56 w-full bg-red-100">
            <Element
              canvas
              is={Container}
              width="800px"
              height="auto"
              background={{ r: 255, g: 255, b: 255, a: 1 }}
              padding={["40", "40", "40", "40"]}
              custom={{ displayName: "App" }}
            >
              <div>Drag and Drop your component</div>
            </Element>
          </Frame>
        </div>
      </Editor>
    </div>
  );
};

export default App;

Here is full code

from craft.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.