Skip to content

Commit

Permalink
Position menu immediately (#7181)
Browse files Browse the repository at this point in the history
Co-authored-by: Sherry Wong <potatowagon@fb.com>
  • Loading branch information
zurfyx and potatowagon authored Feb 17, 2025
1 parent f3983c5 commit 9deab94
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ test.describe('ContextMenuCopyAndPaste', () => {
await doubleClick(page, 'div[contenteditable="false"] span');
await withExclusiveClipboardAccess(async () => {
await click(page, 'div[contenteditable="false"] span', {button: 'right'});
await click(page, '#typeahead-menu [role="option"] :text("Copy")');
await click(
page,
'div[class="typeahead-popover"] [role="option"] :text("Copy")',
);

await click(page, '.unlock');
await focusEditor(page);
Expand Down Expand Up @@ -78,7 +81,10 @@ test.describe('ContextMenuCopyAndPaste', () => {

await doubleClick(page, 'div[contenteditable="false"] span');
await click(page, 'div[contenteditable="false"] span', {button: 'right'});
await click(page, '#typeahead-menu [role="option"] :text("Copy")');
await click(
page,
'div[class="typeahead-popover"] [role="option"] :text("Copy")',
);

await click(page, '.unlock');
await focusEditor(page);
Expand Down
12 changes: 9 additions & 3 deletions packages/lexical-playground/__tests__/e2e/Tables.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,10 @@ test.describe.parallel('Tables', () => {
focusPath: [1, ...WRAPPER, 1, 0, 0, 0, 0],
});

await waitForSelector(page, `#typeahead-menu ul li:first-child.selected`);
await waitForSelector(
page,
`div[class="typeahead-popover mentions-menu"] ul li:first-child.selected`,
);

await moveDown(page, 1);
await assertSelection(page, {
Expand All @@ -725,7 +728,7 @@ test.describe.parallel('Tables', () => {

await waitForSelector(
page,
'#typeahead-menu ul li:nth-child(2).selected',
'div[class="typeahead-popover mentions-menu"] ul li:nth-child(2).selected',
);
});
});
Expand Down Expand Up @@ -5538,7 +5541,10 @@ test.describe.parallel('Tables', () => {
await click(page, 'div[contenteditable] th p', {
button: 'right',
});
await click(page, '#typeahead-menu [role="option"] :text("Cut")');
await click(
page,
'div[class="typeahead-popover"] [role="option"] :text("Cut")',
);
});

await assertHTML(
Expand Down
16 changes: 9 additions & 7 deletions packages/lexical-react/src/shared/LexicalMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,6 @@ export function useMenuAnchorRef(
const anchorElementRef = useRef<HTMLElement | null>(
CAN_USE_DOM ? document.createElement('div') : null,
);
const hasPositioned = useRef(false);
const positionMenu = useCallback(() => {
if (anchorElementRef.current === null || parent === undefined) {
return;
Expand Down Expand Up @@ -562,12 +561,6 @@ export function useMenuAnchorRef(
parent,
]);

// position menu on first render
if (!hasPositioned.current) {
positionMenu();
hasPositioned.current = true;
}

useEffect(() => {
const rootElement = editor.getRootElement();
if (resolution !== null) {
Expand Down Expand Up @@ -603,6 +596,15 @@ export function useMenuAnchorRef(
onVisibilityChange,
);

// Append the context for the menu immediately
const containerDiv = anchorElementRef.current;
if (containerDiv != null) {
containerDiv.style.position = 'absolute';
if (parent != null) {
parent.append(containerDiv);
}
}

return anchorElementRef;
}

Expand Down

0 comments on commit 9deab94

Please sign in to comment.