Skip to content

Commit

Permalink
feat(CustomScrollView): improve customScrollView code (#7281)
Browse files Browse the repository at this point in the history
h2. Описание

Заметил, что хук useDetectScrllDirection можно оптимизировать. А именно убрать логику сброса scrollDirection по таймеру.

h2. Изменения

- Переписал логику хука useDetectScrollDirection без использования setTimeout. Проверил, что все работает также.
- В других файлах поменял логику if-ов, чтобы в будущем не покрывать кейс с `return`
  • Loading branch information
EldarMuhamethanov authored Aug 5, 2024
1 parent 06195d3 commit cd5495c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import * as React from 'react';
import { TimeoutId } from '../../types';

type ScrollDirection = 'vertical' | 'horizontal';

/**
* Хук определяет в каком измерении происходит скролл(в горизонтальном или вертикальном)
Expand All @@ -10,24 +7,15 @@ export const useDetectScrollDirection = () => {
const lastScrollLeft = React.useRef(0);
const lastScrollTop = React.useRef(0);

const timeoutId = React.useRef<TimeoutId>(null);
const scrollDirectionRef = React.useRef<ScrollDirection | null>(null);

return React.useCallback((event: React.UIEvent<HTMLElement>) => {
if (timeoutId.current) {
clearTimeout(timeoutId.current);
}
const { scrollTop, scrollLeft } = event.currentTarget;
if (scrollTop !== lastScrollTop.current) {
scrollDirectionRef.current = 'vertical';
lastScrollTop.current = scrollTop;
return 'vertical';
} else if (scrollLeft !== lastScrollLeft.current) {
scrollDirectionRef.current = 'horizontal';
lastScrollLeft.current = scrollLeft;
return 'horizontal';
}
if (scrollDirectionRef.current !== null) {
timeoutId.current = setTimeout(() => (scrollDirectionRef.current = null), 200);
}
return scrollDirectionRef.current;
return null;
}, []);
};
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ export const useVerticalScrollController = (
if (autoHideScrollbar) {
onTargetScroll();
}

setTrackerPositionFromScroll(boxRef.current.scrollTop);
};

Expand Down

0 comments on commit cd5495c

Please sign in to comment.