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

elements respond to enter or spacebar #590

Merged
40 changes: 40 additions & 0 deletions packages/widgets/src/tabbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ export class TabBar<T> extends Widget {
protected onBeforeAttach(msg: Message): void {
this.node.addEventListener('pointerdown', this);
this.node.addEventListener('dblclick', this);
this.node.addEventListener('keydown', this);
}

/**
Expand Down Expand Up @@ -711,6 +712,44 @@ export class TabBar<T> extends Widget {
* Handle the `'keydown'` event for the tab bar.
*/
private _evtKeyDown(event: KeyboardEvent): void {
// Allow for navigation using tab key
if (event.key === 'Tab') {
return;
}

// Check if Enter or Spacebar key has been pressed and open that tab
if (
event.key === 'Enter' ||
event.key === 'Spacebar' ||
event.key === ' '
) {
event.preventDefault();
event.stopPropagation();

// Get focus element that is in focus by the tab key
let focusedElement = document.activeElement;

if (focusedElement) {
if (focusedElement.className === 'lm-TabBar-addButton') {
g547315 marked this conversation as resolved.
Show resolved Hide resolved
this._addRequested.emit();
} else {
// Genatate the coordinates for the tab in focus.
let tab = focusedElement.getBoundingClientRect();
let clientX = tab.left + tab.width / 2;
let clientY = tab.top + tab.height / 2;

// send simulated request with genatated coordinates.
let simPointerClick = new PointerEvent('', {
button: 0,
clientX: clientX,
clientY: clientY
});

this._evtPointerDown(simPointerClick);
this._evtPointerUp(simPointerClick);
g547315 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
// Stop all input events during drag.
event.preventDefault();
event.stopPropagation();
Expand Down Expand Up @@ -1840,6 +1879,7 @@ namespace Private {

let add = document.createElement('div');
add.className = 'lm-TabBar-addButton lm-mod-hidden';
add.setAttribute('tabindex', '0');
node.appendChild(add);
return node;
}
Expand Down