Skip to content

Commit

Permalink
feat(client): close vite-error-overlay with Escape key (#13795)
Browse files Browse the repository at this point in the history
  • Loading branch information
chaejunlee authored Jul 13, 2023
1 parent 72a6985 commit 85bdcda
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/vite/src/client/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ const codeframeRE = /^(?:>?\s+\d+\s+\|.*|\s+\|\s*\^.*)\r?\n/gm
const { HTMLElement = class {} as typeof globalThis.HTMLElement } = globalThis
export class ErrorOverlay extends HTMLElement {
root: ShadowRoot
closeOnEsc: (e: KeyboardEvent) => void

constructor(err: ErrorPayload['err'], links = true) {
super()
Expand Down Expand Up @@ -171,9 +172,18 @@ export class ErrorOverlay extends HTMLElement {
this.root.querySelector('.window')!.addEventListener('click', (e) => {
e.stopPropagation()
})

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

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

document.addEventListener('keydown', this.closeOnEsc)
}

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

close(): void {
this.parentNode?.removeChild(this)
document.removeEventListener('keydown', this.closeOnEsc)
}
}

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

0 comments on commit 85bdcda

Please sign in to comment.