Unverified Commit b40e1a2d authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

Port ErrorPagination (#74097)

Ported `ErrorPagination` and replaced in the `Errors` component.
parent 6fa146a4
import type { Meta, StoryObj } from '@storybook/react'
import { ErrorPagination } from './ErrorPagination'
import { withShadowPortal } from '../../../storybook/with-shadow-portal'
const meta: Meta<typeof ErrorPagination> = {
title: 'ErrorPagination',
component: ErrorPagination,
parameters: {
layout: 'centered',
},
decorators: [withShadowPortal],
}
export default meta
type Story = StoryObj<typeof ErrorPagination>
// Mock errors for stories
const mockErrors = [
{
id: 1,
runtime: true as const,
error: new Error('First error'),
frames: [],
},
{
id: 2,
runtime: true as const,
error: new Error('Second error'),
frames: [],
},
{
id: 3,
runtime: true as const,
error: new Error('Third error'),
frames: [],
},
]
export const SingleError: Story = {
args: {
activeIdx: 0,
previous: () => console.log('Previous clicked'),
next: () => console.log('Next clicked'),
readyErrors: [mockErrors[0]],
minimize: () => console.log('Minimize clicked'),
isServerError: false,
},
}
export const MultipleErrors: Story = {
args: {
activeIdx: 1,
previous: () => console.log('Previous clicked'),
next: () => console.log('Next clicked'),
readyErrors: mockErrors,
minimize: () => console.log('Minimize clicked'),
isServerError: false,
},
}
export const LastError: Story = {
args: {
activeIdx: 2,
previous: () => console.log('Previous clicked'),
next: () => console.log('Next clicked'),
readyErrors: mockErrors,
minimize: () => console.log('Minimize clicked'),
isServerError: false,
},
}
export const ServerError: Story = {
args: {
activeIdx: 0,
previous: () => console.log('Previous clicked'),
next: () => console.log('Next clicked'),
readyErrors: [mockErrors[0]],
minimize: () => console.log('Minimize clicked'),
isServerError: true,
},
}
import type { ReadyRuntimeError } from '../../../helpers/get-error-by-type'
import { useCallback, useEffect, useRef, useState } from 'react'
type ErrorPaginationProps = {
activeIdx: number
previous: () => void
next: () => void
readyErrors: ReadyRuntimeError[]
minimize: () => void
isServerError: boolean
}
export function ErrorPagination({
activeIdx,
previous,
next,
readyErrors,
minimize,
isServerError,
}: ErrorPaginationProps) {
const previousHandler = activeIdx > 0 ? previous : null
const nextHandler = activeIdx < readyErrors.length - 1 ? next : null
const close = isServerError ? undefined : minimize
const buttonLeft = useRef<HTMLButtonElement | null>(null)
const buttonRight = useRef<HTMLButtonElement | null>(null)
const buttonClose = useRef<HTMLButtonElement | null>(null)
const [nav, setNav] = useState<HTMLElement | null>(null)
const onNav = useCallback((el: HTMLElement) => {
setNav(el)
}, [])
useEffect(() => {
if (nav == null) {
return
}
const root = nav.getRootNode()
const d = self.document
function handler(e: KeyboardEvent) {
if (e.key === 'ArrowLeft') {
e.preventDefault()
e.stopPropagation()
if (buttonLeft.current) {
buttonLeft.current.focus()
}
previousHandler && previousHandler()
} else if (e.key === 'ArrowRight') {
e.preventDefault()
e.stopPropagation()
if (buttonRight.current) {
buttonRight.current.focus()
}
nextHandler && nextHandler()
} else if (e.key === 'Escape') {
e.preventDefault()
e.stopPropagation()
if (root instanceof ShadowRoot) {
const a = root.activeElement
if (a && a !== buttonClose.current && a instanceof HTMLElement) {
a.blur()
return
}
}
close?.()
}
}
root.addEventListener('keydown', handler as EventListener)
if (root !== d) {
d.addEventListener('keydown', handler)
}
return function () {
root.removeEventListener('keydown', handler as EventListener)
if (root !== d) {
d.removeEventListener('keydown', handler)
}
}
}, [close, nav, nextHandler, previousHandler])
// Unlock focus for browsers like Firefox, that break all user focus if the
// currently focused item becomes disabled.
useEffect(() => {
if (nav == null) {
return
}
const root = nav.getRootNode()
// Always true, but we do this for TypeScript:
if (root instanceof ShadowRoot) {
const a = root.activeElement
if (previousHandler == null) {
if (buttonLeft.current && a === buttonLeft.current) {
buttonLeft.current.blur()
}
} else if (nextHandler == null) {
if (buttonRight.current && a === buttonRight.current) {
buttonRight.current.blur()
}
}
}
}, [nav, nextHandler, previousHandler])
return (
<div data-nextjs-dialog-left-right>
<nav ref={onNav}>
<button
ref={buttonLeft}
type="button"
disabled={previousHandler == null ? true : undefined}
aria-disabled={previousHandler == null ? true : undefined}
onClick={previousHandler ?? undefined}
>
<svg
viewBox="0 0 14 14"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<title>previous</title>
<path
d="M6.99996 1.16666L1.16663 6.99999L6.99996 12.8333M12.8333 6.99999H1.99996H12.8333Z"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
</button>
<button
ref={buttonRight}
type="button"
disabled={nextHandler == null ? true : undefined}
aria-disabled={nextHandler == null ? true : undefined}
onClick={nextHandler ?? undefined}
>
<svg
viewBox="0 0 14 14"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<title>next</title>
<path
d="M6.99996 1.16666L12.8333 6.99999L6.99996 12.8333M1.16663 6.99999H12H1.16663Z"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
</button>
<small>
<span>{activeIdx + 1}</span> of{' '}
<span data-nextjs-dialog-header-total-count>
{readyErrors.length}
</span>
{' issue'}
{readyErrors.length < 2 ? '' : 's'}
</small>
</nav>
</div>
)
}
import * as React from 'react'
import { CloseIcon } from '../../icons/CloseIcon'
export type LeftRightDialogHeaderProps = {
children?: React.ReactNode
className?: string
previous: (() => void) | null
next: (() => void) | null
close?: () => void
}
const LeftRightDialogHeader: React.FC<LeftRightDialogHeaderProps> =
function LeftRightDialogHeader({
children,
className,
previous,
next,
close,
}) {
const buttonLeft = React.useRef<HTMLButtonElement | null>(null)
const buttonRight = React.useRef<HTMLButtonElement | null>(null)
const buttonClose = React.useRef<HTMLButtonElement | null>(null)
const [nav, setNav] = React.useState<HTMLElement | null>(null)
const onNav = React.useCallback((el: HTMLElement) => {
setNav(el)
}, [])
React.useEffect(() => {
if (nav == null) {
return
}
const root = nav.getRootNode()
const d = self.document
function handler(e: KeyboardEvent) {
if (e.key === 'ArrowLeft') {
e.preventDefault()
e.stopPropagation()
if (buttonLeft.current) {
buttonLeft.current.focus()
}
previous && previous()
} else if (e.key === 'ArrowRight') {
e.preventDefault()
e.stopPropagation()
if (buttonRight.current) {
buttonRight.current.focus()
}
next && next()
} else if (e.key === 'Escape') {
e.preventDefault()
e.stopPropagation()
if (root instanceof ShadowRoot) {
const a = root.activeElement
if (a && a !== buttonClose.current && a instanceof HTMLElement) {
a.blur()
return
}
}
close?.()
}
}
root.addEventListener('keydown', handler as EventListener)
if (root !== d) {
d.addEventListener('keydown', handler)
}
return function () {
root.removeEventListener('keydown', handler as EventListener)
if (root !== d) {
d.removeEventListener('keydown', handler)
}
}
}, [close, nav, next, previous])
// Unlock focus for browsers like Firefox, that break all user focus if the
// currently focused item becomes disabled.
React.useEffect(() => {
if (nav == null) {
return
}
const root = nav.getRootNode()
// Always true, but we do this for TypeScript:
if (root instanceof ShadowRoot) {
const a = root.activeElement
if (previous == null) {
if (buttonLeft.current && a === buttonLeft.current) {
buttonLeft.current.blur()
}
} else if (next == null) {
if (buttonRight.current && a === buttonRight.current) {
buttonRight.current.blur()
}
}
}
}, [nav, next, previous])
return (
<div data-nextjs-dialog-left-right className={className}>
<nav ref={onNav}>
<button
ref={buttonLeft}
type="button"
disabled={previous == null ? true : undefined}
aria-disabled={previous == null ? true : undefined}
onClick={previous ?? undefined}
>
<svg
viewBox="0 0 14 14"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<title>previous</title>
<path
d="M6.99996 1.16666L1.16663 6.99999L6.99996 12.8333M12.8333 6.99999H1.99996H12.8333Z"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
</button>
<button
ref={buttonRight}
type="button"
disabled={next == null ? true : undefined}
aria-disabled={next == null ? true : undefined}
onClick={next ?? undefined}
>
<svg
viewBox="0 0 14 14"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<title>next</title>
<path
d="M6.99996 1.16666L12.8333 6.99999L6.99996 12.8333M1.16663 6.99999H12H1.16663Z"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
</button>
{children}
</nav>
{close ? (
<button
data-nextjs-errors-dialog-left-right-close-button
ref={buttonClose}
type="button"
onClick={close}
aria-label="Close"
>
<span aria-hidden="true">
<CloseIcon />
</span>
</button>
) : null}
</div>
)
}
export { LeftRightDialogHeader }
export { LeftRightDialogHeader } from './LeftRightDialogHeader'
export { styles } from './styles'
import { noop as css } from '../../helpers/noop-template'
const styles = css`
[data-nextjs-dialog-left-right] {
display: flex;
flex-direction: row;
align-content: center;
align-items: center;
justify-content: space-between;
}
[data-nextjs-dialog-left-right] > nav {
flex: 1;
display: flex;
align-items: center;
margin-right: var(--size-gap);
}
[data-nextjs-dialog-left-right] > nav > button {
display: inline-flex;
align-items: center;
justify-content: center;
width: calc(var(--size-gap-double) + var(--size-gap));
height: calc(var(--size-gap-double) + var(--size-gap));
font-size: 0;
border: none;
background-color: rgba(255, 85, 85, 0.1);
color: var(--color-ansi-red);
cursor: pointer;
transition: background-color 0.25s ease;
}
[data-nextjs-dialog-left-right] > nav > button > svg {
width: auto;
height: calc(var(--size-gap) + var(--size-gap-half));
}
[data-nextjs-dialog-left-right] > nav > button:hover {
background-color: rgba(255, 85, 85, 0.2);
}
[data-nextjs-dialog-left-right] > nav > button:disabled {
background-color: rgba(255, 85, 85, 0.1);
color: rgba(255, 85, 85, 0.4);
cursor: not-allowed;
}
[data-nextjs-dialog-left-right] > nav > button:first-of-type {
border-radius: var(--size-gap-half) 0 0 var(--size-gap-half);
margin-right: 1px;
}
[data-nextjs-dialog-left-right] > nav > button:last-of-type {
border-radius: 0 var(--size-gap-half) var(--size-gap-half) 0;
}
[data-nextjs-dialog-left-right] > button:last-of-type {
border: 0;
padding: 0;
background-color: transparent;
appearance: none;
opacity: 0.4;
transition: opacity 0.25s ease;
color: var(--color-font);
}
[data-nextjs-dialog-left-right] > button:last-of-type:hover {
opacity: 0.7;
}
`
export { styles }
......@@ -12,7 +12,6 @@ import {
DialogContent,
DialogHeader,
} from '../components/Dialog'
import { LeftRightDialogHeader } from '../components/LeftRightDialogHeader'
import { Overlay } from '../components/Overlay'
import { getErrorByType } from '../helpers/get-error-by-type'
import type { ReadyRuntimeError } from '../helpers/get-error-by-type'
......@@ -35,6 +34,7 @@ import {
} from '../helpers/console-error'
import { extractNextErrorCode } from '../../../../../../lib/error-telemetry-utils'
import { ErrorIndicator } from '../components/Errors/ErrorIndicator/ErrorIndicator'
import { ErrorPagination } from '../components/Errors/ErrorPagination/ErrorPagination'
export type SupportedErrorEvent = {
id: number
......@@ -273,21 +273,15 @@ export function Errors({
>
<DialogContent>
<DialogHeader className="nextjs-container-errors-header">
<LeftRightDialogHeader
previous={activeIdx > 0 ? previous : null}
next={activeIdx < readyErrors.length - 1 ? next : null}
close={isServerError ? undefined : minimize}
>
<small>
<span>{activeIdx + 1}</span> of{' '}
<span data-nextjs-dialog-header-total-count>
{readyErrors.length}
</span>
{' issue'}
{readyErrors.length < 2 ? '' : 's'}
</small>
<VersionStalenessInfo versionInfo={versionInfo} />
</LeftRightDialogHeader>
<ErrorPagination
activeIdx={activeIdx}
readyErrors={readyErrors}
previous={previous}
next={next}
minimize={minimize}
isServerError={isServerError}
/>
<VersionStalenessInfo versionInfo={versionInfo} />
<div
className="nextjs__container_errors__error_title"
......
import { styles as codeFrame } from '../components/CodeFrame/styles'
import { styles as dialog } from '../components/Dialog'
import { styles as leftRightDialogHeader } from '../components/LeftRightDialogHeader/styles'
import { styles as overlay } from '../components/Overlay/styles'
import { styles as terminal } from '../components/Terminal/styles'
import { styles as toast } from '../components/Toast'
......@@ -17,7 +16,6 @@ export function ComponentStyles() {
${overlay}
${toast}
${dialog}
${leftRightDialogHeader}
${codeFrame}
${terminal}
${buildErrorStyles}
......
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