Search and Replace
SearchAndReplaceExtension adds search, match navigation, and replacement to your editor. It is opt-in and is not registered in the default editor. UI is deliberately left to consumers: the only implemented UI is the Mantine component in this example, which you can copy or adapt to your preferences.
Try it out: Click Find or press Cmd/Ctrl+F inside the editor, search for fox, and expand Replace to change one match or all matches.
Usage
Install the extension when creating your editor, then use its methods to drive your own controls:
import { BlockNoteEditor } from "@blocknote/core";
import { SearchAndReplaceExtension } from "@blocknote/core/extensions";
const editor = BlockNoteEditor.create({
extensions: [SearchAndReplaceExtension()],
});
const search = editor.getExtension(SearchAndReplaceExtension)!;
search.setQuery({ searchTerm: "fox" });
search.nextMatch();
search.replace("wolf");
search.replaceAll("wolf");
search.clearSearch();For the example UI, copy SearchAndReplace.tsx and style.css, then wrap your BlockNoteView with <SearchAndReplace editor={editor}> as shown in App.tsx.
Relevant Docs:
import "@blocknote/core/fonts/inter.css";import { SearchAndReplaceExtension } from "@blocknote/core/extensions";import { BlockNoteView } from "@blocknote/mantine";import "@blocknote/mantine/style.css";import { useCreateBlockNote } from "@blocknote/react";import { SearchAndReplace } from "./SearchAndReplace";import "./style.css";export default function App() { const editor = useCreateBlockNote({ extensions: [SearchAndReplaceExtension()], initialContent: [ { type: "heading", content: "The fox field guide" }, { type: "paragraph", content: [ { type: "text", text: "A curious ", styles: {} }, { type: "text", text: "f", styles: { bold: true } }, { type: "text", text: "ox", styles: { italic: true } }, { type: "text", text: " crosses a formatting boundary. Find it just like any other fox.", styles: {}, }, ], }, { type: "bulletListItem", content: "Fox sightings", children: [ { type: "bulletListItem", content: "A red fox near the river." }, { type: "bulletListItem", content: "An arctic FOX in the snow." }, ], }, { type: "table", content: { type: "tableContent", rows: [ { cells: ["Animal", "Habitat"] }, { cells: ["Fennec fox", "Desert"] }, { cells: ["Gray fox", "Woodland"] }, ], }, }, { type: "paragraph", content: "Try Match case, use Enter / Shift+Enter to navigate, or replace fox with wolf. Undo works as usual.", }, ], }); return ( <SearchAndReplace editor={editor}> <BlockNoteView editor={editor} className="search-example-editor" /> </SearchAndReplace> );}import type { BlockNoteEditor } from "@blocknote/core";import { SearchAndReplaceExtension } from "@blocknote/core/extensions";import { useBlockNoteContext, useExtensionState, usePrefersColorScheme,} from "@blocknote/react";import { ActionIcon, Button, CloseButton, Group, MantineProvider, Paper, Text, TextInput,} from "@mantine/core";import { type ReactNode, useCallback, useId, useRef, useState } from "react";const navigationButtons = [ { direction: "previous", label: "Previous match (Shift+Enter)", path: "M8 13V3m-5 5 5-5 5 5", }, { direction: "next", label: "Next match (Enter)", path: "M8 3v10m-5-5 5 5 5-5", },] as const;/** Example-only UI: wrap an editor view to scope shortcuts to that editor. */export function SearchAndReplace({ editor, children,}: { editor: BlockNoteEditor; children: ReactNode;}) { const [open, setOpen] = useState(false); const search = editor.getExtension(SearchAndReplaceExtension)!; const { query, matches, currentIndex } = useExtensionState( SearchAndReplaceExtension, { editor }, ); const searchTerm = query?.searchTerm ?? ""; const caseSensitive = query?.caseSensitive ?? false; const [showReplace, setShowReplace] = useState(false); const [replacement, setReplacement] = useState(""); const searchInput = useRef<HTMLInputElement>(null); const replaceId = useId(); const context = useBlockNoteContext(); const systemColorScheme = usePrefersColorScheme(); const colorScheme = context?.colorSchemePreference || (systemColorScheme === "dark" ? "dark" : "light"); const panelRef = useCallback( (element: HTMLDivElement | null) => { if (!element) { return; } // Treat this sibling of BlockNoteView as editor UI for focus handling. editor.registerPortalElement(element); return () => editor.unregisterPortalElement(element); }, [editor], ); function openSearch() { setOpen(true); searchInput.current?.focus(); searchInput.current?.select(); } function closeSearch() { search.clearSearch(); setOpen(false); editor.focus(); } function navigate(direction: "next" | "previous") { if (direction === "next") { search.nextMatch(); } else { search.previousMatch(); } // Navigation must not move keyboard focus into the document. searchInput.current?.focus(); } return ( <MantineProvider cssVariablesSelector=".search-and-replace-example" deduplicateCssVariables={false} forceColorScheme={colorScheme} getRootElement={() => undefined} withGlobalClasses={false} > <div className="search-and-replace-example" data-mantine-color-scheme={colorScheme} data-search-state={ open ? (showReplace ? "replace" : "search") : "closed" } onKeyDownCapture={(event) => { if ( (event.metaKey || event.ctrlKey) && !event.altKey && event.key.toLowerCase() === "f" ) { event.preventDefault(); event.stopPropagation(); openSearch(); } else if ( event.key === "Escape" && open && !event.nativeEvent.isComposing ) { event.preventDefault(); event.stopPropagation(); closeSearch(); } }} > {open ? ( <Paper className="search-overlay" ref={panelRef} role="search" aria-label="Find and replace" withBorder radius="lg" shadow="lg" p={8} > <Group gap={4} wrap="nowrap"> <ActionIcon type="button" variant="subtle" color="gray" size={24} aria-label={ showReplace ? "Hide replacement controls" : "Show replacement controls" } aria-expanded={showReplace} aria-controls={replaceId} onClick={() => setShowReplace(!showReplace)} > <svg width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" aria-hidden="true" > <path d={showReplace ? "m4 6 4 4 4-4" : "m6 4 4 4-4 4"} /> </svg> </ActionIcon> <TextInput ref={searchInput} autoFocus size="xs" flex={1} miw={0} aria-label="Find in document" placeholder="Find" rightSectionWidth={72} rightSectionPointerEvents="none" rightSection={ <Text component="output" size="xs" c="dimmed" aria-live="polite" aria-atomic="true" style={{ whiteSpace: "nowrap" }} > {searchTerm ? matches.length === 0 ? "No matches" : `${currentIndex === null ? 0 : currentIndex + 1} of ${matches.length}` : ""} </Text> } value={searchTerm} onChange={(event) => search.setQuery({ searchTerm: event.target.value, caseSensitive, }) } onKeyDown={(event) => { if (event.key === "Enter" && !event.nativeEvent.isComposing) { event.preventDefault(); navigate(event.shiftKey ? "previous" : "next"); } }} /> <ActionIcon type="button" variant={caseSensitive ? "light" : "subtle"} color={caseSensitive ? undefined : "gray"} size={26} aria-label="Match case" aria-pressed={caseSensitive} title="Match case" onClick={() => search.setQuery({ searchTerm, caseSensitive: !caseSensitive }) } > <span style={{ fontSize: 12, fontWeight: 500 }}>Aa</span> </ActionIcon> {navigationButtons.map(({ direction, label, path }) => ( <ActionIcon key={direction} type="button" variant="subtle" color="gray" size={24} aria-label={label} disabled={matches.length === 0} onClick={() => navigate(direction)} > <svg width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" aria-hidden="true" > <path d={path} /> </svg> </ActionIcon> ))} <CloseButton type="button" size={24} aria-label="Close search (Escape)" onClick={closeSearch} /> </Group> {showReplace && ( <Group gap={4} mt={6} pl={28} wrap="nowrap" id={replaceId}> <TextInput size="xs" flex={1} miw={0} aria-label="Replace with" placeholder="Replace with" value={replacement} onChange={(event) => setReplacement(event.target.value)} onKeyDown={(event) => { if ( event.key === "Enter" && !event.nativeEvent.isComposing ) { event.preventDefault(); search.replace(replacement); } }} /> <Button type="button" variant="subtle" color="gray" size="xs" px={8} fw={500} disabled={currentIndex === null} onClick={() => search.replace(replacement)} > Replace </Button> <Button type="button" variant="subtle" color="gray" size="xs" px={8} fw={500} disabled={matches.length === 0} onClick={() => search.replaceAll(replacement)} > Replace all </Button> </Group> )} </Paper> ) : ( <div className="search-launcher"> <Button type="button" variant="default" size="xs" title="Find (Cmd/Ctrl+F inside the editor)" onClick={openSearch} > Find </Button> </div> )} {children} </div> </MantineProvider> );}.search-and-replace-example { --search-editor-inset: 54px; --search-editor-top-space: 16px; color: var(--mantine-color-text); font-family: var(--mantine-font-family); margin-inline: auto; max-width: 731px; position: relative;}.search-and-replace-example[data-search-state="search"] { --search-editor-top-space: 72px;}.search-and-replace-example[data-search-state="replace"] { --search-editor-top-space: 108px;}/* Mantine's global reset is intentionally not installed by this example. */.search-and-replace-example,.search-and-replace-example *,.search-and-replace-example *::before,.search-and-replace-example *::after { box-sizing: border-box;}/* The controls are a sibling of the scrollable editor, never part of its flow. */.search-and-replace-example .search-overlay,.search-and-replace-example .search-launcher { position: absolute; right: 12px; top: 12px; z-index: 20;}.search-and-replace-example .search-overlay { width: min(520px, calc(100% - 24px));}.search-and-replace-example .search-example-editor { border: 1px solid var(--mantine-color-default-border); border-radius: var(--mantine-radius-md); height: min(65vh, 480px); overflow-y: auto; overscroll-behavior: contain; scrollbar-gutter: stable;}.search-and-replace-example .search-example-editor .bn-editor { padding-inline: var(--search-editor-inset); padding-top: var(--search-editor-top-space); padding-bottom: 24px;}