Fixed minor UI issue with the nav bar on mobile
This commit is contained in:
@@ -1,12 +1,17 @@
|
|||||||
import { useState, useEffect, useCallback, useRef } from 'react'
|
import { useState, useEffect, useCallback, useRef } from 'react'
|
||||||
|
|
||||||
const sectionTileMap: Record<string, string> = {
|
const MOBILE_NAV_QUERY = '(max-width: 599px)'
|
||||||
'patient-summary': 'overview',
|
|
||||||
|
const SCROLL_BOTTOM_THRESHOLD = 40
|
||||||
|
|
||||||
|
function buildSectionTileMap(isMobile: boolean): Record<string, string> {
|
||||||
|
return {
|
||||||
|
'mobile-overview': 'overview',
|
||||||
|
'patient-summary': isMobile ? 'summary' : 'overview',
|
||||||
'section-experience': 'experience',
|
'section-experience': 'experience',
|
||||||
'section-skills': 'skills',
|
'section-skills': 'skills',
|
||||||
}
|
}
|
||||||
|
}
|
||||||
const SCROLL_BOTTOM_THRESHOLD = 40
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hook to track which section is currently visible using IntersectionObserver.
|
* Hook to track which section is currently visible using IntersectionObserver.
|
||||||
@@ -20,6 +25,7 @@ const SCROLL_BOTTOM_THRESHOLD = 40
|
|||||||
export function useActiveSection(): string {
|
export function useActiveSection(): string {
|
||||||
const [activeSection, setActiveSection] = useState<string>('overview')
|
const [activeSection, setActiveSection] = useState<string>('overview')
|
||||||
const scrollOverrideRef = useRef<string | null>(null)
|
const scrollOverrideRef = useRef<string | null>(null)
|
||||||
|
const sectionTileMapRef = useRef(buildSectionTileMap(window.matchMedia(MOBILE_NAV_QUERY).matches))
|
||||||
|
|
||||||
const updateFromScroll = useCallback((root: HTMLElement) => {
|
const updateFromScroll = useCallback((root: HTMLElement) => {
|
||||||
const { scrollTop, scrollHeight, clientHeight } = root
|
const { scrollTop, scrollHeight, clientHeight } = root
|
||||||
@@ -37,6 +43,15 @@ export function useActiveSection(): string {
|
|||||||
}
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const mq = window.matchMedia(MOBILE_NAV_QUERY)
|
||||||
|
const handleChange = (e: MediaQueryListEvent) => {
|
||||||
|
sectionTileMapRef.current = buildSectionTileMap(e.matches)
|
||||||
|
}
|
||||||
|
mq.addEventListener('change', handleChange)
|
||||||
|
return () => mq.removeEventListener('change', handleChange)
|
||||||
|
}, [])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const tiles = Array.from(
|
const tiles = Array.from(
|
||||||
document.querySelectorAll('[data-tile-id]')
|
document.querySelectorAll('[data-tile-id]')
|
||||||
@@ -57,8 +72,8 @@ export function useActiveSection(): string {
|
|||||||
)
|
)
|
||||||
|
|
||||||
const tileId = mostVisible.target.getAttribute('data-tile-id')
|
const tileId = mostVisible.target.getAttribute('data-tile-id')
|
||||||
if (tileId && sectionTileMap[tileId]) {
|
if (tileId && sectionTileMapRef.current[tileId]) {
|
||||||
setActiveSection(sectionTileMap[tileId])
|
setActiveSection(sectionTileMapRef.current[tileId])
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user