Skip to content

useScrollDirection

This hook detects the scroll direction of the window or a target element.

Parameters

  • the ref object of the target element (default is window).

Return Values

NameTypeDescription
refRefObjectthe target element reference.
isUpBooleanindicates if the user is scrolling up.
isDownBooleanindicates if the user is scrolling down.
isLeftBooleanindicates if the user is scrolling left.
isRightBooleanindicates if the user is scrolling right.

Example Usage

tsx
import { useScrollDirection } from 'react-pre-hooks';

export default function Example() {
  const { isDown, isUp } = useScrollDirection();

  return (
    <main>
      <nav className={isUp || !isDown ? "show" : ""}>Navigation</nav>
    </main>
  );
}
import { useScrollDirection } from 'react-pre-hooks';

export default function Example() {
  const { isDown, isUp } = useScrollDirection();

  return (
    <main>
      <nav className={isUp || !isDown ? "show" : ""}>Navigation</nav>
    </main>
  );
}

Released under the MIT License.