Topics

On this page

Last updated on Nov 7, 2024

Creating Custom Block Toolbar

Block toolbars are an essential part of the WordPress editor experience, offering users quick access to frequently used controls directly on the block itself. These toolbars provide inline, contextual actions like text formatting, alignment, or custom features that are relevant to the selected block. In this guide, we’ll walk through how to create custom block toolbars for your blocks and enhance the editing experience.

Screenshot-2024-10-23-at-7.12.55 PM
Block toolbar

Understanding the Block Toolbar

In the Gutenberg editor, the BlockControls component is used to add custom buttons and actions to the block toolbar. This toolbar typically appears above the block when the user selects it, providing easy access to important settings.

The toolbar can include buttons for:

Why do Toolbars Matter?
Toolbars enhance usability by allowing users to perform quick actions without diving into sidebar settings, making the block editor more intuitive and efficient.

Using the BlockControls Component

The BlockControls component is the foundation for adding toolbar buttons to your blocks. By wrapping your custom toolbar buttons inside, they will integrate seamlessly into the WordPress editor, appearing when the user selects the associated block.

Example: Adding a Custom Toolbar Button

Let’s start by adding a simple button to the toolbar for a custom action. In this case, we’ll add a “Bold Text” button that toggles bold formatting on the selected text.

import { __ } from "@wordpress/i18n";
import { BlockControls } from "@wordpress/block-editor";
import { ToolbarGroup, ToolbarButton } from "@wordpress/components";

const Edit = (props) => {
    const { attributes, setAttributes } = props;
    const { isBold } = attributes;
    const toggleBold = () => setAttributes({ isBold: !isBold });

    return (
        <div>
            <BlockControls>
                <ToolbarGroup>
                    <ToolbarButton
                        label={__("Bold Text")}
                        icon="editor-bold"
                        isActive={isBold}
                        onClick={toggleBold}
                    />
                </ToolbarGroup>
            </BlockControls>

            <p style={{ fontWeight: isBold ? "bold" : "normal" }}>
                This is some text content.
            </p>
        </div>
    );
};

export default Edit;

Grouping Toolbar Buttons

To keep toolbars organized, you can group related actions using the ToolbarGroup component. This ensures that users can find relevant actions in one place without cluttering the interface.

Example: Grouping Alignment Buttons in the Toolbar

import { BlockControls } from "@wordpress/block-editor";
import { ToolbarGroup, ToolbarButton } from "@wordpress/components";

const Edit = (props) => {
    const { attributes, setAttributes } = props;
    const { textAlign } = attributes;
    const setAlignment = (alignment) => setAttributes({ textAlign: alignment });

    return (
        <div>
            <BlockControls>
                <ToolbarGroup>
                    <ToolbarButton
                        label="Align Left"
                        icon="editor-alignleft"
                        onClick={() => setAlignment("left")}
                        isActive={textAlign === "left"}
                    />

                    <ToolbarButton
                        label="Align Center"
                        icon="editor-aligncenter"
                        onClick={() => setAlignment("center")}
                        isActive={textAlign === "center"}
                    />

                    <ToolbarButton
                        label="Align Right"
                        icon="editor-alignright"
                        onClick={() => setAlignment("right")}
                        isActive={textAlign === "right"}
                    />
                </ToolbarGroup>
            </BlockControls>
            <p style={{ textAlign }}>{"Aligned text"}</p>
        </div>
    );
};

export default Edit;

Using ToolbarDropdownMenu for Multiple Actions

Sometimes, you may have a set of related actions that should be grouped into a dropdown menu rather than individual buttons. This is especially useful when you have more than a few options.

Example: Adding a Dropdown Menu for Text Styles

import { ToolbarGroup, ToolbarDropdownMenu } from "@wordpress/components";

const Edit = (props) => {
    const { attributes, setAttributes } = props;
    const { textStyle } = attributes;
    const setTextStyle = (style) => setAttributes({ textStyle: style });

    return (
        <div>
            <BlockControls>
                <ToolbarGroup>
                    <ToolbarDropdownMenu
                        icon="editor-textcolor"
                        label="Text Style"
                        controls={[
                            { title: "Normal", onClick: () => setTextStyle("normal") },

                            { title: "Italic", onClick: () => setTextStyle("italic") },

                            { title: "Bold", onClick: () => setTextStyle("bold") },
                        ]}
                    />
                </ToolbarGroup>
            </BlockControls>

            <p
                style={{
                    fontStyle: textStyle === "italic" ? "italic" : "normal",
                    fontWeight: textStyle === "bold" ? "bold" : "normal",
                }}
            >
                This is styled text.
            </p>
        </div>
    );
};

export default Edit;

ToolbarDropdownMenu: Creates a dropdown menu with multiple text style options. Users can switch between normal, italic, or bold text styles by selecting from the dropdown.

Best Practices for Designing Block Toolbars

1. Keep the Toolbar Focused

2. Group Related Actions

3. Use Familiar Icons

4. Provide Clear Labels

5. Handle Loading and Disabled States

Example: Handling a Save Operation with a Disabled Button

const Edit = (props) => {
    const { attributes, setAttributes } = props;

    const { isSaving } = attributes;

    const handleSave = () => {
        // Simulate a save operation
        setAttributes({ isSaving: true });
        setTimeout(() => setAttributes({ isSaving: false }), 2000);
    };

    return (
        <div>
            <BlockControls>
                <ToolbarGroup>
                    <ToolbarButton
                        label="Save"
                        icon="save"
                        onClick={handleSave}
                        isBusy={isSaving}
                        disabled={isSaving}
                    />
                </ToolbarGroup>
            </BlockControls>
            <p>{isSaving ? "Saving..." : "Content"}</p>
        </div>
    );
};

ToolbarButton with isBusy: The button shows a “loading” state when a save operation is in progress, preventing the user from clicking multiple times.

Making Toolbars Accessible

Toolbars need to be fully accessible for all users, including those using screen readers or keyboard navigation.

Accessibility Tips:

Example: Accessible Button with ARIA Label

<ToolbarButton
    label="Bold Text"
    icon="editor-bold"
    onClick={ toggleBold }
    aria-label="Toggle bold text"
/>

ARIA Label: Provides an accessible description for screen readers to convey the purpose of the button.

Keyboard Accessibility and Shortcuts in Toolbars

For a great user experience, especially for power users, keyboard shortcuts should be provided where possible. Users should be able to navigate and activate toolbar buttons without relying solely on the mouse.

Best Practices:

Example: Adding Keyboard Shortcuts to Toolbar Buttons

import { ToolbarButton } from "@wordpress/components";
import { useEffect } from "react";

const Edit = (props) => {
    const { attributes, setAttributes } = props;
    const { isBold } = attributes;
    const toggleBold = () => setAttributes({ isBold: !isBold });
    // Adding a keyboard shortcut for bold (Ctrl + B)

    useEffect(() => {
        const handleKeyDown = (event) => {
            if (event.ctrlKey && event.key === "b") {
                event.preventDefault();

                toggleBold();
            }
        };

        window.addEventListener("keydown", handleKeyDown);

        return () => {
            window.removeEventListener("keydown", handleKeyDown);
        };
    }, [isBold]);

    return (
        <div>
            <BlockControls>
                <ToolbarButton
                    label="Bold Text"
                    icon="editor-bold"
                    onClick={toggleBold}
                    isActive={isBold}
                    aria-label="Toggle bold text"
                />
            </BlockControls>

            <p style={{ fontWeight: isBold ? "bold" : "normal" }}>
                This is some boldable text.
            </p>
        </div>
    );
};

export default Edit;

Contextual Toolbars for Dynamic Block States

In some cases, your block might need to display different toolbar buttons depending on the current state of the block. For example, a block could offer different actions based on the selected view mode (list view vs grid view).

Example: Contextual Toolbar for Grid and List Views

{ isGridView ? (
    <ToolbarButton label="Switch to List" icon="list-view" onClick={ switchToList } />
) : (
    <ToolbarButton label="Switch to Grid" icon="grid-view" onClick={ switchToGrid } />
) }

In this example, the toolbar dynamically changes depending on whether the user is in a grid view or a list view. This ensures the toolbar remains relevant and only shows actions that make sense for the current context.

Explanation:

Handling Loading and Disabled States in Toolbar Actions

When toolbar actions involve asynchronous processes, such as saving data or fetching content, it’s crucial to handle loading states properly. This provides feedback to users, prevents accidental multiple clicks, and improves the overall user experience.

Best Practices:

Example: Toolbar Button with Loading and Disabled States

const Edit = (props) => {
    const { attributes, setAttributes } = props;
    const { isSaving } = attributes;

    const handleSave = () => {
        setAttributes({ isSaving: true });
        setTimeout(() => setAttributes({ isSaving: false }), 2000); // Simulating save delay
    };

    return (
        <div>
            <BlockControls>
                <ToolbarButton
                    label="Save"
                    icon="save"
                    onClick={handleSave}
                    isBusy={isSaving}
                    disabled={isSaving}
                />
            </BlockControls>
            <p>{isSaving ? "Saving content..." : "Content saved!"}</p>
        </div>
    );
};

Performance Considerations for Custom Toolbars

Custom toolbars, especially those involving complex operations or multiple components, can affect the editor’s performance if not optimized properly. Follow these performance tips to ensure smooth operation:

Minimize Re-Renders

useMemo and useCallback to avoid unnecessary re-renders, especially when handling the toolbar button state.

Lazy Load Data

If your toolbar relies on external data (such as pulling items from an API), only load the data when needed.

Reduce Expensive Operations

Avoid running heavy computations or data fetches on every render of the block.

Example: Optimizing Toolbar Actions with useMemo and useCallback

import { useCallback, useMemo } from "react";
import { ToolbarButton } from "@wordpress/components";

const Edit = (props) => {
    const { attributes, setAttributes } = props;
    const { isBold } = attributes;

    const toggleBold = useCallback(() => {
        setAttributes({ isBold: !isBold });
    }, [isBold]);

    const toolbarLabel = useMemo(() => {
        return isBold ? "Unbold Text" : "Bold Text";
    }, [isBold]);

    return (
        <div>
            <BlockControls>
                <ToolbarButton
                    label={toolbarLabel}
                    icon="editor-bold"
                    onClick={toggleBold}
                    isActive={isBold}
                />
            </BlockControls>

            <p style={{ fontWeight: isBold ? "bold" : "normal" }}>
                This is boldable text.
            </p>
        </div>
    );
};

Creating custom block toolbars in the WordPress editor enhances the user experience by providing quick, intuitive access to block controls. By leveraging BlockControls, ToolbarButton, and ToolbarDropdownMenu, you can build toolbars that are efficient, accessible, and tailored to your block’s functionality.


Contributor

Shreya Agarwal

Shreya

Shreya Agarwal

Not Available