Skip to content

Commit c210817

Browse files
docs: Updated formatting toolbar buttons example (#1915)
1 parent 28d808b commit c210817

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

examples/03-ui-components/02-formatting-toolbar-buttons/src/App.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ export default function App() {
6060
type: "paragraph",
6161
content: "Select some text to try them out",
6262
},
63+
{
64+
type: "paragraph",
65+
content:
66+
"Notice that the button doesn't appear when the image block above is selected, as it has no inline content.",
67+
},
6368
{
6469
type: "paragraph",
6570
},

examples/03-ui-components/02-formatting-toolbar-buttons/src/BlueButton.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
useBlockNoteEditor,
44
useComponentsContext,
55
useEditorContentOrSelectionChange,
6+
useSelectedBlocks,
67
} from "@blocknote/react";
78
import { useState } from "react";
89

@@ -26,6 +27,14 @@ export function BlueButton() {
2627
);
2728
}, editor);
2829

30+
// Doesn't render unless a at least one block with inline content is
31+
// selected. You can use a similar pattern of returning `null` to
32+
// conditionally render buttons based on the editor state.
33+
const blocks = useSelectedBlocks();
34+
if (blocks.filter((block) => block.content !== undefined)) {
35+
return null;
36+
}
37+
2938
return (
3039
<Components.FormattingToolbar.Button
3140
mainTooltip={"Blue Text & Background"}

0 commit comments

Comments
 (0)