Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React - Slide fixed navbar up on scroll down and slide down on scroll up

tl;dr Scroll down for the solution that worked for me!

How to implement a slide up and down on a fixed navbar in react?

What is the better approach using refs or using the componentDidMount lifecycle hook?

  hideNav = (navbar) => {
    const hide = () => {
      let lastScrollTop = 0;
      const currentScrollTop = navbar.scrollTop;

      // scroll down
      if (currentScrollTop > lastScrollTop) {
      navbar.classList.add('hidden');
      } else {
      // scroll up
        navbar.classList.remove('hidden');
      }
      lastScrollTop = currentScrollTop;
    };

    window.addEventListener('scroll', hide);
  };

... further down in the render method:

 render() {
      return <Navbar ref={this.hideNav} />

UPDATE:

Solution:

class Navbar extends React.Component {
  state = {
    auth: false,
    slide: 0,  // How much should the Navbar slide up or down
    lastScrollY: 0,  // Keep track of current position in state
  };

  componentWillMount() {
    // When this component mounts, begin listening for scroll changes
    window.addEventListener('scroll', this.handleScroll);
  }

  componentWillUnmount() {
    // If this component is unmounted, stop listening
    window.removeEventListener('scroll', this.handleScroll);
  }

  handleScroll = () => {
    const { lastScrollY } = this.state; 
    const currentScrollY = window.scrollY;


    if (currentScrollY > lastScrollY) {
      this.setState({ slide: '-48px' });
    } else {
      this.setState({ slide: '0px' });
    }
    this.setState({ lastScrollY: currentScrollY });
  };

   render() {    
    return (
      <Navbar
        style={{
          transform: `translate(0, ${this.state.slide})`,
          transition: 'transform 90ms linear',
        }}
      />
     );
   }
 }

I haven't done any optimizations yet so it is recommended to throttle the event using requestAnimationFrame, setTimeout or customEvent. Like here.

like image 753
chinchilla Avatar asked Nov 22 '17 18:11

chinchilla


1 Answers

You shouldn't use refs as a solution to register event listeners or adding/removing classes. As you suggested, you should use component lifecycle hooks to begin (and stop) listening for scrolls on the window.

export default class App extends Component {
  state = { hidden: false };

  constructor(props) {
    super(props);

    // Bind the function to this component, so it has access to this.state
    this.handleScroll = this.handleScroll.bind(this);
  }

  componentWillMount() {
    // When this component mounts, begin listening for scroll changes
    window.addEventListener('scroll', this.handleScroll);
  }

  componentWillUnmount() {
    // If this component is unmounted, stop listening
    window.removeEventListener('scroll', this.handleScroll);
  }

  handleScroll(e) {
    let lastScrollTop = 0;
    const currentScrollTop = navbar.scrollTop;

    // Set the state of hidden depending on scroll position
    // We only change the state if it needs to be changed
    if (!this.state.hidden && currentScrollTop > lastScrollTop) {
      this.setState({ hidden: true });
    } else if(this.state.hidden) {
      this.setState({ hidden: false });
    }
    lastScrollTop = currentScrollTop;
  }

  render() {
    // We pass a hidden prop to Navbar which can render className="hidden" if the prop is true
    return (
      <Navbar hidden={this.state.hidden} />
    );
  }
}

Also, looking at the scroll function you provided, it won't work, as lastScrollTop will always be 0. If you're looking for a scroll solution take a look at this answer as it has a similar solution to what your fixed navbar would need (except, hiding instead of being shown) : Sticky Header after scrolling down

like image 135
Dan Bovey Avatar answered Oct 21 '22 18:10

Dan Bovey