Skip to content

Commit

Permalink
Revert to use body for scroll to top becasue of bug with smoothscroll…
Browse files Browse the repository at this point in the history
…-polyfill
  • Loading branch information
rafgraph committed Oct 10, 2020
1 parent 4e0f79e commit f0992b4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ import { HashLink } from 'react-router-hash-link';
```js
import { HashLink } from 'react-router-hash-link';
<HashLink to="/path#top">Link to Top of Page</HashLink>
// or
<HashLink to="#top">Link to Top of Page</HashLink>
```

### Scroll with offset
Expand Down
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ function reset() {
function getElAndScroll() {
let element = null;
if (hashFragment === '#') {
element = document.documentElement;
// use document.body instead of document.documentElement because of a bug in smoothscroll-polyfill in safari
// see https://github.com/iamdustan/smoothscroll/issues/138
// while smoothscroll-polyfill is not included, it is the recommended way to implement smoothscroll
// in browsers that don't natively support el.scrollIntoView({ behavior: 'smooth' })
element = document.body;
} else {
// check for element with matching id before assume '#top' is the top of the document
// see https://html.spec.whatwg.org/multipage/browsing-the-web.html#target-element
const id = hashFragment.replace('#', '');
element = document.getElementById(id);
if (element === null && hashFragment === '#top') {
element = document.documentElement;
// see above comment for why document.body instead of document.documentElement
element = document.body;
}
}

Expand Down

0 comments on commit f0992b4

Please sign in to comment.