Skip to content

Commit 5ddd854

Browse files
committed
Initial commit
0 parents  commit 5ddd854

File tree

9 files changed

+3642
-0
lines changed

9 files changed

+3642
-0
lines changed

.babelrc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"env": {
3+
"commonjs": {
4+
"plugins": [
5+
"flow-react-proptypes",
6+
[
7+
"transform-react-remove-prop-types", {
8+
"mode": "wrap"
9+
}
10+
]
11+
],
12+
"presets": [
13+
"es2015",
14+
"react",
15+
"stage-1"
16+
]
17+
},
18+
"es": {
19+
"plugins": [
20+
"transform-runtime",
21+
"flow-react-proptypes",
22+
[
23+
"transform-react-remove-prop-types", {
24+
"mode": "wrap"
25+
}
26+
]
27+
],
28+
"presets": [
29+
"es2015-rollup",
30+
"react",
31+
"stage-1"
32+
]
33+
},
34+
"production": {
35+
"comments": false,
36+
"plugins": [
37+
"transform-runtime"
38+
],
39+
"presets": [
40+
"es2015",
41+
"react",
42+
"stage-1"
43+
]
44+
},
45+
}
46+
}

.flowconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[ignore]
2+
3+
[include]
4+
5+
[libs]
6+
7+
[options]
8+
9+
[lints]

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
build
3+
coverage
4+
dist
5+
node_modules
6+
npm-debug.log
7+
styles.css
8+
.vscode

package.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "react-error-boundary",
3+
"version": "0.0.1",
4+
"description": "Simple reusable React error boundary component",
5+
"main": "dist/commonjs/react-error-boundary.js",
6+
"scripts": {
7+
"build": "npm run build:commonjs && npm run build:es && npm run build:umd",
8+
"build:commonjs": "npm run clean:commonjs && cross-env NODE_ENV=production cross-env BABEL_ENV=commonjs babel src --out-dir dist/commonjs",
9+
"build:es": "npm run clean:es && cross-env NODE_ENV=production cross-env BABEL_ENV=es babel src --out-dir dist/es",
10+
"build:umd": "npm run clean:umd && cross-env NODE_ENV=production webpack --config webpack.config.umd.js --bail",
11+
"clean": "npm run clean:commonjs && npm run clean:es && npm run clean:umd",
12+
"clean:commonjs": "rimraf dist/commonjs",
13+
"clean:es": "rimraf dist/es",
14+
"clean:umd": "rimraf dist/umd",
15+
"flow": "flow check src",
16+
"lint": "eslint 'src/**/*.js'",
17+
"test": "echo \"Error: no test specified\" && exit 1"
18+
},
19+
"author": "Brian Vaughn <brian.david.vaughn@gmail.com>",
20+
"license": "MIT",
21+
"devDependencies": {
22+
"babel-cli": "^6.24.1",
23+
"babel-plugin-flow-react-proptypes": "^3.4.2",
24+
"babel-plugin-transform-react-remove-prop-types": "^0.4.6",
25+
"babel-plugin-transform-runtime": "^6.23.0",
26+
"babel-plugin-typecheck": "^3.9.0",
27+
"babel-preset-es2015": "^6.24.1",
28+
"babel-preset-es2015-rollup": "^3.0.0",
29+
"babel-preset-react": "^6.24.1",
30+
"babel-preset-stage-1": "^6.24.1",
31+
"cross-env": "^5.0.1",
32+
"eslint": "^4.2.0",
33+
"flow-cli": "^0.0.0-pre",
34+
"rimraf": "^2.6.1"
35+
},
36+
"dependencies": {
37+
"babel-core": "^6.25.0",
38+
"babel-loader": "^7.1.1",
39+
"babel-runtime": "^6.23.0",
40+
"prop-types": "^15.5.10",
41+
"webpack": "^3.3.0"
42+
}
43+
}

src/ErrorBoundary.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/** @flow */
2+
3+
import React, { Component } from 'react';
4+
import ErrorBoundaryFallbackComponent from './ErrorBoundaryFallbackComponent';
5+
6+
type Props = {
7+
children?: any,
8+
FallbackComponent: any,
9+
onError?: (error: Error) => void,
10+
};
11+
12+
type ErrorInfo = {
13+
componentStack: string,
14+
};
15+
16+
type State = {
17+
error: ?Error,
18+
info: ?ErrorInfo,
19+
};
20+
21+
class ErrorBoundary extends Component {
22+
props: Props;
23+
state: State;
24+
25+
static defaultProps = {
26+
FallbackComponent: ErrorBoundaryFallbackComponent,
27+
};
28+
29+
constructor(props: Props, context: any) {
30+
super(props, context);
31+
32+
this.state = {
33+
error: null,
34+
info: null,
35+
};
36+
}
37+
38+
unstable_handleError(error: Error, info: ErrorInfo):void {
39+
// This method is a fallback for react <= 16.0.0-alpha.13
40+
this.componentDidError(error, info);
41+
}
42+
43+
componentDidError(error: Error, info: ErrorInfo):void {
44+
this.setState({error, info});
45+
}
46+
47+
render() {
48+
const {children, FallbackComponent} = this.props;
49+
const {error, info} = this.state;
50+
51+
if (error !== null) {
52+
return (
53+
<FallbackComponent componentStack={info && info.componentStack} error={error} />
54+
);
55+
}
56+
57+
return children;
58+
}
59+
}
60+
61+
export default ErrorBoundary;

src/ErrorBoundaryFallbackComponent.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/** @flow */
2+
3+
import React from 'react';
4+
5+
type Props = {
6+
componentStack: string,
7+
error: Error,
8+
};
9+
10+
const ErrorBoundaryFallbackComponent = ({ componentStack, error }: Props) => (
11+
<div style={style} title={error.toString()}>
12+
<svg style={svgStyle} viewBox="0 0 24 24" preserveAspectRatio="xMidYMid">
13+
<path d={`M20,12A8,8 0 0,0 12,4A8,8 0 0,0 4,12A8,8 0 0,0 12,20A8,8 0 0,0 20,
14+
12M22,12A10,10 0 0,1 12,22A10,10 0 0,1 2,12A10,10 0 0,1 12,2A10,10 0 0,1 22,
15+
12M15.5,8C16.3,8 17,8.7 17,9.5C17,10.3 16.3,11 15.5,11C14.7,11 14,10.3 14,
16+
9.5C14,8.7 14.7,8 15.5,8M10,9.5C10,10.3 9.3,11 8.5,11C7.7,11 7,10.3 7,9.5C7,
17+
8.7 7.7,8 8.5,8C9.3,8 10,8.7 10,9.5M12,14C13.75,14 15.29,14.72 16.19,
18+
15.81L14.77,17.23C14.32,16.5 13.25,16 12,16C10.75,16 9.68,16.5 9.23,
19+
17.23L7.81,15.81C8.71,14.72 10.25,14 12,14Z`} />
20+
</svg>
21+
</div>
22+
);
23+
24+
const style = {
25+
height: '100%',
26+
maxHeight: '100vh',
27+
width: '100%',
28+
maxWidth: '100vw',
29+
display: 'flex',
30+
flexDirection: 'column',
31+
alignItems: 'center',
32+
textAlign: 'center',
33+
backgroundColor: '#C00',
34+
color: '#FFF',
35+
boxSizing: 'border-box',
36+
};
37+
38+
const svgStyle = {
39+
fill: 'currentColor',
40+
flex: '1 1 auto',
41+
};
42+
43+
export default ErrorBoundaryFallbackComponent;

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/** @flow */
2+
3+
export default from './ErrorBoundary';

webpack.config.umd.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const path = require('path')
2+
const webpack = require('webpack')
3+
4+
module.exports = {
5+
devtool: 'source-map',
6+
entry: {
7+
'react-error-boundary': './src/index.js'
8+
},
9+
output: {
10+
path: path.join(__dirname, 'dist/umd'),
11+
filename: '[name].js',
12+
libraryTarget: 'umd',
13+
library: 'ReactErrorBoundary'
14+
},
15+
externals: {
16+
'react': 'React',
17+
'react-dom': 'ReactDOM'
18+
},
19+
plugins: [
20+
new webpack.optimize.UglifyJsPlugin({
21+
beautify: true,
22+
comments: true,
23+
mangle: false
24+
})
25+
],
26+
module: {
27+
loaders: [
28+
{
29+
test: /\.js$/,
30+
loaders: ['babel-loader'],
31+
include: path.join(__dirname, 'src')
32+
}
33+
]
34+
}
35+
}

0 commit comments

Comments
 (0)