From fe9de056df7ee1a65ca59d04e3e50434327bc27c Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Sun, 25 Mar 2018 22:22:43 +0300 Subject: [PATCH] Fix scrollTo function: targetEl is now has priority on location hash --- src/components/ui/scroll/scroll.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/components/ui/scroll/scroll.js b/src/components/ui/scroll/scroll.js index 17995d7..a7d316c 100644 --- a/src/components/ui/scroll/scroll.js +++ b/src/components/ui/scroll/scroll.js @@ -106,24 +106,27 @@ function normalizeScrollPosition(y: number): number { * @param {?HTMLElement} targetEl - the element to scroll to */ export function restoreScroll(targetEl: ?HTMLElement = null) { - const {hash} = location; - + const { hash } = window.location; setTimeout(() => { isFirstScroll = false; - const id = hash.replace('#', ''); - const el = id ? document.getElementById(id) : targetEl; - const viewPort = document.body; + if (targetEl === null) { + const id = hash.substr(1); + if (!id) { + return; + } + targetEl = document.getElementById(id); + } + + const viewPort = document.body; if (!viewPort) { console.log('Can not find viewPort element'); // eslint-disable-line return; } let y = 0; - - if (el) { - const {top} = el.getBoundingClientRect(); - + if (targetEl) { + const { top } = targetEl.getBoundingClientRect(); y = getScrollTop() + top - SCROLL_ANCHOR_OFFSET; }