1 | React-Quill [![Build Status](https://travis-ci.org/zenoamaro/react-quill.svg?branch=master)](https://travis-ci.org/zenoamaro/react-quill) [![npm](https://img.shields.io/npm/v/react-quill.svg)](https://www.npmjs.com/package/react-quill)
|
2 | [![npm downloads](https://img.shields.io/npm/dt/react-quill.svg?maxAge=2592000)](http://www.npmtrends.com/react-quill)
|
3 | ==============================================================================
|
4 |
|
5 | A [Quill] component for [React].
|
6 |
|
7 | See a [live demo] or [Codepen](http://codepen.io/alexkrolick/pen/xgyOXQ/left?editors=0010#0).
|
8 |
|
9 | [Quill]: https://quilljs.com
|
10 | [React]: https://facebook.github.io/react/
|
11 | [live demo]: https://zenoamaro.github.io/react-quill/
|
12 |
|
13 | 1. [Quick start](#quick-start)
|
14 | 1. [Import the component](#import-the-component)
|
15 | 1. [Import the stylesheet](#import-the-stylesheet)
|
16 | 1. [Use the component](#use-the-component)
|
17 | 1. [Using Deltas](#using-deltas)
|
18 | 1. [Controlled vs Uncontrolled Mode](#controlled-vs-uncontrolled-mode)
|
19 | 1. [Options](#options)
|
20 | 1. [Theme](#theme)
|
21 | 1. [Custom Toolbar](#custom-toolbar)
|
22 | 1. [Custom Formats](#custom-formats)
|
23 | 1. [Custom Editing Area](#custom-editing-area)
|
24 | 1. [Mixin](#mixin)
|
25 | 1. [Upgrading to React-Quill v1.0.0](#upgrading-to-react-quill-v100)
|
26 | 1. [API reference](#api-reference)
|
27 | 1. [Exports](#exports)
|
28 | 1. [Props](#props)
|
29 | 1. [Methods](#methods)
|
30 | 1. [Browser support](#browser-support)
|
31 | 1. [Building and testing](#building-and-testing)
|
32 | 1. [Bundling with Webpack](#bundling-with-webpack)
|
33 | 1. [Changelog](#changelog)
|
34 | 1. [Contributors](#contributors)
|
35 | 1. [License](#license)
|
36 |
|
37 | ---
|
38 |
|
39 | 💯 **React Quill now supports Quill v1.0.0!**
|
40 | Thanks to @clemmy and @alexkrolick for landing this much-awaited change. There are many breaking changes, so be sure to read the [migration guide](#upgrading-to-react-quill-v100).
|
41 |
|
42 | ---
|
43 |
|
44 | ```sh
|
45 | npm install react-quill
|
46 | yarn add react-quill
|
47 | ```
|
48 |
|
49 | Special thank you to everyone who contributed during the 1.0.0 release cycle!
|
50 |
|
51 | ---
|
52 |
|
53 | 🏵 **Welcoming @alexkrolick to the team!**
|
54 | His contributions have been incredible so far, and his passion and dedication will bring some much-deserved love to this project.
|
55 |
|
56 | ---
|
57 |
|
58 |
|
59 | ## Quick Start
|
60 |
|
61 | ### Import the component
|
62 |
|
63 | ```jsx
|
64 | const ReactQuill = require('react-quill'); // CommonJS
|
65 | import ReactQuill from 'react-quill'; // ES6
|
66 | ```
|
67 |
|
68 | ### Import the stylesheet
|
69 |
|
70 | _Two common examples are shown below. How stylesheets are included in your app depends on build system (Webpack, SCSS, LESS, etc). See the documentation on [Themes](#theme) for more information._
|
71 |
|
72 | #### Fetching styles from the CDN
|
73 |
|
74 | ```html
|
75 | <link rel="stylesheet" href="//cdn.quilljs.com/1.2.6/quill.snow.css">
|
76 | ```
|
77 |
|
78 | #### Using `css-loader` with Webpack or `create-react-app`
|
79 |
|
80 | ```jsx
|
81 | require('react-quill/dist/quill.snow.css'); // CommonJS
|
82 | import 'react-quill/dist/quill.snow.css'; // ES6
|
83 | ```
|
84 |
|
85 | ### Use the component
|
86 |
|
87 | ```jsx
|
88 | class MyComponent extends React.Component {
|
89 | constructor(props) {
|
90 | super(props)
|
91 | this.state = { text: '' } // You can also pass a Quill Delta here
|
92 | this.handleChange = this.handleChange.bind(this)
|
93 | }
|
94 |
|
95 | handleChange(value) {
|
96 | this.setState({ text: value })
|
97 | }
|
98 |
|
99 | render() {
|
100 | return (
|
101 | <ReactQuill value={this.state.text}
|
102 | onChange={this.handleChange} />
|
103 | )
|
104 | }
|
105 | }
|
106 | ```
|
107 |
|
108 | ### Using Deltas
|
109 |
|
110 | You can pass a [Quill Delta](https://quilljs.com/docs/delta/), instead of an HTML string, as the `value` and `defaultValue` properties. Deltas have a number of advantages over HTML strings, so you might want use them instead. Be aware, however, that comparing Deltas for changes is more expensive than comparing HTML strings, so it might be worth to profile your usage patterns.
|
111 |
|
112 | Note that switching `value` from an HTML string to a Delta, or vice-versa, will trigger a change, regardless of whether they represent the same document, so you might want to stick to a format and keep using it consistently throughout.
|
113 |
|
114 | ⚠️ Do not use the `delta` object you receive from the `onChange` event as `value`. This object does not contain the full document, but only the last modifications, and doing so will most likely trigger an infinite loop where the same changes are applied over and over again. Use `editor.getContents()` during the event to obtain a Delta of the full document instead. ReactQuill will prevent you from making such a mistake, however if you are absolutely sure that this is what you want, you can pass the object through `new Delta()` again to un-taint it.
|
115 |
|
116 | ### Controlled vs Uncontrolled Mode
|
117 |
|
118 | Pass `defaultValue` instead of `value` if you need to use DOM or [Quill API](https://quilljs.com/docs/api/)s to imperatively manipulate the editor state.
|
119 | In this "uncontrolled" mode ReactQuill uses the prop as the initial value but allows the element to deviate after that. The `onChange` callback still works normally.
|
120 |
|
121 | - Read more about uncontrolled components in the [React docs][defaultvalues].
|
122 | - Read more about the available [props](#props).
|
123 |
|
124 | [defaultvalues]: https://facebook.github.io/react/docs/uncontrolled-components.html#default-values
|
125 |
|
126 | ## Options
|
127 |
|
128 | ### Theme
|
129 |
|
130 | The Quill editor supports [themes](http://quilljs.com/docs/themes/). It includes a full-fledged theme, called _snow_, that is Quill's standard appearance, a _bubble_ theme that is similar to the inline editor on Medium, and a _core_ theme containing only the bare essentials to allow modules like toolbars or tooltips to work.
|
131 |
|
132 | These stylesheets can be found in the Quill distribution, but for convenience they are also linked in React Quill's `dist` folder. In a common case you would activate a theme by setting the theme [prop](#props). Pass a falsy value (`null`) to disable the theme.
|
133 |
|
134 | ```jsx
|
135 | <ReactQuill theme="snow" /> // or "bubble", null to use minimal core theme
|
136 | ```
|
137 |
|
138 | And then link the appropriate stylesheet (only link the CSS for the themes you want to use):
|
139 |
|
140 | ```html
|
141 | <link rel="stylesheet" href="node_modules/react-quill/dist/quill.snow.css">
|
142 | <link rel="stylesheet" href="node_modules/react-quill/dist/quill.bubble.css">
|
143 | <link rel="stylesheet" href="node_modules/react-quill/dist/quill.core.css">
|
144 | ```
|
145 |
|
146 | This may vary depending how application is structured, directories or otherwise. For example, if you use a CSS pre-processor like SASS, you may want to import that stylesheet inside your own.
|
147 |
|
148 | ### Custom Toolbar
|
149 |
|
150 | #### Default Toolbar Elements
|
151 |
|
152 | The [Quill Toolbar Module](http://quilljs.com/docs/modules/toolbar/) API provides an easy way to configure the default toolbar icons using an array of format names.
|
153 |
|
154 | <details>
|
155 | <summary>Example Code</summary>
|
156 |
|
157 | ```jsx
|
158 | var MyComponent = React.createClass({
|
159 |
|
160 | modules: {
|
161 | toolbar: [
|
162 | [{ 'header': [1, 2, false] }],
|
163 | ['bold', 'italic', 'underline','strike', 'blockquote'],
|
164 | [{'list': 'ordered'}, {'list': 'bullet'}, {'indent': '-1'}, {'indent': '+1'}],
|
165 | ['link', 'image'],
|
166 | ['clean']
|
167 | ],
|
168 | },
|
169 |
|
170 | formats: [
|
171 | 'header',
|
172 | 'bold', 'italic', 'underline', 'strike', 'blockquote',
|
173 | 'list', 'bullet', 'indent',
|
174 | 'link', 'image'
|
175 | ],
|
176 |
|
177 | render: function() {
|
178 | return (
|
179 | <div className="text-editor">
|
180 | <ReactQuill theme="snow"
|
181 | modules={this.modules}
|
182 | formats={this.formats}>
|
183 | </ReactQuill>
|
184 | </div>
|
185 | );
|
186 | },
|
187 |
|
188 | });
|
189 | ```
|
190 |
|
191 | </details>
|
192 |
|
193 | #### HTML Toolbar
|
194 |
|
195 | You can also supply your own HTML/JSX toolbar with custom elements that are not part of the Quill theme.
|
196 |
|
197 | See this example live on Codepen: [Custom Toolbar Example](https://codepen.io/alexkrolick/pen/gmroPj?editors=0010)
|
198 |
|
199 | <details>
|
200 | <summary>Example Code</summary>
|
201 |
|
202 | ```jsx
|
203 | /*
|
204 | * Custom "star" icon for the toolbar using an Octicon
|
205 | * https://octicons.github.io
|
206 | */
|
207 | const CustomButton = () => <span className="octicon octicon-star" />
|
208 |
|
209 | /*
|
210 | * Event handler to be attached using Quill toolbar module
|
211 | * http://quilljs.com/docs/modules/toolbar/
|
212 | */
|
213 | function insertStar () {
|
214 | const cursorPosition = this.quill.getSelection().index
|
215 | this.quill.insertText(cursorPosition, "★")
|
216 | this.quill.setSelection(cursorPosition + 1)
|
217 | }
|
218 |
|
219 | /*
|
220 | * Custom toolbar component including insertStar button and dropdowns
|
221 | */
|
222 | const CustomToolbar = () => (
|
223 | <div id="toolbar">
|
224 | <select className="ql-header">
|
225 | <option value="1"></option>
|
226 | <option value="2"></option>
|
227 | <option selected></option>
|
228 | </select>
|
229 | <button className="ql-bold"></button>
|
230 | <button className="ql-italic"></button>
|
231 | <select className="ql-color">
|
232 | <option value="red"></option>
|
233 | <option value="green"></option>
|
234 | <option value="blue"></option>
|
235 | <option value="orange"></option>
|
236 | <option value="violet"></option>
|
237 | <option value="#d0d1d2"></option>
|
238 | <option selected></option>
|
239 | </select>
|
240 | <button className="ql-insertStar">
|
241 | <CustomButton />
|
242 | </button>
|
243 | </div>
|
244 | )
|
245 |
|
246 | /*
|
247 | * Editor component with custom toolbar and content containers
|
248 | */
|
249 | class Editor extends React.Component {
|
250 | constructor (props) {
|
251 | super(props)
|
252 | this.state = { editorHtml: '' }
|
253 | this.handleChange = this.handleChange.bind(this)
|
254 | }
|
255 |
|
256 | handleChange (html) {
|
257 | this.setState({ editorHtml: html });
|
258 | }
|
259 |
|
260 | render() {
|
261 | return (
|
262 | <div className="text-editor">
|
263 | <CustomToolbar />
|
264 | <ReactQuill
|
265 | onChange={this.handleChange}
|
266 | placeholder={this.props.placeholder}
|
267 | modules={Editor.modules}
|
268 | />
|
269 | </div>
|
270 | )
|
271 | }
|
272 | }
|
273 |
|
274 | /*
|
275 | * Quill modules to attach to editor
|
276 | * See http://quilljs.com/docs/modules/ for complete options
|
277 | */
|
278 | Editor.modules = {
|
279 | toolbar: {
|
280 | container: "#toolbar",
|
281 | handlers: {
|
282 | "insertStar": insertStar,
|
283 | }
|
284 | }
|
285 | }
|
286 |
|
287 | /*
|
288 | * Quill editor formats
|
289 | * See http://quilljs.com/docs/formats/
|
290 | */
|
291 | Editor.formats = [
|
292 | 'header', 'font', 'size',
|
293 | 'bold', 'italic', 'underline', 'strike', 'blockquote',
|
294 | 'list', 'bullet', 'indent',
|
295 | 'link', 'image', 'color',
|
296 | ]
|
297 |
|
298 | /*
|
299 | * PropType validation
|
300 | */
|
301 | Editor.propTypes = {
|
302 | placeholder: React.PropTypes.string,
|
303 | }
|
304 |
|
305 | /*
|
306 | * Render component on page
|
307 | */
|
308 | ReactDOM.render(
|
309 | <Editor placeholder={'Write something or insert a star ★'}/>,
|
310 | document.querySelector('.app')
|
311 | )
|
312 | ```
|
313 |
|
314 | </details>
|
315 |
|
316 | ### Custom Formats
|
317 |
|
318 | The component has two types of formats:
|
319 |
|
320 | 1. The default [Quill formats](http://quilljs.com/docs/formats/) that are enabled/disabled using the [`formats` prop](#props). All formats are enabled by default.
|
321 | 2. Custom formats created using Parchment and registered with your component's Quill instance
|
322 |
|
323 | <details>
|
324 | <summary>Example Code</summary>
|
325 |
|
326 | ```js
|
327 | const ReactQuill = require('react-quill'); // CommonJS
|
328 | import ReactQuill, { Quill } from 'react-quill'; // ES6
|
329 | ```
|
330 |
|
331 |
|
332 | ```jsx
|
333 | /*
|
334 | * Example Parchment format from
|
335 | * https://quilljs.com/guides/cloning-medium-with-parchment/
|
336 | */
|
337 | let Inline = Quill.import('blots/inline');
|
338 | class BoldBlot extends Inline { }
|
339 | BoldBlot.blotName = 'bold';
|
340 | BoldBlot.tagName = 'strong';
|
341 | Quill.register(BoldBlot);
|
342 |
|
343 | /*
|
344 | * Editor component with default and custom formats
|
345 | */
|
346 | class MyComponent extends React.Component {
|
347 | constructor(props) {
|
348 | this.formats = ['italic', 'underline'] // default formats
|
349 | this.state = { text: '' }
|
350 | }
|
351 |
|
352 | handleChange(value) {
|
353 | this.setState({text: value})
|
354 | }
|
355 |
|
356 | render() {
|
357 | return (
|
358 | <ReactQuill
|
359 | value={this.state.text}
|
360 | onChange={this.handleChange}
|
361 | formats={this.formats} // the custom format is already registered
|
362 | />
|
363 | )
|
364 | }
|
365 | }
|
366 | ```
|
367 |
|
368 | </details>
|
369 |
|
370 | ### Custom editing area
|
371 |
|
372 | If you instantiate ReactQuill without children, it will create a `<div>` for you, to be used as the editing area for Quill. If you prefer, you can specify your own element for ReactQuill to use. Note that `<textarea>`s are not supported by Quill at this time.
|
373 |
|
374 | <details>
|
375 |
|
376 | ```jsx
|
377 | class MyComponent extends React.Component {
|
378 |
|
379 | render() {
|
380 | return (
|
381 | <ReactQuill>
|
382 | <div className="my-editing-area"/>
|
383 | </ReactQuill>
|
384 | );
|
385 | }
|
386 |
|
387 | });
|
388 | ```
|
389 |
|
390 | </details>
|
391 |
|
392 | ### Mixin
|
393 |
|
394 | The module exports a mixin which can be used to create custom editor components. (Note that mixins will be deprecated in a future version of React).
|
395 |
|
396 | <details>
|
397 | <summary>Example Code</summary>
|
398 |
|
399 | The ReactQuill default component is built using the mixin. See [component.js](src/component.js) for source.
|
400 |
|
401 | ```jsx
|
402 | import {Mixin} from 'react-quill'
|
403 |
|
404 | var MyComponent = React.createClass({
|
405 | mixins: [ ReactQuill.Mixin ],
|
406 |
|
407 | componentDidMount: function() {
|
408 | var editor = this.createEditor(
|
409 | this.getEditingArea(),
|
410 | this.getEditorConfig()
|
411 | );
|
412 | this.setState({ editor:editor });
|
413 | },
|
414 |
|
415 | componentWillReceiveProps: function(nextProps) {
|
416 | if ('value' in nextProps && nextProps.value !== this.props.value) {
|
417 | this.setEditorContents(this.state.editor, nextProps.value);
|
418 | }
|
419 | },
|
420 |
|
421 | });
|
422 | ```
|
423 |
|
424 | </details>
|
425 |
|
426 | ## Upgrading to React-Quill v1.0.0
|
427 |
|
428 | In most cases, ReactQuill will raise useful warnings to help you perform any necessary migration steps.
|
429 |
|
430 | Please note that many [migration steps to Quill v1.0](http://quilljs.com/guides/upgrading-to-1-0/) may also apply.
|
431 |
|
432 | <details>
|
433 | <summary>Expand Upgrade Guide</summary>
|
434 |
|
435 | ### The toolbar module
|
436 |
|
437 | With v1.0.0, Quill adopted a new [toolbar configuration format](https://quilljs.com/docs/modules/toolbar/), to which React Quill will delegates all toolbar functionality, and which is now the preferred way to customize the toolbar.
|
438 |
|
439 | Previously, toolbar properties could be set by passing a `toolbar` prop to React Quill. Pass the same options as `modules.toolbar` instead.
|
440 |
|
441 | <details>
|
442 | <summary>Read More</summary>
|
443 |
|
444 | ```diff
|
445 | + modules: {
|
446 | toolbar: [
|
447 | ...
|
448 | ],
|
449 | + },
|
450 |
|
451 | <ReactQuill
|
452 | - toolbar={this.toolbar}
|
453 | + modules={this.modules}
|
454 | />
|
455 | ```
|
456 |
|
457 | If you used to provide your own HTML toolbar component, you can still do the same:
|
458 |
|
459 | ```diff
|
460 | + modules: {
|
461 | + toolbar: '#my-toolbar-component',
|
462 | + },
|
463 |
|
464 | <ReactQuill
|
465 | - toolbar="#my-toolbar-component"
|
466 | + modules={this.modules}
|
467 | />
|
468 | ```
|
469 |
|
470 | Note that it is not possible to pass a toolbar component as a child to ReactQuill anymore.
|
471 |
|
472 | Previously, React Quill would create a custom HTML toolbar for you if you passed a configuration object as the `toolbar` prop. This will not happen anymore. You can still create a `ReactQuill.Toolbar` explicitly:
|
473 |
|
474 | ```diff
|
475 | + modules: {
|
476 | + toolbar: '#my-quill-toolbar',
|
477 | + },
|
478 |
|
479 | + <ReactQuill.Toolbar
|
480 | + id='my-quill-toolbar'
|
481 | + items={this.oldStyleToolbarItems}
|
482 | + />
|
483 |
|
484 | <ReactQuill
|
485 | - toolbar={this.oldStyleToolbarItems}
|
486 | + modules={this.modules}
|
487 | />
|
488 | ```
|
489 |
|
490 | However, consider switching to the new Quill format instead, or provide your own [toolbar component](#html-toolbar).
|
491 |
|
492 | React Quill now follows the Quill toolbar format closely. See the [Quill toolbar documentation](https://quilljs.com/docs/modules/toolbar/) for a complete reference on all supported options.
|
493 |
|
494 | </details>
|
495 |
|
496 | ### Custom editing areas and refs
|
497 |
|
498 | Previously, to provide a custom element for Quill to mount on, it was necessary to pass a child identified by a specific `ref`: `editor`.
|
499 |
|
500 | This is now unnecessary, so you can omit the `ref` entirely if you don't need it. In addition, any `ref` you keep won't be stolen from the owner component anymore.
|
501 |
|
502 | Note, however, that React Quill will now ensure that the element is compatible with Quill. This means that passing a `<textarea>` now produces an error.
|
503 |
|
504 | ### Passing children to ReactQuill
|
505 |
|
506 | Previously, it was possible to pass arbitrary components as children of React Quill. Their `ref` would identify them as either a custom toolbar or a custom editing area.
|
507 |
|
508 | This is not possible anymore, and the only child you can pass now is an optional [custom Editing Area](#custom-editing-area) element.
|
509 |
|
510 | ### Adding custom formats with the `formats` property is deprecated
|
511 |
|
512 | As of 1.0.0, [use Parchment to define new formats](https://github.com/quilljs/parchment). Use the [Quill export](#exports) from the module to register and extend formats:
|
513 |
|
514 | ```js
|
515 | Quill.register('formats/CustomFormat', MyCustomFormat);
|
516 | ```
|
517 |
|
518 | ### The `styles` property
|
519 |
|
520 | Previously, it was allowed to inject CSS styles by providing an object to the `styles` property. This option has been removed from Quill 1.0, and support for it in React Quill has gone as well. If you need to inject styles, link an external stylesheet instead.
|
521 |
|
522 | See the [Quill Release Notes](http://quilljs.com/guides/upgrading-to-1-0/#configuration).
|
523 |
|
524 | ### The `pollInterval` property
|
525 |
|
526 | This property previously set the frequency with which Quill polled the DOM for changes. It does not have any effect anymore, and can safely be removed from the props.
|
527 |
|
528 | </details>
|
529 |
|
530 | ## API reference
|
531 |
|
532 | ### Exports
|
533 |
|
534 | ```jsx
|
535 | const ReactQuill = require('react-quill'); // CommonJS
|
536 | const {Quill, Mixin, Toolbar} = ReactQuill;
|
537 |
|
538 | import ReactQuill, { Quill, Mixin, Toolbar } from 'react-quill'; // ES6
|
539 | ```
|
540 |
|
541 | `Mixin`
|
542 | : Provides the bridge between React and Quill. `ReactQuill` implements this mixin; in the same way you can use it to build your own component, or replace it to implement a new core for the default component. _Note that mixins are deprecated in React and this export will be replaced by an HOC in the future._
|
543 |
|
544 | `Toolbar`
|
545 | : The component that renders the custom ReactQuill toolbar. The default collection of items and color swatches is available as `ReactQuill.Toolbar.defaultItems` and `ReactQuill.Toolbar.defaultColors` respectively. ⚠️ The Toolbar component is deprecated since v1.0.0. See [upgrading to React Quill v1.0.0](#upgrading-to-react-quill-v100).
|
546 |
|
547 | `Quill`
|
548 | : The `Quill` namespace on which you can call `registerModule` and such.
|
549 |
|
550 |
|
551 | ### Props
|
552 |
|
553 | `id`
|
554 | : ID to be applied to the DOM element.
|
555 |
|
556 | `className`
|
557 | : Classes to be applied to the DOM element.
|
558 |
|
559 | `value`
|
560 | : Value for the editor as a controlled component. Can be a string containing HTML, a [Quill Delta](https://quilljs.com/docs/delta/) instance, or a plain object representing a Delta.
|
561 | Note that due to limitations in Quill, this is actually a _semi-controlled_ mode, meaning that the edit is not prevented, but changing `value` will still replace the contents.
|
562 | Also note that passing a Quill Delta here, and then an HTML string, or vice-versa, will always trigger a change, regardless of whether they represent the same document.
|
563 | ⚠️ Do not pass the `delta` object from the `onChange` event as `value`, as it will cause a loop. See [Using Deltas](#using-deltas) for details.
|
564 |
|
565 | `defaultValue`
|
566 | : Initial value for the editor as an uncontrolled component. Can be a string containing HTML, a [Quill Delta](https://quilljs.com/docs/delta/), or a plain object representing a Delta.
|
567 |
|
568 | `readOnly`
|
569 | : If true, the editor won't allow changing its contents. Wraps the Quill [`disable` API](https://quilljs.com/docs/api/#enable).
|
570 |
|
571 | `placeholder`
|
572 | : The default value for the empty editor.
|
573 |
|
574 | `modules`
|
575 | : An object specifying which modules are enabled, and their configuration. The editor toolbar is a commonly customized module. See the [modules section](http://quilljs.com/docs/modules/) over the Quill documentation for more information on what modules are available.
|
576 |
|
577 | `formats`
|
578 | : An array of formats to be enabled during editing. All implemented formats are enabled by default. See [Formats](http://quilljs.com/docs/formats/) for a list.
|
579 | Custom formats should not be included in the array as of version 1.0.0. Instead they should be created through [Parchment](https://github.com/quilljs/parchment) and registered with the module's [Quill export](#exports).
|
580 |
|
581 | `style`
|
582 | : An object with custom CSS rules to apply on the editor's container. Rules should be in React's "camelCased" naming style.
|
583 |
|
584 | `theme`
|
585 | : The name of the theme to apply to the editor. Defaults to `snow`, Quill's standard theme. Pass `null` to use the minimal core theme. See the [docs on themes](#theme) for more information on including the required stylesheets.
|
586 |
|
587 | `tabIndex`
|
588 | : The order in which the editor becomes focused, among other controls in the page, during keyboard navigation.
|
589 |
|
590 | `bounds`
|
591 | : Selector or DOM element used by Quill to constrain position of popups. Defaults to `document.body`.
|
592 |
|
593 | `children`
|
594 | : A single React element that will be used as the editing area for Quill in place of the default, which is a `<div>`. Note that you cannot use a `<textarea>`, as it is not a supported target. Also note that updating children is costly, as it will cause the Quill editor to be recreated. Set the `value` prop if you want to control the html contents of the editor.
|
595 |
|
596 | `onChange(content, delta, source, editor)`
|
597 | : Called back with the new contents of the editor after change. It will be passed the HTML contents of the editor, a delta object expressing the change, the source of the change, and finally a read-only proxy to [editor accessors](#the-unprivileged-editor) such as `getHTML()`.
|
598 | ⚠️ Do not use this `delta` object as `value`, as it will cause a loop. Use `editor.getContents()` instead. See [Using Deltas](#using-deltas) for details.
|
599 |
|
600 | `onChangeSelection(range, source, editor)`
|
601 | : Called back with the new selected range, or null when unfocused. It will be passed the selection range, the source of the change, and finally a read-only proxy to editor accessors such as `getBounds()`.
|
602 |
|
603 | `onFocus(range, source, editor)`
|
604 | : Called when the editor becomes focused. It will receive the new selection range.
|
605 |
|
606 | `onBlur(previousRange, source, editor)`
|
607 | : Called when the editor loses focus. It will receive the selection range it had right before losing focus.
|
608 |
|
609 | `onKeyPress(event)`
|
610 | : Called after a key has been pressed and released.
|
611 | : Note that, like its native counterpart, this won't be called for special keys such as <kbd>shift</kbd> or <kbd>enter</kbd>. If you need those, hook onto `onKeyDown` or `onKeyUp`.
|
612 |
|
613 | `onKeyDown(event)`
|
614 | : Called after a key has been pressed, but before it is released.
|
615 | : Note that, due to how Quill works, it's possible that you won't receive events for keys such as <kbd>enter</kbd>, <kbd>backspace</kbd> or <kbd>delete</kbd>. If that's the case, try hooking onto `onKeyUp` instead.
|
616 |
|
617 | `onKeyUp(event)`
|
618 | : Called after a key has been released.
|
619 |
|
620 |
|
621 | ### Methods
|
622 |
|
623 | If you have [a ref](https://facebook.github.io/react/docs/more-about-refs.html) to a ReactQuill node, you will be able to invoke the following methods:
|
624 |
|
625 | `focus()`
|
626 | : Focuses the editor.
|
627 |
|
628 | `blur()`
|
629 | : Removes focus from the editor.
|
630 |
|
631 | `getEditor()`
|
632 | : Returns the Quill instance that backs the editor. While you can freely use this to access methods such as `getText()`, please avoid from imperatively manipulating the instance, to avoid getting ReactQuill and Quill out-of-sync. A much-safer [unprivileged editor](#the-unprivileged-editor) is available as replacement.
|
633 |
|
634 | <details>
|
635 | <summary>Example</summary>
|
636 |
|
637 | [View this example on Codepen](https://codepen.io/alexkrolick/pen/YNmGar?editors=0011)
|
638 |
|
639 | ```jsx
|
640 | class Editor extends React.Component {
|
641 | constructor(props) {
|
642 | super(props)
|
643 | this.quillRef = null; // Quill instance
|
644 | this.reactQuillRef = null; // ReactQuill component
|
645 | }
|
646 |
|
647 | componentDidMount() {
|
648 | this.attachQuillRefs()
|
649 | }
|
650 |
|
651 | componentDidUpdate() {
|
652 | this.attachQuillRefs()
|
653 | }
|
654 |
|
655 | attachQuillRefs = () => {
|
656 | if (typeof this.reactQuillRef.getEditor !== 'function') return;
|
657 | this.quillRef = this.reactQuillRef.getEditor();
|
658 | }
|
659 |
|
660 | insertText = () => {
|
661 | var range = this.quillRef.getSelection();
|
662 | let position = range ? range.index : 0;
|
663 | this.quillRef.insertText(position, 'Hello, World! ')
|
664 | }
|
665 |
|
666 | render() {
|
667 | return (
|
668 | <div>
|
669 | <ReactQuill
|
670 | ref={(el) => { this.reactQuillRef = el }}
|
671 | theme={'snow'} />
|
672 | <button onClick={this.insertText}>Insert Text</button>
|
673 | </div>
|
674 | )
|
675 | }
|
676 | }
|
677 | ```
|
678 |
|
679 | </details>
|
680 |
|
681 | ### The unprivileged editor
|
682 |
|
683 | During events, ReactQuill will make a restricted subset of the Quill API available as the `editor` argument. This prevents access to destructive methods, which might case ReactQuill to get out-of-sync with the component. It provides the following methods, which are mostly proxies of existing [Quill methods](https://quilljs.com/docs/api/):
|
684 |
|
685 | `getLength()`
|
686 | : Returns the length of the editor contents, in characters, not including any HTML tag.
|
687 |
|
688 | `getText()`
|
689 | : Returns the string contents of the editor, not including any HTML tag.
|
690 |
|
691 | `getHTML()`
|
692 | : Returns the full HTML contents of the editor.
|
693 |
|
694 | `getContents()`
|
695 | : Returns a [Quill Delta](https://quilljs.com/docs/delta/) of the complete document.
|
696 |
|
697 | `getSelection()`
|
698 | : Returns the current selection range, or `null` if the editor is unfocused.
|
699 |
|
700 | `getBounds()`
|
701 | : Returns the pixel position, relative to the editor container, and dimensions, of a selection, at a given location.
|
702 |
|
703 |
|
704 | ## Building and testing
|
705 |
|
706 | You can run the automated test suite:
|
707 |
|
708 | ```sh
|
709 | npm test
|
710 | ```
|
711 |
|
712 | And build a minificated version of the source:
|
713 |
|
714 | ```sh
|
715 | npm run build
|
716 | ```
|
717 |
|
718 | More tasks are available on the [Makefile](Makefile):
|
719 |
|
720 | lint: lints the source
|
721 | spec: runs the test specs
|
722 | coverage: runs the code coverage test
|
723 | test: lint, spec and coverage threshold test
|
724 | build: builds the minified version
|
725 |
|
726 | Note that `dist` is ignored in the git repository as of version 1.0.0. If you need to use the built files without downloading the package from NPM, you can run the build tasks yourself or use a CDN like [unpkg](https://unpkg.com/react-quill@1.0.0-beta-1/dist/react-quill.min.js).
|
727 |
|
728 | ### Bundling with Webpack
|
729 |
|
730 | Quill ships only a pre-built javascript file, so Webpack will complain after building a bundle:
|
731 |
|
732 | ```
|
733 | Error: ./~/react-quill/~/quill/dist/quill.js
|
734 | Critical dependencies:
|
735 | 6:478-485 This seems to be a pre-built javascript file. Though this is possible, it's not recommended. Try to require the original source to get better results.
|
736 | @ ./~/react-quill/~/quill/dist/quill.js 6:478-485
|
737 | ```
|
738 |
|
739 | The warning is harmless, but if you want to silence it you can avoid parsing Quill by adding this to your Webpack configuration:
|
740 |
|
741 | ```js
|
742 | module: {
|
743 | // Shut off warnings about using pre-built javascript files
|
744 | // as Quill.js unfortunately ships one as its `main`.
|
745 | noParse: /node_modules\/quill\/dist/
|
746 | }
|
747 | ```
|
748 |
|
749 | See [#7](https://github.com/zenoamaro/react-quill/issues/7) for more details.
|
750 |
|
751 | ## Browser support
|
752 |
|
753 | Please check the browser support table for the upstream [Quill](https://github.com/quilljs/quill) dependency. The React part of the codebase is ES5-compatible.
|
754 |
|
755 | ## Changelog
|
756 |
|
757 | ### v1.2.4
|
758 |
|
759 | - Only restore focus if editor had focus (#312 @MattKunze)
|
760 |
|
761 | ### v1.2.0
|
762 |
|
763 | - Add Typescript definitions (#277 @Charrondev)
|
764 |
|
765 | ### v1.1.0
|
766 | - Add support for React 16 and onwards by depending on `prop-types` and `create-react-class` (#181 @mikecousins)
|
767 | - Allow setting contents with a Quill Delta via the `value` prop (#101)
|
768 | - Add onFocus/onBlur props (#110)
|
769 | - Add tabindex support (#232)
|
770 |
|
771 | #### v1.0.0
|
772 | This release adds support for Quill v1.0.0+. ⚠️ There are many breaking changes, both in Quill and in ReactQuill. See [Upgrading to React-Quill v1.0.0](#upgrading-to-react-quill-v100).
|
773 |
|
774 | - Updated to support Quill v1.0.0+ (@clemmy, @alexkrolick)
|
775 | - Bundling Quill with ReactQuill (@clemmy)
|
776 | - Bundling CSS files in the NPM package
|
777 | - Removed `dist` from source control (@alexkrolick)
|
778 | - Deprecated `toolbar` property and component
|
779 | - Deprecated the `styles` property
|
780 | - Deprecated custom formats via the `formats` property
|
781 | - Deprecated the `pollInterval` property
|
782 | - Rerendering on `style` property change (@lavrton)
|
783 | - Improved docs for `bounds`, which now rerenders on change
|
784 | - Performing deep props comparison to avoid rerenders
|
785 | - Fixed the unprivileged editor not returning values
|
786 | - Restoring selection event after text change
|
787 | - Fixed the order of parameters in change events (@webcarrot)
|
788 | - Using 'core' instead of 'base' CSS (@alexkrolick)
|
789 | - Added support for the `placeholder` property (@alexkrolick)
|
790 | - Enable/disable editor using top-level Quill API (@alexkrolick)
|
791 | - Prevent whitespace issues when initializing the editor (@bobrafie)
|
792 | - Using buttons instead of spans for toolbar actions (@clemmy)
|
793 | - Removed getHtml from unprivileged editor (@clemmy)
|
794 | - Fixed calculations for range fields (@clemmy)
|
795 | - Removed deprecated destroy functionality (@clemmy)
|
796 | - Added return statement to proxy editor methods (@druti)
|
797 | - Inline styles support for Quill Toolbar (@e-jigsaw)
|
798 | - Fixed custom font size definitions (@clemmy)
|
799 | - Support for bullet and ordered lists in toolbar (@clemmy)
|
800 | - Updated the toolbar alignment section (@clemmy)
|
801 | - Updated rendering of toolbar actions (@clemmy)
|
802 | - Improved toolbar renderChoices implementation (@zhang-z)
|
803 | - Fixed use of `defaultValue` in Toolbar selects
|
804 | - Fixed bounds validation in setEditorSelection (@wouterh)
|
805 | - Exposed Quill in exports (@tdg5)
|
806 | - Added unhook function to clean up event listeners on unmount (@alexkrolick, @jrmmnr)
|
807 | - Fixed documentation typos (@l3kn)
|
808 | - Started testing with Enzyme (@alexkrolick)
|
809 | - Fixed issue where changing props caused re-render artifacts (#147)
|
810 | - Fixed bounds validation in setEditorSelection (@wouterh)
|
811 | - Updated README.md to reference core.css instead of base.css (@sandbochs)
|
812 | - Updated React peerDependency (@rpellerin)
|
813 | - Removed inline Parchment formats for font-size and font-family (#217)
|
814 |
|
815 | #### v0.4.1
|
816 | - Added contents of `dist` to NPM package.
|
817 |
|
818 | #### v0.4.0
|
819 | This release adds support for React 0.14. ⚠️ Shims to support older versions of React have been removed.
|
820 |
|
821 | - React 0.14 support (@jacktrades, #49)
|
822 | - Removed shims for React 0.12 and 0.13
|
823 | - Bumped Quill.js to v0.20.1
|
824 | - _Normal_ and _smaller_ sizes are not swapped anymore. (#63)
|
825 | - Various toolbar choice items are now correctly ordered.
|
826 | - Added image tooltips to the default set of modules (@kairxa, #54)
|
827 | - Fixed extra white-space in classnames (@asiniy, #67)
|
828 | - Published the Quill namespace on ReactQuill (@Sajam, #60)
|
829 | - Quill stylesheets are now linked to `dist/` for convenience. (#70)
|
830 | - Exposed editor accessor methods in change events. (#33)
|
831 |
|
832 | [Full changelog](CHANGELOG.md)
|
833 |
|
834 |
|
835 | ## Contributors
|
836 |
|
837 | React Quill would not be where it is today without the contributions of many people, which we are incredibly grateful for:
|
838 | - @zenoamaro (maintainer)
|
839 | - @alexkrolick (maintainer)
|
840 | - @clemmy
|
841 | - @asiniy
|
842 | - @webcarrot
|
843 | - @druti
|
844 | - @e-jigsaw
|
845 | - @zhang-z
|
846 | - @Sajam
|
847 | - @0bird512
|
848 | - @jacktrades
|
849 | - @1000hz
|
850 | - @kkerr1
|
851 | - @csk157
|
852 | - @Janekk
|
853 | - @AndyTheGiant
|
854 | - @chrismcv
|
855 | - @wouterh
|
856 | - @tdg5
|
857 | - @jrmmnr
|
858 | - @l3kn
|
859 | - @rpellerin
|
860 | - @sandbochs
|
861 | - @wouterh
|
862 | - @MattKunze
|
863 |
|
864 | ## Roadmap
|
865 |
|
866 | - [x] React 0.14 support
|
867 | - [x] Quill v1.0.0+ support
|
868 | - [x] Tests!
|
869 | - [x] Compatibility with React 16
|
870 | - [ ] Additional APIs for working with Quill
|
871 |
|
872 | ## License
|
873 |
|
874 | The MIT License (MIT)
|
875 |
|
876 | Copyright (c) 2016, zenoamaro <zenoamaro@gmail.com>
|
877 |
|
878 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
879 |
|
880 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
881 |
|
882 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|