keermanish / react-page-maker

A drag-drop featured lib which will help you to build the layout and generate the markup or JSON out of it
66 stars 13 forks source link
drag-drop javascript layout-builder markup-builder react

React Page Maker

npm version npm bundle size Netlify Status

React-page-maker, A drag-drop featured lib which will help you to build the page layout and generate the markup or JSON out of it. You can store this JSON at server side or anywhere based on your requirement and use it later for Preview.

Library is designed in such a way that you will get enough flexibility in terms of defining any type of elements/layout which you are going to use throughout the application.

If you are building custom layout builder/dashboard then probably you are in the right place! :)

Install

npm install --save react-page-maker

Demo

How to use

  1. Define the type of elements
  2. Create and Register elements/layouts
  3. Render Palette and Canvas
  4. Working example - Git repo

Define the type of elements

Create and Register elements/Layouts

Render Palette and Canvas

Pass list of elements which we need to show inside palette (since not every time we will be using all elements).

  import { Palette, Canvas } from 'react-page-maker';

  class PageConfigurator extends Component {
    constructor(props) {
      super(props);
      this.registerPaletteElements();
    }

    registerPaletteElements = () => {
      registerPaletteElements([{ // <-- registered palette elements
        type: elements.TEXTBOX,
        component: DraggableTextbox
      }, {
        type: elements.LAYOUT_GRID_1_2,
        component: DragItemGridLayoutR1C2
      }]);
    }

    paletteElements = [{ // <-- palette elements to be shown
      id: 'f1', // make sure ID is unique
      name: 'Input Field',
      type: elements.TEXTBOX
    }, {
      id: 'g1',
      name: 'Two Dropzones',
      type: elements.LAYOUT_GRID_1_2
    }]

    render() {
      return (
        <div className="container-fluid">
          <div className="row">
            <div className="col-sm-8 canvas-wrapper">
              <Canvas />
            </div>
            <div className="col-sm-4 palette-wrapper">
              <Palette paletteElements={this.paletteElements} />
            </div>
          </div>
        </div>
      );
    }
  }

  export default PageConfigurator;

Note - Make sure every palette elements has unique ID.

By now, you would be able to see Canvas and Palette (with those provided elements).

Components

API

State

It provide access to the state object which holds all the meta data.

Prop Type Description
getState function returns current state of canvas
getStorableState function returns current state which can be stored at servers side for future use
clearState function flush current state
getElementParent function returns parent layout element
getElement function returns details of element
removeElement function remove element from tree
updateElement function update specific element
addEventListener function to add event, supported events change, flush, removeElement, updateElement
removeEventListener function to remove event, pass proper event cb

Thanks

Please feel free to raise PR for any new feature or bug (if you find any).