Draft.js

A React framework for building text editors.

README

draftjs-logo

  Draft.js

Build Status npm version

Live Demo


Status


THIS PROJECT IS CURRENTLY IN MAINTENANCE MODE. It will not receive any feature updates, only critical security bug patches. On 31st December 2022 the repo will be fully archived.

For users looking for an open source alternative, Meta have been working on migrating to a new framework, called Lexical. It's still experimental, and we're working on adding migration guides, but, we believe, it provides a more performant and accessible alternative.

Draft.js is a JavaScript rich text editor framework, built for React and
backed by an immutable model.

- Extensible and Customizable: We provide the building blocks to enable
the creation of a broad variety of rich text composition experiences, from
basic text styles to embedded media.
- Declarative Rich Text: Draft.js fits seamlessly into
React applications,
abstracting away the details of rendering, selection, and input behavior with a
familiar declarative API.
- Immutable Editor State: The Draft.js model is built
with immutable-js, offering
an API with functional state updates and aggressively leveraging data persistence
for scalable memory usage.


Draft.js is used in production on Facebook, including status and
comment inputs, Notes, and

API Notice


Before getting started, please be aware that we recently changed the API of
Entity storage in Draft.

Previously, the old API was set to be removed in v0.11.0. Since, the plans have changed— v0.11.0 still supports the old API and v0.12.0 will remove it. Refer to the docs for more information and information on how to migrate.

Getting Started


  1. ```
  2. npm install --save draft-js react react-dom

  3. or

  4. yarn add draft-js react react-dom
  5. ```

Draft.js depends on React and React DOM which must also be installed.

Using Draft.js


  1. ```javascript
  2. import React from 'react';
  3. import ReactDOM from 'react-dom';
  4. import {Editor, EditorState} from 'draft-js';

  5. function MyEditor() {

  6.   
  7.   constructor(props) {
  8.     super(props);
  9.     this.state = {editorState: EditorState.createEmpty()};
  10.     this.onChange = (editorState) => this.setState({editorState});
  11.     this.setEditor = (editor) => {
  12.       this.editor = editor;
  13.     };
  14.     this.focusEditor = () => {
  15.       if (this.editor) {
  16.         this.editor.focus();
  17.       }
  18.     };
  19.   }

  20.   componentDidMount() {
  21.     this.focusEditor();
  22.   }

  23.   render() {
  24.     return (
  25.       <div style={styles.editor} onClick={this.focusEditor}>
  26.         <Editor
  27.           ref={this.setEditor}
  28.           editorState={this.state.editorState}
  29.           onChange={this.onChange}
  30.         />
  31.       </div>
  32.     );
  33.   }
  34. }

  35. const styles = {
  36.   editor: {
  37.     border: '1px solid gray',
  38.     minHeight: '6em'
  39.   }
  40. };

  41. ReactDOM.render(
  42.   <MyEditor />,
  43.   document.getElementById('container')
  44. );
  45. ```

Since the release of React 16.8, you can use Hooks as a way to work withEditorState without using a class.


  1. ```js
  2. import React from "react";
  3. import { Editor, EditorState } from "draft-js";
  4. import "draft-js/dist/Draft.css";

  5. export default function MyEditor() {
  6.   const [editorState, setEditorState] = React.useState(() =>
  7.     EditorState.createEmpty()
  8.   );

  9.   const editor = React.useRef(null);
  10.   function focusEditor() {
  11.     editor.current.focus();
  12.   }

  13.   return (
  14.     <div
  15.       style={{ border: "1px solid black", minHeight: "6em", cursor: "text" }}
  16.       onClick={focusEditor}
  17.     >
  18.       <Editor
  19.         ref={editor}
  20.         editorState={editorState}
  21.         onChange={setEditorState}
  22.         placeholder="Write something!"
  23.       />
  24.     </div>
  25.   );
  26. }

  27. ```

Note that the editor itself is only as tall as its contents. In order to give users a visual cue, we recommend setting a border and a minimum height via the .DraftEditor-root CSS selector, or using a wrapper div like in the above example.

Because Draft.js supports unicode, you must have the following meta tag in the `` `` block of your HTML file:

  1. ```html
  2. <meta charset="utf-8" />
  3. ```

Further examples of how Draft.js can be used are provided in the /examples directory of this repo.

Building Draft.js


Draft.js is built with Yarn v1. Using other package managers mgiht work, but is not officially supported.

To clone and build, run:

  1. ```
  2. git clone https://github.com/facebook/draft-js.git
  3. cd draft-js
  4. yarn install
  5. yarn run build
  6. ```

Examples


To run the examples in the /examples directory, first build Draft.js locally as described above. Then, open the example HTML files in your browser.

Browser Support


![IE![Firefox](https://raw.githubusercontent.com/alrra/browser-logos/master/src/firefox/firefox_32x32.png)![Chrome](https://raw.githubusercontent.com/alrra/browser-logos/master/src/chrome/chrome_32x32.png)![Safari](https://raw.githubusercontent.com/alrra/browser-logos/master/src/safari/safari_32x32.png)![iOS![Chrome
------------------------------------------------------
IE11,lastlastlastnotnot

[1] May need a shim or a polyfill for some syntax used in Draft.js (docs).

[2] IME inputs have known issues in these browsers, especially Korean (docs).

[3] There are known issues with mobile browsers, especially on Android (docs).

Resources and Ecosystem


Check out this curated list of articles and open-sourced projects/utilities: Awesome Draft-JS.

Discussion and Support


Join our Slack team!

Contribute


We welcome pull requests. Learn how to

License


Draft.js is MIT licensed.

Examples provided in this repository and in the documentation are separately
licensed.