Router-aware scroll management for React applications.
Automatic scroll restoration, navigation-aware scrolling, reusable hooks, utilities, and optional UI components for React Router apps.
- Router-aware scroll management
- Automatic scroll restoration
- Reading progress bar
- Smooth scrolling utilities
- Scroll-to-top button
- Infinite scrolling hooks
- Scroll direction detection
- Scroll spy support
- IntersectionObserver utilities
- Container-aware scrolling support
- Reduced motion accessibility support
- SSR safe
- Lightweight and tree-shakeable
Install package and peer dependencies:
npm install react-scroll-kit react react-router-domUsing yarn:
yarn add react-scroll-kit react react-router-domUsing pnpm:
pnpm add react-scroll-kit react react-router-domPlace ScrollToTop inside your React Router application.
import { ScrollToTop } from 'react-scroll-kit';
function App() {
return (
<>
<ScrollToTop />
{routes}
</>
);
}Programmatically scroll the page or a custom container.
import { scrollTo } from 'react-scroll-kit';
scrollTo('#contact', {
behavior:'smooth',
duration:700,
easing:'easeOutExpo',
offset:80
});| Option | Type | Default |
|---|---|---|
| behavior | smooth | instant | instant |
| duration | number | 500 |
| easing | string | function | easeOutQuad |
| offset | number | 0 |
| delay | number | 0 |
| scrollAxis | x | y | both | y |
| container | HTMLElement | RefObject | window |
| respectReducedMotion | boolean | true |
Smoothly scroll to URL hash elements.
import { hashScroll } from 'react-scroll-kit';
hashScroll('#pricing');Lock scrolling during modals or overlays.
import {
lockScroll,
unlockScroll
} from 'react-scroll-kit';
lockScroll();
unlockScroll();Automatically scroll to top on route changes.
<ScrollToTop
behavior="smooth"
duration={700}
easing="easeOutExpo"
excludeRoutes={['/exclusive']}
/>Props:
| Prop | Type | Default |
|---|---|---|
| behavior | smooth | instant | instant |
| duration | number | 500 |
| easing | string | function | easeOutQuad |
| delay | number | 0 |
| scrollAxis | x | y | both | y |
| container | HTMLElement | RefObject | window |
| excludeRoutes | string[] | [] |
| respectReducedMotion | boolean | true |
Restore previous page positions.
<ScrollRestoration
storageKey="app-scroll"
container={contentRef}
/>Show page reading progress.
<ScrollProgressBar
height={4}
color="linear-gradient(to right,#3b82f6,#10b981)"
position="bottom"
container={contentRef}
/>Props:
| Prop | Type | Default |
|---|---|---|
| height | string | number | 3 |
| color | string | #1565C0 |
| zIndex | number | 9999 |
| position | top | bottom | top |
| container | HTMLElement | RefObject | window |
Display a floating button after scrolling.
<ScrollToTopButton
threshold={400}
/>Navigation-aware scroll links.
<ScrollLink
to="/about#pricing"
scrollTo="#pricing"
scrollBehavior="smooth"
>
Go to pricing
</ScrollLink>If your app uses:
overflow-y:autopass the container reference.
import { useRef } from 'react';
const contentRef = useRef(null);
return (
<>
<ScrollProgressBar
container={contentRef}
/>
<ScrollToTop
container={contentRef}
/>
<main
ref={contentRef}
style={{
height:'100vh',
overflowY:'auto'
}}
>
{children}
</main>
</>
)import {
useScrollPosition
} from 'react-scroll-kit';
const {x,y} =
useScrollPosition();import {
useScrollDirection
} from 'react-scroll-kit';
const direction=
useScrollDirection({
threshold:10
});import {
useScrolledPast
} from 'react-scroll-kit';
const visible=
useScrolledPast(300);import {
useInView
} from 'react-scroll-kit';
const {
ref,
inView
}=useInView({
threshold:0.5
});import {
useScrollSpy
} from 'react-scroll-kit';
const active=
useScrollSpy([
'#home',
'#pricing',
'#contact'
]);import {
useInfiniteScroll
} from 'react-scroll-kit';
const {
loading,
sentinelRef
}
=
useInfiniteScroll(
async()=>{
await loadMore();
}
);Supports:
- prefers-reduced-motion
- keyboard navigation
- passive event listeners
- requestAnimationFrame optimization
Example:
import {
prefersReducedMotion
} from 'react-scroll-kit';
if(prefersReducedMotion()){
// skip animations
}If using:
overflow-y:autopass container reference:
<ScrollProgressBar
container={contentRef}
/>For asynchronous rendering:
hashScroll('#target');Scroll restoration only triggers on browser back/forward navigation.
- Lightweight
- Tree-shakeable
- requestAnimationFrame optimized
- passive listeners
- SSR safe
- Chrome >= 90
- Firefox >= 88
- Safari >= 14
- Edge >= 90
Clone repository:
git clone https://github.com/Coderkube-App/react-scroll-kit.git
cd react-scroll-kit
npm install
npm run buildMIT © Ammar Shaikh