Skip to content

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

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

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

Conversation

@yanglbme
Copy link
Copy Markdown
Member

Problem

When clicking/focusing the editor area, the CodeMirror editor would jump/scroll automatically, causing a jarring visual experience.

This is caused by CodeMirror 6's default scrollIntoView behavior on focus, which tries to scroll the cursor into the visible viewport.

Solution

Added a focus event handler in the theme extension that preserves the current scroll position when the editor gains focus:

  • packages/shared/src/editor/themes.ts: Added noScrollOnFocus dom event handler that locks scrollTop on focus
  • Added CSS styles: &.cm-focused { outline: none } and .cm-scroller { overscrollBehavior: contain }
  • Applied to both light and dark themes

Disable scrollIntoView behavior when the editor gains focus,
which caused a jarring jump effect on mouse click.
Copilot AI review requested due to automatic review settings May 16, 2026 06:20
@github-actions
Copy link
Copy Markdown

🚀 Cloudflare Workers Preview has been successfully deployed!

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

Built with commit 61fd0ec

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 aims to stop the CodeMirror editor from “jumping” (auto-scrolling) when the editor gains focus, improving the UX when clicking into the editor.

Changes:

  • Added a CodeMirror domEventHandlers focus handler (noScrollOnFocus) intended to preserve scroll position on focus.
  • Updated CodeMirror theme styles to remove focus outline and set overscroll-behavior on the scroller.
  • Applied the new handler/styles to both light and dark editor themes.
Comments suppressed due to low confidence (1)

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

  • Removing the focus outline (&.cm-focused { outline: none }) without providing an alternative visible focus indicator makes keyboard focus hard/impossible to see, which is an accessibility regression. Either keep the outline or replace it with an explicit focus ring style that meets contrast/visibility requirements.
  '&.cm-focused': {
    outline: `none`,
  },

const noScrollOnFocus = EditorView.domEventHandlers({
focus(_event, _view) {
const scrollDOM = _view.scrollDOM
scrollDOM.scrollTop = scrollDOM.scrollTop
Comment on lines +15 to +18
// 禁止编辑器获得焦点时浏览器自动滚动到视图中
'&.cm-focused': {
outline: `none`,
},
@github-actions
Copy link
Copy Markdown

🚀 Surge Preview has been successfully deployed!

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

Built with commit 61fd0ec

@yanglbme yanglbme closed this May 16, 2026
@yanglbme yanglbme deleted the fix/no-scroll-on-focus branch May 16, 2026 06:26
@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