react-dates: SingleDatePicker focus problems on touch devices
Hello,
Related to #560 – Even after the recent change that allows readonly to be set and respected rather than default to isTouchDevice, things don’t behave right on touch devices that also have keyboards, like a laptop or desktop with a touch screen.
Then problem seems to be that in SingleDatePicker there is focus logic that steals away the inputs focus and places focus on the DayPicker if isTouchDevice is true.
https://github.com/airbnb/react-dates/blob/v13.0.0/src/components/SingleDatePicker.jsx#L182
There is no way to short-circut this logic and even if you manage to set readonly correctly, you still are forced to use the DayPicker rather than the input on any touch device, even if you have a keyboard available.
Any ideas on how to proceed?
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 2
- Comments: 22 (13 by maintainers)
@majapw would still consider this an issue. The current feature detection for touch devices (
readOnly&isTouchDevice) is not sufficient, and isTouchDevice returns a false positive in Chrome on Mac + PC, causing the date picker’s input field to not be editable with the keyboard on Desktop.Probably don’t want to import an entire library like Modernizr for this, but maybe we can copy what they’re doing for the default behavior: https://github.com/Modernizr/Modernizr/blob/master/feature-detects/touchevents.js
Otherwise, there should be a way for the user to control this logic, as device detection tends to be a controversial topic and everyone has their own way of doing things. It would be nice if we could set the
isTouchDeviceprop as a function that contained our own logic.I would be willing to draft a PR to test either the Modernizr-style touch detection or the getting isTouchDevice working as a prop that accepts a function, but would like to hear some suggestions.
@majapw Probably, I’ll put together a PR later today.
@erin-doyle I think the problem is if we’re not using
touch, we usually end up using user-agent, which is unreliable.@comron @ljharb Perhaps we should have a prop to allow the consumer to specify the behavior they want or maybe we should be relying on screen-size instead (that’s generally how we select mobile devices at airbnb, although devices like the iPad prop make it a bit difficult).
btw the code for
isTouchDeviceis here — https://github.com/airbnb/is-touch-device/blob/master/src/index.jsIt doesn’t look like there is anyway to get focus back to the input. The code seems to steal focus from the input whenever it gains focus if isTouchDevice is true.
I think the problem here is that “touch device” is too broad of a check, and specifically not allowing a user to type into the field if their browser responds to ‘ontouchstart’ seems overly strict. In this case the user doesn’t even know they have a touch device, it just happens to be a PC with a keyboard and mouse and a touch monitor. The user expects this form to work like any other web input, especially when typing specific far-future dates typing is the most intuitive interaction.
I think you’re idea about only forcing the focus when presenting the DatePicker in a portal because of screen size is the better idea. At a minimum it would be nice to be in control of this behavior, even if the default doesn’t change.
The “keepFocusOnInputs” is something I’m still working on. It’s still needed, I just have been caught up with other items and haven’t been able to push anything up.
On Tue, Jan 9, 2018 at 6:08 AM Erin Doyle notifications@github.com wrote:
@majapw I’ll investigate and let you know, thanks for all your help.
Okay, I would add a prop named something like
keepFocusOnInput(s)and then have that short-circuit the focus movement when true. @comron would that work for you?I think indeed that providing a way for the user to override this default logic is the simplest way forward.
We should be detecting features, not sniffing user agents - “mobile” isn’t a feature, while “touch” is.