Skip to content

Commit

Permalink
feat: wait 1ms before menu collision detection (#859)
Browse files Browse the repository at this point in the history
  • Loading branch information
amanharwara authored Feb 10, 2022
1 parent 3b830b0 commit 9c046a3
Showing 1 changed file with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
TransactionalMutation,
} from '@standardnotes/snjs';
import { FunctionComponent } from 'preact';
import { useEffect, useLayoutEffect, useRef, useState } from 'preact/hooks';
import { useEffect, useRef, useState } from 'preact/hooks';
import { Icon } from '../Icon';
import { createEditorMenuGroups } from './changeEditor/createEditorMenuGroups';
import { EditorAccordionMenu } from './changeEditor/EditorAccordionMenu';
Expand Down Expand Up @@ -111,7 +111,7 @@ const calculateMenuPosition = (
}
}

if (menuBoundingRect && buttonRect) {
if (menuBoundingRect && menuBoundingRect.height && buttonRect) {
if (menuBoundingRect.y < MENU_MARGIN_FROM_APP_BORDER) {
if (
buttonRect.right + maxChangeEditorMenuSize >
Expand All @@ -130,11 +130,13 @@ const calculateMenuPosition = (
};
}
}
} else {
return position;
}

return position;
};

const TIME_IN_MS_TO_WAIT_BEFORE_REPAINT = 1;

export const ChangeEditorOption: FunctionComponent<ChangeEditorOptionProps> = ({
application,
appState,
Expand Down Expand Up @@ -182,16 +184,18 @@ export const ChangeEditorOption: FunctionComponent<ChangeEditorOptionProps> = ({
setChangeEditorMenuOpen(!changeEditorMenuOpen);
};

useLayoutEffect(() => {
useEffect(() => {
if (changeEditorMenuOpen) {
const newMenuPosition = calculateMenuPosition(
changeEditorButtonRef.current,
changeEditorMenuRef.current
);
setTimeout(() => {
const newMenuPosition = calculateMenuPosition(
changeEditorButtonRef.current,
changeEditorMenuRef.current
);

if (newMenuPosition) {
setChangeEditorMenuPosition(newMenuPosition);
}
if (newMenuPosition) {
setChangeEditorMenuPosition(newMenuPosition);
}
}, TIME_IN_MS_TO_WAIT_BEFORE_REPAINT);
}
}, [changeEditorMenuOpen]);

Expand Down

0 comments on commit 9c046a3

Please sign in to comment.