Skip to content

Commit

Permalink
fix(mentions): Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingze Xiao committed Jun 24, 2020
1 parent e7d1063 commit 8d1c35b
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions src/components/ItemList/ItemList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,22 @@ const ItemList = <T extends { id: string }>({
...rest
}: Props<T>): JSX.Element => {
const listRef = React.useRef<HTMLUListElement>(null);
const listHeightRef = React.useRef<number | null>(null);

// cache list height, since it won't change
React.useEffect(() => {
const { current: listEl } = listRef;

if (listEl) {
listHeightRef.current = listEl.offsetHeight;
}
}, [listRef]);

const activeRef = React.useCallback(
(activeEl: ItemRowRef) => {
const { current: listEl } = listRef;
const { current: listHeight } = listHeightRef;

if (!activeEl || !listEl || !listHeight) {
if (!activeEl || !listEl) {
return;
}

if (listEl.scrollTop > activeEl.offsetTop) {
listEl.scrollTop -= activeEl.offsetHeight;
} else if (listEl.scrollTop + listHeight < activeEl.offsetTop + activeEl.offsetHeight) {
listEl.scrollTop += activeEl.offsetHeight;
const { offsetHeight: activeHeight, offsetTop: activeTop } = activeEl;
const { offsetHeight: listHeight, scrollTop: listTop } = listEl;

if (listTop > activeTop) {
listEl.scrollTop -= activeHeight;
} else if (listTop + listHeight < activeTop + activeHeight) {
listEl.scrollTop += activeHeight;
}
},
[listRef],
Expand Down

0 comments on commit 8d1c35b

Please sign in to comment.