diff --git a/packages/grafana-ui/.storybook/preview.ts b/packages/grafana-ui/.storybook/preview.ts index 21d1c945f7b..9e40a52a344 100644 --- a/packages/grafana-ui/.storybook/preview.ts +++ b/packages/grafana-ui/.storybook/preview.ts @@ -15,7 +15,6 @@ import lightTheme from '../../../public/sass/grafana.light.scss'; // @ts-ignore import darkTheme from '../../../public/sass/grafana.dark.scss'; import { GrafanaLight, GrafanaDark } from './storybookTheme'; -import { configure } from '@storybook/react'; import addons from '@storybook/addons'; const handleThemeChange = (theme: any) => { diff --git a/packages/grafana-ui/src/components/ErrorBoundary/ErrorBoundary.mdx b/packages/grafana-ui/src/components/ErrorBoundary/ErrorBoundary.mdx new file mode 100644 index 00000000000..042e977a10b --- /dev/null +++ b/packages/grafana-ui/src/components/ErrorBoundary/ErrorBoundary.mdx @@ -0,0 +1,56 @@ +import { ArgsTable } from "@storybook/addon-docs/blocks"; +import { ErrorBoundary, ErrorBoundaryAlert } from "./ErrorBoundary"; +import { ErrorWithStack } from "./ErrorWithStack"; + +# ErrorBoundary + +A React component that catches errors in child components. Useful for logging or displaying a fallback UI in case of errors. More information about error boundaries is available at [React documentation website](https://reactjs.org/docs/error-boundaries.html). + +### Usage + +```jsx +import { ErrorBoundary, Alert } from '@grafana/ui'; + + + {({ error }) => { + if (error) { + return ; + } + return ; + }} + +``` + +# ErrorBoundaryAlert + +An error boundary component with built-in alert to display in case of error. + +### Usage + +```jsx +import { ErrorBoundaryAlert } from '@grafana/ui'; + + + + +``` + +### Props + + + +# ErrorWithStack +A component that displays error together with its stack trace. + +### Usage + +```jsx +import { ErrorWithStack } from '@grafana/ui'; + + +``` + +### Props + + + diff --git a/packages/grafana-ui/src/components/ErrorBoundary/ErrorBoundary.story.tsx b/packages/grafana-ui/src/components/ErrorBoundary/ErrorBoundary.story.tsx new file mode 100644 index 00000000000..2231c657fdb --- /dev/null +++ b/packages/grafana-ui/src/components/ErrorBoundary/ErrorBoundary.story.tsx @@ -0,0 +1,58 @@ +import React, { useState } from 'react'; +import { ErrorBoundary, ErrorBoundaryAlert } from './ErrorBoundary'; +import { withCenteredStory } from '@grafana/ui/src/utils/storybook/withCenteredStory'; +import mdx from './ErrorBoundary.mdx'; +import { Button } from '../Button'; +import { ErrorWithStack } from './ErrorWithStack'; +import { Alert } from '../Alert/Alert'; + +export default { + title: 'General/ErrorBoundary', + component: ErrorBoundary, + decorators: [withCenteredStory], + parameters: { + docs: { + page: mdx, + }, + }, +}; + +const BuggyComponent = () => { + const [count, setCount] = useState(0); + + if (count > 2) { + throw new Error('Crashed'); + } + + return ( + + Increase the count to 3 to trigger error + setCount(count + 1)}>{count.toString()} + + ); +}; + +export const Basic = () => { + return ( + + {({ error }) => { + if (error) { + return ; + } + return ; + }} + + ); +}; + +export const WithStack = () => { + return ; +}; + +export const BoundaryAlert = () => { + return ( + + + + ); +};
Increase the count to 3 to trigger error