-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert react-error-overlay to React
- Loading branch information
Showing
25 changed files
with
866 additions
and
1,189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/react-error-overlay/src/components/CloseButton.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
/* @flow */ | ||
import React from 'react'; | ||
import { black } from '../styles'; | ||
|
||
const closeButtonStyle = { | ||
color: black, | ||
lineHeight: '1rem', | ||
fontSize: '1.5rem', | ||
padding: '1rem', | ||
cursor: 'pointer', | ||
position: 'absolute', | ||
right: 0, | ||
top: 0, | ||
}; | ||
|
||
type CloseCallback = () => void; | ||
function CloseButton({ close }: { close: CloseCallback }) { | ||
return ( | ||
<span | ||
title="Click or press Escape to dismiss." | ||
onClick={close} | ||
style={closeButtonStyle} | ||
> | ||
× | ||
</span> | ||
); | ||
} | ||
|
||
export default CloseButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
packages/react-error-overlay/src/components/Collapsible.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
/* @flow */ | ||
import React, { Component } from 'react'; | ||
import { black } from '../styles'; | ||
|
||
const _collapsibleStyle = { | ||
color: black, | ||
cursor: 'pointer', | ||
border: 'none', | ||
display: 'block', | ||
width: '100%', | ||
textAlign: 'left', | ||
background: '#fff', | ||
fontFamily: 'Consolas, Menlo, monospace', | ||
fontSize: '1em', | ||
padding: '0px', | ||
lineHeight: '1.5', | ||
}; | ||
|
||
const collapsibleCollapsedStyle = { | ||
..._collapsibleStyle, | ||
marginBottom: '1.5em', | ||
}; | ||
|
||
const collapsibleExpandedStyle = { | ||
..._collapsibleStyle, | ||
marginBottom: '0.6em', | ||
}; | ||
|
||
class Collapsible extends Component { | ||
state = { | ||
collapsed: true, | ||
}; | ||
|
||
toggleCollaped = () => { | ||
this.setState(state => ({ | ||
collapsed: !state.collapsed, | ||
})); | ||
}; | ||
|
||
render() { | ||
const count = this.props.children.length; | ||
const collapsed = this.state.collapsed; | ||
return ( | ||
<div> | ||
<button | ||
onClick={this.toggleCollaped} | ||
style={ | ||
collapsed ? collapsibleCollapsedStyle : collapsibleExpandedStyle | ||
} | ||
> | ||
{ | ||
(collapsed ? '▶' : '▼') + | ||
` ${count} stack frames were ` + | ||
(collapsed ? 'collapsed.' : 'expanded.') | ||
} | ||
</button> | ||
<div style={{ display: collapsed ? 'none' : 'block' }}> | ||
{this.props.children} | ||
<button | ||
onClick={this.toggleCollaped} | ||
style={collapsibleExpandedStyle} | ||
> | ||
{`▲ ${count} stack frames were expanded.`} | ||
</button> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default Collapsible; |
112 changes: 112 additions & 0 deletions
112
packages/react-error-overlay/src/components/ErrorOverlay.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
/* @flow */ | ||
import React, { PureComponent } from 'react'; | ||
import CloseButton from './CloseButton'; | ||
import NavigationBar from './NavigationBar'; | ||
import ErrorView from './ErrorView'; | ||
import Footer from './Footer'; | ||
import { black } from '../styles'; | ||
|
||
const overlayStyle = { | ||
position: 'relative', | ||
display: 'inline-flex', | ||
flexDirection: 'column', | ||
height: '100%', | ||
width: '1024px', | ||
maxWidth: '100%', | ||
overflowX: 'hidden', | ||
overflowY: 'auto', | ||
padding: '0.5rem', | ||
boxSizing: 'border-box', | ||
textAlign: 'left', | ||
fontFamily: 'Consolas, Menlo, monospace', | ||
fontSize: '11px', | ||
whiteSpace: 'pre-wrap', | ||
wordBreak: 'break-word', | ||
lineHeight: 1.5, | ||
color: black, | ||
}; | ||
|
||
class ErrorOverlay extends PureComponent { | ||
state = { | ||
currentIndex: 0, | ||
}; | ||
iframeWindow: window = null; | ||
|
||
previous = () => { | ||
this.setState((state, props) => ({ | ||
currentIndex: state.currentIndex > 0 | ||
? state.currentIndex - 1 | ||
: props.errorRecords.length - 1, | ||
})); | ||
}; | ||
|
||
next = () => { | ||
this.setState((state, props) => ({ | ||
currentIndex: state.currentIndex < props.errorRecords.length - 1 | ||
? state.currentIndex + 1 | ||
: 0, | ||
})); | ||
}; | ||
|
||
handleSortcuts = (e: KeyboardEvent) => { | ||
const { key } = e; | ||
if (key === 'Escape') { | ||
this.props.close(); | ||
} else if (key === 'ArrowLeft') { | ||
this.previous(); | ||
} else if (key === 'ArrowRight') { | ||
this.next(); | ||
} | ||
}; | ||
|
||
getIframeWindow = (element: HTMLDivElement) => { | ||
if (element) { | ||
const document = element.ownerDocument; | ||
this.iframeWindow = document.defaultView; | ||
} | ||
}; | ||
|
||
componentDidMount() { | ||
window.addEventListener('keydown', this.handleSortcuts); | ||
if (this.iframeWindow) { | ||
this.iframeWindow.addEventListener('keydown', this.handleSortcuts); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
window.removeEventListener('keydown', this.handleSortcuts); | ||
if (this.iframeWindow) { | ||
this.iframeWindow.removeEventListener('keydown', this.handleSortcuts); | ||
} | ||
} | ||
|
||
render() { | ||
const { errorRecords, close } = this.props; | ||
const totalErrors = errorRecords.length; | ||
return ( | ||
<div style={overlayStyle} ref={this.getIframeWindow}> | ||
<CloseButton close={close} /> | ||
{totalErrors > 1 && | ||
<NavigationBar | ||
currentError={this.state.currentIndex + 1} | ||
totalErrors={totalErrors} | ||
previous={this.previous} | ||
next={this.next} | ||
/>} | ||
<ErrorView errorRecord={errorRecords[this.state.currentIndex]} /> | ||
<Footer /> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default ErrorOverlay; |
Oops, something went wrong.