Skip to content

Embed the Blocks Editor anywhere using a React component.

Notifications You must be signed in to change notification settings

Blocks-Editor/react-blocks-editor

Repository files navigation

react-blocks-editor

Embed the Blocks Editor anywhere using a React component.

NPM JavaScript Style Guide

Install

npm install --save react-blocks-editor

Quick Start

import React from 'react'
import ReactDOM from 'react-dom'
import BlocksEditor from 'react-blocks-editor'

ReactDOM.render(
  <BlocksEditor />,
  document.getElementById('root'),
)

Advanced Usage

import React from 'react'
import { BlocksEditor, EditorState } from 'react-blocks-editor'

const App = () => {

  // Called whenever the user changes (or manually saves) the editor state
  const handleSave = (state: EditorState) => {
    console.log('Editor state:', state)
  }

  return (
    <BlocksEditor
      style={{ height: '100vh' }}
      options={{
        theme: 'dark',
        menu: 'hidden',
        tutorial: 'hello-world'
      }}
      onSave={handleSave}>

      {({ loadState }) => {
        console.log('Loaded editor iframe.')

        // Load a specific project state (.blocks JSON format).
        loadState({
          name: 'Project name',
          description: 'Project description',
          nodes: {
            // Text block
            'message': {
              type: 'LiteralText',
              position: [0, 0],
              data: {
                value: 'Hello world!'
              },
              outputs: {
                value: [{
                  // Reference to 'motoko' node and 'value' input socket
                  node: 'motoko',
                  input: 'input'
                }]
              }
            },
            // Motoko compiler block
            'motoko': {
              type: 'CompileMotoko',
              position: [250, 0]
            }
          }
        })
      }}
    </BlocksEditor>
  )
}

export default App

This project was made possible via the DFINITY Developer Grant Program.

About

Embed the Blocks Editor anywhere using a React component.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published