Skip to content

Commit

Permalink
3317 - Navigation menu sticky
Browse files Browse the repository at this point in the history
  • Loading branch information
minotogna committed Feb 2, 2024
1 parent 91f1e07 commit 5447d08
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@import 'src/client/style/partials';

.nav-assessment {
overflow-y: auto;
padding: $spacing-xxs 0;
position: sticky;
top: 50px;
}
12 changes: 8 additions & 4 deletions src/client/components/Navigation/NavAssessment/NavAssessment.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import './NavAssessment.scss'
import React, { useState } from 'react'

import { Objects } from 'utils/objects'

import { useSections } from 'client/store/metadata'
import Header from 'client/components/Navigation/NavAssessment/Header'
import NavigationSection from 'client/components/Navigation/NavAssessment/Section'

import Header from './Header'
import NavigationSection from './Section'
import { useMaxHeight } from './hooks/useMaxHeight'

const NavAssessment: React.FC = () => {
const sections = useSections()
const [showSections, setShowSections] = useState(false)

const maxHeight = useMaxHeight()
const [showSections, setShowSections] = useState<boolean>(false)

if (Objects.isEmpty(sections)) return null

return (
<div className="nav-assessment">
<div className="nav-assessment" style={{ maxHeight }}>
<Header showSections={showSections} setShowSections={setShowSections} />

{sections.map((section) => (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useEffect, useRef, useState } from 'react'

// document height - toolbar
const baseHeight = `100dvh - 50px`

export const useMaxHeight = (): string => {
const [maxHeight, setMaxHeight] = useState<string>(`calc(${baseHeight})`)
const heightsRef = useRef<Record<string, number>>({})

useEffect(() => {
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
heightsRef.current[entry.target.tagName] = entry.intersectionRect.height
})

const heightsCalc = Object.values(heightsRef.current)
.map((v) => `${v}px`)
.join(' - ')

setMaxHeight(`calc(100dvh - 50px - ${heightsCalc})`)
},
{ threshold: [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1] }
)

observer.observe(document.getElementsByTagName('header')[0])
observer.observe(document.getElementsByTagName('footer')[0])

return () => {
observer.disconnect()
}
}, [])

return maxHeight
}
3 changes: 0 additions & 3 deletions src/client/components/Navigation/Navigation.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
.nav {
@extend .nav-base;
grid-area: navigation;
overflow-x: hidden;
overflow-y: auto;
height: 100%;
padding: 8px 0;

.nav__bulk-download {
margin-left: 16px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const FraHeader: React.FC = () => {
const cycle = useCycle()

return (
<div className="app-header no-print">
<header className="app-header no-print">
<img alt="FAO" className="app-header__fao-logo" src={`/img/fao/FAO${i18n.resolvedLanguage}.svg`} />
<div className="app-header__separator" />
<div className="app-header__global-fra">
Expand All @@ -29,7 +29,7 @@ const FraHeader: React.FC = () => {
<UserLinks />
<LinkHome />
</div>
</div>
</header>
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const PanEuropeanHeader: React.FC = () => {
const cycle = useCycle()

return (
<div className="app-header no-print">
<header className="app-header no-print">
<img alt="FAO" src={`/img/fao/FAO${i18n.resolvedLanguage}.svg`} />
<div className="app-header__separator" />
<div className="app-header__global-fra">
Expand All @@ -24,7 +24,7 @@ const PanEuropeanHeader: React.FC = () => {
<UserLinks />
<LinkHome />
</div>
</div>
</header>
)
}

Expand Down

0 comments on commit 5447d08

Please sign in to comment.