3.5.2 • Published 1 month ago

@oakjs/react v3.5.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

GitHub npm CI codecov

@oakjs/react

A React renderer for the (maybe) popular block-based builder

Installation

yarn add @oakjs/react @oakjs/theme

Usage

import { useState } from 'react';
import { Builder, baseAddon } from '@oakjs/react';

import '@oakjs/theme/dist/oak.min.css';

export default () => {
  const [content, setContent] = useState([]);

  return (
    <Builder
      addons={[baseAddon()]}
      value={content}
      onChange={setContent}
    />
  );
};

Addons

Oak is by definition an empty shell, composed of addons. Using addons, you can add components, field definitions, settings, texts sheets, or even override some default settings or props.

In order for you to start using oak with a solid base, we created the baseAddon(), which is composed of:

Fields:

  • textField() -> A simple text field
  • textareaField() -> A simple textarea
  • selectField() -> A full-featured select field with deletable items & search
  • colorField() -> A text field with a color picker
  • imageField() -> An upload file button with image preview & file name
  • dateField() -> A date field with a beautiful calendar
  • toggleField() -> A fancy toogle like on iOS (because we love toggles)

Components:

  • rowComponent() and colComponent() -> An almost complete 12-cols grid layout using flex
  • emptySpaceComponent() -> A special component to avoid using vertical paddings & margins everywhere
  • titleComponent() -> A simple component to display h1 to h6 headings
  • textComponent() -> An even more simple component to display, well, text
  • imageComponent() -> A component to display images with sizes presets
  • buttonComponent() -> A button/link component
  • foldableComponent() -> A component to display collapsed things like

and Settings:

  • stylingSettings() -> Allows to set paddings, margins, background image, colors & custom css class to an element
  • responsiveSettings() -> Allows to show/display an element for various screen sizes

You can either import the addon itself with everything inside:

import { Builder, baseAddon } from '@oakjs/react';

export default () => (
  <Builder
    addons={[baseAddon()]}
  />
);

Or import everything manually (in case you need to disable something you don't plan to use):

import { Builder } from '@oakjs/react';
import * as oakAddons from '@oakjs/react/addons';

export default () => (
  <Builder
    addons={[{
      fields: [
        oakAddons.textField(),
        oakAddons.textareaField(),
        oakAddons.selectField(),
        oakAddons.colorField(),
        oakAddons.imageField(),
        oakAddons.dateField(),
        oakAddons.toggleField(),
      ],
      components: [{
        id: 'core',
        type: 'group',
        name: t => t('core.components.core.title', 'Core components'),
        components: [
          oakAddons.rowComponent(),
          oakAddons.colComponent(),
          oakAddons.emptySpaceComponent(),
          oakAddons.titleComponent(),
          oakAddons.textComponent(),
          oakAddons.imageComponent(),
          oakAddons.buttonComponent(),
          oakAddons.foldableComponent(),
        ],
      }],
      settings: [
        oakAddons.stylingSettings(),
        oakAddons.responsiveSettings(),
      ],
    }]}
  />
);

Custom addons

Creating an addon is as simple as using one:

const myAddon = () => ({
  fields: [{
    id: 'my-field',
    name: 'My Field',
    render: ({ value, onChange }) => (
      <input
        type="email"
        value={value}
        onChange={e => onChange({ value: e.target.value })}
      />
    ),
  }]
  components: [{
    id: 'my-component',
    name: 'My Component',
    construct: () => ({ content: 'This is my component' }),
    render: ({ content }) => <div>{ content }</div>,
  }],
});

export default () => (
  <Builder
    addons={[myAddon()]}
  />
);

For more information about addons and the various needed props for fields or components, see the core addons documentation.

Documentation

<Builder />

defaultValue

  • Type: Array<ElementObject|Element>

Just like an uncontrolled react field, used to provide a default value to the builder.

value

  • Type: Array<ElementObject|Element>

Used with onChange to provide a controlled value to the builder.

addons

  • Type: Array<AddonObject>
  • default: []

An array of addons to use with the builder.

activeTextSheet

  • Type: string
  • default: null

The id of the active text sheet.

rootBoundary

  • Type: string | Element | DocumentFragment
  • default: '.oak'

A reference element used by FloatingUI to determine the boundaries of everything that floats (tooltips, menus, ...)

historyEnabled

  • Type: boolean
  • default: true

Whether or not to enable the history (undo/redo) feature.

topHistoryButtonsContainer

  • Type: string | Element | DocumentFragment

An element used by ReactDOM.createPortal to render the top undo/redo buttons.

topHistoryButtonsEnabled

  • Type: boolean
  • default: true

Whether or not to show the top undo/redo buttons.

bottomHistoryButtonsContainer

  • Type: string | Element | DocumentFragment

An element used by ReactDOM.createPortal to render the bottom undo/redo buttons.

bottomHistoryButtonsEnabled

  • Type: boolean
  • default: true

Whether or not to show the bottom undo/redo buttons.

onChange

  • Type: function(value: Array<ElementObject|Element>): void

A callback function called when the builder value changes.

onImageUpload

  • Type: function(event: FileEvent): Promise<{ url: string; name: string; [key: string]: any }>

A callback function called when an image should be uplodaded. It should return a promise that resolves with an object containing the image url, name and any other data you want to store in the image field.

Contributing

npm.io

Please check the CONTRIBUTING.md doc for contribution guidelines.

License

This software is licensed under MIT.

3.5.2

1 month ago

3.5.1

3 months ago

3.5.0

3 months ago

3.4.0

6 months ago

3.2.2

9 months ago

3.2.1

9 months ago

3.2.0

10 months ago

3.0.10

10 months ago

3.3.0

6 months ago

3.1.0

10 months ago

3.0.9

11 months ago

3.0.4

12 months ago

3.0.2

12 months ago

3.0.1

12 months ago

3.0.7

12 months ago

3.0.6

12 months ago

3.0.0

12 months ago

3.0.0-alpha.40

1 year ago

3.0.0-alpha.39

1 year ago

3.0.0-alpha.37

1 year ago

3.0.0-alpha.35

1 year ago

3.0.0-alpha.33

1 year ago

3.0.0-alpha.32

1 year ago

3.0.0-alpha.31

1 year ago

3.0.0-alpha.29

1 year ago

3.0.0-alpha.28

1 year ago

3.0.0-alpha.27

1 year ago

3.0.0-alpha.26

1 year ago

3.0.0-alpha.25

1 year ago

3.0.0-alpha.24

1 year ago

3.0.0-alpha.22

1 year ago

3.0.0-alpha.21

1 year ago

3.0.0-alpha.19

1 year ago

3.0.0-alpha.18

1 year ago

3.0.0-alpha.17

1 year ago

3.0.0-alpha.15

1 year ago

3.0.0-alpha.14

1 year ago

3.0.0-alpha.13

1 year ago

3.0.0-alpha.11

1 year ago

3.0.0-alpha.10

1 year ago

3.0.0-alpha.9

1 year ago

3.0.0-alpha.8

1 year ago

3.0.0-alpha.7

1 year ago

3.0.0-alpha.6

1 year ago

3.0.0-alpha.5

1 year ago

3.0.0-alpha.4

1 year ago

3.0.0-alpha.3

1 year ago

3.0.0-alpha.2

1 year ago

3.0.0-alpha.1

1 year ago

3.0.0-alpha.0

1 year ago