Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(notebooks): avoid selection scans on scroll (#116043)

Marius Andra28d1ec1458f4
diff --git a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.test.ts b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.test.ts
index 796cdd1d14c7..193054e87ec2 100644
--- a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.test.ts
+++ b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.test.ts
@@ -1890,6 +1890,15 @@ Repeated block`),
componentShell.focus()
expect(onCaretChange).toHaveBeenLastCalledWith({ nodeIndex: 1 })
+
+ onCaretChange.mockClear()
+ fireEvent.scroll(window)
+ fireEvent.scroll(componentShell)
+ fireEvent.resize(window)
+ expect(onCaretChange).not.toHaveBeenCalled()
+
+ fireEvent(document, new Event('selectionchange'))
+ expect(onCaretChange).toHaveBeenLastCalledWith({ nodeIndex: 1 })
})
it('does not open editors or steal focus for blocks that mount on load', () => {
diff --git a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx
index f89d878c141f..446baa0a531a 100644
--- a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx
+++ b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx
@@ -3270,7 +3270,7 @@ function MarkdownNotebookEditor({
return
}
- const handleDocumentSelectionChange = (): void => {
+ const updateSelectionToolbar = (): void => {
if (isTextSelectionPointerActiveRef.current) {
setFloatingToolbar(null)
} else {
@@ -3278,6 +3278,10 @@ function MarkdownNotebookEditor({
Math.max(0, floatingToolbarRevealAfterRef.current - Date.now())
)
}
+ }
+
+ const handleDocumentSelectionChange = (): void => {
+ updateSelectionToolbar()
updateSelectedComponentBlocksFromSelection()
// Non-text blocks (queries, dividers, comments…) never produce a text caret, so
// fall back to the focused block — collaborators still see who is on it.
@@ -3292,6 +3296,18 @@ function MarkdownNotebookEditor({
)
}
+ let viewportUpdateFrame: number | null = null
+ const handleViewportChange = (): void => {
+ // Scrolling moves the toolbar, not the selection or a collaborator's caret.
+ if (viewportUpdateFrame !== null || window.getSelection()?.isCollapsed !== false) {
+ return
+ }
+ viewportUpdateFrame = window.requestAnimationFrame(() => {
+ viewportUpdateFrame = null
+ updateSelectionToolbar()
+ })
+ }
+
const handleDocumentPointerStart = (event: MouseEvent | PointerEvent | TouchEvent): void => {
if (event.target instanceof HTMLElement && event.target.closest('.MarkdownNotebook__format-toolbar')) {
return
@@ -3307,8 +3323,8 @@ function MarkdownNotebookEditor({
window.document.addEventListener('mousedown', handleDocumentPointerStart, true)
window.document.addEventListener('pointerdown', handleDocumentPointerStart, true)
window.document.addEventListener('touchstart', handleDocumentPointerStart, true)
- window.addEventListener('resize', handleDocumentSelectionChange)
- window.addEventListener('scroll', handleDocumentSelectionChange, true)
+ window.addEventListener('resize', handleViewportChange)
+ window.addEventListener('scroll', handleViewportChange, { capture: true, passive: true })
return () => {
window.document.removeEventListener('selectionchange', handleDocumentSelectionChange)
@@ -3316,8 +3332,11 @@ function MarkdownNotebookEditor({
window.document.removeEventListener('mousedown', handleDocumentPointerStart, true)
window.document.removeEventListener('pointerdown', handleDocumentPointerStart, true)
window.document.removeEventListener('touchstart', handleDocumentPointerStart, true)
- window.removeEventListener('resize', handleDocumentSelectionChange)
- window.removeEventListener('scroll', handleDocumentSelectionChange, true)
+ window.removeEventListener('resize', handleViewportChange)
+ window.removeEventListener('scroll', handleViewportChange, true)
+ if (viewportUpdateFrame !== null) {
+ window.cancelAnimationFrame(viewportUpdateFrame)
+ }
}
}, [
mode,