craft.js icon indicating copy to clipboard operation
craft.js copied to clipboard

How to add a custom component to the bottom of the page by clicking on the left side

Open guanfengwu opened this issue 1 year ago • 2 comments

How to add a custom component to the bottom of the page by clicking on the left side

guanfengwu avatar Jan 10 '24 02:01 guanfengwu

Document is here(https://craft.js.org/docs/api/useEditor#creating-new-nodes)

This is simple example.

import { Editor, Element, Frame, UserComponent, useEditor } from "@craftjs/core"

const Text: UserComponent<{ children: string }> = ({ children }) => {
  return <div>{children}</div>
}

const Others: UserComponent = () => {
  return <div style={{ padding: 20, backgroundColor: "pink" }}>others</div>
}

const Toolbox = () => {
  const { actions, query } = useEditor()

  return (
    <div>
      <button
        style={{ backgroundColor: "cyan" }}
        onClick={() => {
          // important!!!
          const newNode = query
            .parseFreshNode({
              data: { type: Text, props: { children: "text-2" } },
            })
            .toNode()
          actions.add(newNode, "ROOT")
        }}
      >
        Add Text to Bottom
      </button>
    </div>
  )
}

export default function Demo() {
  return (
    <div>
      <h1>Demo to Add anywhere</h1>
      <Editor resolver={{ Text, Others }}>
        <Toolbox />
        <Frame>
          <Element is="div" canvas>
            <Text>text-1</Text>
            <Others />
          </Element>
        </Frame>
      </Editor>
    </div>
  )
}

shota-f avatar Jan 23 '24 15:01 shota-f

it only root that works why is that

MARKOTHEDEV avatar Feb 02 '24 13:02 MARKOTHEDEV