Skip to content

Commit ca26d5c

Browse files
fix: Suggestion menu positioning (#2232)
Co-authored-by: Nick the Sick <[email protected]>
1 parent 5d13b0a commit ca26d5c

File tree

2 files changed

+24
-43
lines changed

2 files changed

+24
-43
lines changed

packages/react/src/components/SuggestionMenu/GridSuggestionMenu/GridSuggestionMenuController.tsx

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BlockSchema, InlineContentSchema, StyleSchema } from "@blocknote/core";
22
import { SuggestionMenu } from "@blocknote/core/extensions";
3-
import { flip, offset, shift, size } from "@floating-ui/react";
3+
import { autoPlacement, offset, shift, size } from "@floating-ui/react";
44
import { FC, useEffect, useMemo } from "react";
55

66
import { useBlockNoteEditor } from "../../../hooks/useBlockNoteEditor.js";
@@ -9,10 +9,7 @@ import {
99
useExtensionState,
1010
} from "../../../hooks/useExtension.js";
1111
import { FloatingUIOptions } from "../../Popovers/FloatingUIOptions.js";
12-
import {
13-
GenericPopover,
14-
GenericPopoverReference,
15-
} from "../../Popovers/GenericPopover.js";
12+
import { GenericPopover } from "../../Popovers/GenericPopover.js";
1613
import { getDefaultReactEmojiPickerItems } from "./getDefaultReactEmojiPickerItems.js";
1714
import { GridSuggestionMenu } from "./GridSuggestionMenu.js";
1815
import { GridSuggestionMenuWrapper } from "./GridSuggestionMenuWrapper.js";
@@ -97,20 +94,15 @@ export function GridSuggestionMenuController<
9794
}, [suggestionMenu, triggerCharacter]);
9895

9996
const state = useExtensionState(SuggestionMenu);
100-
const referencePos = useExtensionState(SuggestionMenu, {
101-
selector: (state) => state?.referencePos || new DOMRect(),
102-
});
103-
104-
const reference = useMemo<GenericPopoverReference>(
105-
() => ({
97+
const reference = useExtensionState(SuggestionMenu, {
98+
selector: (state) => ({
10699
// Use first child as the editor DOM element may itself be scrollable.
107100
// For FloatingUI to auto-update the position during scrolling, the
108101
// `contextElement` must be a descendant of the scroll container.
109102
element: editor.domElement?.firstChild || undefined,
110-
getBoundingClientRect: () => referencePos,
103+
getBoundingClientRect: () => state?.referencePos || new DOMRect(),
111104
}),
112-
[editor.domElement?.firstChild, referencePos],
113-
);
105+
});
114106

115107
const floatingUIOptions = useMemo<FloatingUIOptions>(
116108
() => ({
@@ -126,17 +118,16 @@ export function GridSuggestionMenuController<
126118
offset(10),
127119
// Flips the menu placement to maximize the space available, and prevents
128120
// the menu from being cut off by the confines of the screen.
129-
flip({
130-
mainAxis: true,
131-
crossAxis: false,
121+
autoPlacement({
122+
allowedPlacements: ["bottom-start", "top-start"],
123+
padding: 10,
132124
}),
133125
shift(),
134126
size({
135-
apply({ availableHeight, elements }) {
136-
Object.assign(elements.floating.style, {
137-
maxHeight: `${availableHeight - 10}px`,
138-
});
127+
apply({ elements, availableHeight }) {
128+
elements.floating.style.maxHeight = `${Math.max(0, availableHeight)}px`;
139129
},
130+
padding: 10,
140131
}),
141132
],
142133
},

packages/react/src/components/SuggestionMenu/SuggestionMenuController.tsx

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from "@blocknote/core/extensions";
66
import {
77
UseFloatingOptions,
8-
flip,
8+
autoPlacement,
99
offset,
1010
shift,
1111
size,
@@ -15,10 +15,7 @@ import { FC, useEffect, useMemo } from "react";
1515
import { useBlockNoteEditor } from "../../hooks/useBlockNoteEditor.js";
1616
import { useExtension, useExtensionState } from "../../hooks/useExtension.js";
1717
import { FloatingUIOptions } from "../Popovers/FloatingUIOptions.js";
18-
import {
19-
GenericPopover,
20-
GenericPopoverReference,
21-
} from "../Popovers/GenericPopover.js";
18+
import { GenericPopover } from "../Popovers/GenericPopover.js";
2219
import { SuggestionMenu } from "./SuggestionMenu.js";
2320
import { SuggestionMenuWrapper } from "./SuggestionMenuWrapper.js";
2421
import { getDefaultReactSlashMenuItems } from "./getDefaultReactSlashMenuItems.js";
@@ -98,20 +95,15 @@ export function SuggestionMenuController<
9895
}, [suggestionMenu, triggerCharacter]);
9996

10097
const state = useExtensionState(SuggestionMenuExtension);
101-
const referencePos = useExtensionState(SuggestionMenuExtension, {
102-
selector: (state) => state?.referencePos || new DOMRect(),
103-
});
104-
105-
const reference = useMemo<GenericPopoverReference>(
106-
() => ({
98+
const reference = useExtensionState(SuggestionMenuExtension, {
99+
selector: (state) => ({
107100
// Use first child as the editor DOM element may itself be scrollable.
108101
// For FloatingUI to auto-update the position during scrolling, the
109102
// `contextElement` must be a descendant of the scroll container.
110103
element: editor.domElement?.firstChild || undefined,
111-
getBoundingClientRect: () => referencePos,
104+
getBoundingClientRect: () => state?.referencePos || new DOMRect(),
112105
}),
113-
[editor.domElement?.firstChild, referencePos],
114-
);
106+
});
115107

116108
const floatingUIOptions = useMemo<FloatingUIOptions>(
117109
() => ({
@@ -127,18 +119,16 @@ export function SuggestionMenuController<
127119
offset(10),
128120
// Flips the menu placement to maximize the space available, and prevents
129121
// the menu from being cut off by the confines of the screen.
130-
flip({
131-
mainAxis: true,
132-
crossAxis: false,
122+
autoPlacement({
123+
allowedPlacements: ["bottom-start", "top-start"],
124+
padding: 10,
133125
}),
134126
shift(),
135127
size({
136-
apply({ availableHeight, elements }) {
137-
Object.assign(elements.floating.style, {
138-
maxHeight: `${availableHeight - 10}px`,
139-
minHeight: "300px",
140-
});
128+
apply({ elements, availableHeight }) {
129+
elements.floating.style.maxHeight = `${Math.max(0, availableHeight)}px`;
141130
},
131+
padding: 10,
142132
}),
143133
],
144134
},

0 commit comments

Comments
 (0)