Back to Hooks

useScrollPosition

useScrollPosition.tsx

useScrollPosition

INTERACTION

// Track current scroll position and direction

Quick Usage

const { position, direction, isScrolling, scrollToTop } = useScrollPosition();

Features

Position Tracking
Direction Detection
Scroll Control
Percentage Calculation

When to Use

Essential for dynamic headers, infinite scrolling, progress indicators, or any scroll-based interactions.

Interactive Example

Position

X: 0px
Y: 0px

Direction

X: none
Y: none

State

Scrolling:
At Top:
At Bottom:

Percentage

X: 0%
Y: 0%

Try scrolling!

Scroll up and down to see the position, direction, and state changes. Perfect for implementing dynamic headers, infinite scrolling, or progress indicators.

Content Block 1

This is sample content to enable scrolling. The scroll position hook will track your scrolling behavior as you move through this content.

Content Block 2

This is sample content to enable scrolling. The scroll position hook will track your scrolling behavior as you move through this content.

Content Block 3

This is sample content to enable scrolling. The scroll position hook will track your scrolling behavior as you move through this content.

Content Block 4

This is sample content to enable scrolling. The scroll position hook will track your scrolling behavior as you move through this content.

Content Block 5

This is sample content to enable scrolling. The scroll position hook will track your scrolling behavior as you move through this content.

Content Block 6

This is sample content to enable scrolling. The scroll position hook will track your scrolling behavior as you move through this content.

Content Block 7

This is sample content to enable scrolling. The scroll position hook will track your scrolling behavior as you move through this content.

Content Block 8

This is sample content to enable scrolling. The scroll position hook will track your scrolling behavior as you move through this content.

Content Block 9

This is sample content to enable scrolling. The scroll position hook will track your scrolling behavior as you move through this content.

Content Block 10

This is sample content to enable scrolling. The scroll position hook will track your scrolling behavior as you move through this content.

Content Block 11

This is sample content to enable scrolling. The scroll position hook will track your scrolling behavior as you move through this content.

Content Block 12

This is sample content to enable scrolling. The scroll position hook will track your scrolling behavior as you move through this content.

Content Block 13

This is sample content to enable scrolling. The scroll position hook will track your scrolling behavior as you move through this content.

Content Block 14

This is sample content to enable scrolling. The scroll position hook will track your scrolling behavior as you move through this content.

Content Block 15

This is sample content to enable scrolling. The scroll position hook will track your scrolling behavior as you move through this content.

Content Block 16

This is sample content to enable scrolling. The scroll position hook will track your scrolling behavior as you move through this content.

Content Block 17

This is sample content to enable scrolling. The scroll position hook will track your scrolling behavior as you move through this content.

Content Block 18

This is sample content to enable scrolling. The scroll position hook will track your scrolling behavior as you move through this content.

Content Block 19

This is sample content to enable scrolling. The scroll position hook will track your scrolling behavior as you move through this content.

Content Block 20

This is sample content to enable scrolling. The scroll position hook will track your scrolling behavior as you move through this content.

Complete Example

import { useScrollPosition } from '@usegrand/react-hokss';

function ScrollingApp() {
  const { 
    position, 
    direction, 
    isScrolling, 
    scrollToTop, 
    getScrollPercentage,
    isAtBottom,
    isAtTop
  } = useScrollPosition({
    throttle: 16,
    trackDirection: true
  });

  const handleInfiniteScroll = () => {
    if (isAtBottom && !isScrolling) {
      // Load more content
      loadMoreItems();
    }
  };

  const percentage = getScrollPercentage();

  return (
    <div>
      <div className="progress-bar">
        <div style={{ width: `${percentage.y}%` }} />
      </div>
      <p>Scroll position: {position.y}px</p>
      <p>Direction: {direction.y}</p>
      <p>Progress: {percentage.y}%</p>
      <button onClick={scrollToTop}>
        Back to Top
      </button>
    </div>
  );
}

API Reference

Parameters

optionsUseScrollPositionOptions

Configuration options for scroll tracking

options.elementHTMLElement

Element to track scroll position for (default: window)

options.throttlenumber

Throttle delay in milliseconds (default: 16)

options.trackDirectionboolean

Whether to track scroll direction (default: true)

options.directionThresholdnumber

Threshold for detecting direction changes (default: 1)

Returns

ScrollPositionResult

Object containing scroll state and control functions

position

Current scroll position {x, y}

direction

Scroll direction {x, y}

isScrolling

Boolean indicating if currently scrolling

scrollTo

Function to scroll to specific position

scrollToTop

Function to scroll to top

scrollToBottom

Function to scroll to bottom

getScrollPercentage

Function to get scroll percentage

isAtBottom

Boolean indicating if scrolled to bottom

isAtTop

Boolean indicating if scrolled to top