A Pen by Alex Krolick on CodePen.
          Created
          March 3, 2017 06:08 
        
      - 
      
- 
        Save alexkrolick/e6fc8f9374c6f1eba2ccba5c6716c32f to your computer and use it in GitHub Desktop. 
    React-Quill Custom HTML Toolbar
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <div class="app"> | |
| </div> | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | /* | |
| * 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 (see line 73) | |
| * 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 | |
| key="editor" | |
| ref="editor" | |
| className="quill-contents" | |
| dangerouslySetInnerHTML={{__html:this.state.editorHtml}} | |
| /> | |
| </ReactQuill> | |
| </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') | |
| ) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js"></script> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.min.js"></script> | |
| <script src="https://unpkg.com/[email protected]/dist/react-quill.min.js"></script> | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | body { | |
| background: #f3f1f2; | |
| } | |
| .app { | |
| margin: 1rem auto; | |
| max-width: 40rem; | |
| } | |
| .app .ql-container { | |
| min-height: 10em; | |
| border-bottom-left-radius: 0.5em; | |
| border-bottom-right-radius: 0.5em; | |
| background: #fefcfc; | |
| } | |
| .app .ql-toolbar { | |
| background: #eaecec; | |
| border-top-left-radius: 0.5em; | |
| border-top-right-radius: 0.5em; | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <link href="https://unpkg.com/[email protected]/dist/quill.snow.css" rel="stylesheet" /> | |
| <link href="https://cdnjs.cloudflare.com/ajax/libs/octicons/4.4.0/font/octicons.css" rel="stylesheet" /> | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment