toolbar.mdx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. ---
  2. title: Toolbar Module
  3. ---
  4. The Toolbar module allow users to easily format Quill's contents.
  5. <div className="quill-wrapper">
  6. <div id="toolbar-toolbar" className="toolbar">
  7. <span className="ql-formats">
  8. <select className="ql-font">
  9. <option selected></option>
  10. <option value="serif"></option>
  11. <option value="monospace"></option>
  12. </select>
  13. <select className="ql-size">
  14. <option value="small"></option>
  15. <option selected></option>
  16. <option value="large"></option>
  17. <option value="huge"></option>
  18. </select>
  19. </span>
  20. <span className="ql-formats">
  21. <button className="ql-bold"></button>
  22. <button className="ql-italic"></button>
  23. <button className="ql-underline"></button>
  24. <button className="ql-strike"></button>
  25. </span>
  26. <span className="ql-formats">
  27. <select className="ql-color"></select>
  28. <select className="ql-background"></select>
  29. </span>
  30. <span className="ql-formats">
  31. <button className="ql-list" value="ordered"></button>
  32. <button className="ql-list" value="bullet"></button>
  33. <select className="ql-align">
  34. <option selected></option>
  35. <option value="center"></option>
  36. <option value="right"></option>
  37. <option value="justify"></option>
  38. </select>
  39. </span>
  40. <span className="ql-formats">
  41. <button className="ql-link"></button>
  42. <button className="ql-image"></button>
  43. </span>
  44. </div>
  45. <Editor
  46. style={{ display: 'none' }}
  47. config={{
  48. modules: {
  49. toolbar: { container: '#toolbar-toolbar' },
  50. },
  51. theme: 'snow',
  52. }}
  53. />
  54. </div>
  55. It can be configured with a custom container and handlers.
  56. ```javascript
  57. const quill = new Quill('#editor', {
  58. modules: {
  59. toolbar: {
  60. container: '#toolbar', // Selector for toolbar container
  61. handlers: {
  62. bold: customBoldHandler
  63. }
  64. }
  65. }
  66. });
  67. ```
  68. Because the `container` option is so common, a top level shorthand is also allowed.
  69. ```javascript
  70. const quill = new Quill('#editor', {
  71. modules: {
  72. // Equivalent to { toolbar: { container: '#toolbar' }}
  73. toolbar: '#toolbar'
  74. }
  75. });
  76. ```
  77. ## Container
  78. Toolbar controls can either be specified by a simple array of format names or a custom HTML container.
  79. To begin with the simpler array option:
  80. ```javascript
  81. const toolbarOptions = ['bold', 'italic', 'underline', 'strike'];
  82. const quill = new Quill('#editor', {
  83. modules: {
  84. toolbar: toolbarOptions
  85. }
  86. });
  87. ```
  88. Controls can also be grouped by one level of nesting an array. This will wrap controls in a `<span>` with class name `ql-formats`, providing structure for themes to utilize. For example [Snow](/docs/themes/#snow/) adds extra spacing between control groups.
  89. ```javascript
  90. const toolbarOptions = [['bold', 'italic'], ['link', 'image']];
  91. ```
  92. Buttons with custom values can be specified with an Object with the name of the format as its only key.
  93. ```javascript
  94. const toolbarOptions = [{ header: '3' }];
  95. ```
  96. Dropdowns are similarly specified by an Object, but with an array of possible values. CSS is used to control the visual labels for dropdown options.
  97. ```javascript
  98. // Note false, not 'normal', is the correct value
  99. // quill.format('size', false) removes the format,
  100. // allowing default styling to work
  101. const toolbarOptions = [
  102. { size: [ 'small', false, 'large', 'huge' ]}
  103. ];
  104. ```
  105. Note [Themes](/docs/themes/) may also specify default values for dropdowns. For example, [Snow](/docs/themes/#snow/) provides a default list of 35 colors for the `color` and `background` formats, if set to an empty array.
  106. ```javascript
  107. const toolbarOptions = [
  108. ['bold', 'italic', 'underline', 'strike'], // toggled buttons
  109. ['blockquote', 'code-block'],
  110. ['link', 'image', 'video', 'formula'],
  111. [{ 'header': 1 }, { 'header': 2 }], // custom button values
  112. [{ 'list': 'ordered'}, { 'list': 'bullet' }, { 'list': 'check' }],
  113. [{ 'script': 'sub'}, { 'script': 'super' }], // superscript/subscript
  114. [{ 'indent': '-1'}, { 'indent': '+1' }], // outdent/indent
  115. [{ 'direction': 'rtl' }], // text direction
  116. [{ 'size': ['small', false, 'large', 'huge'] }], // custom dropdown
  117. [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
  118. [{ 'color': [] }, { 'background': [] }], // dropdown with defaults from theme
  119. [{ 'font': [] }],
  120. [{ 'align': [] }],
  121. ['clean'] // remove formatting button
  122. ];
  123. const quill = new Quill('#editor', {
  124. modules: {
  125. toolbar: toolbarOptions
  126. },
  127. theme: 'snow'
  128. });
  129. ```
  130. For use cases requiring even more customization, you can manually create a toolbar in HTML, and pass the DOM element or selector into Quill. The `ql-toolbar` class will be added to the toolbar container and Quill attach appropriate handlers to `<button>` and `<select>` elements with a class name in the form `ql-${format}`. Buttons element may optionally have a custom `value` attribute.
  131. ```html
  132. <!-- Create toolbar container -->
  133. <div id="toolbar">
  134. <!-- Add font size dropdown -->
  135. <select class="ql-size">
  136. <option value="small"></option>
  137. <!-- Note a missing, thus falsy value, is used to reset to default -->
  138. <option selected></option>
  139. <option value="large"></option>
  140. <option value="huge"></option>
  141. </select>
  142. <!-- Add a bold button -->
  143. <button class="ql-bold"></button>
  144. <!-- Add subscript and superscript buttons -->
  145. <button class="ql-script" value="sub"></button>
  146. <button class="ql-script" value="super"></button>
  147. </div>
  148. <div id="editor"></div>
  149. <!-- Initialize editor with toolbar -->
  150. <script>
  151. const quill = new Quill('#editor', {
  152. modules: {
  153. toolbar: '#toolbar'
  154. }
  155. });
  156. </script>
  157. ```
  158. Note by supplying your own HTML element, Quill searches for particular input elements, but your own inputs that have nothing to do with Quill can still be added and styled and coexist.
  159. ```html
  160. <div id="toolbar">
  161. <!-- Add buttons as you would before -->
  162. <button class="ql-bold"></button>
  163. <button class="ql-italic"></button>
  164. <!-- But you can also add your own -->
  165. <button id="custom-button"></button>
  166. </div>
  167. <div id="editor"></div>
  168. <script>
  169. const quill = new Quill('#editor', {
  170. modules: {
  171. toolbar: '#toolbar',
  172. },
  173. });
  174. const customButton = document.querySelector('#custom-button');
  175. customButton.addEventListener('click', function () {
  176. console.log('Clicked!');
  177. });
  178. </script>
  179. ```
  180. ## Handlers
  181. The toolbar controls by default applies and removes formatting, but you can also overwrite this with custom handlers, for example in order to show external UI.
  182. Handler functions will be bound to the toolbar (so using `this` will refer to the toolbar instance) and passed the `value` attribute of the input if the corresponding format is inactive, and `false` otherwise. Adding a custom handler will overwrite the default toolbar and theme behavior.
  183. ```javascript
  184. const toolbarOptions = {
  185. handlers: {
  186. // handlers object will be merged with default handlers object
  187. link: function (value) {
  188. if (value) {
  189. const href = prompt('Enter the URL');
  190. this.quill.format('link', href);
  191. } else {
  192. this.quill.format('link', false);
  193. }
  194. }
  195. }
  196. };
  197. const quill = new Quill('#editor', {
  198. modules: {
  199. toolbar: toolbarOptions
  200. }
  201. });
  202. // Handlers can also be added post initialization
  203. const toolbar = quill.getModule('toolbar');
  204. toolbar.addHandler('image', showImageUI);
  205. ```