File tree 1 file changed +20
-10
lines changed
examples/03-ui-components/13-custom-ui
1 file changed +20
-10
lines changed Original file line number Diff line number Diff line change @@ -262,18 +262,24 @@ function MUITextAlignButton(props: {
262
262
const editor = useBlockNoteEditor < TextBlockSchema > ( ) ;
263
263
264
264
// 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
+ } ) ;
268
274
269
275
// 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 ) ;
277
283
278
284
// Tooltip for the button.
279
285
const tooltip = useMemo (
@@ -293,6 +299,10 @@ function MUITextAlignButton(props: {
293
299
editor . focus ( ) ;
294
300
} , [ editor , props . textAlignment ] ) ;
295
301
302
+ if ( ! activeTextAlignment ) {
303
+ return null ;
304
+ }
305
+
296
306
return (
297
307
< MUIToolbarButton
298
308
tooltip = { tooltip }
You can’t perform that action at this time.
0 commit comments