-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(v2): hide navbar on scroll #2055
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* Copyright (c) 2017-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React, {useState, useCallback, useEffect} from 'react'; // eslint-disable-line no-unused-vars | ||
|
||
const useHideableNavbar = hideOnScroll => { | ||
const [isNavbarVisible, setIsNavbarVisible] = useState(true); | ||
const [lastScrollTop, setLastScrollTop] = useState(0); | ||
const [navbarHeight, setNavbarHeight] = useState(0); | ||
const navbarRef = useCallback(node => { | ||
if (node !== null) { | ||
setNavbarHeight(node.getBoundingClientRect().height); | ||
} | ||
}, []); | ||
|
||
const handleScroll = () => { | ||
const scrollTop = window.pageYOffset || document.documentElement.scrollTop; | ||
const documentHeight = document.documentElement.scrollHeight - navbarHeight; | ||
const windowHeight = window.innerHeight; | ||
|
||
if (scrollTop < navbarHeight) { | ||
return; | ||
} | ||
|
||
if (lastScrollTop && scrollTop > lastScrollTop) { | ||
setIsNavbarVisible(false); | ||
} else if (scrollTop + windowHeight < documentHeight) { | ||
setIsNavbarVisible(true); | ||
} | ||
|
||
setLastScrollTop(scrollTop); | ||
}; | ||
|
||
useEffect(() => { | ||
if (!hideOnScroll) { | ||
return undefined; | ||
} | ||
|
||
window.addEventListener('scroll', handleScroll); | ||
|
||
return () => { | ||
window.removeEventListener('scroll', handleScroll); | ||
}; | ||
}, [lastScrollTop, navbarHeight]); | ||
|
||
return { | ||
navbarRef, | ||
isNavbarVisible, | ||
}; | ||
}; | ||
|
||
export default useHideableNavbar; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,6 +90,23 @@ module.exports = { | |
|
||
Outbound links automatically get `target="_blank" rel="noopener noreferrer"`. | ||
|
||
### Auto-hide sticky navbar | ||
|
||
You can enable this cool UI feature that automatically hides the navbar when a user starts scrolling down the page, and show it again when the user scrolls up. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it feels a bit weird that here we first address reader by "you" then later on say "a user" or "the user". I think it is ok to keep using you. just personal opinion: i prefer it to be less objective. "cool" is also a very vague adjective and i found it a bit weird to read this from our docs |
||
|
||
```js | ||
// docusaurus/config.js | ||
module.exports = { | ||
... | ||
themeConfig: { | ||
navbar: { | ||
hideOnScroll: true, | ||
}, | ||
... | ||
}, | ||
} | ||
``` | ||
|
||
## Footer | ||
|
||
## `CodeBlock` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why need eslint disable ? is it React :O