Unverified Commit 5ff57ead authored by rauno's avatar rauno Committed by GitHub

[dev-overlay] Fix error dialog resizing logic (#77830)

This PR improves our height resizing strategy. Animating the height on
page load or when the dialog opens is not ideal and it would be better
if the dialog would just fade in and assume it's intrinsic height
without motion.

This is what happens before this PR when you open errors, and it can be
distracting to experience such amount of motion at once:


https://github.com/user-attachments/assets/eb03cc23-395e-4ff4-9ed2-ace1694ad917

The reason this happens is because some of the dialog contents load in
async—for example the errors are actually suspended so that the message
can be viewed without waiting for the stack frames to load:


https://github.com/vercel/next.js/blob/1751db082d8c791e05ac1481a5036c8fb381d831/packages/next/src/client/components/react-dev-overlay/ui/container/errors.tsx#L202-L208

---

After this PR, we still measure the `height` of the dialog and animate
it in response to user input, but only use the value when the height has
stabilised. You can see what happens by observing the blue height value:




https://github.com/user-attachments/assets/cf4c1e0f-5e2e-49ed-b94a-1df9822f20cc


And another video from the fixture app:



https://github.com/user-attachments/assets/a285253f-f320-4638-b19e-b7a27a434ee6






---
Closes NDX-983
parent b84a02be
import * as React from 'react' import * as React from 'react'
import { useOnClickOutside } from '../../hooks/use-on-click-outside' import { useOnClickOutside } from '../../hooks/use-on-click-outside'
import { useMeasureHeight } from '../../hooks/use-measure-height'
export type DialogProps = { export type DialogProps = {
children?: React.ReactNode children?: React.ReactNode
...@@ -37,9 +36,6 @@ const Dialog: React.FC<DialogProps> = function Dialog({ ...@@ -37,9 +36,6 @@ const Dialog: React.FC<DialogProps> = function Dialog({
: undefined : undefined
) )
const ref = React.useRef<HTMLDivElement | null>(null)
const [height, pristine] = useMeasureHeight(ref)
useOnClickOutside( useOnClickOutside(
dialogRef.current, dialogRef.current,
CSS_SELECTORS_TO_EXCLUDE_ON_CLICK_OUTSIDE, CSS_SELECTORS_TO_EXCLUDE_ON_CLICK_OUTSIDE,
...@@ -102,19 +98,7 @@ const Dialog: React.FC<DialogProps> = function Dialog({ ...@@ -102,19 +98,7 @@ const Dialog: React.FC<DialogProps> = function Dialog({
}} }}
{...props} {...props}
> >
<div {children}
ref={dialogResizerRef}
data-nextjs-dialog-sizer
// [x] Don't animate on initial load
// [x] No duplicate elements
// [x] Responds to content growth
style={{
height,
transition: pristine ? undefined : 'height 250ms var(--timing-swift)',
}}
>
<div ref={ref}>{children}</div>
</div>
</div> </div>
) )
} }
......
...@@ -3,7 +3,7 @@ const styles = ` ...@@ -3,7 +3,7 @@ const styles = `
--next-dialog-radius: var(--rounded-xl); --next-dialog-radius: var(--rounded-xl);
--next-dialog-max-width: 960px; --next-dialog-max-width: 960px;
--next-dialog-row-padding: 16px; --next-dialog-row-padding: 16px;
--next-dialog-padding-x: 12px; --next-dialog-padding: 12px;
--next-dialog-notch-height: 42px; --next-dialog-notch-height: 42px;
--next-dialog-border-width: 1px; --next-dialog-border-width: 1px;
...@@ -14,7 +14,7 @@ const styles = ` ...@@ -14,7 +14,7 @@ const styles = `
max-width: var(--next-dialog-max-width); max-width: var(--next-dialog-max-width);
margin-right: auto; margin-right: auto;
margin-left: auto; margin-left: auto;
scale: 0.98; scale: 0.97;
opacity: 0; opacity: 0;
transition-property: scale, opacity; transition-property: scale, opacity;
transition-duration: var(--transition-duration); transition-duration: var(--transition-duration);
...@@ -28,7 +28,7 @@ const styles = ` ...@@ -28,7 +28,7 @@ const styles = `
[data-nextjs-scroll-fader][data-side="top"] { [data-nextjs-scroll-fader][data-side="top"] {
left: 1px; left: 1px;
top: calc(var(--next-dialog-notch-height) + var(--next-dialog-border-width)); top: calc(var(--next-dialog-notch-height) + var(--next-dialog-border-width));
width: calc(100% - var(--next-dialog-padding-x)); width: calc(100% - var(--next-dialog-padding));
opacity: 0; opacity: 0;
} }
} }
...@@ -82,7 +82,7 @@ const styles = ` ...@@ -82,7 +82,7 @@ const styles = `
display: flex; display: flex;
flex-direction: column; flex-direction: column;
position: relative; position: relative;
padding: 16px var(--next-dialog-padding-x); padding: var(--next-dialog-padding);
} }
[data-nextjs-dialog-content] > [data-nextjs-dialog-header] { [data-nextjs-dialog-content] > [data-nextjs-dialog-header] {
......
...@@ -24,6 +24,7 @@ const renderTestComponent = () => { ...@@ -24,6 +24,7 @@ const renderTestComponent = () => {
rendered={true} rendered={true}
transitionDurationMs={200} transitionDurationMs={200}
isTurbopack={false} isTurbopack={false}
errorCount={1}
versionInfo={{ versionInfo={{
installed: '15.0.0', installed: '15.0.0',
staleness: 'fresh', staleness: 'fresh',
......
...@@ -36,6 +36,7 @@ import type { ReadyRuntimeError } from '../../../../utils/get-error-by-type' ...@@ -36,6 +36,7 @@ import type { ReadyRuntimeError } from '../../../../utils/get-error-by-type'
import { EnvironmentNameLabel } from '../environment-name-label/environment-name-label' import { EnvironmentNameLabel } from '../environment-name-label/environment-name-label'
import { useFocusTrap } from '../dev-tools-indicator/utils' import { useFocusTrap } from '../dev-tools-indicator/utils'
import { Fader } from '../../fader' import { Fader } from '../../fader'
import { Resizer } from '../../resizer'
export interface ErrorOverlayLayoutProps extends ErrorBaseProps { export interface ErrorOverlayLayoutProps extends ErrorBaseProps {
errorMessage: ErrorMessageType errorMessage: ErrorMessageType
...@@ -59,6 +60,7 @@ export function ErrorOverlayLayout({ ...@@ -59,6 +60,7 @@ export function ErrorOverlayLayout({
errorType, errorType,
children, children,
errorCode, errorCode,
errorCount,
error, error,
debugInfo, debugInfo,
isBuildError, isBuildError,
...@@ -83,6 +85,10 @@ export function ErrorOverlayLayout({ ...@@ -83,6 +85,10 @@ export function ErrorOverlayLayout({
} as React.CSSProperties, } as React.CSSProperties,
} }
const [animating, setAnimating] = React.useState(
Boolean(transitionDurationMs)
)
const faderRef = React.useRef<HTMLDivElement | null>(null) const faderRef = React.useRef<HTMLDivElement | null>(null)
const hasFooter = Boolean(footerMessage || errorCode) const hasFooter = Boolean(footerMessage || errorCode)
const dialogRef = React.useRef<HTMLDivElement | null>(null) const dialogRef = React.useRef<HTMLDivElement | null>(null)
...@@ -95,9 +101,23 @@ export function ErrorOverlayLayout({ ...@@ -95,9 +101,23 @@ export function ErrorOverlayLayout({
} }
} }
function onTransitionEnd({ propertyName, target }: React.TransitionEvent) {
// We can only measure height after the `scale` transition ends,
// otherwise we will measure height as a multiple of the animating value
// which will give us an incorrect value.
if (propertyName === 'scale' && target === dialogRef.current) {
setAnimating(false)
}
}
return ( return (
<ErrorOverlayOverlay fixed={isBuildError} {...animationProps}> <ErrorOverlayOverlay fixed={isBuildError} {...animationProps}>
<div data-nextjs-dialog-root ref={dialogRef} {...animationProps}> <div
data-nextjs-dialog-root
onTransitionEnd={onTransitionEnd}
ref={dialogRef}
{...animationProps}
>
<ErrorOverlayNav <ErrorOverlayNav
runtimeErrors={runtimeErrors} runtimeErrors={runtimeErrors}
activeIdx={activeIdx} activeIdx={activeIdx}
...@@ -119,30 +139,37 @@ export function ErrorOverlayLayout({ ...@@ -119,30 +139,37 @@ export function ErrorOverlayLayout({
) )
} }
> >
<DialogContent> <Resizer
<ErrorOverlayDialogHeader> ref={dialogResizerRef}
<div measure={!animating}
className="nextjs__container_errors__error_title" data-nextjs-dialog-sizer
// allow assertion in tests before error rating is implemented >
data-nextjs-error-code={errorCode} <DialogContent>
> <ErrorOverlayDialogHeader>
<span data-nextjs-error-label-group> <div
<ErrorTypeLabel errorType={errorType} /> className="nextjs__container_errors__error_title"
{error.environmentName && ( // allow assertion in tests before error rating is implemented
<EnvironmentNameLabel data-nextjs-error-code={errorCode}
environmentName={error.environmentName} >
/> <span data-nextjs-error-label-group>
)} <ErrorTypeLabel errorType={errorType} />
</span> {error.environmentName && (
<ErrorOverlayToolbar error={error} debugInfo={debugInfo} /> <EnvironmentNameLabel
</div> environmentName={error.environmentName}
<ErrorMessage errorMessage={errorMessage} /> />
</ErrorOverlayDialogHeader> )}
</span>
<ErrorOverlayToolbar error={error} debugInfo={debugInfo} />
</div>
<ErrorMessage errorMessage={errorMessage} />
</ErrorOverlayDialogHeader>
<ErrorOverlayDialogBody>{children}</ErrorOverlayDialogBody>
</DialogContent>
</Resizer>
<ErrorOverlayDialogBody>{children}</ErrorOverlayDialogBody>
</DialogContent>
<ErrorOverlayBottomStack <ErrorOverlayBottomStack
errorCount={runtimeErrors?.length ?? 0} errorCount={errorCount}
activeIdx={activeIdx ?? 0} activeIdx={activeIdx ?? 0}
/> />
</ErrorOverlayDialog> </ErrorOverlayDialog>
......
...@@ -13,6 +13,7 @@ export interface ErrorBaseProps { ...@@ -13,6 +13,7 @@ export interface ErrorBaseProps {
transitionDurationMs: number transitionDurationMs: number
isTurbopack: boolean isTurbopack: boolean
versionInfo: OverlayState['versionInfo'] versionInfo: OverlayState['versionInfo']
errorCount: number
} }
export function ErrorOverlay({ export function ErrorOverlay({
...@@ -20,11 +21,13 @@ export function ErrorOverlay({ ...@@ -20,11 +21,13 @@ export function ErrorOverlay({
runtimeErrors, runtimeErrors,
isErrorOverlayOpen, isErrorOverlayOpen,
setIsErrorOverlayOpen, setIsErrorOverlayOpen,
errorCount,
}: { }: {
state: OverlayState state: OverlayState
runtimeErrors: ReadyRuntimeError[] runtimeErrors: ReadyRuntimeError[]
isErrorOverlayOpen: boolean isErrorOverlayOpen: boolean
setIsErrorOverlayOpen: (value: boolean) => void setIsErrorOverlayOpen: (value: boolean) => void
errorCount: number
}) { }) {
const isTurbopack = !!process.env.TURBOPACK const isTurbopack = !!process.env.TURBOPACK
...@@ -38,6 +41,7 @@ export function ErrorOverlay({ ...@@ -38,6 +41,7 @@ export function ErrorOverlay({
transitionDurationMs, transitionDurationMs,
isTurbopack, isTurbopack,
versionInfo: state.versionInfo, versionInfo: state.versionInfo,
errorCount,
} }
if (state.buildError !== null) { if (state.buildError !== null) {
......
import { useEffect, useRef, useState } from 'react'
export function Resizer({
children,
measure,
...props
}: {
children: React.ReactNode
measure: boolean
} & React.HTMLProps<HTMLDivElement> & { ref?: React.Ref<HTMLDivElement> }) {
const ref = useRef<HTMLDivElement | null>(null)
const [height, measuring] = useMeasureHeight(ref, measure)
return (
<div
{...props}
// [x] Don't animate on initial load
// [x] No duplicate elements
// [x] Responds to content growth
style={{
height: measuring ? 'auto' : height,
transition: 'height 250ms var(--timing-swift)',
}}
>
<div ref={ref}>{children}</div>
</div>
)
}
function useMeasureHeight(
ref: React.RefObject<HTMLDivElement | null>,
measure: boolean
): [number, boolean] {
const [height, setHeight] = useState<number>(0)
const [measuring, setMeasuring] = useState<boolean>(true)
useEffect(() => {
if (!measure) {
return
}
let timerId: number
const el = ref.current
if (!el) {
return
}
const observer = new ResizeObserver(([{ contentRect }]) => {
clearTimeout(timerId)
timerId = window.setTimeout(() => {
setMeasuring(false)
}, 100)
setHeight(contentRect.height)
})
observer.observe(el)
return () => observer.disconnect()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [measure])
return [height, measuring]
}
import { useState, useMemo, useEffect, useRef, Suspense } from 'react' import { useState, useMemo, useRef, Suspense } from 'react'
import type { DebugInfo } from '../../types' import type { DebugInfo } from '../../types'
import { Overlay } from '../components/overlay' import { Overlay } from '../components/overlay'
import { RuntimeError } from './runtime-error' import { RuntimeError } from './runtime-error'
...@@ -89,18 +89,6 @@ export function Errors({ ...@@ -89,18 +89,6 @@ export function Errors({
}: ErrorsProps) { }: ErrorsProps) {
const dialogResizerRef = useRef<HTMLDivElement | null>(null) const dialogResizerRef = useRef<HTMLDivElement | null>(null)
useEffect(() => {
// Close the error overlay when pressing escape
function handleKeyDown(event: KeyboardEvent) {
if (event.key === 'Escape') {
onClose()
}
}
document.addEventListener('keydown', handleKeyDown)
return () => document.removeEventListener('keydown', handleKeyDown)
}, [onClose])
const isLoading = useMemo<boolean>(() => { const isLoading = useMemo<boolean>(() => {
return runtimeErrors.length < 1 return runtimeErrors.length < 1
}, [runtimeErrors.length]) }, [runtimeErrors.length])
......
...@@ -92,9 +92,8 @@ export const Default: Story = { ...@@ -92,9 +92,8 @@ export const Default: Story = {
src={imgApp} src={imgApp}
style={{ style={{
width: '100%', width: '100%',
height: '100%', height: '100vh',
objectFit: 'contain', objectFit: 'contain',
filter: 'invert(1)',
}} }}
/> />
<DevOverlay <DevOverlay
......
...@@ -50,6 +50,7 @@ export function DevOverlay({ ...@@ -50,6 +50,7 @@ export function DevOverlay({
<ErrorOverlay <ErrorOverlay
state={state} state={state}
runtimeErrors={runtimeErrors} runtimeErrors={runtimeErrors}
errorCount={totalErrorCount}
isErrorOverlayOpen={isErrorOverlayOpen} isErrorOverlayOpen={isErrorOverlayOpen}
setIsErrorOverlayOpen={setIsErrorOverlayOpen} setIsErrorOverlayOpen={setIsErrorOverlayOpen}
/> />
......
import { useEffect, useState } from 'react'
export function useMeasureHeight(
ref: React.RefObject<HTMLDivElement | null>
): [number, boolean] {
const [pristine, setPristine] = useState<boolean>(true)
const [height, setHeight] = useState<number>(0)
useEffect(() => {
const el = ref.current
if (!el) {
return
}
const observer = new ResizeObserver(() => {
const { height: h } = el.getBoundingClientRect()
setHeight((prevHeight) => {
if (prevHeight !== 0) {
setPristine(false)
}
return h
})
})
observer.observe(el)
return () => {
observer.disconnect()
setPristine(true)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
return [height, pristine]
}
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