Unverified Commit be5dedf6 authored by John Pham's avatar John Pham Committed by GitHub

Allowing hiding the ReactDevOverlay (#37417)

* Add hide button

* Update label

* Add data attributes for testing

* Add e2e tests

* update test
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 6914a25d
import * as React from 'react' import * as React from 'react'
import { CloseIcon } from '../../icons/CloseIcon'
export type LeftRightDialogHeaderProps = { export type LeftRightDialogHeaderProps = {
className?: string className?: string
...@@ -147,34 +148,14 @@ const LeftRightDialogHeader: React.FC<LeftRightDialogHeaderProps> = ...@@ -147,34 +148,14 @@ const LeftRightDialogHeader: React.FC<LeftRightDialogHeaderProps> =
</nav> </nav>
{close ? ( {close ? (
<button <button
data-nextjs-errors-dialog-left-right-close-button
ref={buttonClose} ref={buttonClose}
type="button" type="button"
onClick={close} onClick={close}
aria-label="Close" aria-label="Close"
> >
<span aria-hidden="true"> <span aria-hidden="true">
<svg <CloseIcon />
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M18 6L6 18"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
/>
<path
d="M6 6L18 18"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
</span> </span>
</button> </button>
) : null} ) : null}
......
...@@ -17,6 +17,7 @@ import { Toast } from '../components/Toast' ...@@ -17,6 +17,7 @@ import { Toast } from '../components/Toast'
import { getErrorByType, ReadyRuntimeError } from '../helpers/getErrorByType' import { getErrorByType, ReadyRuntimeError } from '../helpers/getErrorByType'
import { isNodeError } from '../helpers/nodeStackFrames' import { isNodeError } from '../helpers/nodeStackFrames'
import { noop as css } from '../helpers/noop-template' import { noop as css } from '../helpers/noop-template'
import { CloseIcon } from '../icons/CloseIcon'
import { RuntimeError } from './RuntimeError' import { RuntimeError } from './RuntimeError'
export type SupportedErrorEvent = { export type SupportedErrorEvent = {
...@@ -136,7 +137,9 @@ export const Errors: React.FC<ErrorsProps> = function Errors({ errors }) { ...@@ -136,7 +137,9 @@ export const Errors: React.FC<ErrorsProps> = function Errors({ errors }) {
} }
}, [nextError]) }, [nextError])
const [isMinimized, setMinimized] = React.useState<boolean>(false) const [displayState, setDisplayState] = React.useState<
'minimized' | 'fullscreen' | 'hidden'
>('fullscreen')
const [activeIdx, setActiveIndex] = React.useState<number>(0) const [activeIdx, setActiveIndex] = React.useState<number>(0)
const previous = React.useCallback((e?: MouseEvent | TouchEvent) => { const previous = React.useCallback((e?: MouseEvent | TouchEvent) => {
e?.preventDefault() e?.preventDefault()
...@@ -162,19 +165,23 @@ export const Errors: React.FC<ErrorsProps> = function Errors({ errors }) { ...@@ -162,19 +165,23 @@ export const Errors: React.FC<ErrorsProps> = function Errors({ errors }) {
React.useEffect(() => { React.useEffect(() => {
if (errors.length < 1) { if (errors.length < 1) {
setLookups({}) setLookups({})
setMinimized(false) setDisplayState('hidden')
setActiveIndex(0) setActiveIndex(0)
} }
}, [errors.length]) }, [errors.length])
const minimize = React.useCallback((e?: MouseEvent | TouchEvent) => { const minimize = React.useCallback((e?: MouseEvent | TouchEvent) => {
e?.preventDefault() e?.preventDefault()
setMinimized(true) setDisplayState('minimized')
}, []) }, [])
const reopen = React.useCallback( const hide = React.useCallback((e?: MouseEvent | TouchEvent) => {
e?.preventDefault()
setDisplayState('hidden')
}, [])
const fullscreen = React.useCallback(
(e?: React.MouseEvent<HTMLDivElement, MouseEvent>) => { (e?: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
e?.preventDefault() e?.preventDefault()
setMinimized(false) setDisplayState('fullscreen')
}, },
[] []
) )
...@@ -190,9 +197,13 @@ export const Errors: React.FC<ErrorsProps> = function Errors({ errors }) { ...@@ -190,9 +197,13 @@ export const Errors: React.FC<ErrorsProps> = function Errors({ errors }) {
return <Overlay /> return <Overlay />
} }
if (isMinimized) { if (displayState === 'hidden') {
return null
}
if (displayState === 'minimized') {
return ( return (
<Toast className="nextjs-toast-errors-parent" onClick={reopen}> <Toast className="nextjs-toast-errors-parent" onClick={fullscreen}>
<div className="nextjs-toast-errors"> <div className="nextjs-toast-errors">
<svg <svg
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
...@@ -212,6 +223,18 @@ export const Errors: React.FC<ErrorsProps> = function Errors({ errors }) { ...@@ -212,6 +223,18 @@ export const Errors: React.FC<ErrorsProps> = function Errors({ errors }) {
<span> <span>
{readyErrors.length} error{readyErrors.length > 1 ? 's' : ''} {readyErrors.length} error{readyErrors.length > 1 ? 's' : ''}
</span> </span>
<button
data-nextjs-toast-errors-hide-button
className="nextjs-toast-errors-hide-button"
type="button"
onClick={(e) => {
e.stopPropagation()
hide()
}}
aria-label="Hide Errors"
>
<CloseIcon />
</button>
</div> </div>
</Toast> </Toast>
) )
...@@ -320,4 +343,16 @@ export const styles = css` ...@@ -320,4 +343,16 @@ export const styles = css`
.nextjs-toast-errors > svg { .nextjs-toast-errors > svg {
margin-right: var(--size-gap); margin-right: var(--size-gap);
} }
.nextjs-toast-errors-hide-button {
margin-left: var(--size-gap-triple);
border: none;
background: none;
color: var(--color-ansi-bright-white);
padding: 0;
transition: opacity 0.25s ease;
opacity: 0.7;
}
.nextjs-toast-errors-hide-button:hover {
opacity: 1;
}
` `
import * as React from 'react'
const CloseIcon = () => {
return (
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M18 6L6 18"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
/>
<path
d="M6 6L18 18"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
)
}
export { CloseIcon }
...@@ -9,6 +9,7 @@ export function Base() { ...@@ -9,6 +9,7 @@ export function Base() {
--size-gap-half: 4px; --size-gap-half: 4px;
--size-gap: 8px; --size-gap: 8px;
--size-gap-double: 16px; --size-gap-double: 16px;
--size-gap-triple: 24px;
--size-gap-quad: 32px; --size-gap-quad: 32px;
--size-font-small: 14px; --size-font-small: 14px;
......
import React from 'react'
// Create a runtime error.
if ('window' in global) {
throw Error('example runtime error')
}
const Page = () => {
return <div>client-react-dev-overlay</div>
}
export default Page
import { createNext, FileRef } from 'e2e-utils'
import webdriver from 'next-webdriver'
import { NextInstance } from 'test/lib/next-modes/base'
import { join } from 'path'
import { BrowserInterface } from 'test/lib/browsers/base'
import { check } from 'next-test-utils'
describe('client-dev-overlay', () => {
let next: NextInstance
let browser: BrowserInterface
beforeAll(async () => {
next = await createNext({
files: {
pages: new FileRef(join(__dirname, 'app/pages')),
},
})
})
beforeEach(async () => {
browser = await webdriver(next.url, '')
})
afterAll(() => next.destroy())
// The `BrowserInterface.hasElementByCssSelector` cannot be used for elements inside a shadow DOM.
function elementExistsInNextJSPortalShadowDOM(selector: string) {
return browser.eval(
`!!document.querySelector('nextjs-portal').shadowRoot.querySelector('${selector}')`
) as any
}
const selectors = {
fullScreenDialog: '[data-nextjs-dialog]',
toast: '[data-nextjs-toast]',
minimizeButton: '[data-nextjs-errors-dialog-left-right-close-button]',
hideButton: '[data-nextjs-toast-errors-hide-button]',
}
function getToast() {
return browser.elementByCss(selectors.toast)
}
function getMinimizeButton() {
return browser.elementByCss(selectors.minimizeButton)
}
function getHideButton() {
return browser.elementByCss(selectors.hideButton)
}
it('should be able to fullscreen the minimized overlay', async () => {
await getMinimizeButton().click()
await getToast().click()
await check(async () => {
return (await elementExistsInNextJSPortalShadowDOM(
selectors.fullScreenDialog
))
? 'success'
: 'missing'
}, 'success')
})
it('should be able to minimize the fullscreen overlay', async () => {
await getMinimizeButton().click()
expect(await elementExistsInNextJSPortalShadowDOM(selectors.toast)).toBe(
true
)
})
it('should be able to hide the minimized overlay', async () => {
await getMinimizeButton().click()
await getHideButton().click()
await check(async () => {
const exists = await elementExistsInNextJSPortalShadowDOM('div')
return exists ? 'found' : 'success'
}, 'success')
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment