Draft-js is Javascript Editor framework that featured in Facebook and it is an open source project which maintained by Facebook. It has larger user base compared to other. Draft-js was a complete framework for creating Ritch text editors and social interactions.
Plugin
Draft-js Plugins allow us to quickly add features such as tool bars , social mentions.
It is a React Plugin Architecture for Draft.js
Emoji Plugin π
The Emoji (π) plugin allow us add emojis to social interaction. This plugin allow use pick and emoji of our choice by pressing :.
Let’s install create a react project
npx create-react-app dracft-editor
In the project add dependecies as follows
npm install @draft-js-plugins/editor
npm install @draft-js-plugins/emoji
We are ready to build our component. Using the Editor component we can use Editor in our app. The Editor state will manage the state of the app.
Let’s initialize the plugin and pass it as props.
import React, { Component } from "react";
import Editor, { createEditorStateWithText } from "@draft-js-plugins/editor";
import createEmojiPlugin from "@draft-js-plugins/emoji";
import editorStyles from "./editorStyles.module.css";
const emojiPlugin = createEmojiPlugin();
const { EmojiSuggestions, EmojiSelect } = emojiPlugin;
const plugins = [emojiPlugin];
const text = `Cool, we can have all sorts of Emojis here. π
πΏβοΈππ aaaand maybe a few more here π²βοΈπ» Quite fun!`;
export default class SimpleEmojiEditor extends Component {
state = {
editorState: createEditorStateWithText(text)
};
onChange = (editorState) => {
this.setState({
editorState
});
};
focus = () => {
this.editor.focus();
};
render() {
return (
<div>
<div className={editorStyles.editor} onClick={this.focus}>
<Editor
editorState={this.state.editorState}
onChange={this.onChange}
plugins={plugins}
ref={(element) => {
this.editor = element;
}}
/>
<EmojiSuggestions />
</div>
<div className={editorStyles.options}>
<EmojiSelect />
</div>
</div>
);
}
}
The EmojiSuggestion component will populate a list emojis for us and EmojiSelect will bring the selected Emoji.
Remember you need to add the CSS
import "@draft-js-plugins/mention/lib/plugin.css";
See the Live project in action on Sandbox
- Create Quill RichText editor using react-quill
- How to get HTML from Remirror editor in Reactjs
- How to get state as JSON from Remirror editor in Reactjs
- Create a TinyMCE Rich Text Editor in React
- Create a Slate Rich Text Editor in React
- Create a Material-UI Rich Text Editor in React
- Create a RichText editor using Quilljs
- Create minimal editor with Tiptap in React
- Create a Social Editor using Remirror toolkit in React
- Create a RichText Editor using Remirror toolkit in React