Creating custom Split toolbar buttons
A split button contains a basic button and a menu button, wrapped up into one toolbar item. Clicking the menu button section opens a dropdown list. The basic button section and the menu items can be configured to trigger different actions when clicked.
Options
Name | Value | Target component | Requirement | Description |
---|---|---|---|---|
text |
string |
Primary button |
optional |
Text displayed if no icon is found. |
icon |
string |
Primary button |
optional |
Name of the icon to be displayed. Must correspond to an icon: in the icon pack, in a custom icon pack, or added using the |
onAction |
|
Primary button |
required |
Function invoked when the basic button section is clicked. |
select |
|
Choice menu items |
optional |
default: |
columns |
number or |
Drop-down menu |
optional |
default: |
fetch |
|
Drop-down menu |
required |
A callback function that should be passed a list of choice menu items for the dropdown menu. |
onItemAction |
|
Choice menu items |
required |
Function invoked when a dropdown list option is clicked. The |
onSetup |
|
All |
optional |
default: |
API
Name | Value | Description |
---|---|---|
isEnabled |
|
Checks if the button is enabled. |
setEnabled |
|
Sets the button’s enabled state. |
isActive |
|
Checks the button’s toggle state. |
setActive |
|
Sets the button’s toggle state. |
NOTE: This feature is only available for TinyMCE 6.4 and later. |
||
setText |
|
Sets the text label to display. |
setIcon |
|
Sets the icon of the button. |
Split button example and explanation
The following example sets up a split button with a text label and a static dropdown menu.
A split button is similar to a basic button in that they both require an onAction
callback. The onAction
callback function should take the button’s API and return nothing. It is called when the basic button section is clicked. In this example, onAction
calls editor.insertContent(value)
which inserts the given content into the editor.
onItemAction
is called when a menu item is clicked. The callback function is passed the split button’s API and the value of the selected menu item. Nothing should be returned. The example calls editor.insertContent(value)
to insert the value into the editor’s content.
The fetch
function is called whenever the split button’s drop-down menu is opened. It is a function that takes a callback and passes it an array of menu items to be rendered in the button’s drop-down menu. This allows for asynchronous fetching of the menu items.
Choice menu items
Choice menu items are a special type of menu item used for split toolbar button menu items. For information on split buttons, see: Split toolbar buttons.
Options
Name | Value | Requirement | Description |
---|---|---|---|
value |
any |
required |
A value that is passed to |
text |
string |
optional |
Text to display. |
icon |
string |
optional |
Name of the icon to be displayed. Must correspond to an icon: in the icon pack, in a custom icon pack, or added using the |
enabled |
boolean |
optional |
default: |
shortcut |
string |
optional |
Text to display in the shortcut label. To register a shortcut, see: Add custom shortcuts to TinyMCE. |
For an example of how choice menu items are used in split toolbar buttons, see: Split button example and explanation.
Using onSetup
onSetup
is a complex property. It takes a function that is passed the component’s API and should return a callback that is passed the component’s API and returns nothing. This occurs because onSetup
runs whenever the component is rendered, and the returned callback is executed when the component is destroyed. This is essentially an onTeardown
handler, and can be used to unbind events and callbacks.
To clarify, in code onSetup
may look like this:
onSetup: (api) => {
// Do something here on component render, like set component properties or bind an event listener
return (api) => {
// Do something here on teardown, like unbind an event listener
};
};
To bind a callback function to an editor event use editor.on(eventName, callback)
. To unbind an event listener use editor.off(eventName, callback)
. Any event listeners should be unbound in the teardown callback. The only editor event which does not need to be unbound is init
e.g. editor.on('init', callback)
.
|