npm install @draft-js-plugins/editor
npm install @draft-js-plugins/static-toolbar
// It is important to import the Editor which accepts plugins.
import Editor from '@draft-js-plugins/editor';
import createToolbarPlugin from '@draft-js-plugins/static-toolbar';
import React from 'react';
// Creates an Instance. At this step, a configuration object can be passed in
// as an argument.
const toolbarPlugin = createToolbarPlugin();
// The Editor accepts an array of plugins. In this case, only the toolbarPlugin
// is passed in, although it is possible to pass in multiple plugins.
const MyEditor = ({ editorState, onChange }) => (
<Editor
editorState={editorState}
onChange={onChange}
plugins={[toolbarPlugin]}
/>
);
export default MyEditor;
The plugin ships with a default styling available at this location in the installed package: node_modules/@draft-js-plugins/static-toolbar/lib/plugin.css
npm i style-loader css-loader --save-dev
module.exports = {
module: {
loaders: [
{
test: /plugin\.css$/,
loaders: ['style-loader', 'css'],
},
],
},
};
import '@draft-js-plugins/static-toolbar/lib/plugin.css';
import React, { Component } from 'react';
import Editor, { createEditorStateWithText } from '@draft-js-plugins/editor';
import createToolbarPlugin from '@draft-js-plugins/static-toolbar';
import editorStyles from './editorStyles.module.css';
const staticToolbarPlugin = createToolbarPlugin();
const { Toolbar } = staticToolbarPlugin;
const plugins = [staticToolbarPlugin];
const text =
'The toolbar above the editor can be used for formatting text, as in conventional static editors …';
export default class SimpleStaticToolbarEditor extends Component {
state = {
editorState: createEditorStateWithText(text),
};
onChange = (editorState) => {
this.setState({
editorState,
});
};
componentDidMount() {
// fixing issue with SSR https://github.com/facebook/draft-js/issues/2332#issuecomment-761573306
// eslint-disable-next-line react/no-did-mount-set-state
this.setState({
editorState: createEditorStateWithText(text),
});
}
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;
}}
/>
<Toolbar />
</div>
</div>
);
}
}
.editor {
box-sizing: border-box;
border: 1px solid #ddd;
cursor: text;
padding: 16px;
border-radius: 2px;
margin-bottom: 2em;
box-shadow: inset 0px 1px 8px -3px #ABABAB;
background: #fefefe;
}
.editor :global(.public-DraftEditor-content) {
min-height: 140px;
}
/* eslint-disable react/no-multi-comp */
import React, { Component } from 'react';
import Editor, { createEditorStateWithText } from '@draft-js-plugins/editor';
import createToolbarPlugin, {
Separator,
} from '@draft-js-plugins/static-toolbar';
import {
ItalicButton,
BoldButton,
UnderlineButton,
CodeButton,
HeadlineOneButton,
HeadlineTwoButton,
HeadlineThreeButton,
UnorderedListButton,
OrderedListButton,
BlockquoteButton,
CodeBlockButton,
} from '@draft-js-plugins/buttons';
import editorStyles from './editorStyles.module.css';
class HeadlinesPicker extends Component {
componentDidMount() {
setTimeout(() => {
window.addEventListener('click', this.onWindowClick);
});
}
componentWillUnmount() {
window.removeEventListener('click', this.onWindowClick);
}
onWindowClick = () =>
// Call `onOverrideContent` again with `undefined`
// so the toolbar can show its regular content again.
this.props.onOverrideContent(undefined);
render() {
const buttons = [HeadlineOneButton, HeadlineTwoButton, HeadlineThreeButton];
return (
<div>
{buttons.map((Button, i) => (
// eslint-disable-next-line
<Button key={i} {...this.props} />
))}
</div>
);
}
}
class HeadlinesButton extends Component {
onClick = () =>
// A button can call `onOverrideContent` to replace the content
// of the toolbar. This can be useful for displaying sub
// menus or requesting additional information from the user.
this.props.onOverrideContent(HeadlinesPicker);
render() {
return (
<div className={editorStyles.headlineButtonWrapper}>
<button onClick={this.onClick} className={editorStyles.headlineButton}>
H
</button>
</div>
);
}
}
const toolbarPlugin = createToolbarPlugin();
const { Toolbar } = toolbarPlugin;
const plugins = [toolbarPlugin];
const text =
'In this editor a toolbar shows up once you select part of the text …';
export default class CustomToolbarEditor extends Component {
state = {
editorState: createEditorStateWithText(text),
};
onChange = (editorState) => {
this.setState({
editorState,
});
};
componentDidMount() {
// fixing issue with SSR https://github.com/facebook/draft-js/issues/2332#issuecomment-761573306
// eslint-disable-next-line react/no-did-mount-set-state
this.setState({
editorState: createEditorStateWithText(text),
});
}
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;
}}
/>
<Toolbar>
{
// may be use React.Fragment instead of div to improve perfomance after React 16
(externalProps) => (
<div>
<BoldButton {...externalProps} />
<ItalicButton {...externalProps} />
<UnderlineButton {...externalProps} />
<CodeButton {...externalProps} />
<Separator {...externalProps} />
<HeadlinesButton {...externalProps} />
<UnorderedListButton {...externalProps} />
<OrderedListButton {...externalProps} />
<BlockquoteButton {...externalProps} />
<CodeBlockButton {...externalProps} />
</div>
)
}
</Toolbar>
</div>
</div>
);
}
}
.editor {
box-sizing: border-box;
border: 1px solid #ddd;
cursor: text;
padding: 16px;
border-radius: 2px;
margin-bottom: 2em;
box-shadow: inset 0px 1px 8px -3px #ABABAB;
background: #fefefe;
}
.editor :global(.public-DraftEditor-content) {
min-height: 140px;
}
.headlineButtonWrapper {
display: inline-block;
}
.headlineButton {
background: #fbfbfb;
color: #888;
font-size: 18px;
border: 0;
padding-top: 5px;
vertical-align: bottom;
height: 34px;
width: 36px;
}
.headlineButton:hover,
.headlineButton:focus {
background: #f3f3f3;
}
import React, { Component } from 'react';
import Editor, { createEditorStateWithText } from '@draft-js-plugins/editor';
import createToolbarPlugin from '@draft-js-plugins/static-toolbar';
import editorStyles from './editorStyles.module.css';
import buttonStyles from './buttonStyles.module.css';
import toolbarStyles from './toolbarStyles.module.css';
const toolbarPlugin = createToolbarPlugin({
theme: { buttonStyles, toolbarStyles },
});
const { Toolbar } = toolbarPlugin;
const plugins = [toolbarPlugin];
const text =
'In this editor a toolbar with a lot more options shows up once you select part of the text …';
export default class ThemedToolbarEditor extends Component {
state = {
editorState: createEditorStateWithText(text),
};
onChange = (editorState) => {
this.setState({
editorState,
});
};
focus = () => {
this.editor.focus();
};
componentDidMount() {
// fixing issue with SSR https://github.com/facebook/draft-js/issues/2332#issuecomment-761573306
// eslint-disable-next-line react/no-did-mount-set-state
this.setState({
editorState: createEditorStateWithText(text),
});
}
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;
}}
/>
<Toolbar />
</div>
</div>
);
}
}
.editor {
box-sizing: border-box;
border: 1px solid #ddd;
cursor: text;
padding: 16px;
border-radius: 2px;
margin-bottom: 2em;
box-shadow: inset 0px 1px 8px -3px #ABABAB;
background: #fefefe;
}
.editor :global(.public-DraftEditor-content) {
min-height: 140px;
}
.buttonWrapper {
display: inline-block;
}
.button {
background: #333;
color: #ddd;
font-size: 18px;
border: 0;
padding-top: 5px;
vertical-align: bottom;
height: 34px;
width: 36px;
border-radius: 4px;
}
.button svg {
fill: #ddd;
}
.button:hover, .button:focus {
background: #444;
outline: 0; /* reset for :focus */
}
.active {
color: #6a9cc9;
}
.active svg {
fill: #6a9cc9;
}
.toolbar {
border: 1px solid #111;
background: #333;
border-radius: 4px;
box-shadow: 0px 1px 3px 0px rgba(220,220,220,1);
z-index: 2;
box-sizing: border-box;
}
.toolbar:after {
border-color: rgba(255, 255, 255, 0);
border-top-color: #333;
border-width: 4px;
margin-left: -4px;
}
.toolbar:before {
border-color: rgba(221, 221, 221, 0);
border-top-color: #111;
border-width: 6px;
margin-left: -6px;
}