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

Fix tab trap in menubar #373

Merged
merged 4 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Lumino

[![Build Status](https://github.com/jupyterlab/lumino/workflows/Tests/badge.svg?branch=main)](https://github.com/jupyterlab/lumino/actions?query=branch%3Amain+workflow%3A%22Tests%22)
[![Documentation Status](https://readthedocs.org/projects/jupyterlab/badge/?version=stable)](http://lumino.readthedocs.io/en/stable/)
[![Documentation Status](https://readthedocs.org/projects/jupyterlab/badge/?version=latest)](https://lumino.readthedocs.io/en/latest/)
[![GitHub](https://img.shields.io/badge/issue_tracking-github-blue.svg)](https://github.com/jupyterlab/lumino/issues)
[![Discourse](https://img.shields.io/badge/help_forum-discourse-blue.svg)](https://discourse.jupyter.org/c/jupyterlab)
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/jupyterlab/lumino/main?urlpath=lab/tree/examples)
Expand Down
16 changes: 12 additions & 4 deletions packages/widgets/src/menubar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,15 +409,23 @@ export class MenuBar extends Widget {

/**
* Handle the `'keydown'` event for the menu bar.
gabalafou marked this conversation as resolved.
Show resolved Hide resolved
*
* #### Notes
* All keys are trapped except the tab key that is ignored.
*/
private _evtKeyDown(event: KeyboardEvent): void {
// A menu bar handles all keydown events.
event.preventDefault();
event.stopPropagation();

// Fetch the key code for the event.
let kc = event.keyCode;

// Do not trap the tab key.
if (kc === 9) {
return;
}

// A menu bar handles all other keydown events.
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 wonder if the logic here is sound.

Originally this handler only called preventDefault and stopPropagation if it actually handled the key. But then the handler was rewritten to handle mnemonics. I lightly suspect that the decision to call prevent and stop on all keys was done to make the handler more readable, without too much thought given to possible negative consequences.

I wrote this PR conservatively, meaning I kept everything else about this function the same EXCEPT when the tab key is pressed. But I guess it's worth asking whether the handler should be refactored to only intercept key events that it actually does something with.

I could imagine a handler that looks more like the following:

keyDown() {
  let kc = event.keyCode;
  let endEvent = () => { 
    event.preventDefault();
    event.stopPropagation();
  };
  // Enter, Up Arrow, Down Arrow
  if (kc === 13 || kc === 38 || kc === 40) {
    endEvent();
    this.openActiveMenu();
    return;
  }
  // ...
}

In other words, it would go back to looking like the original implementation.

Copy link
Member

Choose a reason for hiding this comment

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

Let's pursue this train of thought in another PR if necessary so we can merge this one and iterate.

event.preventDefault();
event.stopPropagation();

// Enter, Up Arrow, Down Arrow
if (kc === 13 || kc === 38 || kc === 40) {
this.openActiveMenu();
Expand Down
6 changes: 6 additions & 0 deletions packages/widgets/tests/src/menubar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,12 @@ describe('@lumino/widgets', () => {
});

context('keydown', () => {
it('should bail on Tab', () => {
let event = new KeyboardEvent('keydown', { keyCode: 9 });
bar.node.dispatchEvent(event);
expect(event.defaultPrevented).to.equal(false);
});

it('should open the active menu on Enter', () => {
let menu = bar.activeMenu!;
bar.node.dispatchEvent(
Expand Down