toolbar.mdx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. [{ 'header': 1 }, { 'header': 2 }], // custom button values
  111. [{ 'list': 'ordered'}, { 'list': 'bullet' }],
  112. [{ 'script': 'sub'}, { 'script': 'super' }], // superscript/subscript
  113. [{ 'indent': '-1'}, { 'indent': '+1' }], // outdent/indent
  114. [{ 'direction': 'rtl' }], // text direction
  115. [{ 'size': ['small', false, 'large', 'huge'] }], // custom dropdown
  116. [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
  117. [{ 'color': [] }, { 'background': [] }], // dropdown with defaults from theme
  118. [{ 'font': [] }],
  119. [{ 'align': [] }],
  120. ['clean'] // remove formatting button
  121. ];
  122. const quill = new Quill('#editor', {
  123. modules: {
  124. toolbar: toolbarOptions
  125. },
  126. theme: 'snow'
  127. });
  128. ```
  129. 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.
  130. ```html
  131. <!-- Create toolbar container -->
  132. <div id="toolbar">
  133. <!-- Add font size dropdown -->
  134. <select class="ql-size">
  135. <option value="small"></option>
  136. <!-- Note a missing, thus falsy value, is used to reset to default -->
  137. <option selected></option>
  138. <option value="large"></option>
  139. <option value="huge"></option>
  140. </select>
  141. <!-- Add a bold button -->
  142. <button class="ql-bold"></button>
  143. <!-- Add subscript and superscript buttons -->
  144. <button class="ql-script" value="sub"></button>
  145. <button class="ql-script" value="super"></button>
  146. </div>
  147. <div id="editor"></div>
  148. <!-- Initialize editor with toolbar -->
  149. <script>
  150. const quill = new Quill('#editor', {
  151. modules: {
  152. toolbar: '#toolbar'
  153. }
  154. });
  155. </script>
  156. ```
  157. 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.
  158. ```html
  159. <div id="toolbar">
  160. <!-- Add buttons as you would before -->
  161. <button class="ql-bold"></button>
  162. <button class="ql-italic"></button>
  163. <!-- But you can also add your own -->
  164. <button id="custom-button"></button>
  165. </div>
  166. <div id="editor"></div>
  167. <script>
  168. const quill = new Quill('#editor', {
  169. modules: {
  170. toolbar: '#toolbar',
  171. },
  172. });
  173. const customButton = document.querySelector('#custom-button');
  174. customButton.addEventListener('click', function () {
  175. console.log('Clicked!');
  176. });
  177. </script>
  178. ```
  179. ## Handlers
  180. 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.
  181. 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.
  182. ```javascript
  183. const toolbarOptions = {
  184. handlers: {
  185. // handlers object will be merged with default handlers object
  186. link: function (value) {
  187. if (value) {
  188. const href = prompt('Enter the URL');
  189. this.quill.format('link', href);
  190. } else {
  191. this.quill.format('link', false);
  192. }
  193. }
  194. }
  195. };
  196. const quill = new Quill('#editor', {
  197. modules: {
  198. toolbar: toolbarOptions
  199. }
  200. });
  201. // Handlers can also be added post initialization
  202. const toolbar = quill.getModule('toolbar');
  203. toolbar.addHandler('image', showImageUI);
  204. ```