Skip to content

Commit

Permalink
Keybindings: Improve esc / exit / blur logic (grafana#19320)
Browse files Browse the repository at this point in the history
* Keybindings: Improve esc / exit / blur logic

* Slight modifications

* removed use of jquery
  • Loading branch information
torkelo authored Sep 24, 2019
1 parent 75dcaec commit 08cc4f0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/grafana-ui/src/components/Select/_Select.scss
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ $select-input-bg-disabled: $input-bg-disabled;
.gf-form-select-box__multi-value__remove {
text-align: center;
display: inline-block;
height: 14px;
vertical-align: middle;
margin-left: 2px;
position: relative;
top: 3px;
}

.gf-form-select-box__multi-value__label {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const ReduceTransformerEditor: React.FC<TransformerUIProps<ReduceTransfor
}) => {
return (
<StatsPicker
width={12}
width={25}
placeholder="Choose Stat"
allowMultiple
stats={options.reducers || []}
Expand Down
33 changes: 27 additions & 6 deletions public/app/core/services/keybindingSrv.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import $ from 'jquery';
import _ from 'lodash';

import coreModule from 'app/core/core_module';
Expand Down Expand Up @@ -47,9 +46,36 @@ export class KeybindingSrv {
this.bind('s o', this.openSearch);
this.bind('f', this.openSearch);
this.bind('esc', this.exit);
this.bindGlobal('esc', this.globalEsc);
}
}

globalEsc() {
const anyDoc = document as any;
const activeElement = anyDoc.activeElement;

// typehead needs to handle it
const typeaheads = document.querySelectorAll('.slate-typeahead--open');
if (typeaheads.length > 0) {
return;
}

// second check if we are in an input we can blur
if (activeElement && activeElement.blur) {
if (
activeElement.nodeName === 'INPUT' ||
activeElement.nodeName === 'TEXTAREA' ||
activeElement.hasAttribute('data-slate-editor')
) {
anyDoc.activeElement.blur();
return;
}
}

// ok no focused input or editor that should block this, let exist!
this.exit();
}

openSearch() {
appEvents.emit('show-dash-search');
}
Expand All @@ -71,11 +97,6 @@ export class KeybindingSrv {
}

exit() {
const popups = $('.popover.in, .slate-typeahead');
if (popups.length > 0) {
return;
}

appEvents.emit('hide-modal');

if (this.modalOpen) {
Expand Down
3 changes: 3 additions & 0 deletions public/app/features/explore/Typeahead.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ class Portal extends React.PureComponent<PortalProps, {}> {
render() {
if (this.props.isOpen) {
this.node.setAttribute('style', this.props.style);
this.node.classList.add(`slate-typeahead--open`);
return ReactDOM.createPortal(this.props.children, this.node);
} else {
this.node.classList.remove(`slate-typeahead--open`);
}

return null;
Expand Down
6 changes: 0 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15138,12 +15138,6 @@ react-popper@^1.3.3:
typed-styles "^0.0.7"
warning "^4.0.2"

react-portal@^3.1.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/react-portal/-/react-portal-3.2.0.tgz#4224e19b2b05d5cbe730a7ba0e34ec7585de0043"
dependencies:
prop-types "^15.5.8"

[email protected]:
version "7.1.1"
resolved "https://registry.yarnpkg.com/react-redux/-/react-redux-7.1.1.tgz#ce6eee1b734a7a76e0788b3309bf78ff6b34fa0a"
Expand Down

0 comments on commit 08cc4f0

Please sign in to comment.