Error Block
The ErrorBlock Component is a fully customizable and accessible error display component built with React and TypeScript. It provides a complete error interface with modern design patterns, flexible customization options, and support for custom error messages and actions. Built on top of MUI components for consistent styling.
π Installationβ
npm install @nodeblocks/frontend-error-block@0.2.1
π Usageβ
import {ErrorBlock} from '@nodeblocks/frontend-error-block';
- Basic Usage
- Advanced Usage
function SimpleErrorBlock() { return ( <ErrorBlock errorTitle="Something went wrong" subtitle="We encountered an unexpected error. Please try again or contact support if the problem persists." actionHref="#home" actionText="Back to Home" titleErrorCode="Error 404" > <ErrorBlock.Icon /> <ErrorBlock.Title /> <ErrorBlock.Subtitle /> <ErrorBlock.Action /> </ErrorBlock> ); }
function AdvancedErrorBlock() { return ( <ErrorBlock errorTitle="Server Connection Failed" subtitle="Unable to connect to our servers. Please check your internet connection and try again." actionHref="#dashboard" actionText="Back to Dashboard" titleErrorCode="Error 500" className="custom-error-block" spacing={3} sx={{ backgroundColor: '#fafafa', border: '1px solid #e0e0e0', borderRadius: '8px', padding: '40px', maxWidth: '600px', margin: '0 auto', }} > {({defaultBlocks}) => ({ blocks: { ...defaultBlocks, troubleshooting: ( <div style={{ backgroundColor: '#e7f3ff', border: '1px solid #b3d9ff', borderRadius: '6px', padding: '16px', marginBottom: '24px', textAlign: 'left', }} > <h4 style={{ margin: '0 0 12px 0', color: '#0066cc', fontSize: '16px', fontWeight: 'bold', }} > π§ Troubleshooting Steps: </h4> <ul style={{ margin: '0', paddingLeft: '20px', color: '#333', fontSize: '14px', lineHeight: '1.5', }} > <li>Check your internet connection</li> <li>Refresh the page (Ctrl+R or Cmd+R)</li> <li>Clear browser cache and cookies</li> <li>Try again in a few minutes</li> </ul> </div> ), support: ( <div style={{ marginTop: '24px', padding: '16px', backgroundColor: '#f8f9fa', border: '1px solid #dee2e6', borderRadius: '6px', textAlign: 'center', }} > <p style={{ margin: '0 0 8px 0', fontSize: '14px', color: '#6c757d', }} > Still having trouble? </p> <button style={{ padding: '8px 16px', backgroundColor: '#28a745', color: 'white', border: 'none', borderRadius: '4px', cursor: 'pointer', fontSize: '14px', fontWeight: 'bold', }} > π Contact Support </button> </div> ), }, blockOrder: ['icon', 'title', 'troubleshooting', 'subtitle', 'action', 'support'], })} </ErrorBlock> ); }
π§ Props Referenceβ
Main Component Propsβ
| Prop | Type | Default | Description |
|---|---|---|---|
errorTitle | string | Required | Title for the error message displayed prominently |
subtitle | string | Required | Subtitle providing additional context about the error |
actionHref | string | Required | URL for the action button navigation |
actionText | string | Required | Text to display in the action button |
titleErrorCode | string | undefined | Error code displayed above the title (e.g., "Error 404") |
spacing | number | string | 4 | Spacing between elements (MUI Stack spacing) |
className | string | undefined | Additional CSS class name for styling the error container |
sx | SxProps<Theme> | undefined | MUI system props for custom styling |
children | BlocksOverride | undefined | Custom block components to override default rendering |
Note: This component inherits all MUI Stack component props.
Sub-Componentsβ
The ErrorBlock component provides several sub-components. All sub-components receive their default values from the main component's context and can override these values through props.
ErrorBlock.Iconβ
Renders the error icon using MUI's ErrorOutline icon from @mui/icons-material.
| Prop | Type | Default | Description |
|---|---|---|---|
sx | SxProps<Theme> | { fontSize: '3rem', color: theme.palette.secondary.main } | MUI system props for custom styling |
className | string | undefined | Additional CSS class name for styling |
Note: This component inherits all MUI SvgIcon component props (from ErrorOutline).
ErrorBlock.Titleβ
Renders the error title using MUI Typography.
| Prop | Type | Default | Description |
|---|---|---|---|
titleErrorCode | ReactNode | Context value | Error code to display above the title |
errorTitle | string | Context value | Title content (overrides context) |
children | ReactNode | Context errorTitle | Custom content - overrides errorTitle |
variant | TypographyVariant | 'h4' | MUI Typography variant |
sx | SxProps<Theme> | { color: theme.palette.secondary.main } | MUI system props for custom styling |
className | string | undefined | Additional CSS class name for styling |
Note: This component inherits all MUI Typography component props.
ErrorBlock.Subtitleβ
Renders the error subtitle using MUI Typography.
| Prop | Type | Default | Description |
|---|---|---|---|
subtitle | string | Context value | Subtitle content (overrides context) |
children | ReactNode | Context subtitle | Custom content - overrides subtitle |
sx | SxProps<Theme> | { color: theme.palette.secondary.main } | MUI system props for custom styling |
className | string | undefined | Additional CSS class name for styling |
Note: This component inherits all MUI Typography component props.
ErrorBlock.Actionβ
Renders the action button using MUI Button.
| Prop | Type | Default | Description |
|---|---|---|---|
actionHref | string | Context value | URL for navigation (overrides context) |
actionText | string | Context value | Button text (overrides context) |
children | ReactNode | Context actionText | Custom content - overrides actionText |
href | string | Context actionHref | Direct MUI Button href (overrides actionHref) |
variant | ButtonVariant | 'contained' | MUI Button variant |
size | 'small' | 'medium' | 'large' | 'medium' | MUI Button size |
className | string | undefined | Additional CSS class name for styling |
Note: This component inherits all MUI Button component props.
π¨ Configuration examplesβ
Custom Icon Stylingβ
<ErrorBlock.Icon
sx={{
fontSize: '5rem',
color: 'warning.main',
backgroundColor: 'warning.light',
borderRadius: '50%',
padding: 2
}}
/>
Custom Typography Stylingβ
<ErrorBlock.Title
variant="h3"
sx={{
color: 'error.main',
fontWeight: 'bold',
textTransform: 'uppercase'
}}>
Custom Error Title
</ErrorBlock.Title>
Custom Button Stylingβ
<ErrorBlock.Action
variant="outlined"
color="secondary"
size="large"
startIcon={<HomeIcon />}
sx={{
borderRadius: 2,
textTransform: 'none'
}}>
Return Home
</ErrorBlock.Action>
π§ TypeScript Supportβ
Full TypeScript support with comprehensive type definitions:
import {ErrorBlock} from '@nodeblocks/frontend-error-block';
import {StackProps, TypographyProps, ButtonProps} from '@mui/material';
// Main component props extend MUI StackProps
interface ErrorBlockProps extends Omit<StackProps, 'children'> {
errorTitle: string;
subtitle: string;
actionHref: string;
actionText: string;
titleErrorCode?: string;
children?: BlocksOverride;
}
// Example with MUI prop customization
function CustomStyledErrorBlock() {
return (
<ErrorBlock
errorTitle="Server Error"
subtitle="Our servers are experiencing issues. Please try again later."
actionHref="#dashboard"
actionText="Back to Dashboard"
titleErrorCode="Error 500"
spacing={3}
sx={{maxWidth: 500}}
>
<ErrorBlock.Icon sx={{fontSize: '4rem', color: 'error.main'}} />
<ErrorBlock.Title variant="h3" sx={{color: 'error.main'}}>
Critical Server Error
</ErrorBlock.Title>
<ErrorBlock.Subtitle variant="body1" sx={{color: 'text.secondary'}}>
We're working on fixing this issue. Try refreshing in a few minutes.
</ErrorBlock.Subtitle>
<ErrorBlock.Action variant="outlined" size="large" href="#support" sx={{minWidth: 200}}>
Contact Support
</ErrorBlock.Action>
</ErrorBlock>
);
}
π Notesβ
- The component uses MUI's
Stackfor layout with defaultspacing={4} - Default max width is 400px with auto horizontal margins for centering
- All sub-components use
theme.palette.secondary.mainfor consistent coloring by default - The Icon component uses MUI's
ErrorOutlineicon from@mui/icons-material - The Title renders with
variant="h4"by default - The Action button uses
variant="contained"andsize="medium"by default - All sub-components support the
sxprop for MUI system styling - Block override pattern allows inserting custom blocks between default blocks
Built with β€οΈ using React, TypeScript, and MUI.