Imitate Facebook hide/show expanding/contracting Navigation Bar
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A Facebook-style navigation bar usually does two things at once: it hides when the user scrolls down to free space for content, and it reappears when the user scrolls up or explicitly opens navigation. The effect feels simple, but the implementation needs to balance animation smoothness, accessibility, and predictable state.
Start with a Stable Navigation Structure
Before adding scroll behavior, build semantic markup with an explicit toggle button. That gives keyboard and screen-reader users the same navigation access as pointer users.
The button is not optional. If navigation can collapse, there must be a clear control that reveals it again.
Use CSS Transforms for Smooth Hide and Show
For the bar itself, transform is usually smoother than constantly changing top or recalculating layout-heavy properties.
This separates two ideas: hiding the whole bar on scroll and collapsing the nav links when the user toggles the menu state.
Detect Scroll Direction Without Jank
The common pattern is to hide the bar only after meaningful downward movement, then reveal it on upward movement. Use requestAnimationFrame to avoid doing expensive work for every raw scroll event.
That threshold prevents the bar from flickering on tiny finger movements.
Add Explicit Expand and Collapse Behavior
The user should also be able to expand or collapse the navigation without relying on scroll direction.
If you want the app to remember the user's preference after reload, persist only the collapsed state, not every transient scroll state.
Accessibility and Mobile Details Matter
A polished version should not hide navigation while focus is inside the menu. It should also handle long labels, larger text sizes, and orientation changes. On touch devices, the threshold for hide-on-scroll usually needs testing because overly aggressive behavior feels unstable.
In practice, test these scenarios:
- fast downward scrolling in a long feed
- upward flicks that should reveal the bar quickly
- keyboard navigation while the menu is open
- large text accessibility settings
- refresh or route change with preserved collapsed state
Common Pitfalls
- Running layout-heavy logic directly inside raw scroll handlers.
- Hiding the navigation bar while a menu item still has keyboard focus.
- Treating visual collapse and accessibility state as separate concerns.
- Using fixed pixel assumptions that break on small screens or large text.
- Copying the motion pattern without deciding whether it actually improves that page.
Summary
- Build the navigation with semantic HTML and an explicit toggle first.
- Use CSS transforms for hide and show animations instead of layout-heavy movement.
- Detect scroll direction with a threshold and
requestAnimationFrame. - Let users explicitly expand or collapse the menu, not only through scrolling.
- Validate the behavior on mobile and with accessibility settings before shipping.

