Skip to content

Commit

Permalink
detect body resize with ResizeObserver at PageProgressBar (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyesungoh authored Jun 20, 2022
1 parent 3d7b90a commit ec9ccd0
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/core/components/PageProgressBar/PageProgressBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,28 @@ export function PageProgressBar() {
}

function setPageOffsetHeight() {
setOffsetHeight(window.document.body.offsetHeight - window.innerHeight);
const diff = window.document.body.offsetHeight - window.innerHeight;
setOffsetHeight(diff);
}

setScroll();
setPageOffsetHeight();
function setScrollAndOffsetHeight() {
setScroll();
setPageOffsetHeight();
}

setScrollAndOffsetHeight();

const resizeObserver = new ResizeObserver(setScrollAndOffsetHeight);
resizeObserver.observe(window.document.body);

window.addEventListener('scroll', setScroll);
window.addEventListener('resize', setPageOffsetHeight);
// for window height changed
window.addEventListener('resize', setScrollAndOffsetHeight);

return () => {
resizeObserver.unobserve(window.document.body);
window.removeEventListener('scroll', setScroll);
window.removeEventListener('resize', setPageOffsetHeight);
window.removeEventListener('resize', setScrollAndOffsetHeight);
};
}, []);

Expand Down

2 comments on commit ec9ccd0

@vercel
Copy link

@vercel vercel bot commented on ec9ccd0 Jun 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

comet-land-blog – ./apps/blog

comet-land-blog.vercel.app
comet-land-blog-git-main-hyesungoh.vercel.app
comet-land-blog-hyesungoh.vercel.app

@vercel
Copy link

@vercel vercel bot commented on ec9ccd0 Jun 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

comet-land-resume – ./apps/resume

comet-land-resume-git-main-hyesungoh.vercel.app
comet-land-resume-hyesungoh.vercel.app
comet-land-resume.vercel.app

Please sign in to comment.