-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
57 changes: 57 additions & 0 deletions
57
src/ui/public/accessibility/__tests__/kbn_ui_ace_keyboard_mode.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,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'); | ||
}); | ||
}); | ||
|
||
}); |
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 @@ | ||
/** | ||
* 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’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); | ||
} | ||
})); |
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,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; | ||
} | ||
} | ||
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
<div | ||
json-input | ||
require-keys="true" | ||
kbn-ui-ace-keyboard-mode | ||
ui-ace="{ | ||
mode: 'json', | ||
onLoad: aceLoaded | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
andtext-align
to refine the appearance.There was a problem hiding this comment.
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. TheisInactive
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 :-)There was a problem hiding this comment.
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.