fix(editor): prevent editor from scrolling on focus#1545
Closed
yanglbme wants to merge 1 commit into
Closed
Conversation
Disable scrollIntoView behavior when the editor gains focus, which caused a jarring jump effect on mouse click.
|
🚀 Cloudflare Workers Preview has been successfully deployed! Preview URL: https://md-pr-1545.doocs.workers.dev Built with commit 61fd0ec |
Contributor
There was a problem hiding this comment.
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
domEventHandlersfocus handler (noScrollOnFocus) intended to preserve scroll position on focus. - Updated CodeMirror theme styles to remove focus outline and set
overscroll-behavioron 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`, | ||
| }, |
|
🚀 Surge Preview has been successfully deployed! Preview URL: https://doocs-md-preview-pr-1545.surge.sh Built with commit 61fd0ec |
|
🗑️ Cloudflare Workers preview deployment has been cleaned up. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
scrollIntoViewbehavior on focus, which tries to scroll the cursor into the visible viewport.Solution
Added a
focusevent handler in the theme extension that preserves the current scroll position when the editor gains focus:noScrollOnFocusdom event handler that locksscrollTopon focus&.cm-focused { outline: none }and.cm-scroller { overscrollBehavior: contain }