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";const CustomFormattingToolbar = () => (  <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>);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://placehold.co/332x322.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={CustomFormattingToolbar} />    </BlockNoteView>  );}