Skip to content

Commit 0073d0e

Browse files
committed
Update readme
1 parent 9b79869 commit 0073d0e

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

README.md

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,27 @@ $ npm install --save react-router-hash-link
2222
```javascript
2323
// In YourComponent.js
2424
...
25-
import { HashLink as Link } from 'react-router-hash-link';
25+
import { HashLink } from 'react-router-hash-link';
2626
...
27-
// Use it just like a RRv4/5 <Link> (to can be a string or an object, see RRv4/5 api for details):
28-
<Link to="/some/path#with-hash-fragment">Link to Hash Fragment</Link>
27+
// Use it just like a RRv4/5 <Link> (to can be a string or an object, see RRv4/5 api for details)
28+
<HashLink to="/some/path#with-hash-fragment">Link to Hash Fragment</HashLink>
2929
```
3030

3131

3232
### `<NavHashLink>`
3333
```javascript
3434
// In YourComponent.js
3535
...
36-
import { NavHashLink as NavLink } from 'react-router-hash-link';
36+
import { NavHashLink } from 'react-router-hash-link';
3737
...
38-
// Use it just like a RRv4/5 <NavLink> (see RRv4/5 api for details):
39-
<NavLink
38+
// Use it just like a RRv4/5 <NavLink> (see RRv4/5 api for details)
39+
// It will be active only if both the path and hash fragment match
40+
<NavHashLink
4041
to="/some/path#with-hash-fragment"
4142
activeClassName="selected"
43+
activeStyle={{ color: 'red' }}
4244
// etc...
43-
>Link to Hash Fragment</NavLink>
45+
>Link to Hash Fragment</NavHashLink>
4446
```
4547

4648
## Scrolling API
@@ -49,22 +51,39 @@ import { NavHashLink as NavLink } from 'react-router-hash-link';
4951
- React Router Hash Link uses the native Element method `element.scrollIntoView()` for scrolling, and when the `smooth` prop is present it will call it with the smooth option, `element.scrollIntoView({ behavior: 'smooth' })`
5052
- Note that not all browsers have implemented options for `scrollIntoView` - see [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView) and [Can I Use](https://caniuse.com/#feat=scrollintoview) - there is also a browser [polyfill for smooth scrolling](https://github.com/iamdustan/smoothscroll) which you can install separately so `smooth` will work in all browsers
5153
```js
52-
import { HashLink as Link } from 'react-router-hash-link';
53-
<Link smooth to="/path#hash">Link to Hash Fragment</Link>
54+
import { HashLink } from 'react-router-hash-link';
55+
<HashLink smooth to="/path#hash">Link to Hash Fragment</HashLink>
5456
```
5557

5658
### `scroll: function`
5759
- Custom scroll function called with the element to scroll to, e.g. `const myScrollFn = element => {...}`
5860
- This allows you to do things like scroll with offset, use a specific smooth scrolling library, or pass in your own options to `scrollIntoView`
5961
```js
60-
import { HashLink as Link } from 'react-router-hash-link';
61-
<Link
62+
import { HashLink } from 'react-router-hash-link';
63+
<HashLink
6264
to="/path#hash"
6365
scroll={el => el.scrollIntoView({ behavior: 'instant', block: 'end' })}
64-
>Link to Hash Fragment</Link>
66+
>Link to Hash Fragment</HashLink>
6567
```
6668

67-
### Custom `Link`
69+
### Scroll to top of page
70+
- To scroll to the top of the page set the hash fragment to `#` (empty) or `#top`
71+
- This is inline with the [HTML spec](https://html.spec.whatwg.org/multipage/browsing-the-web.html#target-element), also see [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Linking_to_an_element_on_the_same_page)
72+
```js
73+
import { HashLink } from 'react-router-hash-link';
74+
<HashLink to="/path#top">Link to Top of Page</HashLink>
75+
```
76+
77+
### Scroll with offset
78+
- 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)
79+
80+
### `elementId: string`
81+
- Scroll to the element with matching id
82+
- 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
83+
- Note that it is generally recommended to use the `to` prop's hash fragment instead of the `elementId`
84+
85+
86+
## Custom `Link`
6887

6988
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.
7089

0 commit comments

Comments
 (0)