Skip to content

Commit 7b15a40

Browse files
committed
Format with prettier
1 parent e6341a8 commit 7b15a40

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

README.md

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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+
**_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)._**
44

55
![npm](https://img.shields.io/npm/dm/react-router-hash-link?label=npm)
66

@@ -19,6 +19,7 @@ $ npm install --save react-router-hash-link
1919
```
2020

2121
### `<HashLink>`
22+
2223
```javascript
2324
// In YourComponent.js
2425
...
@@ -28,15 +29,15 @@ import { HashLink } from 'react-router-hash-link';
2829
<HashLink to="/some/path#with-hash-fragment">Link to Hash Fragment</HashLink>
2930
```
3031

31-
3232
### `<NavHashLink>`
33+
3334
```javascript
3435
// In YourComponent.js
3536
...
3637
import { NavHashLink } from 'react-router-hash-link';
3738
...
3839
// 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
4041
<NavHashLink
4142
to="/some/path#with-hash-fragment"
4243
activeClassName="selected"
@@ -46,29 +47,40 @@ import { NavHashLink } from 'react-router-hash-link';
4647
```
4748

4849
## Scrolling API
50+
4951
### `smooth: boolean`
52+
5053
- Smooth scroll to the element
5154
- 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' })`
5255
- 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+
5357
```js
5458
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>;
5662
```
5763

5864
### `scroll: function`
65+
5966
- Custom scroll function called with the element to scroll to, e.g. `const myScrollFn = element => {...}`
6067
- This allows you to do things like scroll with offset, use a specific smooth scrolling library, or pass in your own options to `scrollIntoView`
68+
6169
```js
6270
import { HashLink } from 'react-router-hash-link';
6371
<HashLink
64-
to="/path#hash"
65-
scroll={el => el.scrollIntoView({ behavior: 'instant', block: 'end' })}
66-
>Link to Hash Fragment</HashLink>
72+
to="/path#hash"
73+
scroll={(el) => el.scrollIntoView({ behavior: 'instant', block: 'end' })}
74+
>
75+
Link to Hash Fragment
76+
</HashLink>;
6777
```
6878

6979
### Scroll to top of page
80+
7081
- To scroll to the top of the page set the hash fragment to `#` (empty) or `#top`
7182
- 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+
7284
```js
7385
import { HashLink } from 'react-router-hash-link';
7486
<HashLink to="/path#top">Link to Top of Page</HashLink>
@@ -77,14 +89,15 @@ import { HashLink } from 'react-router-hash-link';
7789
```
7890

7991
### Scroll with offset
92+
8093
- 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)
8194

8295
### `elementId: string`
96+
8397
- Scroll to the element with matching id
8498
- 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
8599
- Note that it is generally recommended to use the `to` prop's hash fragment instead of the `elementId`
86100

87-
88101
## Custom `Link`
89102

90103
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 = () => (
110123
`react-router-hash-link` attempts to recreate the native browser focusing behavior as closely as possible.
111124
112125
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.
114128
- If the target element is focusable (interactive elements and elements with a `tabindex`), then the target element is focused.
115129
116130
To recreate this `react-router-hash-link` does the following:
131+
117132
- 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.
118133
- For focusable elements, it calls `element.focus()` and leaves focus on the target element.
119134

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ function reset() {
1919
function isInteractiveElement(element) {
2020
const formTags = ['BUTTON', 'INPUT', 'SELECT', 'TEXTAREA'];
2121
const linkTags = ['A', 'AREA'];
22-
return (formTags.includes(element.tagName) && !element.hasAttribute('disabled'))
23-
|| (linkTags.includes(element.tagName) && element.hasAttribute('href'));
22+
return (
23+
(formTags.includes(element.tagName) && !element.hasAttribute('disabled')) ||
24+
(linkTags.includes(element.tagName) && element.hasAttribute('href'))
25+
);
2426
}
2527

2628
function getElAndScroll() {

0 commit comments

Comments
 (0)