Skip to content

fix(editor): prevent editor from scrolling on focus#1546

Closed
yanglbme wants to merge 1 commit into
mainfrom
fix/no-scroll-on-focus-v2
Closed

fix(editor): prevent editor from scrolling on focus#1546
yanglbme wants to merge 1 commit into
mainfrom
fix/no-scroll-on-focus-v2

Conversation

@yanglbme
Copy link
Copy Markdown
Member

Problem

When clicking/focusing the editor area, the CodeMirror editor would jump/scroll automatically due to CodeMirror 6's default scrollIntoView behavior on focus.

Solution

Added a focus event handler that:

  1. Captures scrollTop and scrollLeft before CodeMirror's automatic scroll
  2. Restores them via requestAnimationFrame after CodeMirror finishes its scrollIntoView

Also added CSS styles:

  • &.cm-focused { outline: none } - removes focus outline
  • .cm-scroller { overscrollBehavior: contain } - prevents scroll chaining

Copilot AI review requested due to automatic review settings May 16, 2026 06:26
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 16, 2026

🚀 Cloudflare Workers Preview has been successfully deployed!

Preview URL: https://md-pr-1546.doocs.workers.dev

Built with commit 03bd95d

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 16, 2026

🚀 Surge Preview has been successfully deployed!

Preview URL: https://doocs-md-preview-pr-1546.surge.sh

Built with commit 03bd95d

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts the shared CodeMirror 6 theme/extensions to stop the editor from “jumping” its scroll position when the editor receives focus, aiming to neutralize CodeMirror’s default scrollIntoView behavior on focus.

Changes:

  • Added a focus DOM event handler extension that restores the scroller’s scrollTop/scrollLeft on the next animation frame.
  • Added CodeMirror theme rules to remove the focused outline and to set overscrollBehavior: contain on the scroller.
  • Included the new event handler extension in both light and dark theme extension arrays.
Comments suppressed due to low confidence (1)

packages/shared/src/editor/themes.ts:23

  • Removing the focus outline on .cm-focused eliminates the default visible focus indicator. Unless another focus style is applied elsewhere, this is an accessibility regression for keyboard users (WCAG focus visible). Prefer keeping the outline or replacing it with an explicit, theme-consistent focus ring (e.g., box-shadow/border using existing CSS variables).
  // 移除聚焦时的蓝色边框
  '&.cm-focused': {
    outline: `none`,
  },

Comment thread packages/shared/src/editor/themes.ts Outdated
Comment on lines +7 to +16
const noScrollOnFocus = EditorView.domEventHandlers({
focus(_event, _view) {
const scrollDOM = _view.scrollDOM
const { scrollTop, scrollLeft } = scrollDOM
requestAnimationFrame(() => {
scrollDOM.scrollTop = scrollTop
scrollDOM.scrollLeft = scrollLeft
})
return false
},
Comment thread packages/shared/src/editor/themes.ts Outdated
Comment on lines +8 to +9
focus(_event, _view) {
const scrollDOM = _view.scrollDOM
Capture scroll position before focus and restore it via
requestAnimationFrame to undo CodeMirror's automatic scrollIntoView.
@yanglbme yanglbme force-pushed the fix/no-scroll-on-focus-v2 branch from 987fcca to 03bd95d Compare May 16, 2026 07:13
@yanglbme yanglbme closed this May 16, 2026
@yanglbme yanglbme deleted the fix/no-scroll-on-focus-v2 branch May 16, 2026 07:15
@github-actions
Copy link
Copy Markdown

🗑️ Cloudflare Workers preview deployment has been cleaned up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants