-
Notifications
You must be signed in to change notification settings - Fork 0
⚡ Bolt: Optimize scroll event listeners #85
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
Changes from all commits
6a73da7
004e298
57543d6
0cd47a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,15 +16,27 @@ export default function DynamicHeaderWrapper({ navigation }: Readonly<DynamicHea | |
| const [scroll, setScroll] = useState<boolean>(false); | ||
|
|
||
| React.useEffect(() => { | ||
| const state = { rafId: 0, isTicking: false }; | ||
|
|
||
| const handleScroll = (): void => { | ||
| const scrollCheck: boolean = window.scrollY > 100; | ||
| if (scrollCheck !== scroll) { | ||
| setScroll(scrollCheck); | ||
| if (!state.isTicking) { | ||
| state.rafId = window.requestAnimationFrame(() => { | ||
| const scrollCheck: boolean = window.scrollY > 100; | ||
| if (scrollCheck !== scroll) { | ||
| setScroll(scrollCheck); | ||
| } | ||
| state.isTicking = false; | ||
| }); | ||
| state.isTicking = true; | ||
| } | ||
| }; | ||
| document.addEventListener("scroll", handleScroll); | ||
|
|
||
| document.addEventListener("scroll", handleScroll, { passive: true }); | ||
| return () => { | ||
| document.removeEventListener("scroll", handleScroll); | ||
| if (state.rafId) { | ||
| window.cancelAnimationFrame(state.rafId); | ||
| } | ||
| }; | ||
| }, [scroll]); | ||
|
Comment on lines
18
to
41
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The The dependency on |
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,17 +79,29 @@ export default function Layout({ headerStyle, footerStyle, breadcrumbTitle: _bre | |
|
|
||
| useEffect(() => { | ||
| AOS.init(); | ||
|
|
||
| const state = { rafId: 0, isTicking: false }; | ||
|
|
||
| const handleScroll = (): void => { | ||
| const scrollCheck: boolean = window.scrollY > 100; | ||
| if (scrollCheck !== scroll) { | ||
| setScroll(scrollCheck); | ||
| if (!state.isTicking) { | ||
| state.rafId = window.requestAnimationFrame(() => { | ||
| const scrollCheck: boolean = window.scrollY > 100; | ||
| if (scrollCheck !== scroll) { | ||
| setScroll(scrollCheck); | ||
| } | ||
| state.isTicking = false; | ||
| }); | ||
| state.isTicking = true; | ||
| } | ||
| }; | ||
|
|
||
| document.addEventListener("scroll", handleScroll); | ||
| document.addEventListener("scroll", handleScroll, { passive: true }); | ||
|
|
||
| return () => { | ||
| document.removeEventListener("scroll", handleScroll); | ||
| if (state.rafId) { | ||
| window.cancelAnimationFrame(state.rafId); | ||
| } | ||
| }; | ||
| }, [scroll]); | ||
|
Comment on lines
80
to
106
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The The dependency on |
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initialize
hasScrolledonce after subscribing.If the page mounts with
scrollY > 100already set, this stays hidden until the next scroll event. Run the handler once during setup so the initial visibility matches the current position.Suggested fix
window.addEventListener("scroll", onScroll, { passive: true }); + onScroll(); return () => { window.removeEventListener("scroll", onScroll); if (state.rafId) { window.cancelAnimationFrame(state.rafId); }📝 Committable suggestion
🤖 Prompt for AI Agents