You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+24-9Lines changed: 24 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# React Router Hash Link
2
2
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
+
**_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)._**
// 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
+
// It will be active only if both the path and hash fragment match
40
41
<NavHashLink
41
42
to="/some/path#with-hash-fragment"
42
43
activeClassName="selected"
@@ -46,29 +47,40 @@ import { NavHashLink } from 'react-router-hash-link';
46
47
```
47
48
48
49
## Scrolling API
50
+
49
51
### `smooth: boolean`
52
+
50
53
- Smooth scroll to the element
51
54
- 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' })`
52
55
- 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
56
+
53
57
```js
54
58
import { HashLink } from'react-router-hash-link';
55
-
<HashLink smooth to="/path#hash">Link to Hash Fragment</HashLink>
59
+
<HashLink smooth to="/path#hash">
60
+
Link to Hash Fragment
61
+
</HashLink>;
56
62
```
57
63
58
64
### `scroll: function`
65
+
59
66
- Custom scroll function called with the element to scroll to, e.g. `const myScrollFn = element => {...}`
60
67
- This allows you to do things like scroll with offset, use a specific smooth scrolling library, or pass in your own options to `scrollIntoView`
- To scroll to the top of the page set the hash fragment to `#` (empty) or `#top`
71
82
- 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)
83
+
72
84
```js
73
85
import { HashLink } from'react-router-hash-link';
74
86
<HashLink to="/path#top">Link to Top of Page</HashLink>
@@ -77,14 +89,15 @@ import { HashLink } from 'react-router-hash-link';
77
89
```
78
90
79
91
### Scroll with offset
92
+
80
93
- 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)
81
94
82
95
### `elementId: string`
96
+
83
97
- Scroll to the element with matching id
84
98
- 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
85
99
- Note that it is generally recommended to use the `to` prop's hash fragment instead of the `elementId`
86
100
87
-
88
101
## Custom `Link`
89
102
90
103
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.
@@ -110,10 +123,12 @@ const MyComponent = () => (
110
123
`react-router-hash-link` attempts to recreate the native browser focusing behavior as closely as possible.
111
124
112
125
The browser native behavior when clicking a hash link is:
113
-
- If the target element is not focusable, then focus is _moved_ to the target element, but the target element is not focused.
126
+
127
+
- If the target element is not focusable, then focus is _moved_ to the target element, but the target element is not focused.
114
128
- If the target element is focusable (interactive elements and elements with a `tabindex`), then the target element is focused.
115
129
116
130
To recreate this `react-router-hash-link` does the following:
131
+
117
132
- For non-focusable elements, it calls `element.focus()` followed by `element.blur()` (using a temporary `tabindex` to ensure that the element can be focused programmatically) so that focus _moves_ to the target element but does not remain on it or trigger any style changes.
118
133
- For focusable elements, it calls `element.focus()` and leaves focus on the target element.
0 commit comments