Adding Formatting Toolbar Buttons

In this example, we add a blue text/background color and code style button to the Formatting Toolbar. We also make sure it only shows up when some text is selected.

Try it out: Select some text to open the Formatting Toolbar, and click one of the new buttons!

Relevant Docs:

import "@blocknote/core/fonts/inter.css";import { BlockNoteView } from "@blocknote/mantine";import "@blocknote/mantine/style.css";import {  BasicTextStyleButton,  BlockTypeSelect,  ColorStyleButton,  CreateLinkButton,  FileCaptionButton,  FileReplaceButton,  FormattingToolbar,  FormattingToolbarController,  NestBlockButton,  TextAlignButton,  UnnestBlockButton,  useCreateBlockNote,} from "@blocknote/react";import { BlueButton } from "./BlueButton";export default function App() {  // Creates a new editor instance.  const editor = useCreateBlockNote({    initialContent: [      {        type: "paragraph",        content: "Welcome to this demo!",      },      {        type: "paragraph",        content: [          {            type: "text",            text: "You can now toggle ",            styles: {},          },          {            type: "text",            text: "blue",            styles: { textColor: "blue", backgroundColor: "blue" },          },          {            type: "text",            text: " and ",            styles: {},          },          {            type: "text",            text: "code",            styles: { code: true },          },          {            type: "text",            text: " styles with new buttons in the Formatting Toolbar",            styles: {},          },        ],      },      {        type: "paragraph",        content: "Select some text to try them out",      },      {        type: "image",        props: {          url: "https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg",        },      },      {        type: "paragraph",        content:          "Notice that the buttons don't appear when the image block above is selected, as it has no inline content.",      },      {        type: "paragraph",      },    ],  });  // Renders the editor instance.  return (    <BlockNoteView editor={editor} formattingToolbar={false}>      <FormattingToolbarController        formattingToolbar={() => (          <FormattingToolbar>            <BlockTypeSelect key={"blockTypeSelect"} />            {/* Extra button to toggle blue text & background */}            <BlueButton key={"customButton"} />            <FileCaptionButton key={"fileCaptionButton"} />            <FileReplaceButton key={"replaceFileButton"} />            <BasicTextStyleButton              basicTextStyle={"bold"}              key={"boldStyleButton"}            />            <BasicTextStyleButton              basicTextStyle={"italic"}              key={"italicStyleButton"}            />            <BasicTextStyleButton              basicTextStyle={"underline"}              key={"underlineStyleButton"}            />            <BasicTextStyleButton              basicTextStyle={"strike"}              key={"strikeStyleButton"}            />            {/* Extra button to toggle code styles */}            <BasicTextStyleButton              key={"codeStyleButton"}              basicTextStyle={"code"}            />            <TextAlignButton              textAlignment={"left"}              key={"textAlignLeftButton"}            />            <TextAlignButton              textAlignment={"center"}              key={"textAlignCenterButton"}            />            <TextAlignButton              textAlignment={"right"}              key={"textAlignRightButton"}            />            <ColorStyleButton key={"colorStyleButton"} />            <NestBlockButton key={"nestBlockButton"} />            <UnnestBlockButton key={"unnestBlockButton"} />            <CreateLinkButton key={"createLinkButton"} />          </FormattingToolbar>        )}      />    </BlockNoteView>  );}