# react-scrollable-anchor

> Provide smooth scrolling anchors in React.

Latest version **0.6.1** (published 2018-02-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-scrollable-anchor
pnpm add react-scrollable-anchor
yarn add react-scrollable-anchor
bun add react-scrollable-anchor
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.1 |
| Published | 2018-02-05 |
| First published | 2016-12-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/react-scrollable-anchor) |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 287 |
| Author | Gabe G'Sell |
| Maintainers | gabergg |
| Keywords | react, reactjs, scroll, hash, scrollable, scrolling, anchor, anchors, single, page, app, static, site, jump |

## Links

- npm: https://www.npmjs.com/package/react-scrollable-anchor
- Repository: https://github.com/gabergg/react-scrollable-anchor
- Issues: https://github.com/gabergg/react-scrollable-anchor/issues
- npm.io page: https://npm.io/package/react-scrollable-anchor

## Dependencies (2)

- [jump.js](https://npm.io/package/jump.js.md) 1.0.1
- [prop-types](https://npm.io/package/prop-types.md) ^15.5.10

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.6.1 (latest) — 2018-02-05
- 0.6.0 — 2018-02-05
- 0.5.0 — 2017-08-15
- 0.4.2 — 2017-04-01
- 0.4.1 — 2017-01-08
- 0.4.0 — 2017-01-08
- 0.3.0 — 2016-12-31
- 0.2.0 — 2016-12-29
- 0.1.2 — 2016-12-27
- 0.1.1 — 2016-12-24
- 0.1.0 — 2016-12-24

## README

react-scrollable-anchor
=====================

[![npm version](https://img.shields.io/npm/v/react-scrollable-anchor.svg?style=flat-square)](https://www.npmjs.com/package/react-scrollable-anchor)

Lightweight library for smooth scrolling anchors in React, tied to URL hash.

* Land on correct anchor when page is loaded, based on URL hash value.
* Scroll smoothly to anchors when URL hash changes. Easy links to sections with `<a href='#sectionId'>`.
* URL hash updates automatically to reflect section in view
* Option to record history on hash changes

```js
npm install --save react-scrollable-anchor
```

## Examples

[Live Demo](http://gabegsell.com/anchors/) or [Source](https://github.com/gabergg/react-scrollable-anchor/tree/master/example/src/components)

To run examples locally, `npm run example`, then open your
browser to localhost:3210.

## Usage

### 1. Creating a scrollable anchor

Use the `ScrollableAnchor` tag to wrap any React element, making it a scrollable anchor.

```js
import React, { Component } from 'react'
import ScrollableAnchor from 'react-scrollable-anchor'

export default class Page extends Component {
  render() {
    return (
      <div>
        <a href='#section1'> Go to section 1 </a>
        <a href='#section2'> Go to section 2 </a>
        <ScrollableAnchor id={'section1'}>
          <div> Hello World </div>
        </ScrollableAnchor>
        <ScrollableAnchor id={'section2'}>
          <div> How are you world? </div>
        </ScrollableAnchor>
      </div>
    )
  }
}
```

### 2. Configure

Access configureAnchors to customize scrolling and anchors.

##### Offset all scrollable anchors by a fixed amount

```js
import { configureAnchors } from 'react-scrollable-anchor'

// Offset all anchors by -60 to account for a fixed header
// and scroll more quickly than the default 400ms
configureAnchors({offset: -60, scrollDuration: 200})
```

##### Options:

| option                | default          |
| --------------------  | ---------------- |
| `offset`              | `0`              |
| `scrollDuration`      | `400`            |
| `keepLastAnchorHash`  | `false`          |

### 3. Utilities

A small toolkit of scrolling utilies for use with anchors

##### Jump to top of page in a way that plays nicely with scrollable anchors

```js
import { goToTop } from 'react-scrollable-anchor'

// scroll to top of the page
goToTop()
```

##### Scroll to any scrollable anchor, with option to record history

```js
import { goToAnchor } from 'react-scrollable-anchor'

// scroll to #section1 without saving that hash update in history
goToAnchor('section1')
goToAnchor('section1', false)

// scroll to #section1, saving that hash update in history
goToAnchor('section1', true)
```

##### Clear the URL hash without affecting scroll location at all

```js
import { removeHash } from 'react-scrollable-anchor'

// clear URL hash
removeHash()
```

## Issues and feature requests

Please open issues on [Github](https://github.com/gabergg/react-scrollable-anchor/issues). Issues are easier to address if you include context and code samples.

## Contributing

Please contribute!

## Feedback or contact

Feel free to contact me at gabergg@gmail.com.

---
_Source: https://npm.io/package/react-scrollable-anchor · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
