Skip to content

Kirby 5.6.4

Textarea buttons

You can add your custom textarea button in a plugin via the textareaButtons extension:

site/plugins/your-plugin/index.js
window.panel.plugin("getkirby/custom-textarea-buttons", {
    textareaButtons: {
        highlight: {
            label: "Highlight",
            icon: "wand",
            click: function () {
                this.command("toggle", "<mark>", "</mark>");
            },
            shortcut: "m"
        }
    }
});

Each button should specify a label, an icon as well as a click handler.

click handler

This function is called when your button is clicked. You can add any code you want, but most likely you want to use the this.command(name, ...args) helper functions to trigger changes in the textarea input.

Don't use arrow function syntax such as click: () =>. We cannot rebind this inside of an arrow function and you won't have access to the this.command helper.

The following commands are supported:

Command name Description Arguments
dialog Opens the k-toolbar-{name}-dialog component and inserts the text it submits string name of the dialog
file Opens the file picker of the field and inserts the selected files
insert Inserts the given text at the current selection string or function text
prepend Prepends the given text to the current selection string text
toggle Toggles wrapping of the current selection string before,
string after (optional)
upload Opens the file upload dialog
wrap Wraps the current selection with the given text string before,
string after (optional)

If you pass a function to insert, it will be called with two parameters and has to return the text to insert:

  • input: the textarea element
  • selection: the currently selected text

For example, the default ordered list button triggers the following command:

this.command("insert", (input, selection) =>
    selection
        .split("\n")
        .map((line, index) => index + 1 + ". " + line)
        .join("\n")
);

Button label translation

You can translate the button label by defining it in the label getter. Your plugin's index.js runs before the Panel has started, so window.panel.t() isn't available yet. The getter is only called when the label is rendered, at which point the translation is available:

site/plugins/your-plugin/index.js
window.panel.plugin("getkirby/custom-textarea-buttons", {
    textareaButtons: {
        highlight: {
            get label() {
                return window.panel.t("highlight");
            },
            icon: "wand",
            click: function () {
                this.command("toggle", "<mark>", "</mark>");
            },
            shortcut: "m"
        }
    }
});

Read more about plugin translations.

Shortcut

Each button can also define a shortcut which will trigger the button's click handler when the shortcut is pressed together with the Cmd/Ctrl key inside the textarea.

Use in the field

Your new textarea button won't automatically be included in the textarea's toolbar. To add it, use the buttons option.