Skip to content

Commit

Permalink
Merge pull request #14 from eea/develop
Browse files Browse the repository at this point in the history
fix: Resolve scroll only if element exists
  • Loading branch information
razvanMiu committed Oct 21, 2022
2 parents f247241 + 621fb61 commit c6b85c7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ All notable changes to this project will be documented in this file. Dates are d

Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

### [3.0.2](https://github.com/eea/volto-block-toc/compare/3.0.1...3.0.2) - 20 October 2022
### [3.0.3](https://github.com/eea/volto-block-toc/compare/3.0.2...3.0.3) - 21 October 2022

#### :bug: Bug Fixes

- fix: Make smooth scroll work when components are rendered when visible in viewport [Miu Razvan - [`971dc85`](https://github.com/eea/volto-block-toc/commit/971dc85b0d79baff9a1ee2d768a03e730007d872)]
- fix: Resolve scroll only if element exists [Miu Razvan - [`ceb1e64`](https://github.com/eea/volto-block-toc/commit/ceb1e6494872254b1f5dbbe731e28b03fe74d189)]

### [3.0.2](https://github.com/eea/volto-block-toc/compare/3.0.1...3.0.2) - 20 October 2022

#### :hammer_and_wrench: Others

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eeacms/volto-block-toc",
"version": "3.0.2",
"version": "3.0.3",
"description": "volto-block-toc: Volto add-on",
"main": "src/index.js",
"author": "European Environment Agency: IDM2 A-Team",
Expand Down
4 changes: 4 additions & 0 deletions src/AnchorLink.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ class AnchorLink extends Component {
() => {
// Scroll done
document.removeEventListener('scroll', this.onScroll);
if (!this.link.current) return;
const id = this.link.current.getAttribute('href').slice(1);
const $anchor = document.getElementById(id);
if (!$anchor) return;
const offsetTop =
$anchor.getBoundingClientRect().top +
window.scrollY -
Expand All @@ -54,8 +56,10 @@ class AnchorLink extends Component {
}
smoothScroll(e) {
e.preventDefault();
if (!this.link.current) return;
const id = this.link.current.getAttribute('href').slice(1);
const $anchor = document.getElementById(id);
if (!$anchor) return;
const offsetTop =
$anchor.getBoundingClientRect().top + window.scrollY - this.getOffset();
document.addEventListener('scroll', this.onScroll);
Expand Down

0 comments on commit c6b85c7

Please sign in to comment.