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

141 lines
3.2 KiB
JavaScript
Raw Normal View History

import React, {Component, PropTypes} from 'react';
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;
2016-04-07 03:09:14 +00:00
class Modal extends Component {
2016-11-29 21:28:22 +00:00
state = {
open: false,
forceRefreshCounter: 0,
zIndex: globalZIndex
};
2016-11-29 21:28:22 +00:00
_handleSetNodeRef = n => this._node = n;
_handleClick = e => {
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++) {
if (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-29 21:28:22 +00:00
};
2016-11-23 23:57:34 +00:00
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),
});
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
}
}
2016-07-07 20:10:55 +00:00
hide () {
this.setState({open: false});
2016-04-07 03:09:14 +00:00
}
componentDidMount () {
// In order for this to work, there needs to be tabIndex of -1 on the modal container
2016-09-07 22:21:10 +00:00
this._keydownCallback = 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();
}
2016-09-13 17:29:09 +00:00
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();
}
2016-09-07 22:21:10 +00:00
};
this._node.addEventListener('keydown', this._keydownCallback);
}
componentWillUnmount () {
2016-09-07 22:21:10 +00:00
this._node.removeEventListener('keydown', this._keydownCallback);
}
2016-07-07 20:10:55 +00:00
render () {
const {tall, top, wide, className} = this.props;
2016-11-29 21:28:22 +00:00
const {open, zIndex, forceRefreshCounter} = this.state;
2016-04-07 03:09:14 +00:00
2016-07-07 20:10:55 +00:00
const classes = classnames(
'modal',
className,
{'modal--open': open},
2016-07-20 18:35:08 +00:00
{'modal--fixed-height': tall},
{'modal--fixed-top': top},
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
);
2016-04-07 03:09:14 +00:00
return (
2016-11-29 21:28:22 +00:00
<div ref={this._handleSetNodeRef}
tabIndex="-1"
className={classes}
style={{zIndex: zIndex}}
2016-11-29 21:28:22 +00:00
onClick={this._handleClick}>
<div className="modal__content" key={forceRefreshCounter}>
2017-01-23 22:41:31 +00:00
<div className="modal__backdrop overlay" onClick={() => this.hide()}></div>
2016-04-07 03:09:14 +00:00
{this.props.children}
</div>
</div>
2016-04-07 03:09:14 +00:00
)
}
}
Modal.propTypes = {
2016-07-20 18:35:08 +00:00
tall: PropTypes.bool,
top: PropTypes.bool,
wide: PropTypes.bool,
dontFocus: PropTypes.bool,
2016-11-29 21:28:22 +00:00
closeOnKeyCodes: PropTypes.array,
freshState: PropTypes.bool,
2016-04-07 03:09:14 +00:00
};
2016-04-15 05:23:54 +00:00
export default Modal;