Skip to content

Commit 6a853ac

Browse files
committed
Add demo app
1 parent 9870824 commit 6a853ac

25 files changed

+44324
-1260
lines changed

README.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
# React Router Hash Link
22

3-
**_Note that this is for React Router v4/5, for v2/3 see [this solution](https://github.com/rafgraph/react-router-hash-link/tree/react-router-v2/3)._**
3+
[![npm](https://img.shields.io/npm/dm/react-router-hash-link?label=npm)](https://www.npmjs.com/package/react-router-hash-link) [![npm bundle size (version)](https://img.shields.io/bundlephobia/minzip/react-router-hash-link?color=purple)](https://bundlephobia.com/result?p=react-router-hash-link)
44

5-
![npm](https://img.shields.io/npm/dm/react-router-hash-link?label=npm)
5+
This is a solution to [React Router's issue of not scrolling to `#hash-fragments`](https://github.com/reactjs/react-router/issues/394#issuecomment-220221604) when using the `<Link>` component to navigate.
66

7-
[Demo website](https://react-router-hash-link.rafgraph.dev/) (code on the [`gh-pages` branch](https://github.com/rafgraph/react-router-hash-link/tree/gh-pages))
7+
When you click on a link created with `react-router-hash-link` it will scroll to the element on the page with the `id` that matches the `#hash-fragment` in the link. This will also work for elements that are created after an asynchronous data load. Note that you must use React Router's `BrowserRouter` for this to work.
88

99
---
1010

11-
This is a solution to [React Router's issue of not scrolling to `#hash-fragments`](https://github.com/reactjs/react-router/issues/394#issuecomment-220221604) when using the `<Link>` component to navigate.
11+
### [Live demo app for React Router Hash Link](https://react-router-hash-link.rafgraph.dev)
1212

13-
When you click on a link created with `react-router-hash-link` it will scroll to the element on the page with the `id` that matches the `#hash-fragment` in the link. This will also work for elements that are created after an asynchronous data load. Note that you must use React Router's `BrowserRouter` for this to work.
13+
Code is in the [`/demo`](/demo) folder.
14+
15+
---
16+
17+
## Basics
1418

1519
```shell
16-
$ yarn add react-router-hash-link
17-
# OR
18-
$ npm install --save react-router-hash-link
20+
npm install --save react-router-hash-link
1921
```
2022

23+
`react-router-dom` is a peer dependency.
24+
25+
---
26+
2127
### `<HashLink>`
2228

2329
```javascript
@@ -29,6 +35,8 @@ import { HashLink } from 'react-router-hash-link';
2935
<HashLink to="/some/path#with-hash-fragment">Link to Hash Fragment</HashLink>
3036
```
3137

38+
---
39+
3240
### `<NavHashLink>`
3341

3442
```javascript
@@ -46,6 +54,8 @@ import { NavHashLink } from 'react-router-hash-link';
4654
>Link to Hash Fragment</NavHashLink>
4755
```
4856

57+
---
58+
4959
## Scrolling API
5060

5161
### `smooth: boolean`
@@ -61,6 +71,8 @@ import { HashLink } from 'react-router-hash-link';
6171
</HashLink>;
6272
```
6373

74+
---
75+
6476
### `scroll: function`
6577

6678
- Custom scroll function called with the element to scroll to, e.g. `const myScrollFn = element => {...}`
@@ -76,6 +88,8 @@ import { HashLink } from 'react-router-hash-link';
7688
</HashLink>;
7789
```
7890

91+
---
92+
7993
### Scroll to top of page
8094

8195
- To scroll to the top of the page set the hash fragment to `#` (empty) or `#top`
@@ -88,16 +102,22 @@ import { HashLink } from 'react-router-hash-link';
88102
<HashLink to="#top">Link to Top of Page</HashLink>
89103
```
90104

105+
---
106+
91107
### Scroll with offset
92108

93109
- To scroll with offset use a custom scroll function, one way of doing this can be found [here](https://github.com/rafgraph/react-router-hash-link/issues/25#issuecomment-536688104)
94110

111+
---
112+
95113
### `elementId: string`
96114

97115
- Scroll to the element with matching id
98116
- Used instead of providing a hash fragment as part of the `to` prop, if both are present then the `elementId` will override the `to` prop's hash fragment
99117
- Note that it is generally recommended to use the `to` prop's hash fragment instead of the `elementId`
100118

119+
---
120+
101121
## Custom `Link`
102122

103123
The exported components are wrapped versions of the `Link` and `NavLink` exports of react-router-dom. In some cases you may need to provide a custom `Link` implementation.
@@ -118,6 +138,8 @@ const MyComponent = () => (
118138
);
119139
```
120140
141+
---
142+
121143
## Focus Management
122144
123145
`react-router-hash-link` attempts to recreate the native browser focusing behavior as closely as possible.

demo/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SKIP_PREFLIGHT_CHECK=true

demo/.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

demo/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Rafael Pedicini
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

demo/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Demo App for [`react-router-hash-link`](https://github.com/rafgraph/react-router-hash-link)
2+
3+
Live demo app: https://react-router-hash-link.rafgraph.dev

0 commit comments

Comments
 (0)