Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create keyboard mode for ui-ace editor #13339

Merged
merged 3 commits into from
Aug 9, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ <h1 class="kuiTitle">

<div
ng-if="field.type === 'json' || field.type === 'array'"
kbn-ui-ace-keyboard-mode
ui-ace="{ onLoad: aceLoaded, mode: 'json' }"
id="{{field.name}}"
ng-model="field.value"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { savedObjectManagementRegistry } from 'plugins/kibana/management/saved_o
import objectViewHTML from 'plugins/kibana/management/sections/objects/_view.html';
import uiRoutes from 'ui/routes';
import { uiModules } from 'ui/modules';
import 'ui/accessibility/kbn_ui_ace_keyboard_mode';
import { castEsToKbnFieldTypeName } from '../../../../../../utils';
import { SavedObjectsClientProvider } from 'ui/saved_objects';

Expand Down
57 changes: 57 additions & 0 deletions src/ui/public/accessibility/__tests__/kbn_ui_ace_keyboard_mode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import angular from 'angular';
import sinon from 'sinon';
import expect from 'expect.js';
import ngMock from 'ng_mock';
import '../kbn_ui_ace_keyboard_mode';
import {
ENTER_KEY,
ESC_KEY_CODE,
} from 'ui_framework/services';

describe('kbnUiAceKeyboardMode directive', () => {
let element;

beforeEach(ngMock.module('kibana'));

beforeEach(ngMock.inject(($compile, $rootScope) => {
element = $compile(`<div ui-ace kbn-ui-ace-keyboard-mode></div>`)($rootScope.$new());
}));

it('should add the hint element', () => {
expect(element.find('.ui-ace-keyboard-hint').length).to.be(1);
});

describe('hint element', () => {
it('should be tabable', () => {
expect(element.find('.ui-ace-keyboard-hint').attr('tabindex')).to.be('0');
});

it('should move focus to textbox and be inactive if pressed enter on it', () => {
const textarea = element.find('textarea');
sinon.spy(textarea[0], 'focus');
const ev = angular.element.Event('keydown'); // eslint-disable-line new-cap
ev.keyCode = ENTER_KEY;
element.find('.ui-ace-keyboard-hint').trigger(ev);
expect(textarea[0].focus.called).to.be(true);
expect(element.find('.ui-ace-keyboard-hint').hasClass('ui-ace-keyboard-hint-inactive')).to.be(true);
});

it('should be shown again, when pressing Escape in ace editor', () => {
const textarea = element.find('textarea');
const hint = element.find('.ui-ace-keyboard-hint');
sinon.spy(hint[0], 'focus');
const ev = angular.element.Event('keydown'); // eslint-disable-line new-cap
ev.keyCode = ESC_KEY_CODE;
textarea.trigger(ev);
expect(hint[0].focus.called).to.be(true);
expect(hint.hasClass('ui-ace-keyboard-hint-inactive')).to.be(false);
});
});

describe('ui-ace textarea', () => {
it('should not be tabable anymore', () => {
expect(element.find('textarea').attr('tabindex')).to.be('-1');
});
});

});
80 changes: 80 additions & 0 deletions src/ui/public/accessibility/kbn_ui_ace_keyboard_mode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* The `kbn-ui-ace-keyboard-mode` directive should be used on any element, that
* `ui-ace` is used on. It will prevent the keyboard trap, that ui-ace usually
* has, i.e. tabbing into the box won't give you any possibilities to leave
* it via keyboard again, since tab inside the textbox works like a tab character.
*
* This directive won't change anything, if the user uses the mouse. But if she
* tabs to the ace editor, an overlay will be shown, that you have to press Enter
* to enter editing mode, and that it can be left by pressing Escape again.
*
* That way the ui-ace editor won't trap keyboard focus, and won't cause that
* accessibility issue anymore.
*/

import angular from 'angular';
import { uiModules } from 'ui/modules';
import './kbn_ui_ace_keyboard_mode.less';
import { ESC_KEY_CODE, ENTER_KEY } from 'ui_framework/services';

let aceKeyboardModeId = 0;

uiModules.get('kibana').directive('kbnUiAceKeyboardMode', () => ({
restrict: 'A',
link(scope, element) {
const uniqueId = `uiAceKeyboardHint-${scope.$id}-${aceKeyboardModeId++}`;

const hint = angular.element(
`<div
class="uiAceKeyboardHint"
id="${uniqueId}"
tabindex="0"
role="application"
>
<p class="kuiText kuiVerticalRhythmSmall">
Press Enter to start editing.
</p>
<p class="kuiText kuiVerticalRhythmSmall">
When you&rsquo;re done, press Escape to stop editing.
</p>
</div>
`);

const uiAceTextbox = element.find('textarea');

function startEditing() {
// We are not using ng-class in the element, so that we won't need to $compile it
hint.addClass('uiAceKeyboardHint-isInactive');
uiAceTextbox.focus();
}

function enableOverlay() {
hint.removeClass('uiAceKeyboardHint-isInactive');
}

hint.keydown((ev) => {
if (ev.keyCode === ENTER_KEY) {
ev.preventDefault();
startEditing();
}
});

uiAceTextbox.blur(() => {
enableOverlay();
});

uiAceTextbox.keydown((ev) => {
if (ev.keyCode === ESC_KEY_CODE) {
ev.preventDefault();
ev.stopPropagation();
enableOverlay();
hint.focus();
}
});

hint.click(startEditing);
// Prevent tabbing into the ACE textarea, we now handle all focusing for it
uiAceTextbox.attr('tabindex', '-1');
element.prepend(hint);
}
}));
26 changes: 26 additions & 0 deletions src/ui/public/accessibility/kbn_ui_ace_keyboard_mode.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@import (reference) "~ui/styles/variables";

.uiAceKeyboardHint {
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
background: rgba(255, 255, 255, 0.7);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
opacity: 0;

&:focus {
opacity: 1;
border: 2px solid @globalColorBlue;
z-index: 1000;
}

&.uiAceKeyboardHint-isInactive {
display: none;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we reformat this to use camel case per the style guide and to nest these states to be more efficient? I also added flex-direction and text-align to refine the appearance.

.uiAceKeyboardHint {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  background: rgba(255, 255, 255, 0.7);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  opacity: 0;

  &.uiAceKeyboardHint-isInactive {
    display: none;
  }

  &:focus {
    opacity: 1;
    border: 2px solid @globalColorBlue;
    z-index: 1000;
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will reformat them to use camel case syntax. Why would nesting the states be more efficient? In CSS the :focus will anyway compile to the same, so no difference there. The isInactive state should actually be slower, since it has to check multiple classes instead of a single on on the element. Though since we have the example for states like this in our styleguide I will of course also fix it (though in my opinion giving states the full prefix (uiAceKeyboardHint-), makes nesting kind of unnecessary, but I won't fight the styleguide here :-)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry I meant efficient in terms of readability. By using &:focus it's a bit easier to scan the styles and grok the context of all the available selectors -- they're all related to the "uiAceKeyboardHint" component.

1 change: 1 addition & 0 deletions src/ui/public/filter_editor/filter_query_dsl_editor.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div
json-input
require-keys="true"
kbn-ui-ace-keyboard-mode
ui-ace="{
mode: 'json',
onLoad: aceLoaded
Expand Down
1 change: 1 addition & 0 deletions src/ui/public/filter_editor/filter_query_dsl_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'ace';
import _ from 'lodash';
import { uiModules } from 'ui/modules';
import template from './filter_query_dsl_editor.html';
import 'ui/accessibility/kbn_ui_ace_keyboard_mode';

const module = uiModules.get('kibana');
module.directive('filterQueryDslEditor', function () {
Expand Down