diff --git a/src/js/components/common/snippets/SnippetPreviewModal.tsx b/src/js/components/common/snippets/SnippetPreviewModal.tsx index 160535a61..5d8247be3 100644 --- a/src/js/components/common/snippets/SnippetPreviewModal.tsx +++ b/src/js/components/common/snippets/SnippetPreviewModal.tsx @@ -132,8 +132,16 @@ export const PreviewModal: React.FC = ({ onRequestClose, titl // 5.59, while WordPress 5.5 ships 5.29, so label the input directly. instance.codemirror.getInputField().setAttribute('aria-label', __('Snippet code preview', 'code-snippets')) + // The modal scales in as it opens. CodeMirror measures the gutter and character + // widths against that scale, and a transform does not resize anything that would + // prompt it to measure again, so the code can end up overlapping the line numbers. + 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) } }, [type]) diff --git a/tests/e2e/code-snippets-preview.spec.ts b/tests/e2e/code-snippets-preview.spec.ts index 68b6fe3d4..a4953e3c9 100644 --- a/tests/e2e/code-snippets-preview.spec.ts +++ b/tests/e2e/code-snippets-preview.spec.ts @@ -6,6 +6,8 @@ import type { Locator, Page } from '@playwright/test' const MAXIMUM_FOCUS_ATTEMPTS = 10 const MAXIMUM_BADGE_ALIGNMENT_OFFSET = 4 +// Allows for sub-pixel rounding between the line number and code boxes. +const MAXIMUM_SUBPIXEL_OVERLAP = 1 const PREVIEW_VIEWPORT_WIDTHS = [1280, 640] const CONTROL_HEIGHT = 38 @@ -131,6 +133,25 @@ test.describe('Code Snippets Preview Modal', () => { await expect(editor.locator('[aria-label="Snippet code preview"]')).toBeAttached() }) + // The modal scales in as it opens, and an editor measured during that animation + // would place its line numbers over the start of the code. + test('Preview code does not overlap the line numbers', async ({ page }) => { + const editor = await openPreviewEditor(page) + const lineNumber = editor.locator('.CodeMirror-linenumber').first() + const line = editor.locator('.CodeMirror-line').first() + + await expect(async () => { + const [lineNumberBox, lineBox] = await Promise.all([lineNumber.boundingBox(), line.boundingBox()]) + + expect(lineNumberBox).not.toBeNull() + expect(lineBox).not.toBeNull() + + if (lineNumberBox && lineBox) { + expect(lineNumberBox.x + lineNumberBox.width).toBeLessThanOrEqual(lineBox.x + MAXIMUM_SUBPIXEL_OVERLAP) + } + }).toPass({ timeout: TIMEOUTS.SHORT }) + }) + test('Preview type badge sits in the header before the close button', async ({ page }) => { await openPreviewEditor(page)