Last active
August 16, 2021 05:42
-
-
Save AmanAgarwal041/60acf331576878a04053dfe9c52851bd to your computer and use it in GitHub Desktop.
Header Animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect } from 'react'; | |
import PropTypes from 'prop-types'; | |
// Animating Header | |
const noop = () => {}; | |
const HeaderAnim = ({ onHide, onShow, active }) => { | |
if (!active || !process.browser) { | |
return null; | |
} | |
/* eslint-disable no-undef */ | |
const hideHeader = e => { | |
if (document.scrollingElement && document.scrollingElement.scrollTop > 200) { | |
setTimeout(() => { | |
const header = document.querySelector('header'); | |
if (header) { | |
header.style.transform = 'translateY(-100%)'; | |
onHide(e); | |
} | |
}, 1500); | |
} | |
}; | |
const showHeader = e => { | |
const header = document.querySelector('header'); | |
if (header) { | |
header.style.transform = ''; | |
onShow(e); | |
} | |
}; | |
useEffect(() => { | |
document.addEventListener('touchstart', showHeader); | |
document.addEventListener('touchend', hideHeader); | |
return () => { | |
document.removeEventListener('touchstart', showHeader); | |
document.removeEventListener('touchend', hideHeader); | |
}; | |
}, []); | |
/* eslint-enable no-undef */ | |
return null; | |
}; | |
HeaderAnim.propTypes = { | |
onShow: PropTypes.func, | |
onHide: PropTypes.func, | |
active: PropTypes.bool, | |
}; | |
HeaderAnim.defaultProps = { | |
onShow: noop, | |
onHide: noop, | |
active: false, | |
}; | |
export default HeaderAnim; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment