# All files / src/hooks useSmartScroll.ts

57.89% Statements 33/57
33.33% Branches 7/21
62.5% Functions 5/8
57.89% Lines 33/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156

    120x
    120x
    120x
    120x

    120x
    78x
    78x

    78x

    78x

    78x
    78x
    66x
    66x

    120x
    78x
    78x

    78x
    78x

    78x

    78x

    120x
    107x
    107x

    107x
    28x
    28x

    79x

    79x
    79x

    79x

    79x

    120x

    120x

     import { useCallback, useEffect, useLayoutEffect, useRef, useState, type RefObject } from "react";
    import {
      isNearAnchor,
      computeScrollCompensation,
      SCROLL_ANCHOR_THRESHOLD_PX,
    } from "../utils/scrollUtils.js";

    /** Options for the useSmartScroll hook. */
    interface UseSmartScrollOptions {
      /** Ref to the scrollable container element. */
      // eslint-disable-next-line @rushstack/no-new-null
      scrollRef: RefObject<HTMLDivElement | null>;
      /** Length of the content list — triggers scroll checks on change. */
      contentLength: number;
      /** Whether newest-at-top mode is active (anchor is top instead of bottom). */
      isReversed: boolean;
      /** When true, auto-scroll is suppressed (e.g. during multi-select mode). */
      paused?: boolean;
    }

    /** Return value from the useSmartScroll hook. */
    interface UseSmartScrollReturn {
      /** Whether the user is currently at the anchor position (bottom or top). */
      isAtAnchor: boolean;
      /** Smooth-scrolls to the anchor and re-enables auto-scroll. */
      scrollToAnchor: () => void;
    }

    /**
     * Smart auto-scroll hook that respects user reading position.
     *
     * - Auto-scrolls to anchor (bottom or top) when new content arrives and user is at anchor.
     * - Detects when user scrolls away from anchor and disables auto-scroll.
     * - Provides a callback to manually scroll back to anchor.
     * - In reverse mode, compensates scrollTop to prevent viewport shift from prepended content.
     */
    export function useSmartScroll({
      scrollRef,
      contentLength,
      isReversed,
      paused,
    }: UseSmartScrollOptions): UseSmartScrollReturn {
      const [isAtAnchor, setIsAtAnchor] = useState(true);
      const prevScrollHeightRef = useRef<number>(0);
      const mountedRef = useRef(false);
      const rafIdRef = useRef<number>(0);

      // Throttled scroll listener — uses rAF to avoid excessive React work during fast scrolling.
      // Only calls setState when the boolean actually changes.
      useEffect(() => {
        const element = scrollRef.current;
        Iif (!element) {
          return;
        }

        let lastKnownValue: boolean = true;

        const handleScroll = (): void => {
          Iif (rafIdRef.current) {
            return;
          }
          rafIdRef.current = requestAnimationFrame(() => {
            rafIdRef.current = 0;
            const near = isNearAnchor(
              element.scrollTop,
              element.scrollHeight,
              element.clientHeight,
              isReversed,
              SCROLL_ANCHOR_THRESHOLD_PX,
            );
            Iif (near !== lastKnownValue) {
              lastKnownValue = near;
              setIsAtAnchor(near);
            }
          });
        };

        element.addEventListener("scroll", handleScroll, { passive: true });
        return () => {
          element.removeEventListener("scroll", handleScroll);
          Iif (rafIdRef.current) {
            cancelAnimationFrame(rafIdRef.current);
            rafIdRef.current = 0;
          }
        };
      }, [scrollRef, isReversed]);

      // Initial scroll — useLayoutEffect to avoid flash before paint
      useLayoutEffect(() => {
        const element = scrollRef.current;
        Iif (!element || mountedRef.current) {
          return;
        }
        mountedRef.current = true;
        Iif (isReversed) {
          element.scrollTop = 0;
        } else {
          element.scrollTop = element.scrollHeight;
        }
        setIsAtAnchor(true);
      }, [scrollRef, isReversed]);

      // Auto-scroll on new content + reverse-mode compensation.
      // prevScrollHeightRef is captured at the END of this effect so the NEXT
      // invocation sees the prior commit's scrollHeight (not the current one).
      useLayoutEffect(() => {
        const element = scrollRef.current;
        Iif (!element) {
          return;
        }

        // Skip auto-scroll while paused (e.g. during multi-select mode) to avoid
        // disrupting the user's selection. Still update prevScrollHeight so scroll
        // compensation is correct when unpaused.
        if (paused) {
          prevScrollHeightRef.current = element.scrollHeight;
          return;
        }

        Iif (isReversed && !isAtAnchor) {
          // Compensate scrollTop to prevent viewport shift from prepended content
          const compensation = computeScrollCompensation(
            prevScrollHeightRef.current,
            element.scrollHeight,
          );
          Iif (compensation > 0) {
            element.scrollTop += compensation;
          }
        }

        if (isAtAnchor) {
          Iif (isReversed) {
            element.scrollTo({ top: 0, behavior: "smooth" });
          } else {
            element.scrollTo({ top: element.scrollHeight, behavior: "smooth" });
          }
        }

        // Update prevScrollHeight AFTER applying compensation/auto-scroll
        // so the next render can compute the delta correctly.
        prevScrollHeightRef.current = element.scrollHeight;
      }, [contentLength, isAtAnchor, isReversed, paused, scrollRef]);

      const scrollToAnchor = useCallback((): void => {
        const element = scrollRef.current;
        Iif (!element) {
          return;
        }
        const target = isReversed ? 0 : element.scrollHeight;
        element.scrollTo({ top: target, behavior: "smooth" });
        setIsAtAnchor(true);
      }, [scrollRef, isReversed]);

      return { isAtAnchor, scrollToAnchor };
    }