Skip to content

Commit

Permalink
Another try to fix hash navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
psdcoder committed Apr 7, 2020
1 parent 7d0fda8 commit 70a2cb1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/components/MainLayout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ const MainLayout: LayoutComponent = ({
<>
<LayoutHeader modifiers={modifiers} />
<HamburgerMenu />
{children}
<div id="layoutContent" className={styles.pageContent}>
{children}
</div>
<LayoutFooter modifiers={modifiers} />
</>
)
Expand Down
11 changes: 7 additions & 4 deletions src/components/Page/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ export const useAnchorNavigation = () => {
const node = document.querySelector(location.hash)

if (node) {
scrollIntoLayout(node)
allImagesLoadedInContainer(document.body).then(() =>
scrollIntoLayout(node)
)
const contentRoot = document.getElementById('layoutContent')

if (contentRoot) {
allImagesLoadedInContainer(contentRoot).then(() =>
scrollIntoLayout(node)
)
}
}
} else {
document.documentElement.scrollTop = 0
Expand Down
2 changes: 1 addition & 1 deletion src/utils/front/images.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Promise from 'promise-polyfill'

export const getImages = (node: Element) =>
Array.from(node.querySelectorAll('img'))
Array.from(node.querySelectorAll('img')).filter((imgNode) => !!imgNode.src)

export const imageLoaded = (imgNode: HTMLImageElement) => {
if (imgNode.complete && imgNode.naturalWidth !== 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/front/scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ export const scrollIntoLayout = (
const nodeOffset = node.getBoundingClientRect()
const position = htmlNode.scrollTop + nodeOffset.top + (opts?.offset || 0)
const headerHeight = getHeaderHeightAt(position)
const scrollTo = position - headerHeight
const scrollTo = Math.floor(position - headerHeight)

if (!opts?.smooth) {
htmlNode.scrollTop = scrollTo
requestAnimationFrame(() => (htmlNode.scrollTop = scrollTo))
return
}

Expand Down

0 comments on commit 70a2cb1

Please sign in to comment.