@@ -5,12 +5,13 @@ import {
55 StyleSchema ,
66} from "@blocknote/core" ;
77import { flip , offset } from "@floating-ui/react" ;
8- import { FC , useState } from "react" ;
8+ import { FC , useMemo , useRef , useState } from "react" ;
99
1010import { useBlockNoteEditor } from "../../hooks/useBlockNoteEditor" ;
1111import { useEditorContentOrSelectionChange } from "../../hooks/useEditorContentOrSelectionChange" ;
1212import { useUIElementPositioning } from "../../hooks/useUIElementPositioning" ;
1313import { useUIPluginState } from "../../hooks/useUIPluginState" ;
14+ import { mergeRefs } from "../../util/mergeRefs" ;
1415import { FormattingToolbarProps } from "./FormattingToolbarProps" ;
1516import { FormattingToolbar } from "./mantine/FormattingToolbar" ;
1617
@@ -32,6 +33,8 @@ const textAlignmentToPlacement = (
3233export const FormattingToolbarController = ( props : {
3334 formattingToolbar ?: FC < FormattingToolbarProps > ;
3435} ) => {
36+ const divRef = useRef < HTMLDivElement > ( null ) ;
37+
3538 const editor = useBlockNoteEditor <
3639 BlockSchema ,
3740 InlineContentSchema ,
@@ -79,14 +82,28 @@ export const FormattingToolbarController = (props: {
7982 }
8083 ) ;
8184
85+ const combinedRef = useMemo ( ( ) => mergeRefs ( [ divRef , ref ] ) , [ divRef , ref ] ) ;
86+
8287 if ( ! isMounted || ! state ) {
8388 return null ;
8489 }
8590
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+
86103 const Component = props . formattingToolbar || FormattingToolbar ;
87104
88105 return (
89- < div ref = { ref } style = { style } >
106+ < div ref = { combinedRef } style = { style } >
90107 < Component />
91108 </ div >
92109 ) ;
0 commit comments