Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added scroll based menu highlight feature #1085

Conversation

Tiru-99
Copy link
Contributor

@Tiru-99 Tiru-99 commented Oct 10, 2024

Enhanced Side Menu with Active Section Tracking #1083

Overview

This PR implements an enhanced side menu component that automatically highlights the current section as users scroll through the page. The menu now provides visual feedback for navigation, improving the overall user experience in our single-page documentation layout.

Key Changes

  • Implemented scroll-based active section detection
  • Added visual highlighting for both parent and child menu items
  • Introduced smooth scrolling behavior when clicking menu items
  • Updated styling to support the new interactive features

Implementation Details

  • Uses useEffect and scroll event listener to track the current section
  • Calculates section positions dynamically to handle content updates
  • Implements a 100px offset for better UX when transitioning between sections
  • Handles both top-level and nested menu items

##Execution Video

Screen.Recording.2024-10-10.224523.mp4

Code Example

// Function to determine which section is currently in view
const handleScroll = () => {
//Create an array of all sections with their IDs and positions
  const sections = pages.flatMap(page => 
    [page, ...(page.pages || [])].map(p => ({
      id: p.pathname.replace('#', ''),
      top: document.getElementById(p.pathname.replace('#', ''))?.offsetTop || 0
    }))
  )

//Find the section that's currently in view
  const currentSection = sections.reduce((acc, section) => {
    const { id, top } = section
    if (window.scrollY >= top - 100) {   // 100px offset for better UX
      return id
    }
    return acc
  }, '')

 //Update the state with the current section
  setActiveSection(currentSection)
}


Copy link

vercel bot commented Oct 10, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-hook-form-website ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 19, 2024 5:37am

Copy link
Member

@bluebill1049 bluebill1049 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@bluebill1049 bluebill1049 merged commit 23c94ef into react-hook-form:master Nov 19, 2024
2 checks passed
bluebill1049 added a commit that referenced this pull request Nov 19, 2024
bluebill1049 added a commit that referenced this pull request Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants