Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): close vite-error-overlay with Escape key #13795

Merged
merged 5 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 15 additions & 5 deletions packages/vite/src/client/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,23 @@ export class ErrorOverlay extends HTMLElement {
this.root.querySelector('.window')!.addEventListener('click', (e) => {
e.stopPropagation()
})

const close = () => {
this.parentNode?.removeChild(this)
document.removeEventListener('keydown', closeOnEsc)
}

this.addEventListener('click', () => {
this.close()
close()
})

const closeOnEsc = (e: KeyboardEvent) => {
if (e.key === 'Escape' || e.code === 'Escape') {
close()
}
}

document.addEventListener('keydown', closeOnEsc)
bluwy marked this conversation as resolved.
Show resolved Hide resolved
}

text(selector: string, text: string, linkFiles = false): void {
Expand Down Expand Up @@ -201,10 +215,6 @@ export class ErrorOverlay extends HTMLElement {
}
}
}

close(): void {
this.parentNode?.removeChild(this)
}
bluwy marked this conversation as resolved.
Show resolved Hide resolved
}

export const overlayId = 'vite-error-overlay'
Expand Down
20 changes: 20 additions & 0 deletions playground/html/__tests__/html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,26 @@ describe.runIf(isServe)('invalid', () => {
expect(message).toMatch(/^Unable to parse HTML/)
})

test('should close overlay when clicked away', async () => {
await page.goto(viteTestUrl + '/invalid.html')
const errorOverlay = await page.waitForSelector('vite-error-overlay')
expect(errorOverlay).toBeTruthy()

await page.click('html')
const isVisbleOverlay = await errorOverlay.isVisible()
expect(isVisbleOverlay).toBeFalsy()
})

test('should close overlay when escape key is pressed', async () => {
await page.goto(viteTestUrl + '/invalid.html')
const errorOverlay = await page.waitForSelector('vite-error-overlay')
expect(errorOverlay).toBeTruthy()

await page.keyboard.press('Escape')
const isVisbleOverlay = await errorOverlay.isVisible()
expect(isVisbleOverlay).toBeFalsy()
})

test('should reload when fixed', async () => {
await page.goto(viteTestUrl + '/invalid.html')
await editFile('invalid.html', (content) => {
Expand Down