I've been working on a mobile app that involves removing the 300ms delay on mobile, focusing on inputs, and bringing up the virtual keyboard (this post isn't about looking for a solution like fastclick or using touch-event, but specifically about preventDefault). A lot of the issues I'm having could be solved if I was able to be explicit on what "prevent default" means.
The preventDefault spec says:
"...any default action normally taken by the implementation as a result of the event will not occur."
http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-Event-preventDefault
The problem is that browsers seem to have a different meaning of what the defaults are, and lumps all of these different types of defaults into one. You can’t prevent one or two of the defaults, you either always do or always do not prevent all of the defaults.
For example, I need disable scrolling when a textarea is focused, so to do this I can set e.preventDefault() on touchstart. However, if I preventDefault on touchstart then the virtual keyboard does not show in Android (it does show on iOS).
This isn't surprising the keyboard doesn't show in Android when e.preventDefault() is called within touchstart, but because both native scrolling and bringing up the virtual keyboard are part of the event defaults for a textarea, I'm not able to specify exactly what should happen. Other related defaults that can get prevented include being able to move the text caret within a textarea, suggested words, setting focus, etc.
So with preventDefault actually meaning to prevent every default that can happen from this event on a target, what if additional parameters could be added to allow developers to be more explicit of what should be prevented. This is far from standards ready and isn’t the proposal, but the code below is an example of how it could work:
e.preventDefault({ scroll: true });
or
e.preventDefault({ keyboard: true, textCaret: true });
So the first example would only prevent the scroll from happening, and the second example would prevent the keyboard from showing or a text cursor from showing. Every target and event has different defaults that happen, and each browser it seems has different ideas on what those defaults are, but this would allow devs to control which ones get prevented instead of all of them. I'm just looking to open a discussion on whether this would be useful and/or possible. Thanks.
--
--
Chromium Discussion mailing list: chromium...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-discuss
This all stems from developing ionicframework.com, and finding that balance when having the ability to scroll an element with touch/mouse events, bringing up the virtual keyboard, moving the input's text caret, removing the 300ms delay, setting an "active" css class on buttons, etc. Most of our code could be simplified if preventDefault wasn't preventing everything from happening, along with the problems of it acting differently on Android and iOS. The end goal is to be able to control what defaults get prevented, so if newer APIs would be the better route then I'd be all for it.
A virtual keyboard API would also solve many of these issues. Currently it's pretty difficult to bring up the keyboard with just focus() because it needs to come from a user interaction like touchstart (but from what I understand this is by design). Other valuable information would be the keyboard height, it's layout type (like iOS split keyboard), removing the "Next" and "Done" accessory buttons if you wanted, adding your own accessory buttons, customizing the "Enter" button text (like to Search, Go, Filter), events triggered on keyboard show, hide and word suggestions, and possibly having some more control of the keyboard's actual keypad layout. When comparing native mobile app development to html5, these are some of the areas that could help web and hybrid app developers further.
Thanks,Adam