Skip to content

Commit a88fb0b

Browse files
Copilotalexr00
andcommitted
Address code review: add constant and use requestAnimationFrame
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent e52df1b commit a88fb0b

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

webviews/editorWebview/overview.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,27 @@ const useMediaQuery = (query: string) => {
2929
return matches;
3030
};
3131

32+
const STICKY_THRESHOLD = 80;
33+
3234
export const Overview = (pr: PullRequest) => {
3335
const isSingleColumnLayout = useMediaQuery('(max-width: 768px)');
3436
const [isSticky, setIsSticky] = React.useState(false);
3537

3638
React.useEffect(() => {
39+
let ticking = false;
40+
3741
const handleScroll = () => {
38-
// Make header sticky when scrolled past 80px
39-
const scrolled = window.scrollY > 80;
40-
setIsSticky(scrolled);
42+
if (!ticking) {
43+
window.requestAnimationFrame(() => {
44+
const scrolled = window.scrollY > STICKY_THRESHOLD;
45+
setIsSticky(scrolled);
46+
ticking = false;
47+
});
48+
ticking = true;
49+
}
4150
};
4251

43-
window.addEventListener('scroll', handleScroll);
52+
window.addEventListener('scroll', handleScroll, { passive: true });
4453
return () => window.removeEventListener('scroll', handleScroll);
4554
}, []);
4655

0 commit comments

Comments
 (0)