Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
react-quill
Advanced tools
react-quill is a React component for Quill, a powerful rich text editor. It provides a customizable and extensible interface for creating and editing content with a variety of formatting options.
Basic Usage
This code demonstrates the basic usage of react-quill. It imports the necessary modules, sets up a state to hold the editor's content, and renders the ReactQuill component.
import React, { useState } from 'react';
import ReactQuill from 'react-quill';
import 'react-quill/dist/quill.snow.css';
function MyEditor() {
const [value, setValue] = useState('');
return (
<ReactQuill value={value} onChange={setValue} />
);
}
export default MyEditor;
Custom Toolbar
This code demonstrates how to create a custom toolbar for the react-quill editor. It defines a CustomToolbar component and uses it in the MyEditor component, specifying the custom toolbar in the modules prop.
import React, { useState } from 'react';
import ReactQuill, { Quill } from 'react-quill';
import 'react-quill/dist/quill.snow.css';
const CustomToolbar = () => (
<div id="toolbar">
<button className="ql-bold">Bold</button>
<button className="ql-italic">Italic</button>
<select className="ql-color">
<option value="red"></option>
<option value="green"></option>
<option value="blue"></option>
</select>
</div>
);
function MyEditor() {
const [value, setValue] = useState('');
return (
<div>
<CustomToolbar />
<ReactQuill value={value} onChange={setValue} modules={{ toolbar: '#toolbar' }} />
</div>
);
}
export default MyEditor;
Formats and Modules
This code demonstrates how to specify custom formats and modules for the react-quill editor. It defines the formats and modules arrays and passes them as props to the ReactQuill component.
import React, { useState } from 'react';
import ReactQuill from 'react-quill';
import 'react-quill/dist/quill.snow.css';
const formats = [
'header', 'bold', 'italic', 'underline', 'strike', 'blockquote',
'list', 'bullet', 'indent',
'link', 'image'
];
const modules = {
toolbar: [
[{ 'header': '1' }, { 'header': '2' }, { 'font': [] }],
[{ 'list': 'ordered'}, { 'list': 'bullet' }],
['bold', 'italic', 'underline', 'strike', 'blockquote'],
['link', 'image'],
[{ 'align': [] }]
]
};
function MyEditor() {
const [value, setValue] = useState('');
return (
<ReactQuill value={value} onChange={setValue} modules={modules} formats={formats} />
);
}
export default MyEditor;
draft-js is a framework for building rich text editors in React, developed by Facebook. It provides a more programmatic approach to managing editor state and content, allowing for extensive customization and control over the editor's behavior. Compared to react-quill, draft-js requires more setup and configuration but offers greater flexibility.
slate is another highly customizable framework for building rich text editors in React. It provides a more modern and flexible approach to handling editor state and content, with a focus on immutability and functional programming. Slate is more complex to set up compared to react-quill but offers extensive customization options and control over the editor's behavior.
ckeditor4-react is a React integration for CKEditor 4, a popular WYSIWYG editor. It offers a wide range of features and plugins for content editing, similar to react-quill. CKEditor 4 provides a more traditional WYSIWYG editing experience and is highly configurable, making it a good alternative for users looking for a feature-rich editor.
💯 React Quill now supports Quill v1.0.0!
Thanks to @clemmy and @alexkrolick for landing this much-awaited change. There are many breaking changes, so be sure to read the migration guide.
🎧 Latest published package version: v1.0.0-beta-4
Follow React Quill's development on the beta channel leading to v1.0.0
.
npm install react-quill@v1.0.0-beta-4
🏵 Welcoming @alexkrolick to the team!
His contributions have been incredible so far, and his passion and dedication will bring some much-deserved love to this project.
class MyComponent extends React.Component {
constructor(props) {
super(props)
this.state = { text: '' }
}
handleChange(value) {
this.setState({ text: value })
}
render() {
return (
<ReactQuill value={this.state.text}
onChange={this.handleChange} />
)
}
}
The Quill editor supports 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 base theme containing only the bare essentials to allow modules like toolbars or tooltips to work.
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 like this:
<ReactQuill theme="snow" /> // or "bubble", "base"
And then link the appropriate stylesheet (only link the CSS for the themes you want to use):
<link rel="stylesheet" href="node_modules/react-quill/dist/quill.snow.css">
<link rel="stylesheet" href="node_modules/react-quill/dist/quill.bubble.css">
<link rel="stylesheet" href="node_modules/react-quill/dist/quill.base.css">
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.
The Quill Toolbar Module API provides an easy way to configure the default toolbar icons using an array of format names.
var MyComponent = React.createClass({
modules: {
toolbar: [
[{ 'header': [1, 2, false] }],
['bold', 'italic', 'underline','strike', 'blockquote'],
[{'list': 'ordered'}, {'list': 'bullet'}, {'indent': '-1'}, {'indent': '+1'}],
['link', 'image'],
['clean']
],
},
formats: [
'header',
'bold', 'italic', 'underline', 'strike', 'blockquote',
'list', 'bullet', 'indent',
'link', 'image'
],
render: function() {
return (
<div className="text-editor">
<ReactQuill theme="snow"
modules={this.modules}
formats={this.formats}>
</ReactQuill>
</div>
);
},
});
You can also supply your own HTML/JSX toolbar with custom elements that are not part of the Quill theme.
See this example live on Codepen: Custom Toolbar Example
/*
* Custom "star" icon for the toolbar using an Octicon
* https://octicons.github.io
*/
const CustomButton = () => <span className="octicon octicon-star" />
/*
* Event handler to be attached using Quill toolbar module
* http://quilljs.com/docs/modules/toolbar/
*/
function insertStar () {
const cursorPosition = this.quill.getSelection().index
this.quill.insertText(cursorPosition, "★")
this.quill.setSelection(cursorPosition + 1)
}
/*
* Custom toolbar component including insertStar button and dropdowns
*/
const CustomToolbar = () => (
<div id="toolbar">
<select className="ql-header">
<option value="1"></option>
<option value="2"></option>
<option selected></option>
</select>
<button className="ql-bold"></button>
<button className="ql-italic"></button>
<select className="ql-color">
<option value="red"></option>
<option value="green"></option>
<option value="blue"></option>
<option value="orange"></option>
<option value="violet"></option>
<option value="#d0d1d2"></option>
<option selected></option>
</select>
<button className="ql-insertStar">
<CustomButton />
</button>
</div>
)
/*
* Editor component with custom toolbar and content containers
*/
class Editor extends React.Component {
constructor (props) {
super(props)
this.state = { editorHtml: '' }
this.handleChange = this.handleChange.bind(this)
}
handleChange (html) {
this.setState({ editorHtml: html });
}
render() {
return (
<div className="text-editor">
<CustomToolbar />
<ReactQuill
onChange={this.handleChange}
placeholder={this.props.placeholder}
modules={Editor.modules}
/>
</div>
)
}
}
/*
* Quill modules to attach to editor
* See http://quilljs.com/docs/modules/ for complete options
*/
Editor.modules = {
toolbar: {
container: "#toolbar",
handlers: {
"insertStar": insertStar,
}
}
}
/*
* Quill editor formats
* See http://quilljs.com/docs/formats/
*/
Editor.formats = [
'header', 'font', 'size',
'bold', 'italic', 'underline', 'strike', 'blockquote',
'list', 'bullet', 'indent',
'link', 'image', 'color',
]
/*
* PropType validation
*/
Editor.propTypes = {
placeholder: React.PropTypes.string,
}
/*
* Render component on page
*/
ReactDOM.render(
<Editor placeholder={'Write something or insert a star ★'}/>,
document.querySelector('.app')
)
The component has two types of formats:
formats
prop. All formats are enabled by default.ES6 Import
import ReactQuill, { Quill } from 'react-quill'
CommonJS Require
var ReactQuill = require('react-quill');
var Quill = ReactQuill.Quill;
/*
* Example Parchment format from
* https://quilljs.com/guides/cloning-medium-with-parchment/
*/
let Inline = Quill.import('blots/inline');
class BoldBlot extends Inline { }
BoldBlot.blotName = 'bold';
BoldBlot.tagName = 'strong';
Quill.register(BoldBlot);
/*
* Editor component with default and custom formats
*/
class MyComponent extends React.Component {
constructor(props) {
this.formats = ['italic, 'underline'] // default formats
this.state = { text: '' }
}
handleChange(value) {
this.setState({text: value})
}
render() {
return (
<ReactQuill
value={this.state.text}
onChange={this.handleChange}
formats={this.formats} // the custom format is already registered
/>
)
}
}
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.
class MyComponent extends React.Component {
render() {
return (
<ReactQuill>
<div className="my-editing-area"/>
</ReactQuill>
);
}
});
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).
The ReactQuill default component is built using the mixin. See component.js for source.
import {Mixin} from 'react-quill'
var MyComponent = React.createClass({
mixins: [ ReactQuill.Mixin ],
componentDidMount: function() {
var editor = this.createEditor(
this.getEditingArea(),
this.getEditorConfig()
);
this.setState({ editor:editor });
},
componentWillReceiveProps: function(nextProps) {
if ('value' in nextProps && nextProps.value !== this.props.value) {
this.setEditorContents(this.state.editor, nextProps.value);
}
},
});
Please note that many migration steps to Quill v1.0 may also apply.
With v1.0.0, Quill adopted a new toolbar configuration format, to which React Quill will delegates all toolbar functionality, and which is now the preferred way to customize the toolbar.
Previously, toolbar properties could be set by passing a toolbar
prop to React Quill. Pass the same options as modules.toolbar
instead.
+ modules: {
toolbar: [
...
],
+ },
<ReactQuill
- toolbar={this.toolbar}
+ modules={this.modules}
/>
If you used to provide your own HTML toolbar component, you can still do the same:
+ modules: {
+ toolbar: '#my-toolbar-component',
+ },
<ReactQuill
- toolbar="#my-toolbar-component"
+ modules={this.modules}
/>
Note that it is not possible to pass a toolbar component as a child to ReactQuill anymore.
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:
+ modules: {
+ toolbar: '#my-quill-toolbar',
+ },
+ <ReactQuill.Toolbar
+ id='my-quill-toolbar'
+ items={this.oldStyleToolbarItems}
+ />
<ReactQuill
- toolbar={this.oldStyleToolbarItems}
+ modules={this.modules}
/>
However, consider switching to the new Quill format instead, or provide your own toolbar component.
React Quill now follows the Quill toolbar format closely. See the Quill toolbar documentation for a complete reference on all supported options.
formats
property is deprecatedAs of 1.0.0, use Parchment to define new formats. Use the Quill export from the module to register and extend formats:
Quill.register('formats/CustomFormat', MyCustomFormat);
styles
propertyPreviously, 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.
See the Quill Release Notes.
pollInterval
propertyThis 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.
ReactQuill.Mixin
: 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.
ReactQuill.Toolbar
: 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.
ReactQuill.Quill
: The Quill
namespace on which you can call registerModule
and such.
id
: ID to be applied to the DOM element.
className
: Classes to be applied to the DOM element.
value
: Value for the editor as a controlled component. 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.
defaultValue
: Initial value for the editor as an uncontrolled component.
readOnly
: If true, the editor won't allow changing its contents.
placeholder
: The default value for the empty editor.
modules
: An object specifying which modules are enabled, and their configuration. The editor toolbar is a commonly customized module. See the modules section over the Quill documentation for more information on what modules are available.
formats
: An array of formats to be enabled during editing. All implemented formats are enabled by default. See Formats for a list.
Custom formats should not be included in the array as of version 1.0.0. Instead they should be created through Parchment and registered with the module's Quill export.
style
: An object with custom CSS rules to apply on the editor's container. Rules should be in React's "camelCased" naming style.
theme
: The name of the theme to apply to the editor. Defaults to base
.
bounds
: Selector or DOM element used by Quill to constrain position of popups. Defaults to document.body
.
children
: 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. Note also 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.
onChange(content, delta, source, editor)
: 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-set itself, the source of the change, and finally a read-only proxy to editor accessors such as getText()
.
onChangeSelection(range, source, editor)
: 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()
.
onKeyPress(event)
: Called after a key has been pressed and released.
: Note that, like its native counterpart, this won't be called for special keys such as shift or enter. If you need those, hook onto onKeyDown
or onKeyUp
.
onKeyDown(event)
: Called after a key has been pressed, but before it is released.
: Note that, due to how Quill works, it's possible that you won't receive events for keys such as enter, backspace or delete. If that's the case, try hooking onto onKeyUp
instead.
onKeyUp(event)
: Called after a key has been released.
If you have a ref to a ReactQuill node (Codepen example), you will be able to invoke the following methods:
focus()
: Focuses the editor.
blur()
: Removes focus from the editor.
getEditor()
: 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.
You can run the automated test suite:
npm test
And build a minificated version of the source:
npm run build
More tasks are available on the Makefile:
lint: lints the source
spec: runs the test specs
coverage: runs the code coverage test
test: lint, spec and coverage threshold test
build: builds the minified version
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.
Quill ships only a pre-built javascript file, so Webpack will complain after building a bundle:
Error: ./~/react-quill/~/quill/dist/quill.js
Critical dependencies:
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.
@ ./~/react-quill/~/quill/dist/quill.js 6:478-485
The warning is harmless, but if you want to silence it you can avoid parsing Quill by adding this to your Webpack configuration:
module: {
// Shut off warnings about using pre-built javascript files
// as Quill.js unfortunately ships one as its `main`.
noParse: /node_modules\/quill\/dist/
}
See #7 for more details.
Please check the browser support table for the upstream Quill dependency. The React part of the codebase is ES5-compatible.
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.
dist
from source control (@alexkrolick)toolbar
property and componentstyles
propertyformats
propertypollInterval
propertystyle
property change (@lavrton)bounds
, which now rerenders on changeplaceholder
property (@alexkrolick)defaultValue
in Toolbar selectsdist
to NPM package.This release adds support for React 0.14. ⚠️ Shims to support older versions of React have been removed.
dist/
for convenience. (#70)React Quill would not be where it is today without the contributions of many people, which we are incredibly grateful for:
The MIT License (MIT)
Copyright (c) 2016, zenoamaro zenoamaro@gmail.com
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.
FAQs
The Quill rich-text editor as a React component.
The npm package react-quill receives a total of 312,740 weekly downloads. As such, react-quill popularity was classified as popular.
We found that react-quill demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.