mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-09 20:29:36 +02:00
improve performance on page ScrollArea by throttling scroll callback
This commit is contained in:
@@ -6,6 +6,7 @@ import styles from './native-scroll-area.module.css';
|
|||||||
import { PageHeader, PageHeaderProps } from '/@/renderer/components/page-header/page-header';
|
import { PageHeader, PageHeaderProps } from '/@/renderer/components/page-header/page-header';
|
||||||
import { useWindowSettings } from '/@/renderer/store/settings.store';
|
import { useWindowSettings } from '/@/renderer/store/settings.store';
|
||||||
import { useMergedRef } from '/@/shared/hooks/use-merged-ref';
|
import { useMergedRef } from '/@/shared/hooks/use-merged-ref';
|
||||||
|
import { useThrottledCallback } from '/@/shared/hooks/use-throttled-callback';
|
||||||
import { Platform } from '/@/shared/types/types';
|
import { Platform } from '/@/shared/types/types';
|
||||||
|
|
||||||
interface NativeScrollAreaProps {
|
interface NativeScrollAreaProps {
|
||||||
@@ -26,35 +27,31 @@ const BaseNativeScrollArea = forwardRef(
|
|||||||
const { windowBarStyle } = useWindowSettings();
|
const { windowBarStyle } = useWindowSettings();
|
||||||
const containerRef = useRef<HTMLDivElement | null>(null);
|
const containerRef = useRef<HTMLDivElement | null>(null);
|
||||||
|
|
||||||
const scrollHandlerRef = useRef<null | number>(null);
|
const scrollHandler = useThrottledCallback((e: Event) => {
|
||||||
|
if (noHeader || !pageHeaderProps) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const scrollElement = e?.target as HTMLDivElement;
|
||||||
|
if (!scrollElement || !containerRef.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const offset = pageHeaderProps.offset || 0;
|
||||||
|
const scrollTop = scrollElement.scrollTop;
|
||||||
|
|
||||||
|
if (scrollTop > offset) {
|
||||||
|
containerRef.current.setAttribute('data-scrolled', 'true');
|
||||||
|
} else {
|
||||||
|
containerRef.current.setAttribute('data-scrolled', 'false');
|
||||||
|
}
|
||||||
|
}, 100);
|
||||||
|
|
||||||
const [initialize] = useOverlayScrollbars({
|
const [initialize] = useOverlayScrollbars({
|
||||||
defer: false,
|
defer: false,
|
||||||
events: {
|
events: {
|
||||||
scroll: (_instance, e) => {
|
scroll: (_instance, e) => {
|
||||||
if (scrollHandlerRef.current) {
|
scrollHandler(e);
|
||||||
cancelAnimationFrame(scrollHandlerRef.current);
|
|
||||||
}
|
|
||||||
|
|
||||||
scrollHandlerRef.current = requestAnimationFrame(() => {
|
|
||||||
if (noHeader || !pageHeaderProps) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const scrollElement = e?.target as HTMLDivElement;
|
|
||||||
if (!scrollElement || !containerRef.current) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const offset = pageHeaderProps.offset || 0;
|
|
||||||
const scrollTop = scrollElement.scrollTop;
|
|
||||||
|
|
||||||
if (scrollTop > offset) {
|
|
||||||
containerRef.current.setAttribute('data-scrolled', 'true');
|
|
||||||
} else {
|
|
||||||
containerRef.current.setAttribute('data-scrolled', 'false');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
options: {
|
options: {
|
||||||
|
|||||||
Reference in New Issue
Block a user