|
1 | 1 | import { BlockNoteEditor } from "@blocknote/core"; |
2 | | -import { MouseEvent, ReactNode, useId, useRef } from "react"; |
| 2 | +import { |
| 3 | + autoUpdate, |
| 4 | + flip, |
| 5 | + offset, |
| 6 | + shift, |
| 7 | + useFloating, |
| 8 | +} from "@floating-ui/react"; |
| 9 | +import { MouseEvent, ReactNode, useCallback, useId, useRef } from "react"; |
3 | 10 | import { MdKeyboardReturn } from "react-icons/md"; |
4 | 11 |
|
5 | 12 | import { PreviewPlaceholder } from "./PreviewPlaceholder.js"; |
@@ -129,6 +136,20 @@ export const SourceWithPreview = ( |
129 | 136 | errorPreview |
130 | 137 | ); |
131 | 138 |
|
| 139 | + const { refs, floatingStyles } = useFloating({ |
| 140 | + placement: "bottom-start", |
| 141 | + strategy: "fixed", |
| 142 | + middleware: [offset(4), flip(), shift({ padding: 8 })], |
| 143 | + whileElementsMounted: autoUpdate, |
| 144 | + }); |
| 145 | + |
| 146 | + const isFloating = inline === true && popup.isOpen; |
| 147 | + |
| 148 | + const setAnchor = useCallback( |
| 149 | + (node: HTMLElement | null) => refs.setPositionReference(node), |
| 150 | + [refs], |
| 151 | + ); |
| 152 | + |
132 | 153 | // Links the source input to the render error so screen readers announce it |
133 | 154 | // when the input is focused. |
134 | 155 | const errorId = useId(); |
@@ -182,13 +203,18 @@ export const SourceWithPreview = ( |
182 | 203 | <PreviewContainer |
183 | 204 | className="bn-preview-container" |
184 | 205 | contentEditable={false} |
| 206 | + ref={setAnchor} |
185 | 207 | // When no source code is available, the "Add source" button should have the appropriate role. |
186 | 208 | role={previewIsButton ? "button" : undefined} |
187 | 209 | onClick={handlePreviewClick} |
188 | 210 | > |
189 | 211 | {previewContent} |
190 | 212 | </PreviewContainer> |
191 | | - <div className="bn-source-block-popup"> |
| 213 | + <div |
| 214 | + className="bn-source-block-popup" |
| 215 | + ref={isFloating ? refs.setFloating : undefined} |
| 216 | + style={isFloating ? floatingStyles : undefined} |
| 217 | + > |
192 | 218 | <div className="bn-code-block-source-popup-body"> |
193 | 219 | <pre |
194 | 220 | className={ |
|
0 commit comments