diff --git a/web/app/components/root/root.jsx b/web/app/components/root/root.jsx index 7eeb1fb8..4744281c 100644 --- a/web/app/components/root/root.jsx +++ b/web/app/components/root/root.jsx @@ -80,8 +80,7 @@ export default class Root extends Component { if (comment) { setTimeout(() => { - comment.scrollIntoView(); - window.scrollTo(window.scrollX, 0); // sometimes smth goes wrong and iframe scrolls; we return it back + window.parent.postMessage(JSON.stringify({ scrollTo: comment.getBoundingClientRect().top }), '*'); }, 500); } } diff --git a/web/app/embed.js b/web/app/embed.js index f69eb2a1..a411f6c2 100644 --- a/web/app/embed.js +++ b/web/app/embed.js @@ -61,6 +61,16 @@ function init() { if (data.remarkIframeHeight) { iframe.style.height = `${data.remarkIframeHeight}px`; } + + if (data.scrollTo) { + const iframeTop = iframe.getBoundingClientRect().top; + + // sometimes browser is already scrolled to the element (like Safari on macOS) + // so let's check it and prevent our scrolling if it so + if (data.scrollTo + iframeTop === 0) return; + + window.scrollTo(window.pageXOffset, data.scrollTo + iframe.getBoundingClientRect().top); + } } catch (e) {} }