Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/js/components/common/snippets/SnippetPreviewModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,16 @@ export const PreviewModal: React.FC<PreviewModalProps> = ({ 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])

Expand Down
21 changes: 21 additions & 0 deletions tests/e2e/code-snippets-preview.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down
Loading