Skip to content

Commit

Permalink
Site Editor: Wrap each router area in 'ErrorBoundary' (#64245)
Browse files Browse the repository at this point in the history
* Use the error boundary from the `editor` package in the Site Editor.
* Update the `ErrorBoundary` markup and remove the `Warning` component. It still matches the design.
* Update letter casing for button labels.
* Change "Copy post text" to "Copy contents" and make the action label more general.
* Make the "Copy contents" action opt-in.
* Use the primary button variant for "Copy error".

Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: jasmussen <[email protected]>
  • Loading branch information
4 people authored Dec 6, 2024
1 parent 6b16c73 commit b8bfb4d
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 137 deletions.
2 changes: 1 addition & 1 deletion packages/edit-post/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ function Layout( {

return (
<SlotFillProvider>
<ErrorBoundary>
<ErrorBoundary canCopyContent>
<CommandMenu />
<WelcomeGuide postType={ currentPostType } />
<div
Expand Down
44 changes: 0 additions & 44 deletions packages/edit-site/src/components/error-boundary/index.js

This file was deleted.

This file was deleted.

30 changes: 0 additions & 30 deletions packages/edit-site/src/components/error-boundary/warning.js

This file was deleted.

12 changes: 7 additions & 5 deletions packages/edit-site/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
import {
EditorSnackbars,
UnsavedChangesWarning,
ErrorBoundary,
privateApis as editorPrivateApis,
} from '@wordpress/editor';
import { privateApis as coreCommandsPrivateApis } from '@wordpress/core-commands';
Expand All @@ -36,7 +37,6 @@ import { useDispatch } from '@wordpress/data';
/**
* Internal dependencies
*/
import ErrorBoundary from '../error-boundary';
import { default as SiteHub, SiteHubMobile } from '../site-hub';
import ResizableFrame from '../resizable-frame';
import { unlock } from '../../lock-unlock';
Expand Down Expand Up @@ -136,7 +136,9 @@ function Layout() {
}
routeKey={ routeKey }
>
{ areas.sidebar }
<ErrorBoundary>
{ areas.sidebar }
</ErrorBoundary>
</SidebarContent>
<SaveHub />
<SavePanel />
Expand All @@ -160,7 +162,7 @@ function Layout() {
/>
</SidebarContent>
) }
{ areas.mobile }
<ErrorBoundary>{ areas.mobile }</ErrorBoundary>
</div>
) }

Expand All @@ -173,7 +175,7 @@ function Layout() {
maxWidth: widths?.content,
} }
>
{ areas.content }
<ErrorBoundary>{ areas.content }</ErrorBoundary>
</div>
) }

Expand All @@ -184,7 +186,7 @@ function Layout() {
maxWidth: widths?.edit,
} }
>
{ areas.edit }
<ErrorBoundary>{ areas.edit }</ErrorBoundary>
</div>
) }

Expand Down
46 changes: 30 additions & 16 deletions packages/editor/src/components/error-boundary/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
*/
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Button } from '@wordpress/components';
import {
Button,
__experimentalHStack as HStack,
__experimentalText as Text,
} from '@wordpress/components';
import { select } from '@wordpress/data';
import { Warning } from '@wordpress/block-editor';
import { useCopyToClipboard } from '@wordpress/compose';
import { doAction } from '@wordpress/hooks';

Expand All @@ -26,10 +29,10 @@ function getContent() {
} catch ( error ) {}
}

function CopyButton( { text, children } ) {
function CopyButton( { text, children, variant = 'secondary' } ) {
const ref = useCopyToClipboard( text );
return (
<Button __next40pxDefaultSize variant="secondary" ref={ ref }>
<Button __next40pxDefaultSize variant={ variant } ref={ ref }>
{ children }
</Button>
);
Expand All @@ -54,23 +57,34 @@ class ErrorBoundary extends Component {

render() {
const { error } = this.state;
const { canCopyContent = false } = this.props;
if ( ! error ) {
return this.props.children;
}

const actions = [
<CopyButton key="copy-post" text={ getContent }>
{ __( 'Copy Post Text' ) }
</CopyButton>,
<CopyButton key="copy-error" text={ error.stack }>
{ __( 'Copy Error' ) }
</CopyButton>,
];

return (
<Warning className="editor-error-boundary" actions={ actions }>
{ __( 'The editor has encountered an unexpected error.' ) }
</Warning>
<HStack
className="editor-error-boundary"
alignment="baseline"
spacing={ 4 }
justify="space-between"
expanded={ false }
wrap
>
<Text as="p">
{ __( 'The editor has encountered an unexpected error.' ) }
</Text>
<HStack expanded={ false }>
{ canCopyContent && (
<CopyButton text={ getContent }>
{ __( 'Copy contents' ) }
</CopyButton>
) }
<CopyButton variant="primary" text={ error?.stack }>
{ __( 'Copy error' ) }
</CopyButton>
</HStack>
</HStack>
);
}
}
Expand Down
6 changes: 5 additions & 1 deletion packages/editor/src/components/error-boundary/style.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
.editor-error-boundary {
font-family: $default-font;
margin: auto;
max-width: 780px;
padding: 20px;
padding: 1em;
margin-top: 60px;
box-shadow: $elevation-large;
border: $border-width solid $gray-900;
border-radius: $radius-small;
background-color: $white;
}

1 comment on commit b8bfb4d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in b8bfb4d.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/12196089297
📝 Reported issues:

Please sign in to comment.