Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/perf-usescrollflash-raf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

perf(useScrollFlash): defer scroll read/write to requestAnimationFrame to avoid forced reflow
15 changes: 10 additions & 5 deletions packages/react/src/hooks/useScrollFlash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ export default function useScrollFlash(scrollContainerRef: React.RefObject<HTMLE
if (!scrollContainer) {
return
}
const currentScroll = scrollContainer.scrollTop
const maxScroll = scrollContainer.scrollHeight

const altScroll = currentScroll < Math.min(1, maxScroll) ? currentScroll + 1 : currentScroll - 1
// Defer to the next frame to avoid forcing a synchronous reflow
// when the effect runs immediately after React commits DOM changes.
const id = requestAnimationFrame(() => {
const currentScroll = scrollContainer.scrollTop
const altScroll = currentScroll < 1 ? currentScroll + 1 : currentScroll - 1

scrollContainer.scrollTop = altScroll
scrollContainer.scrollTop = currentScroll
scrollContainer.scrollTop = altScroll
scrollContainer.scrollTop = currentScroll
})

return () => cancelAnimationFrame(id)
}, [scrollContainerRef])
}
Loading