File size: 3,315 Bytes
1dbc34b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState, useEffect, useRef, useCallback } from 'react';

interface ScrollTrackingItem {
  id: string;
}

interface UseScrollTrackingOptions<T extends ScrollTrackingItem> {
  /** Navigation items with at least an id property */
  items: T[];
  /** Optional filter function to determine which items should be tracked */
  filterFn?: (item: T) => boolean;
  /** Optional initial active section (defaults to first item's id) */
  initialSection?: string;
  /** Optional offset from top when scrolling to section (defaults to 24) */
  scrollOffset?: number;
}

/**
 * Generic custom hook for managing scroll-based navigation tracking
 * Automatically highlights the active section based on scroll position
 * and provides smooth scrolling to sections
 */
export function useScrollTracking<T extends ScrollTrackingItem>({
  items,
  filterFn = () => true,
  initialSection,
  scrollOffset = 24,
}: UseScrollTrackingOptions<T>) {
  const [activeSection, setActiveSection] = useState(initialSection || items[0]?.id || '');
  const scrollContainerRef = useRef<HTMLDivElement>(null);

  // Track scroll position to highlight active nav item
  useEffect(() => {
    const container = scrollContainerRef.current;
    if (!container) return;

    const handleScroll = () => {
      const sections = items
        .filter(filterFn)
        .map((item) => ({
          id: item.id,
          element: document.getElementById(item.id),
        }))
        .filter((s) => s.element);

      const containerRect = container.getBoundingClientRect();
      const scrollTop = container.scrollTop;
      const scrollHeight = container.scrollHeight;
      const clientHeight = container.clientHeight;

      // Check if scrolled to bottom (within a small threshold)
      const isAtBottom = scrollTop + clientHeight >= scrollHeight - 50;

      if (isAtBottom && sections.length > 0) {
        // If at bottom, highlight the last visible section
        setActiveSection(sections[sections.length - 1].id);
        return;
      }

      for (let i = sections.length - 1; i >= 0; i--) {
        const section = sections[i];
        if (section.element) {
          const rect = section.element.getBoundingClientRect();
          const relativeTop = rect.top - containerRect.top + scrollTop;
          if (scrollTop >= relativeTop - 100) {
            setActiveSection(section.id);
            break;
          }
        }
      }
    };

    container.addEventListener('scroll', handleScroll);
    return () => container.removeEventListener('scroll', handleScroll);
  }, [items, filterFn]);

  // Scroll to a specific section with smooth animation
  const scrollToSection = useCallback(
    (sectionId: string) => {
      const element = document.getElementById(sectionId);
      if (element && scrollContainerRef.current) {
        const container = scrollContainerRef.current;
        const containerRect = container.getBoundingClientRect();
        const elementRect = element.getBoundingClientRect();
        const relativeTop = elementRect.top - containerRect.top + container.scrollTop;

        container.scrollTo({
          top: relativeTop - scrollOffset,
          behavior: 'smooth',
        });
      }
    },
    [scrollOffset]
  );

  return {
    activeSection,
    scrollToSection,
    scrollContainerRef,
  };
}