Skip to content
Quannt's Technical Blog

Adding a custom drop-down toolbar to react-quill

web dev1 min read

Surprisingly it is not very intuitive to add a custom drop-down toolbar to quilljs. Anyway, this is one way of doing so

import React from "react";
import 'react-quill/dist/quill.snow.css';
import ReactQuill from 'react-quill';
import PropTypes from 'prop-types';

class Editor extends React.Component {
    constructor(props) {
        super(props)
    }
    componentDidMount() {
        const placeholderPickerItems = Array.prototype.slice.call(document.querySelectorAll('.ql-placeholder .ql-picker-item'));

        placeholderPickerItems.forEach(item => item.textContent = item.dataset.value);

        document.querySelector('.ql-placeholder .ql-picker-label').innerHTML
            = 'Insert placeholder' + document.querySelector('.ql-placeholder .ql-picker-label').innerHTML;
    }
    render() {
        return (
            <div className="text-editor">
                <ReactQuill
                    theme={'snow'}
                    onChange={this.props.handleChange}
                    value={this.props.value}
                    modules={Editor.modules}
                    placeholder={this.props.placeholder}
                />
            </div>

        )
    }
}


/* 
 * Quill modules to attach to editor
 * See http://quilljs.com/docs/modules/ for complete options
 */
Editor.modules = {
    toolbar: {
        container:
        [
            [{ 'placeholder': ['[GuestName]', '[HotelName]'] }], // my custom dropdown
            ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
            ['blockquote', 'code-block'],

            [{ 'header': 1 }, { 'header': 2 }],               // custom button values
            [{ 'list': 'ordered' }, { 'list': 'bullet' }],
            [{ 'script': 'sub' }, { 'script': 'super' }],      // superscript/subscript
            [{ 'indent': '-1' }, { 'indent': '+1' }],          // outdent/indent
            [{ 'direction': 'rtl' }],                         // text direction

            [{ 'size': ['small', false, 'large', 'huge'] }],  // custom dropdown
            [{ 'header': [1, 2, 3, 4, 5, 6, false] }],

            [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
            [{ 'font': [] }],
            [{ 'align': [] }],

            ['clean']                                    // remove formatting button
           
        ],
        handlers: {
            "placeholder": function (value) { 
                if (value) {
                    const cursorPosition = this.quill.getSelection().index;
                    this.quill.insertText(cursorPosition, value);
                    this.quill.setSelection(cursorPosition + value.length);
                }
            }
        }
    }
}


/* 
 * PropType validation
 */
Editor.propTypes = {
    placeholder: PropTypes.string,
    onChange: PropTypes.func,
    value: PropTypes.string
}

export default Editor;
© 2022 by Quannt's Technical Blog. All rights reserved.
Theme by LekoArts