Skip to content

fix: cherry pick of fixing sticky issue #1232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"prettier": "prettier --write \"**/*.{js,jsx,tsx,ts,less,md,json}\"",
"test": "vitest --watch false",
"coverage": "vitest run --coverage",
"prepublishOnly": "npm run compile && np --no-cleanup --yolo --no-publish",
"prepublishOnly": "npm run compile && np --no-cleanup --yolo --no-publish --any-branch",
"lint": "eslint src/ --ext .tsx,.ts",
"tsc": "tsc -p tsconfig.json --noEmit",
"now-build": "npm run docs:build",
Expand Down
24 changes: 20 additions & 4 deletions src/stickyScrollBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
};

const checkScrollBarVisible = () => {
raf.cancel(rafRef.current);

rafRef.current = raf(() => {
if (!scrollBodyRef.current) {
return;
Expand Down Expand Up @@ -144,13 +146,27 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
};
}, [scrollBarWidth, isActive]);

// Loop for scroll event check
React.useEffect(() => {
const onScrollListener = addEventListener(container, 'scroll', checkScrollBarVisible, false);
const onResizeListener = addEventListener(window, 'resize', checkScrollBarVisible, false);
if (!scrollBodyRef.current) return;

const scrollParents: HTMLElement[] = [];
let parent: HTMLElement = scrollBodyRef.current;
while (parent) {
scrollParents.push(parent);
parent = parent.parentElement;
}

scrollParents.forEach(p => p.addEventListener('scroll', checkScrollBarVisible, false));
window.addEventListener('resize', checkScrollBarVisible, false);
window.addEventListener('scroll', checkScrollBarVisible, false);
container.addEventListener('scroll', checkScrollBarVisible, false);

return () => {
onScrollListener.remove();
onResizeListener.remove();
scrollParents.forEach(p => p.removeEventListener('scroll', checkScrollBarVisible));
window.removeEventListener('resize', checkScrollBarVisible);
window.removeEventListener('scroll', checkScrollBarVisible);
container.removeEventListener('scroll', checkScrollBarVisible);
};
}, [container]);

Expand Down