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

Should addAccessibleInputListener automatically preventDefault? #636

Closed
samreid opened this issue Jun 23, 2017 · 6 comments
Closed

Should addAccessibleInputListener automatically preventDefault? #636

samreid opened this issue Jun 23, 2017 · 6 comments
Assignees

Comments

@samreid
Copy link
Member

samreid commented Jun 23, 2017

In phetsims/circuit-construction-kit-common#307 @jessegreenberg added preventDefault calls to prevent e.g., firefox from navigating pages back. Should this be automatically implemented in addAccessibleInputListener so we don't end up scattering preventDefault everywhere?

@jessegreenberg
Copy link
Contributor

I am afraid that preventing default in addAccessibleInputListener would be too aggressive. For instance, calling event.preventDefault() on all keydown event listeners will also prevent use of tab key to go to the next item and ctrl + r to refresh the page. We only want to preventDefault when specific keys are pressed in the listener.

@jessegreenberg
Copy link
Contributor

jessegreenberg commented Jul 6, 2017

Maybe we can factor out common interactions that will require us to prevent default. Like SimpleDragHadler we could have a KeyboardDeleteHandler (we can think of a better name later). In CCK we have

        // on delete or backspace, the focused circuit element should be deleted
        if ( code === Input.KEY_DELETE || code === Input.KEY_BACKSPACE ) {

          // prevent default so 'backspace' and 'delete' don't navigate back a page in Firefox
          event.preventDefault();
          circuit.circuitElements.remove( circuitElement );
        }

Perhaps the API could look like

var myKeyboardDeleteHandler = new KeyboardDeleteHandler( {
  onDelete: function( event ) {
    circuit.circuitElements.remove( circuitElement );
  }
})
myNode.addAccessibleInputListener( myKeyboardDeleteHandler )

And the handler calls preventDefault for the correct keys so it isn't needed in sim code?

@jessegreenberg
Copy link
Contributor

We could end up with a lot of these
KeyboardDeleteHandler
KeyboardSliderHandler
KeyboardDragHandler
KeyboardArrowKeyHandler
KeyboardNumberHandler

I am not sure how I feel about lots of these handlers. But at least the reasons for preventDefault would be isolated and documented in each one.

@samreid
Copy link
Member Author

samreid commented Jul 6, 2017

Perhaps we should see (or anticipate) what patterns come up frequently. Perhaps an abstraction like:

new KeyHandler([Input.KEY_DELETE,Input.KEY_BACKSPACE],{
    preventDefault:true
},function(){ /*callback*/});

would work well? It's difficult to say whether this would be necessary or useful.

@jessegreenberg
Copy link
Contributor

jessegreenberg commented Jul 6, 2017

Agreed, though an abstraction does seem useful. I found 44 usages of addAccessibleInputListener, and inspection showed about 1/4 use event.preventDefault() in some way.

@zepumph
Copy link
Member

zepumph commented Oct 14, 2022

I spoke with @jessegreenberg about this today.

We believe that preventDefault should still be called in the exact locations that it is needed, and not eagerly.

I believe this could be handled by better encapsulation of keyboard input handlers that may be worked on in #1445. Like from #636 (comment)

We are ready to close this issue.

@zepumph zepumph closed this as completed Oct 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants