🚀 BlockNote AI is here! Access the early preview.
Examples/UI Components/Adding Link Toolbar Buttons

Adding Link Toolbar Buttons

In this example, we add a button to the Link Toolbar which opens a browser alert.

Try it out: Hover the link open the Link Toolbar, and click the new "Open Alert" button!

Relevant Docs:

import { LinkToolbarProps, useComponentsContext } from "@blocknote/react";

// Custom Link Toolbar button to open a browser alert.
export function AlertButton(props: LinkToolbarProps) {
  const Components = useComponentsContext()!;

  return (
    <Components.LinkToolbar.Button
      mainTooltip={"Open Alert with URL"}
      onClick={() => window.alert(`Link URL: ${props.url}`)}
    >
      Open Alert
    </Components.LinkToolbar.Button>
  );
}
import "@blocknote/core/fonts/inter.css";
import { BlockNoteView } from "@blocknote/mantine";
import "@blocknote/mantine/style.css";
import {
  DeleteLinkButton,
  EditLinkButton,
  LinkToolbar,
  LinkToolbarController,
  OpenLinkButton,
  useCreateBlockNote,
} from "@blocknote/react";

import { AlertButton } from "./AlertButton";

export default function App() {
  // Creates a new editor instance.
  const editor = useCreateBlockNote({
    initialContent: [
      {
        type: "paragraph",
        content: "Welcome to this demo!",
      },
      {
        type: "paragraph",
        content: "Hover the link below to see the modified Link Toolbar",
      },
      {
        type: "paragraph",
        content: [
          {
            type: "link",
            href: "https://www.blocknotejs.org/",
            content: [
              {
                type: "text",
                text: "Home Page",
                styles: {},
              },
            ],
          },
        ],
      },
      {
        type: "paragraph",
      },
    ],
  });

  // Renders the editor instance.
  return (
    <BlockNoteView editor={editor} linkToolbar={false}>
      <LinkToolbarController
        linkToolbar={(props) => (
          <LinkToolbar {...props}>
            <EditLinkButton
              url={props.url}
              text={props.text}
              editLink={props.editLink}
            />
            <OpenLinkButton url={props.url} />
            <DeleteLinkButton deleteLink={props.deleteLink} />
            {/* Extra button to open alert. */}
            <AlertButton {...props} />
          </LinkToolbar>
        )}
      />
    </BlockNoteView>
  );
}