@@ -5,12 +5,13 @@ import {
5
5
StyleSchema ,
6
6
} from "@blocknote/core" ;
7
7
import { flip , offset } from "@floating-ui/react" ;
8
- import { FC , useState } from "react" ;
8
+ import { FC , useMemo , useRef , useState } from "react" ;
9
9
10
10
import { useBlockNoteEditor } from "../../hooks/useBlockNoteEditor" ;
11
11
import { useEditorContentOrSelectionChange } from "../../hooks/useEditorContentOrSelectionChange" ;
12
12
import { useUIElementPositioning } from "../../hooks/useUIElementPositioning" ;
13
13
import { useUIPluginState } from "../../hooks/useUIPluginState" ;
14
+ import { mergeRefs } from "../../util/mergeRefs" ;
14
15
import { FormattingToolbarProps } from "./FormattingToolbarProps" ;
15
16
import { FormattingToolbar } from "./mantine/FormattingToolbar" ;
16
17
@@ -32,6 +33,8 @@ const textAlignmentToPlacement = (
32
33
export const FormattingToolbarController = ( props : {
33
34
formattingToolbar ?: FC < FormattingToolbarProps > ;
34
35
} ) => {
36
+ const divRef = useRef < HTMLDivElement > ( null ) ;
37
+
35
38
const editor = useBlockNoteEditor <
36
39
BlockSchema ,
37
40
InlineContentSchema ,
@@ -79,14 +82,28 @@ export const FormattingToolbarController = (props: {
79
82
}
80
83
) ;
81
84
85
+ const combinedRef = useMemo ( ( ) => mergeRefs ( [ divRef , ref ] ) , [ divRef , ref ] ) ;
86
+
82
87
if ( ! isMounted || ! state ) {
83
88
return null ;
84
89
}
85
90
91
+ if ( ! state . show && divRef . current ) {
92
+ // The component is fading out. Use the previous state to render the toolbar with innerHTML,
93
+ // because otherwise the toolbar will quickly flickr (i.e.: show a different state) while fading out,
94
+ // which looks weird
95
+ return (
96
+ < div
97
+ ref = { combinedRef }
98
+ style = { style }
99
+ dangerouslySetInnerHTML = { { __html : divRef . current . innerHTML } } > </ div >
100
+ ) ;
101
+ }
102
+
86
103
const Component = props . formattingToolbar || FormattingToolbar ;
87
104
88
105
return (
89
- < div ref = { ref } style = { style } >
106
+ < div ref = { combinedRef } style = { style } >
90
107
< Component />
91
108
</ div >
92
109
) ;
0 commit comments