All files / web/src/components/practice ScrollspyNav.tsx

0% Statements 0/186
0% Branches 0/1
0% Functions 0/1
0% Lines 0/186

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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187                                                                                                                                                                                                                                                                                                                                                                                     
'use client'

import { useCallback, useEffect, useRef, useState } from 'react'
import { css } from '../../../styled-system/css'

export interface ScrollspySection {
  id: string
  label: string
  icon?: string
}

export interface ScrollspyNavProps {
  /** Sections to track and navigate to */
  sections: ScrollspySection[]
  /** Offset from top of viewport for intersection detection */
  topOffset?: number
  /** Dark mode */
  isDark: boolean
}

/**
 * ScrollspyNav - Mobile navigation that tracks scroll position
 *
 * Features:
 * - Fixed at bottom of viewport (48px height)
 * - Highlights active section based on scroll position
 * - Click to smooth-scroll to section
 * - Only shows on mobile/tablet (hidden at 1200px+)
 */
export function ScrollspyNav({ sections, topOffset = 200, isDark }: ScrollspyNavProps) {
  const [activeSection, setActiveSection] = useState<string | null>(sections[0]?.id ?? null)
  const [isVisible, setIsVisible] = useState(false)
  const navRef = useRef<HTMLDivElement>(null)

  // Check if we should show the fixed nav (based on viewport width)
  useEffect(() => {
    const checkVisibility = () => {
      setIsVisible(window.innerWidth < 1200)
    }

    checkVisibility()
    window.addEventListener('resize', checkVisibility)
    return () => window.removeEventListener('resize', checkVisibility)
  }, [])

  // Update active section based on scroll position
  useEffect(() => {
    const handleScroll = () => {
      // Find the section that's currently in view
      // Start from the bottom and work up to find the topmost visible section
      let currentSection: string | null = null

      for (const section of sections) {
        const element = document.querySelector(`[data-scrollspy-section="${section.id}"]`)
        if (element) {
          const rect = element.getBoundingClientRect()
          // Section is considered active if its top is above the threshold
          if (rect.top < topOffset) {
            currentSection = section.id
          }
        }
      }

      // If no section is active (at the very top), use the first section
      if (!currentSection && sections.length > 0) {
        currentSection = sections[0].id
      }

      if (currentSection !== activeSection) {
        setActiveSection(currentSection)
      }
    }

    // Initial check
    handleScroll()

    window.addEventListener('scroll', handleScroll, { passive: true })
    return () => window.removeEventListener('scroll', handleScroll)
  }, [sections, topOffset, activeSection])

  // Scroll to section when clicked
  const scrollToSection = useCallback(
    (sectionId: string) => {
      const element = document.querySelector(`[data-scrollspy-section="${sectionId}"]`)
      if (element) {
        const rect = element.getBoundingClientRect()
        const scrollY = window.scrollY + rect.top - topOffset + 20 // 20px padding
        window.scrollTo({ top: scrollY, behavior: 'smooth' })
      }
    },
    [topOffset]
  )

  if (sections.length === 0) {
    return null
  }

  return (
    <nav
      ref={navRef}
      data-component="scrollspy-nav"
      className={css({
        position: 'fixed',
        bottom: 0,
        left: 0,
        right: 0,
        zIndex: 80,
        backgroundColor: isDark ? 'gray.900' : 'white',
        borderTop: '1px solid',
        borderColor: isDark ? 'gray.700' : 'gray.200',
        boxShadow: '0 -2px 8px rgba(0, 0, 0, 0.1)',
        // Hide on desktop - only show on mobile/tablet
        display: isVisible ? 'block' : 'none',
      })}
    >
      <div
        data-element="scrollspy-track"
        className={css({
          display: 'flex',
          justifyContent: 'center',
          gap: '0.5rem',
          overflowX: 'auto',
          padding: '0.75rem 1rem',
          // Hide scrollbar but allow scrolling
          scrollbarWidth: 'none',
          '&::-webkit-scrollbar': {
            display: 'none',
          },
        })}
      >
        {sections.map((section) => {
          const isActive = activeSection === section.id

          return (
            <button
              key={section.id}
              type="button"
              onClick={() => scrollToSection(section.id)}
              data-section-link={section.id}
              data-active={isActive}
              className={css({
                display: 'flex',
                flexDirection: 'column',
                alignItems: 'center',
                gap: '0.125rem',
                padding: '0.25rem 0.75rem',
                borderRadius: '8px',
                fontSize: '0.75rem',
                fontWeight: 'medium',
                whiteSpace: 'nowrap',
                cursor: 'pointer',
                transition: 'all 0.2s',
                border: 'none',
                backgroundColor: 'transparent',
                color: isActive
                  ? isDark
                    ? 'blue.400'
                    : 'blue.600'
                  : isDark
                    ? 'gray.400'
                    : 'gray.500',
                _hover: {
                  color: isDark ? 'gray.200' : 'gray.700',
                },
              })}
            >
              <span>{section.label}</span>
              {/* Dot indicator for active section */}
              <span
                className={css({
                  width: '4px',
                  height: '4px',
                  borderRadius: 'full',
                  backgroundColor: isActive ? (isDark ? 'blue.400' : 'blue.600') : 'transparent',
                  transition: 'background-color 0.2s',
                })}
              />
            </button>
          )
        })}
      </div>
    </nav>
  )
}

export default ScrollspyNav