Skip to content

Commit

Permalink
Minor optimization on compact mode on scroll.
Browse files Browse the repository at this point in the history
  • Loading branch information
jkguidaven committed Dec 11, 2024
1 parent ea3664a commit 2399b22
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useCallback, useRef, useState } from 'react';

const SCROLL_THRESHOLD_PERCENTAGE = 0.2;
const SCROLL_THRESHOLD_BUFFER = 5;
const SCROLL_THRESHOLD_NORMAL_BUFFER = 5;
const SCROLL_THRESHOLD_COMPACT_BUFFER = 20;

export default function useCompactOnScroll() {
const [ isCompact, setIsCompact ] = useState( false );
Expand Down Expand Up @@ -31,15 +32,15 @@ export default function useCompactOnScroll() {
if (
isScrollingDown &&
! isCompact &&
scrollPosition > normalScrollThreshold + SCROLL_THRESHOLD_BUFFER
scrollPosition > normalScrollThreshold + SCROLL_THRESHOLD_NORMAL_BUFFER
) {
setIsCompact( true );
setIsTransitioning( true );
ref.current?.addEventListener( 'transitionend', onTransitionEnd );
} else if (
! isScrollingDown &&
isCompact &&
scrollPosition < normalScrollThreshold - SCROLL_THRESHOLD_BUFFER
scrollPosition < normalScrollThreshold - SCROLL_THRESHOLD_COMPACT_BUFFER
) {
setIsCompact( false );
setIsTransitioning( true );
Expand Down

0 comments on commit 2399b22

Please sign in to comment.