import React, {useEffect, useRef, useState} from 'react'
import {background, container} from "../../../UI_Style/default";
import {NavLink} from "react-router-dom";
const Header = () => {
const [scroll, setScroll] = useState<boolean>(false)
const heightHeaderRef = useRef<HTMLDivElement | null>(null)
const scrollPage = ():void => {
window.addEventListener('scroll', () => {
const heightTop: number = window.scrollY
if(heightHeaderRef.current && heightHeaderRef.current.offsetHeight < heightTop && !scroll)
setScroll(true)
if(heightHeaderRef.current && heightHeaderRef.current.offsetHeight > heightTop && scroll)
setScroll(false)
if(heightHeaderRef.current)
console.log(heightTop, heightHeaderRef.current?.offsetHeight, scroll)
})
}
useEffect(() => {
scrollPage()
}, [])
return (
<header className={`${background} sticky top-0 left-0`} ref={heightHeaderRef}>
<div className={`${container} ${scroll ? 'py-2' : 'py-6'} duration-300`}>
<div className={'flex justify-between items-center'}>
<div className={'flex'}>
<NavLink to={''} className={'text-xl text-white mr-6'}>ГЛАВНАЯ</NavLink>
<NavLink to={'/about'} className={'text-xl text-white mr-6'}>О НАС</NavLink>
<NavLink to={'/crm'} className={'text-xl text-white mr-6'}>CRM</NavLink>
<NavLink to={'/licenses'} className={'text-xl text-white mr-6'}>ЛИЦЕНЗИИ</NavLink>
<NavLink to={'/blog'} className={'text-xl text-white mr-6'}>БЛОГ</NavLink>
<NavLink to={'/faq'} className={'text-xl text-white'}>FAQ</NavLink>
</div>
<div>
<a className={'text-white text-center block text-2xl'} href="tel::+7(385) 299-20-21">+7(385) 299-20-21</a>
<div className={'text-white text-center block text-xs'}>ЗВОНОК ПО РОССИИ БЕСПЛАТНЫЙ</div>
</div>
</div>
</div>
</header>
)
}
export default Header
useScroll.ts
import React, { useLayoutEffect, useState} from 'react';
export const useScroll = ( ref ) => {
const [ scroll, setScroll ] = useState(false);
const updateScroll = () => {
If (!ref.current) return
let isScroll = false
const heightTop: number = window.scrollY
if(ref.current.offsetHeight < heightTop)
isScroll = true
if(heightHeaderRef.current.offsetHeight > heightTop)
isScroll = false
setScroll(isScroll)
}
useLayoutEffect( () => {
window.addEventListener('scroll',updateScroll)
updateScroll();
return () => {
window.removeEventListener('scroll',updateScroll)
}
}, [] );
return scroll;
}
const Header = () => {
const heightHeaderRef = useRef<HTMLDivElement | null>(null)
const scroll = useScroll<boolean>(heightHeaderRef)
console.log(scroll)
return (
<header className={`${background} sticky top-0 left-0`} ref={heightHeaderRef}>
<div className={`${container} ${scroll ? 'py-2' : 'py-6'} duration-300`}>
<div className={'flex justify-between items-center'}>
<div className={'flex'}>
<NavLink to={''} className={'text-xl text-white mr-6'}>ГЛАВНАЯ</NavLink>
<NavLink to={'/about'} className={'text-xl text-white mr-6'}>О НАС</NavLink>
<NavLink to={'/crm'} className={'text-xl text-white mr-6'}>CRM</NavLink>
<NavLink to={'/licenses'} className={'text-xl text-white mr-6'}>ЛИЦЕНЗИИ</NavLink>
<NavLink to={'/blog'} className={'text-xl text-white mr-6'}>БЛОГ</NavLink>
<NavLink to={'/faq'} className={'text-xl text-white'}>FAQ</NavLink>
</div>
<div>
<a className={'text-white text-center block text-2xl'} href="tel::+7(385) 299-20-21">+7(385) 299-20-21</a>
<div className={'text-white text-center block text-xs'}>ЗВОНОК ПО РОССИИ БЕСПЛАТНЫЙ</div>
</div>
</div>
</div>
</header>
)
}
export default Header