Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use in scrollable div, not just on body scroll #122

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,39 @@ those elements within the DOM.
</ScrollTrigger>
```

You can also use inside of a scrollable container.

```jsx
/**
* Scrollable div container is needed
*
* We need the 'ref' from this container div, since this component will be loaded before our 'ref',
* is ready I find it easier to store the 'ref' in state.
*/
const [myRef, setMyRef] = useState();
<div ref={r => setMyRef(r)} style={{ height: '300px', overflow: 'scroll'}}>
{/**
* ScrollTrigger must use the 'ref' inside of the 'containerRef' prop!
*
* Use an array of components. This allows you to trigger when a component
* becomes visible within the scrollable div
*/}
{myObjects.map((obj, index) => {
return (
<ScrollTrigger
containerRef={myRef}
onEnter={e => alert(`${obj.attribute} at index ${index} is now visible inside scrollable div!`)}
onExit={e => alert(`${obj.attribute} at index ${index} is no longer visible inside scrollable div!`)}
onProgress={e => alert(`${obj.attribute} Progress: ${e}`)}
key={`${obj.attribute}_${index}`}
>
<SomeOtherComponent someProp={obj.attribute} />
</ScrollTrigger>
)
})}
</div>
```

The beauty of this component is its flexibility. I’ve used it to trigger
AJAX requests based on either the `onEnter` or `progress` of the component within
the viewport. Or, as a way to control animations or other transitions that you
Expand Down
Loading