Skip to content

Commit b4b0ca2

Browse files
committed
constantly check alignment during opening transitions to preventing layout jump after
1 parent 71b749f commit b4b0ca2

1 file changed

Lines changed: 41 additions & 8 deletions

File tree

src/main/components/editors/linked/UIEditorLinked.tsx

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,16 +1012,38 @@ const UIEditorLinked: FC<IEditorLinked & IExtendableLinkedEditor & IComponentCon
10121012
}
10131013
}, [linkedRef]);
10141014

1015-
const alignOverlay = useCallback(() => {
1015+
/**
1016+
* manually align autocomplete overlay if the width of the overlay changed
1017+
*/
1018+
const lastOverlayWidth = useRef(0);
1019+
const alignOverlay = useCallback((force:boolean = false) => {
10161020
if(linkedRef.current) {
1017-
DomHandler.alignOverlay(
1018-
linkedRef.current.getOverlay(),
1019-
linkedRef.current.getInput() as any,
1020-
document.body as any
1021-
);
1021+
const w = linkedRef.current.getOverlay().clientWidth;
1022+
if(force || w !== lastOverlayWidth.current) {
1023+
DomHandler.alignOverlay(
1024+
linkedRef.current.getOverlay(),
1025+
linkedRef.current.getInput() as any,
1026+
document.body as any
1027+
);
1028+
}
1029+
lastOverlayWidth.current = w;
10221030
}
10231031
}, [])
10241032

1033+
const keepCheckingAndAligningOverlay = useRef(false);
1034+
const checkAndAlignOverlay = useCallback((initial = false) => {
1035+
if (initial) {
1036+
lastOverlayWidth.current = 0;
1037+
keepCheckingAndAligningOverlay.current = true;
1038+
}
1039+
requestAnimationFrame(() => {
1040+
alignOverlay();
1041+
if (keepCheckingAndAligningOverlay.current) {
1042+
checkAndAlignOverlay();
1043+
}
1044+
});
1045+
}, [alignOverlay]);
1046+
10251047
return (
10261048
<span
10271049
ref={props.forwardedRef}
@@ -1043,6 +1065,14 @@ const UIEditorLinked: FC<IEditorLinked & IExtendableLinkedEditor & IComponentCon
10431065
autoHighlight={true}
10441066
autoFocus={props.autoFocus ? true : props.isCellEditor ? true : false}
10451067
appendTo={document.body}
1068+
transitionOptions={{
1069+
onEntering: () => {
1070+
checkAndAlignOverlay(true);
1071+
},
1072+
onEntered: () => {
1073+
keepCheckingAndAligningOverlay.current = false;
1074+
}
1075+
} as any}
10461076
className={concatClassnames(
10471077
"rc-editor-linked",
10481078
props.columnMetaData?.nullable === false ? "required-field" : "",
@@ -1138,22 +1168,25 @@ const UIEditorLinked: FC<IEditorLinked & IExtendableLinkedEditor & IComponentCon
11381168
const scrollpos = index < last ? 0 : index - 2;
11391169
linkedRef.current?.getVirtualScroller().scrollToIndex(scrollpos, "auto");
11401170

1171+
checkAndAlignOverlay(true);
11411172
setTimeout(() => {
11421173
const el = linkedRef.current?.getOverlay().querySelectorAll('.p-autocomplete-item')[index - linkedRef.current?.getVirtualScroller().getRenderedRange().first];
11431174
el?.classList.add('p-highlight');
11441175
el?.setAttribute('data-p-highlight', 'true');
1176+
keepCheckingAndAligningOverlay.current = false;
1177+
alignOverlay(true);
11451178
}, 50);
11461179
}
11471180
}
11481181

1149-
alignOverlay();
1182+
alignOverlay(true);
11501183
}}
11511184
virtualScrollerOptions={{
11521185
itemSize: 38,
11531186
lazy: true,
11541187
scrollHeight: suggestions?.length ? `${Math.min(6.66, (suggestions[0].items?.length ?? (suggestions.length - 1)) + 1) * 38}px` : undefined,
11551188
onLazyLoad: handleLazyLoad,
1156-
onScroll: alignOverlay,
1189+
onScroll: () => alignOverlay(),
11571190
className: props.isCellEditor
11581191
? "celleditor-dropdown-virtual-scroller"
11591192
: "dropdown-virtual-scroller"

0 commit comments

Comments
 (0)