|
1 | 1 | # @ppasmik/react-scroll-trigger
|
2 | 2 |
|
3 |
| -Modern React component for detecting and responding to scroll events with TypeScript support. |
| 3 | +[](https://www.npmjs.com/package/@ppasmik/react-scroll-trigger) |
| 4 | +[](https://www.npmjs.com/package/@ppasmik/react-scroll-trigger) |
| 5 | +[](https://www.npmjs.com/package/@ppasmik/react-scroll-trigger) |
| 6 | + |
| 7 | +A modern, TypeScript-based React component for monitoring scroll events and triggering callbacks when elements enter, exit, or progress through the viewport. This is a rewritten and modernized version of the original react-scroll-trigger package. |
| 8 | + |
| 9 | +Each callback includes `progress` and `velocity` parameters, enabling precise control over animations and transitions based on scroll position and speed. |
4 | 10 |
|
5 | 11 | ## Installation
|
6 | 12 |
|
7 |
| -```bash |
| 13 | +```sh |
8 | 14 | npm install @ppasmik/react-scroll-trigger
|
9 |
| -# or |
10 |
| -yarn add @ppasmik/react-scroll-trigger |
| 15 | +``` |
| 16 | + |
| 17 | +or via Yarn: |
| 18 | + |
| 19 | +```sh |
| 20 | +yarn add @ppasmik/react-scroll-trigger |
| 21 | +``` |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +```tsx |
| 26 | +import ScrollTrigger from '@ppasmik/react-scroll-trigger'; |
| 27 | + |
| 28 | +const MyComponent = () => { |
| 29 | + const [visible, setVisible] = useState(false); |
| 30 | + |
| 31 | + const onEnterViewport = () => { |
| 32 | + setVisible(true); |
| 33 | + }; |
| 34 | + |
| 35 | + const onExitViewport = () => { |
| 36 | + setVisible(false); |
| 37 | + }; |
| 38 | + |
| 39 | + return ( |
| 40 | + <ScrollTrigger onEnter={onEnterViewport} onExit={onExitViewport}> |
| 41 | + <div className={`container ${visible ? 'container-animate' : ''}`} /> |
| 42 | + </ScrollTrigger> |
| 43 | + ); |
| 44 | +}; |
| 45 | +``` |
| 46 | + |
| 47 | +The `ScrollTrigger` component is designed to be highly flexible. You can use it: |
| 48 | + |
| 49 | +- As a standalone element without children |
| 50 | + |
| 51 | +```tsx |
| 52 | +<ScrollTrigger onEnter={handleEnter} onExit={handleExit} /> |
| 53 | +``` |
| 54 | + |
| 55 | +- With children to receive events based on their dimensions |
| 56 | + |
| 57 | +```tsx |
| 58 | +<ScrollTrigger onEnter={handleEnter} onProgress={handleProgress}> |
| 59 | + <section> |
| 60 | + <h1>Your content here</h1> |
| 61 | + </section> |
| 62 | +</ScrollTrigger> |
| 63 | +``` |
| 64 | + |
| 65 | +Common use cases include: |
| 66 | + |
| 67 | +- Triggering animations when elements become visible |
| 68 | +- Loading content dynamically based on scroll position |
| 69 | +- Creating scroll-based transitions and effects |
| 70 | +- Implementing infinite scroll functionality |
| 71 | + |
| 72 | +## Props |
| 73 | + |
| 74 | +| Prop | Type | Default | Description | |
| 75 | +| ---------------- | -------------------- | ------------------------ | -------------------------------------------------------------------- | |
| 76 | +| `component` | ElementType | 'div' | React component or HTML element to render as wrapper | |
| 77 | +| `containerRef` | HTMLElement ⎮ string | document.documentElement | Scrolling container reference | |
| 78 | +| `throttleResize` | number | 100 | Resize event throttle in ms | |
| 79 | +| `throttleScroll` | number | 100 | Scroll event throttle in ms | |
| 80 | +| `triggerOnLoad` | boolean | true | Whether to trigger onEnter on mount | |
| 81 | +| `onEnter` | function | - | Called when element enters viewport `({progress, velocity}) => void` | |
| 82 | +| `onExit` | function | - | Called when element exits viewport `({progress, velocity}) => void` | |
| 83 | +| `onProgress` | function | - | Called during scroll `({progress, velocity}) => void` | |
| 84 | + |
| 85 | +Standard React props (className, style, etc.) are also supported and will be passed to the wrapper element. |
| 86 | + |
| 87 | +## Technical Details |
| 88 | + |
| 89 | +The component uses React hooks for efficient state management: |
| 90 | +- `useRef` to track the DOM element position |
| 91 | +- `useState` for viewport visibility and scroll tracking |
| 92 | +- `useEffect` for handling scroll and resize events with proper cleanup |
| 93 | + |
| 94 | +Visibility detection: |
| 95 | +- Uses `getBoundingClientRect()` for accurate element position calculation |
| 96 | +- Progress is calculated based on element's position relative to viewport: |
| 97 | + ```ts |
| 98 | + progress = 1 - elementRect.bottom / (viewportEnd + elementRect.height) |
| 99 | + ``` |
| 100 | +- Velocity is derived from scroll position changes over time |
| 101 | +- All calculations are throttled (default 100ms) to optimize performance |
| 102 | + |
| 103 | +The component is designed to work with both window-level scrolling and custom scroll containers (via `containerRef` prop), making it suitable for various layout scenarios. |
| 104 | + |
| 105 | +## License |
| 106 | + |
| 107 | +MIT © [Peter Pasmik] |
| 108 | + |
| 109 | +## Acknowledgments |
| 110 | + |
| 111 | +This package is a TypeScript rewrite of the original [react-scroll-trigger](https://www.npmjs.com/package/react-scroll-trigger) package, modernized with current React practices and enhanced type safety. |
0 commit comments