An open API service indexing awesome lists of open source software.

https://github.com/tomassestak/react-spring-scroll-to-hook

Simple custom hook for scrollTo functionality in react-spring v9
https://github.com/tomassestak/react-spring-scroll-to-hook

custom-hook hook react-hook react-spring scrollto-functionality

Last synced: 11 months ago
JSON representation

Simple custom hook for scrollTo functionality in react-spring v9

Awesome Lists containing this project

README

          

# react-spring-scroll-to-hook

> Simple custom hook for ScrollTo functionality using react-spring v9+.

[![NPM](https://img.shields.io/npm/v/react-spring-scroll-to-hook.svg)](https://www.npmjs.com/package/react-spring-scroll-to-hook) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

## Install

```bash
npm install react-spring-scroll-to-hook
```

```bash
yarn add react-spring-scroll-to-hook
```

## Usage

useScrollTo hook accepts 1 argument a that is react-spring config: https://react-spring.io/common/configs
scrollTo function returned from useScrollTo hook accepts 2 arguments:

1: Element | string | number (number of pixels from top of the window)
2: Number (offset in pixels from element you want to scroll to)

I don't know how three shaking works but I would recommend using @react-spring/web (used in this hook) instead of react-spring

## Examples

```tsx

import {config} from '@react-spring/web'
import useScrollTo from 'react-spring-scroll-to-hook'

//Scroll to element
const BasicExampleWthConfig = () => {
const {scrollTo} = useScrollTo(config.molasses)
return (
scrollTo(document.querySelector('#id'))}>
Scroll to Element with predefined config from react-spring

)
}

//Scroll to top
const ScrollToTop = () => {
const {scrollTo} = useScrollTo({ mass: 1, tension: 180, friction: 12 })
return (

Scroll to the top of the page with your config

)
}

//Scroll to top with offset and first argument as string only (document.querySelector is used in the hook)
const ScrollToTopWithOffset = () => {
const {scrollTo} = useScrollTo()
return (
scrollTo('#id', 300)}>
Scroll to the top of the page with default config and offset from element

)
}

```

## License

MIT © [TomasSestak](https://github.com/TomasSestak)