Skip to content

Commit 99f5a0c

Browse files
Copilotalexr00
andcommitted
Add requestAnimationFrame throttling and extract threshold constant
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent 030cf1c commit 99f5a0c

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

webviews/editorWebview/overview.tsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,25 @@ export const Overview = (pr: PullRequest) => {
4343
// Initially ensure title is not stuck
4444
title.classList.remove('stuck');
4545

46-
// Use scroll event to detect when title actually becomes sticky
46+
// Small threshold to account for sub-pixel rendering
47+
const STICKY_THRESHOLD = 1;
48+
49+
// Use scroll event with requestAnimationFrame to detect when title becomes sticky
4750
// Check if the title's top position is at the viewport top (sticky position)
51+
let ticking = false;
4852
const handleScroll = () => {
49-
const rect = title.getBoundingClientRect();
50-
// Title is stuck when its top is at position 0 (sticky top: 0)
51-
// Add small threshold to account for sub-pixel rendering
52-
if (rect.top <= 1) {
53-
title.classList.add('stuck');
54-
} else {
55-
title.classList.remove('stuck');
53+
if (!ticking) {
54+
window.requestAnimationFrame(() => {
55+
const rect = title.getBoundingClientRect();
56+
// Title is stuck when its top is at position 0 (sticky top: 0)
57+
if (rect.top <= STICKY_THRESHOLD) {
58+
title.classList.add('stuck');
59+
} else {
60+
title.classList.remove('stuck');
61+
}
62+
ticking = false;
63+
});
64+
ticking = true;
5665
}
5766
};
5867

0 commit comments

Comments
 (0)