Skip to content

fix: stop preview code from overlapping the line numbers - #595

Open
faisalahammad wants to merge 1 commit into
codesnippetspro:release/v4.1.2/corefrom
faisalahammad:fix/448-preview-editor-init/core
Open

faisalahammad wants to merge 1 commit into
codesnippetspro:release/v4.1.2/corefrom
faisalahammad:fix/448-preview-editor-init/core

Conversation

@faisalahammad

@faisalahammad faisalahammad commented Oct 13, 2026 •

Copy link
Copy Markdown

Summary

In the snippet preview modal, the code could sit on top of the line numbers. The modal scales in from 90% as it opens, and CodeMirror is set up during that animation, so it measures the gutter and character widths at the smaller scale. A CSS transform does not trigger a resize, so CodeMirror never measures again. This PR refreshes the editor once the modal finishes its opening animation.

Fixes #448

Changes

SnippetPreviewModal.tsx

Before:

instance.codemirror.getInputField().setAttribute('aria-label', __('Snippet code preview', 'code-snippets'))

return () => (instance.codemirror as EditorFromTextArea).toTextArea()

After:

instance.codemirror.getInputField().setAttribute('aria-label', __('Snippet code preview', 'code-snippets'))

const modalFrame = textareaRef.current.closest('.components-modal__frame')
const refresh = () => instance.codemirror.refresh()
modalFrame?.addEventListener('animationend', refresh)

return () => {
	(instance.codemirror as EditorFromTextArea).toTextArea()
	modalFrame?.removeEventListener('animationend', refresh)
}

Why: refresh() makes CodeMirror measure again at the final size. When reduced motion is on there is no animation, and there is no bug either. The CodeMirror 6 work in #557 handles the same case with requestMeasure on animationend.

tests/e2e/code-snippets-preview.spec.ts

Added "Preview code does not overlap the line numbers". It checks that the first line number ends before the first code line starts, with 1px allowed for sub-pixel rounding.

Testing

Test 1: Preview alignment

  1. Make sure Syntax Highlighting is on in your profile.
  2. Go to Snippets > All Snippets and open Preview on a few snippets, in card and table view.
  3. Open and close it 10 to 20 times. In DevTools, set CPU throttling to 4x to make the old bug easier to hit.
  4. Repeat with a snippet of 100+ lines.

Result: the line numbers stay in their own column and the code never overlaps them.

Test 2: Fallback and keyboard

  1. Turn Syntax Highlighting off and open Preview.
  2. Turn it back on, Tab into the code and select with Shift+Arrow, then Tab out.

Result: the fallback textarea shows with no console errors. Selection works and the code is not editable.

ESLint, Stylelint and the build pass.

The preview modal scales in as it opens, and CodeMirror was measuring the
gutter and character widths while the modal was still scaled down. A
transform does not trigger a resize, so CodeMirror never measured again and
the code could sit on top of the line numbers.

Refresh the editor when the modal frame finishes its animation.

Fixes codesnippetspro#448
@imantsk
imantsk changed the base branch from core-beta to release/v4.1.2/core November 27, 2026 17:22
@imantsk imantsk added the run-tests Trigger automated tests label Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-tests Trigger automated tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix intermittent CodeMirror editor initialization in snippet preview modal

2 participants