Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ryanhefner/react-target-scroller
π― React component that smoothly scrolls to the target element passed in via props.
https://github.com/ryanhefner/react-target-scroller
react react-component scroller scrolling target-scroller
Last synced: about 1 month ago
JSON representation
π― React component that smoothly scrolls to the target element passed in via props.
- Host: GitHub
- URL: https://github.com/ryanhefner/react-target-scroller
- Owner: ryanhefner
- License: mit
- Created: 2018-05-08T18:28:06.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-12-25T22:47:30.000Z (almost 3 years ago)
- Last Synced: 2024-10-14T22:31:57.485Z (about 1 month ago)
- Topics: react, react-component, scroller, scrolling, target-scroller
- Language: JavaScript
- Homepage: https://www.pkgstats.com/pkg:react-target-scroller
- Size: 563 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# π― react-target-scroller
![npm](https://img.shields.io/npm/v/react-target-scroller?style=flat-square)
![NPM](https://img.shields.io/npm/l/react-target-scroller?style=flat-square)
![npm](https://img.shields.io/npm/dt/react-target-scroller?style=flat-square)
![Coveralls github](https://img.shields.io/coveralls/github/ryanhefner/react-target-scroller?style=flat-square)
![CircleCI](https://img.shields.io/circleci/build/github/ryanhefner/react-target-scroller?style=flat-square)
![Snyk Vulnerabilities for GitHub Repo](https://img.shields.io/snyk/vulnerabilities/github/ryanhefner/react-target-scroller?style=flat-square)React component that smoothly scrolls to the `target` element passed in via props.
## Install
Via [npm](https://npmjs.com/package/react-target-scroller)
```sh
npm install --save react-target-scroller
```Via [Yarn](https://yarn.fyi/react-target-scroller)
```sh
yarn add react-target-scroller
```## How to use
The `TargetScroller` is very configurable and designed to do a single thing well. I have another project, [`react-hash-handler`](https://github.com/ryanhefner/react-hash-handler), that pairs well with this component, but can easily be used alone, or coupled with your own components.
Below are the properties and callbacks that can be set on the component, along with an example of some general use.
### Properties
* `delay:Number` - The time in milliseconds to delay the scroll after the `target` has been set/changed. (default: `0`)
* `direction:String` - The direction the `scrollingElement` will incremented/decremented in order to reach its target. The options are available via an `export` available from the component. Example: `import TargetScroller, {Direction} from 'react-target-scroller;` The options are, `Direction.VERTICAL` or `Direction.HORIZONTAL`. (default: `Direction.VERTICAL`)
* `duration:Number` - The duration in milliseconds that the page will take to transition to the `target`. (default: `650`)
* `ease:Func` - The easing function used to define the tween of the transition. `TargetScroller` uses [`tweenkle`](https://github.com/ryanhefner/tweenkle) and supports all the [easing equations](https://github.com/ryanhefner/tweenkle#easing) offered in that library, or you can write your own based on the ease spec. (default: `Quad.InOut`)
* `offset:Number` - Number used to offset the scroll position. Useful for making sure fixed headers donβt cover up the target object.
* `scrollingElement:[String || Element]` - The element that will be scrolled in order to reach the `target`. This is handy if you have a scrollable element in the page and want to target its children. (default: `'document.scrollingElement'`).
* `target:[String || Element]` - The property that defines which element to scroll to. You can either supply an actual reference to the element, or a string that can target the element. (Note: If you supply a string, make sure itβs a string format that is supported by `querySelector`.)
### Callbacks
* `onTweenComplete:Func` - A function that is called when the scroll transition has finished.
* `onTweenTick:Func` - A function that is called during the scroll transition on the way to the `target`.
### Examples
```js
import React, { Component } from 'react';
import TargetScroller from 'react-target-scroller';...
class ExampleComponent extends Compnonent {
constructor(props) {
super(props);this.state = {
scrollTarget: null,
};this.onNavLinkClick = this.onNavLinkClick.bind(this);
}onNavLinkClick(evt) {
const hash = href.indexOf('#') > -1 ? href.split('#')[1] : null;if (!hash) {
return;
}this.setState({
scrollTarget: `#${hash}`,
});
}render() {
const {
scrollTarget,
} = this.state;return (
);
}
}
```## License
[MIT](LICENSE) Β© [Ryan Hefner](https://www.ryanhefner.com)