Skip to content

Commit 2bbdd6c

Browse files
committed
Small fix
1 parent ced52b4 commit 2bbdd6c

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

examples/03-ui-components/13-custom-ui/MUIFormattingToolbar.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -262,18 +262,24 @@ function MUITextAlignButton(props: {
262262
const editor = useBlockNoteEditor<TextBlockSchema>();
263263

264264
// The text alignment of the block currently containing the text cursor.
265-
const [activeTextAlignment, setActiveTextAlignment] = useState(
266-
() => editor.getTextCursorPosition().block.props.textAlignment
267-
);
265+
const [activeTextAlignment, setActiveTextAlignment] = useState(() => {
266+
const blockProps = editor.getTextCursorPosition().block.props;
267+
268+
if ("textAlignment" in blockProps) {
269+
return blockProps.textAlignment;
270+
}
271+
272+
return undefined;
273+
});
268274

269275
// Updates the text alignment when the editor content or selection changes.
270-
useEditorContentOrSelectionChange(
271-
() =>
272-
setActiveTextAlignment(
273-
editor.getTextCursorPosition().block.props.textAlignment
274-
),
275-
editor
276-
);
276+
useEditorContentOrSelectionChange(() => {
277+
const blockProps = editor.getTextCursorPosition().block.props;
278+
279+
if ("textAlignment" in blockProps) {
280+
setActiveTextAlignment(blockProps.textAlignment);
281+
}
282+
}, editor);
277283

278284
// Tooltip for the button.
279285
const tooltip = useMemo(
@@ -293,6 +299,10 @@ function MUITextAlignButton(props: {
293299
editor.focus();
294300
}, [editor, props.textAlignment]);
295301

302+
if (!activeTextAlignment) {
303+
return null;
304+
}
305+
296306
return (
297307
<MUIToolbarButton
298308
tooltip={tooltip}

0 commit comments

Comments
 (0)