Custom Nested menu items
A nested menu item is a menu item with a submenu. Registering a submenu this way allows it to be reused in menubar menus and toolbar button menus without having to define the submenu twice. The submenu can contain any combination of basic menu items and toggle menu items.
| Name | Value | Requirement | Description |
|---|---|---|---|
text |
string |
optional |
Text to display if no icon is found. |
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 |
value |
string |
optional |
A value to associate with the menu item. |
onSetup |
|
optional |
default: |
getSubmenuItems |
|
required |
Function invoked when the menu item is clicked to open its submenu. Must return either a space separated string of registered menu names or an array of basic, toggle or nested menu items specifications. |
shortcut |
string |
optional |
Text to display in the shortcut label. To register a shortcut, see: Add custom shortcuts to TinyMCE. |
context |
string |
optional |
default: |
API
| Name | Value | Description |
|---|---|---|
isEnabled |
|
Checks if the menu item is enabled. |
setEnabled |
|
Sets the menu item’s enabled state. |
Example: creating a nested menu item
tinymce.init({
selector: 'textarea',
menu: {
custom: { title: 'Custom Menu', items: 'undo redo nesteditem' }
},
menubar: 'file edit custom',
setup: (editor) => {
editor.ui.registry.addNestedMenuItem('nesteditem', {
text: 'My nested menu item',
getSubmenuItems: () => [{
type: 'menuitem',
text: 'My submenu item',
onAction: () => alert('Submenu item clicked')
}]
});
}
});
Using onSetup
onSetup accepts a function that receives the component’s API. This function should return a callback that returns nothing after being passed the component’s API. This occurs because onSetup runs whenever the component is rendered, and the callback returned by onSetup is executed when the component is destroyed. The function returned from onSetup 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) => {
// Runs when the component is created
// Configure the component or bind event listeners
return (api) => {
// Runs when the component is destroyed
// Unbind event listeners or clean up resources
};
};
To bind a callback function to an editor event use +editor.off(eventName, callback)`]. To unbind an event listener use `xref:apis/tinymce.editor.adoc#off[`+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).
|