Skip to content

Repository files navigation

View live example

npm

A library for rendering styled, responsive and flexible React components from block style data objects.

This package works well with output from the Editor.js rich text editor library. However, there is no dependency on Editor.js. We only require that your data is in a similar block style format.

Setup

Install the package via NPM

npm i editorjs-react-renderer

Install React if you don't already have it in your project

npm i react

CDN usage will be available soon...

Add to your module/application

importOutputfrom'editorjs-react-renderer';ORconstOutput=require('editorjs-react-renderer');

Output accepts a block style data object as prop

constdata={"time": 1564767102436,"blocks": [{"type" : "header","data" : {"level" : 4,"text" : "Editor.js React Renderer"}},{"type": "image","data": {"file": {"url": "<image url here>"},"caption": "Test Caption","withBorder": false,"stretched": false,"withBackground": false}},{"type": "paragraph","data": {"text": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Doloremque accusantium veritatis dolorum cum amet! Ipsa ullam nisi, dolor explicabo ut nobis repudiandae saepe illo error facilis consectetur, quisquam assumenda dolorum."}},
...
],"version": "2.14.0"};

The time and version properties are not required. Only the blocks array is required

Notice that your block data can also be HTML markup. Pass your block style data to Output() and ERR will take care of the rest :)

constPost=()=><section><Outputdata={data}/></section>;exportdefaultPost;

Each object in the blocks property of your block style data is converted to a responsive, styled React component.

Output() accepts a style prop through which you can add custom style to the supported components.

See the Style section for more

Render Single Block

Sometimes you might want to render just a single component like a paragraph or header. While this is possible with the Output component, you should consider using one of the more specific block output components.

import{ListOutput}from'editorjs-react-renderer';constlistData={"items" : ["Item one","Another item","Item 3"],"style" : "unordered"// ordered or unordered};// Your custom style will be merged with the defaults, with yours as priority// You can use inline styles or classesconstlistStyle={textAlign: 'left'};constlistClass='some-class-name';constTodo=()=><ListOutputdata={listData}style={style}classNames={listClass}/>;exportdefaultTodo;

See the API section for more block output components

Custom Renderers

We provide several granular styling options so that you have the ability and flexibility to customize the look and feel of the rendered components. However, you might still have a need to override the default renderers for certain blocks or implement a renderer for an unsupported block. You can do that by passing a renderers prop to the Output component. The renderers prop is an object whose keys are the names of the supported components and whose values are the corresponding renderer definitions to override the defaults. Custom renderers should expect to receive data, style, classNames and config props.

// Define your custom renderer// It should expect to receive data, style, classNames and config props. It's up to you to handle those props.constCustomParagraphRenderer=({ data, style, classNames, config })=>{// validate props here...or not :)letcontent=null;if(typeofdata==='string')content=data;elseif(typeofdata==='object'&&data.text&&typeofdata.text==='string')content=data.text;returncontent ? <pstyle={style}className={classNames}>{ReactHtmlParser(content)}</p> : '';};// You can define a renderer for unsupported blocks.// Structure your data however you like, and handle it in your custom renderer.constAvatarRenderer=({ data, style, classNames, config })=>{letcontent=null;if(typeofdata==='string')content=data;elseif(typeofdata==='object'&&data.imageURL&&typeofdata.imageURL==='string')content=data.imageURL;returncontent ? <imgstyle={style}className={classNames}src={content}/> : '';};// Pass your custom renderers to Outputconstrenderers={paragraph: CustomParagraphRenderer,avatar: AvatarRenderer};// **Your data type should match the renderer keyconstdata={blocks: [
...,{"type": "avatar","data": {"imageURL": "<image url here>"}},]}constTodo=()=><Outputrenderers={renderers}data={data}style={...}classNames={...}config={...}/>;exportdefaultTodo;

Style

You can use inline and/or className styling to change the default look and feel of all supported components The following examples will show you how to use both

import{HeaderOutput,ParagraphOutput}from'editorjs-react-renderer';constdata={header: {...},paragraph: {...}};// All valid JSX inline styles are allowedconststyle={header: {textAlign: 'left',margin: '10px 20px',},paragraph: {fontSize: '16px',}};constclasses={header: 'header-class1 header-class2',paragraph: 'paragraph-class',};constPost=()=>(<section><HeaderOutputdata={data.header}style={style.header}classNames={classes.header}/><ParagraphOutputdata={data.paragraph}style={style.paragraph}classNames={classes.paragraph}/></section>);exportdefaultPost;

Most components have sub-components which can also be styled separately

import{ImageOutput}from'editorjs-react-renderer';constimage={...};// All valid JSX inline styles are allowedconststyle={image: {img: {maxHeight: '400px',},figure: {...},figcaption: {...}},};constclasses={image: {img: 'img-class',figure: 'figure-c',figcaption: 'someClassName'},};constPost=()=>(<section><ImageOutputdata={image}style={style.image}classNames={classes.image}/></section>);exportdefaultPost;

You can also pass these styles through the Output component. In this case, the style prop must be an object whose keys correspond to the names of the supported blocks you intend to style. The following example highlights the current possible nestings and keys for the supported block.

NB If you prefer classes, remember the keys are the same but the values must be class names (strings NOT objects) and the prop name should be classNames

// All valid JSX inline styles are allowedconststyle={paragraph: {...},header: {h1: {...},h2: {...},h3: {...},h4: {...},h5: {...},h6: {...},},image: {img: {...},figure: {...},figcaption: {...}},video: {video: {...},figure: {...},figcaption: {...}},embed: {video: {...},figure: {...},figcaption: {...}},list: {container: {...},listItem: {...},},checklist: {container: {...},item: {...},checkbox: {...},label: {...},},table: {table: {...},tr: {...},th: {...},td: {...},},quote: {container: {...},content: {...},author: {...},message: {...}},codeBox: {container: {...},code: {...},},warning: {container: {...},icon: {...},title: {...},message: {...},},delimiter: {container: {...},svg: {...},path: {...}},personality: {container: {...},textHolder: {...},name: {...},description: {...},photo: {...},link: {...}},linkTool: {container: {...},textHolder: {...},title: {...},description: {...},image: {...},siteName: {...}},};constPost=()=><section><Outputdata={...}style={style}/></section>;exportdefaultPost;

Config

All renderers accept a config object parameter. If you wish to disable a default styles and only apply your own, you can pass a disableDefaultStyle value of true to the element's config options

constconfig={header: {disableDefaultStyle: true,},image: {disableDefaultStyle: true,},video: {disableDefaultStyle: true,},};constPost=()=><section><Outputdata={...}config={config}/></section>;exportdefaultPost;

Server Side Rendering

SSR was broken in V3 to reduce bundle size. This package can only be loaded client-side.

In nextjs you can use dynamic imports to only load the renderers on the client.

importdynamicfrom'next/dynamic';constOutput=dynamic(async()=>(awaitimport('editorjs-react-renderer')).default,{ssr: false});

Note that dynamic imports only work with nextjs 12.0 and above. If you are using a lower version you might need another solution. e.g only import inside useEffect hook.

API

  • Output(data[,style, config, classNames, renderers])
  • CodeBoxOutput(data[,style, config, classNames])
  • HeaderOutput(data[,style, config, classNames])
  • ParagraphOutput(data[,style, config, classNames])
  • TableOutput(data[,style, config, classNames])
  • ImageOutput(data[,style, config, classNames])
  • VideoOutput(data[,style, config, classNames])
  • EmbedOutput(data[,style, config, classNames])
  • ListOutput(data[,style, config, classNames])
  • ChecklistOutput(data[,style, config, classNames])
  • QuoteOutput(data[,style, config, classNames])
  • WarningOutput(data[,style, config, classNames])
  • DelimiterOutput([,style, config, classNames])
  • LinkToolOutput([,style, config, classNames])
  • PersonalityOutput([,style, config, classNames])

Supported blocks

There's more coming...

Author

Dev Juju

Contact Us

Learn

About

A library for rendering styled, responsive and flexible React components from block style data objects like those generated by codex editors like Editor.js

Resources

Code of conduct

Contributing

Stars

117 stars

Watchers

3 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages