craft.js

A React Framework for building extensible drag and drop page editors

README


craft.js


undefined undefined
undefined 72202590-4d05f500-349c-11ea-9e43-1da1cb0c30e9.gif

Page editors are a great way to provide an excellent user experience. However, to build one is often a pretty dreadful task.

There're existing libraries that come with a fully working page editor out of the box with a user interface and editable components. However, if you wish to make customisations such as modifying the user interface and its behavior, it will most definitely involve modifying the library itself.

Craft.js solves this problem by modularising the building blocks of a page editor. It ships with a drag-n-drop system and handles the way user components should be rendered, updated and moved - among other things. With this, you'll be able to build your own page editor exactly how you want it to look and behave.

Docs



Examples


These examples should give you an idea on the flexibility of Craft.js.

Both these examples look very different from each other, with very different UI. But they are both built with Craft.js! 🤯


Features 🔥


It's just React


No need for complicated plugin systems. Design your editor from top to bottom the same way as you would design any other frontend application in React.

A simple user component can easily be defined as such:

  1. ``` js
  2. import {useNode} from "@craftjs/core";

  3. const TextComponent = ({text}) => {
  4.   const { connectors: {drag} } = useNode();

  5.   return (
  6.     <div ref={drag}>
  7.       <h2>{text}</h2>
  8.     </div>
  9.   )
  10. }
  11. ```

Heck, the entire UI of your page editor is built using just React.

  1. ``` js
  2. import React from "react";
  3. import {Editor, Frame, Canvas, Selector} from "@craftjs/core";
  4. const App = () => {
  5.   return (
  6.     <div>
  7.       <header>Some fancy header or whatever</header>
  8.       <Editor>
  9.         // Editable area starts here
  10.         <Frame resolver={{TextComponent, Container}}>
  11.           <Canvas>
  12.             <TextComponent text="I'm already rendered here" />
  13.           </Canvas>
  14.         </Frame>
  15.       </Editor>
  16.     </div>
  17.   )
  18. }
  19. ```

Control how your components are edited


An obvious requirement for page editors is that they need to allow users to edit components. With Craft.js, you control the process of which these components should be edited.

In the following example, when the user clicks on a component, we'll display a modal that requires the user to input a value for the textprop. As the input value changes, the component will be re-rendered with updated prop.

  1. ``` js
  2. import {useNode} from "@craftjs/core";

  3. const TextComponent = ({text}) => {
  4.   const { connectors: { connect, drag }, isClicked, actions: {setProp} } = useNode(
  5.     (state) => ({
  6.       isClicked: state.event.selected,
  7.     })
  8.   );

  9.   return (
  10.     <div ref={dom => connect(drag(dom))}>
  11.       <h2>{text}</h2>
  12.       {
  13.         isClicked ? (
  14.           <Modal>
  15.             <input
  16.               type="text"
  17.               value={text}
  18.               onChange={e => setProp(e.target.value)}
  19.             />
  20.           </Modal>
  21.         )
  22.       }
  23.     </div>
  24.   )
  25. }
  26. ```

With this, you could easily implement content editable text or drag-to-resize components, just as any modern page editor would have.

User components with droppable regions


Let's say we need a "Container" component which users can drop into the editor. Additionally, we would also like them to be able to drag and drop other components into the Container.

In Craft.js, it's as simple as calling the ``

  1. ``` js
  2. import {useNode} from "@craftjs/core";
  3. const Container = () => {
  4.   const { connectors: {drag} } = useNode();

  5.   return (
  6.     <div ref={drag}>
  7.       <Canvas id="drop_section">
  8.          // Now users will be able to drag/drop components into this section
  9.         <TextComponent />
  10.       </Canvas>
  11.     </div>
  12.   )
  13. }
  14. ```

Extensible


Craft.js provides an expressive API which allows you to easily read and manipulate the editor state. Let's say you would like to implement a copy function for a component:

  1. ``` js
  2. import {useEditor, useNode} from "@craftjs/core";
  3. const Container = () => {
  4.   const { actions: {add}, query: { createNode, node } } = useEditor();
  5.   const { id, connectors: {drag, connect} } = useNode();
  6.   return (
  7.     <div ref={dom => connect(drag(dom))}>
  8.       ...
  9.       <a onClick={() => {
  10.         const { data: {type, props}} = node(id).get();
  11.         add(
  12.           createNode(React.createElement(type, props));
  13.         );
  14.       }}>
  15.         Make a copy of me
  16.       </a>
  17.     </div>
  18.   )
  19. }
  20. ```

Serializable state


The editor's state can be serialized into JSON which you can then apply a compression technique of your choice for storage.

  1. ``` js
  2. const SaveButton = () => {
  3.   const { query } = useEditor();
  4.   return <a onClick={() => console.log(query.serialize()) }>Get JSON</a>
  5. }
  6. ```

Of course, Craft.js will also able to recreate the entire state from the JSON string.

  1. ``` js
  2. const App = () => {
  3.   const jsonString = /* retrieve JSON from server */
  4.   return (
  5.     <Editor>
  6.       <Frame json={jsonString}>
  7.         ...
  8.       </Frame>
  9.     </Editor>
  10.   )
  11. }
  12. ```

Who is this for? 🤔


You should use this if:

✅* You want to design your page editor according to your own UI specifications. With Craft.js, you control almost every aspect of the look and feel of your page editor.
✅* You like the React ecosystem. Being a React framework, not only do you get to build your user interface declaratively, but you will also be able to extend upon thousands of existing React components for your page editor.
✅* You're the coolest kid in class 😎

You should not use this if:

❌* You need a page editor that works out of the box. Craft.js is an abstraction where you implement your own page editor upon. For example, it does not come with a ready-made user interface.
However, you could still consider using the examples as a starting point.

Additional Packages 🎉


@craftjs/layers * A Photoshop-like layers panel

Acknowledgements 🙌


react-dnd * The React drag-n-drop library. Although it is not actually used here, many aspects of Craft.js are written with react-dnd as a reference along with some utilities and functions being borrowed.
Grape.js * The HTML web builder framework. This has served as an inspiration for Craft.js. The element positioning logic used in Craft.js is borrowed from Grape.js
use-methods * A super handy hook when dealing with reducers. Craft.js uses a slightly modified version ofuse-methodsto better fit our API.

Getting Help 👋


If you have questions or there's something you'd like to discuss (eg: contributing), please head over to our Discord server.

Contributors ✨


Craft.js is made with ❤️by these wonderful people (emoji key ):

|   |   |   |   |   |   |
| :--- | :--- | :--- | :--- | :--- | :--- |
| undefined  Prev Wong 💻 🎨 📖 🤔 💡| undefined  azreenashah 📖| undefined  MO 📖| undefined  Andy Krings-Stern 💻 📖 🤔| undefined  Shingo Yamazaki 📖| undefined  Joel Schneider 🐛 📖 |
| undefined  Evan Rusmisel 📖| undefined  Michele Riccardo Esposito 💻 🤔| undefined  Mateusz Drulis 💻 🤔| undefined  Sigit Prabowo 🐛| undefined  Srinath Janakiraman 📖| undefined  Kim 🐛 |

This project follows the all-contributors specification. Contributions of any kind are welcome!

Support 💟


Craft.js is released under the MIT license and is built with 100% love. If you found it useful and would like to ensure its continued development, please consider becoming a backer/sponsor or making a one-time donation via Open Collective or Paypal .

undefined