Skip to content

Commit f105792

Browse files
authored
alternative solution for toolbar fadeout (#645)
1 parent 9c37f3e commit f105792

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

packages/react/src/components/FormattingToolbar/FormattingToolbarController.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import {
55
StyleSchema,
66
} from "@blocknote/core";
77
import { flip, offset } from "@floating-ui/react";
8-
import { FC, useState } from "react";
8+
import { FC, useMemo, useRef, useState } from "react";
99

1010
import { useBlockNoteEditor } from "../../hooks/useBlockNoteEditor";
1111
import { useEditorContentOrSelectionChange } from "../../hooks/useEditorContentOrSelectionChange";
1212
import { useUIElementPositioning } from "../../hooks/useUIElementPositioning";
1313
import { useUIPluginState } from "../../hooks/useUIPluginState";
14+
import { mergeRefs } from "../../util/mergeRefs";
1415
import { FormattingToolbarProps } from "./FormattingToolbarProps";
1516
import { FormattingToolbar } from "./mantine/FormattingToolbar";
1617

@@ -32,6 +33,8 @@ const textAlignmentToPlacement = (
3233
export 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

Comments
 (0)