insomnia/app/ui/components/base/modal.js

167 lines
3.7 KiB
JavaScript
Raw Normal View History

2017-08-10 01:56:27 +00:00
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import autobind from 'autobind-decorator';
import classnames from 'classnames';
2016-11-10 02:40:53 +00:00
import {isMac} from '../../../common/constants';
2016-04-07 03:09:14 +00:00
// Keep global z-index reference so that every modal will
// appear over top of an existing one.
let globalZIndex = 1000;
@autobind
class Modal extends PureComponent {
constructor (props) {
super(props);
this.state = {
open: false,
forceRefreshCounter: 0,
zIndex: globalZIndex
};
}
_handleKeyDown (e) {
if (!this.state.open) {
return;
}
// Don't bubble up meta events up past the modal no matter what
// Example: ctrl+Enter to send requests
const isMeta = isMac() ? e.metaKey : e.ctrlKey;
if (isMeta) {
e.stopPropagation();
}
// Don't check for close keys if we don't want them
if (this.props.noEscape) {
return;
}
const closeOnKeyCodes = this.props.closeOnKeyCodes || [];
const pressedEscape = e.keyCode === 27;
const pressedElse = closeOnKeyCodes.find(c => c === e.keyCode);
if (pressedEscape || pressedElse) {
e.preventDefault();
// Pressed escape
this.hide();
}
}
_handleClick (e) {
// Don't check for close keys if we don't want them
if (this.props.noEscape) {
return;
}
2016-04-07 03:09:14 +00:00
// Did we click a close button. Let's check a few parent nodes up as well
// because some buttons might have nested elements. Maybe there is a better
// way to check this?
let target = e.target;
let shouldHide = false;
2016-04-08 04:05:08 +00:00
2016-04-07 03:09:14 +00:00
for (let i = 0; i < 5; i++) {
2017-07-21 18:59:17 +00:00
if (target instanceof HTMLElement && target.hasAttribute('data-close-modal')) {
shouldHide = true;
2016-04-07 03:09:14 +00:00
break;
}
target = target.parentNode;
}
2016-04-08 04:05:08 +00:00
if (shouldHide) {
this.hide();
2016-04-07 03:27:45 +00:00
}
}
2016-11-23 23:57:34 +00:00
_setModalRef (n) {
this._node = n;
}
2016-07-07 20:10:55 +00:00
show () {
2016-11-29 21:28:22 +00:00
const {freshState} = this.props;
const {forceRefreshCounter} = this.state;
this.setState({
open: true,
zIndex: globalZIndex++,
forceRefreshCounter: forceRefreshCounter + (freshState ? 1 : 0)
2016-11-29 21:28:22 +00:00
});
if (this.props.dontFocus) {
return;
}
setTimeout(() => this._node && this._node.focus());
}
2016-07-07 20:10:55 +00:00
toggle () {
if (this.state.open) {
this.hide();
} else {
this.show();
2016-04-09 19:24:33 +00:00
}
}
isOpen () {
return this.state.open;
}
2016-07-07 20:10:55 +00:00
hide () {
this.setState({open: false});
this.props.onHide && this.props.onHide();
2016-04-07 03:09:14 +00:00
}
2016-07-07 20:10:55 +00:00
render () {
const {tall, wide, noEscape, className, children} = this.props;
2016-11-29 21:28:22 +00:00
const {open, zIndex, forceRefreshCounter} = this.state;
2016-04-07 03:09:14 +00:00
if (!open) {
return null;
}
2016-07-07 20:10:55 +00:00
const classes = classnames(
'modal',
className,
2016-07-20 18:35:08 +00:00
{'modal--fixed-height': tall},
{'modal--noescape': noEscape},
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
2016-10-21 17:20:36 +00:00
{'modal--wide': wide},
2016-07-14 22:48:56 +00:00
);
const styles = {};
if (open) {
styles.zIndex = zIndex;
}
2016-04-07 03:09:14 +00:00
return (
<div ref={this._setModalRef}
onKeyDown={this._handleKeyDown}
2016-11-29 21:28:22 +00:00
tabIndex="-1"
className={classes}
style={styles}
2016-11-29 21:28:22 +00:00
onClick={this._handleClick}>
2017-06-16 22:31:22 +00:00
<div className="modal__backdrop overlay theme--overlay" data-close-modal></div>
<div className="modal__content__wrapper">
<div className="modal__content" key={forceRefreshCounter}>
{children}
</div>
2016-04-07 03:09:14 +00:00
</div>
</div>
);
2016-04-07 03:09:14 +00:00
}
}
Modal.propTypes = {
2016-07-20 18:35:08 +00:00
tall: PropTypes.bool,
wide: PropTypes.bool,
noEscape: PropTypes.bool,
dontFocus: PropTypes.bool,
2016-11-29 21:28:22 +00:00
closeOnKeyCodes: PropTypes.array,
onHide: PropTypes.func,
freshState: PropTypes.bool,
children: PropTypes.node,
className: PropTypes.string
2016-04-07 03:09:14 +00:00
};
2016-04-15 05:23:54 +00:00
export default Modal;