Fix block and options menu triggers alignment with hovered block
Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
@@ -51,6 +51,8 @@ const blockMenuVariants = tv({
|
|||||||
|
|
||||||
const { trigger, menu } = blockMenuVariants();
|
const { trigger, menu } = blockMenuVariants();
|
||||||
|
|
||||||
|
const TRIGGER_HEIGHT = 24;
|
||||||
|
|
||||||
function findClosestRootBlock(element: Element, editorDom: Element): HTMLElement | null {
|
function findClosestRootBlock(element: Element, editorDom: Element): HTMLElement | null {
|
||||||
let current: Element | null = element;
|
let current: Element | null = element;
|
||||||
|
|
||||||
@@ -198,13 +200,16 @@ export function BlockMenu({ editor }: BlockMenuProps) {
|
|||||||
};
|
};
|
||||||
}, [editor, slashState.active, slashState.query, filteredItems, slashActiveIndex, handleSlashAction]);
|
}, [editor, slashState.active, slashState.query, filteredItems, slashActiveIndex, handleSlashAction]);
|
||||||
|
|
||||||
|
const blockHeight = hoveredBlock?.getBoundingClientRect().height ?? 0;
|
||||||
|
const triggerPlacement = blockHeight > 2 * TRIGGER_HEIGHT ? "left-start" as const : "left" as const;
|
||||||
|
|
||||||
const {
|
const {
|
||||||
refs: triggerRefs,
|
refs: triggerRefs,
|
||||||
floatingStyles: triggerStyles,
|
floatingStyles: triggerStyles,
|
||||||
isPositioned,
|
isPositioned,
|
||||||
} = useFloating({
|
} = useFloating({
|
||||||
strategy: "fixed",
|
strategy: "fixed",
|
||||||
placement: "left-start",
|
placement: triggerPlacement,
|
||||||
middleware: [offset(32)],
|
middleware: [offset(32)],
|
||||||
whileElementsMounted: autoUpdate,
|
whileElementsMounted: autoUpdate,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ const optionsMenuVariants = tv({
|
|||||||
|
|
||||||
const { trigger, menu } = optionsMenuVariants();
|
const { trigger, menu } = optionsMenuVariants();
|
||||||
|
|
||||||
|
const TRIGGER_HEIGHT = 24;
|
||||||
|
|
||||||
function findClosestRootBlock(element: Element, editorDom: Element): HTMLElement | null {
|
function findClosestRootBlock(element: Element, editorDom: Element): HTMLElement | null {
|
||||||
let current: Element | null = element;
|
let current: Element | null = element;
|
||||||
|
|
||||||
@@ -59,13 +61,16 @@ export function OptionsMenu({ editor }: OptionsMenuProps) {
|
|||||||
const [hoveredBlock, setHoveredBlock] = useState<HTMLElement | null>(null);
|
const [hoveredBlock, setHoveredBlock] = useState<HTMLElement | null>(null);
|
||||||
const rafId = useRef<number | null>(null);
|
const rafId = useRef<number | null>(null);
|
||||||
|
|
||||||
|
const blockHeight = hoveredBlock?.getBoundingClientRect().height ?? 0;
|
||||||
|
const triggerPlacement = blockHeight > 2 * TRIGGER_HEIGHT ? "left-start" as const : "left" as const;
|
||||||
|
|
||||||
const {
|
const {
|
||||||
refs: triggerRefs,
|
refs: triggerRefs,
|
||||||
floatingStyles: triggerStyles,
|
floatingStyles: triggerStyles,
|
||||||
isPositioned,
|
isPositioned,
|
||||||
} = useFloating({
|
} = useFloating({
|
||||||
strategy: "fixed",
|
strategy: "fixed",
|
||||||
placement: "left-start",
|
placement: triggerPlacement,
|
||||||
middleware: [offset(8)],
|
middleware: [offset(8)],
|
||||||
whileElementsMounted: autoUpdate,
|
whileElementsMounted: autoUpdate,
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user