react-use#usePageLeave JavaScript Examples
The following examples show how to use
react-use#usePageLeave.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Navbar.js From covid19india-react with MIT License | 4 votes |
function Navbar({pages, showLanguageSwitcher, setShowLanguageSwitcher}) {
const {i18n, t} = useTranslation();
const currentLanguage = Object.keys(locales).includes(i18n?.language)
? i18n?.language
: i18n?.options?.fallbackLng[0];
const [expand, setExpand] = useState(false);
const darkMode = useDarkMode(false);
useLockBodyScroll(expand);
const windowSize = useWindowSize();
usePageLeave(() => setExpand(false));
const navbarTransition = useTransition(true, {
from: {opacity: 0},
enter: {opacity: 1},
});
const expandTransition = useTransition(expand, {
from: windowSize.width < 769 ? SLIDE_IN_MOBILE : SLIDE_IN,
enter: windowSize.width < 769 ? SLIDE_OUT_MOBILE : SLIDE_OUT,
leave: windowSize.width < 769 ? SLIDE_IN_MOBILE : SLIDE_IN,
config: {mass: 1, tension: 210, friction: 26},
});
const handleMouseEnter = useCallback(() => {
if (windowSize.width >= 769) {
setExpand(true);
}
}, [windowSize.width]);
const handleLanguageSwitcher = useCallback(() => {
if (expand) setExpand(false);
setShowLanguageSwitcher(!showLanguageSwitcher);
}, [expand, showLanguageSwitcher, setExpand, setShowLanguageSwitcher]);
return navbarTransition((style, item) => (
<animated.div className="Navbar" {...{style}}>
<div className="navbar-left" onClick={handleLanguageSwitcher}>
{locales[currentLanguage]}
</div>
<div className="navbar-middle">
<Link to="/" onClick={setExpand.bind(this, false)}>
Covid19<span>India</span>
</Link>
</div>
<div
className="navbar-right"
onMouseEnter={handleMouseEnter}
{...(windowSize.width < 769 && {
onClick: setExpand.bind(this, !expand),
})}
>
{windowSize.width < 769 && (
<span>{expand ? t('Close') : t('Menu')}</span>
)}
{windowSize.width >= 769 && (
<>
<Link to="/">
<span>
<Home {...activeNavIcon('/')} />
</span>
</Link>
<Link to="/blog">
<span>
<Book {...activeNavIcon('/blog')} />
</span>
</Link>
<Link to="/volunteers">
<span>
<Users {...activeNavIcon('/volunteers')} />
</span>
</Link>
<Link to="/about">
<span>
<HelpCircle {...activeNavIcon('/about')} />
</span>
</Link>
<span>
<SunMoon {...{darkMode}} />
</span>
</>
)}
</div>
{expandTransition(
(style, item) =>
item && (
<animated.div {...{style}}>
<Expand {...{pages, setExpand, darkMode, windowSize}} />
</animated.div>
)
)}
</animated.div>
));
}