Repository navigation
Description
Background:
Developers have been asking for years for a way to programmatically open a browser date picker. See https://www.google.com/search?q=programmatically+open+date+picker+site:stackoverflow.com
Because of that, they had to rely on custom widget libraries and CSS hacks for specific browsers.
Date pickers in Chrome Desktop, Chrome Mobile, Safari Desktop, Safari Mobile, and Firefox Desktop (July 2021).
Proposal
A new showPicker() method to the HTMLInputElement interface would show a browser picker (if supported) for
<input type="date">, <input type="month">, <input type="week">, <input type="datetime-local">, and <input type="time"> HTML elements . This method is handy, easy to use, and friendly to discover.
As it's already possible to listen tochange events to know when the HTML element value changes, I believe it's OK to make this method synchronous.
Example usage
// Show a date picker
const inputDateElement = document.querySelector('input[type="date"]');
inputDateElement.showPicker();Feature detection
if ("showPicker" in HTMLInputElement.prototype) {
...
}Open questions
- Should we restrict the number of date pickers opened?
- Date pickers are modal on Mobile. They're not modal on Desktop. Should we require a user activation?
Alternative considered
-
Adding a brand new
DatePickerobject that could be used with HTML elements or in standalone mode seems overkill. -
According to Should <input type=date> have a "input activation behavior" defined? #3232 (comment), it seems like a synthetic
click()should not trigger a browser date picker.





Activity
tomayac commentedon Jul 28, 2021
Should this also come along with an
openattribute, similar todialog?domenic commentedon Jul 28, 2021
Should this explicitly only work for these inputs? Other inputs which have pickers in some browsers are
tel/email(contact picker),url(history picker),password(password manager),number,color, andfile.annevk commentedon Jul 29, 2021
Also
<select>.(The other thing I was thinking about, which is somewhat off-topic, is that you might want to display the picker exclusively as some sites do (i.e., without the input box), but perhaps that's best left to libraries as there's often additional requirements.)
beaufortfrancois commentedon Jul 29, 2021
I'm not sure. According to the spec, a dialog element without an open attribute specified should not be shown to the user. Shall we then have an "open" attribute for
HTMLInputElementthat prevents user to show a picker? I don't think so. Thoughts?beaufortfrancois commentedon Jul 29, 2021
FYI There's a JS API already for Contact picker
Currently
tel,email,url,password, andnumberhave no pickers from what I can see.colorhowever seems like a good candidate that would benefit from this.Having
showPicker()forfilemay not be appropriate as we already have showOpenFilePicker().beaufortfrancois commentedon Jul 29, 2021
We'd have to find out if that's something developers need.
domenic commentedon Jul 29, 2021
In Chrome I agree those do not have pickers. In other browsers they do. (In some cases historical browsers like Opera.)
Yay295 commentedon Jul 29, 2021
Not supported in Firefox or Safari though. And we could still have a more general method even with an existing method for a specific case.
showOpenFilePicker()isasyncthough, and it returns the file that was picked. So if we want the generic method to work likeshowOpenFilePicker(), it should also beasyncand return whatever was picked.domenic commentedon Aug 11, 2021
I was given an action item to see what
click()does today to guide this discussion. It turns out interop here is a bit messy. Test page: https://boom-bath.glitch.me/pickers.html . Results:<input type="date"><input type="month"><input type="week"><input type="time"><input type="datetime-local"><input type="color"><input type="file"><select><select multiple>❗ means the picker does open, on both programmatic
click()and when the user clicks on the<label>'s text.* means the picker does not open via programmatic
click(), but it does open by the user click on the<label>'s text.Blank means neither clicking on the label nor programmatic
click()will open the picker.E.g. on mobile Chrome, the user clicking on the label text for
<input type="date">will open the picker, but the user clicking on the label text for<select>will not open the picker. And on desktop Chrome, the user clicking on the label only opens the picker for<input type="file">and<input type="color">.None of the elements in "Not very likely to have a picker, but could conceivably", besides sometimes besides
<select multiple>, ever show pickers, so I omit them from above.Thankfully nobody showed pickers on
focus().I can't think of anything unifying these. E.g. you might think Chrome's approach is "display fullscreen pickers, don't display anchored pickers", but
<select>on mobile is a fullscreen picker and not displayed, whereas<input type="color">on desktop is an achored picker and is displayed.Here's my proposal:
Standardize on Firefox/Chrome desktop's
click()behavior as the minimal interoperable subset. That is, for legacy reasons,click()shows the picker on color and file, on both desktop and mobile, but it does nothing in all other cases.Introduce
showPicker()which is supposed to show the picker whenever it exists. Namely,Pick one behavior for the user user clicking on a label. That behavior should either be the same as
click()or the same asshowPicker(), but I'm not sure which. We should debate what folks feel a better user experience/more compat risky/etc.After
showPicker()has been available for some time, hopefully Chrome mobile and Safari can align on the above minimal behavior forclick().We could also contemplate not special-casing
<input type="color">, as I suspect fewer people are relying on it showing the picker than<input type="file">, but probably that's not worth the risk.beaufortfrancois commentedon Aug 12, 2021
Thank you @domenic for the thorough investigation.
Here are my results on Safari Mobile. Feel free to edit your post earlier.
<input type="date"><input type="month"><input type="week"><input type="time"><input type="datetime-local"><input type="color"><input type="file"><select>domenic commentedon Aug 12, 2021
Thanks @beaufortfrancois! You inspired me to borrow my girlfriend's iPhone and do some of my own testing on the
<label>issue. I've updated the above with full results, and also split out Firefox desktop/mobile since they are sufficiently different on the<label>issue as well.37 remaining items
beaufortfrancois commentedon Nov 25, 2021
@mnoorenberghe https://chromiumdash.appspot.com/commit/92e5fe8556d41ba34766129574df624ea2413ffa has landed. Give https://show-picker.glitch.me/ a try in Chrome Canary 98 98.0.4726.0. You'll need to enable the experimental web platform features flag for this.
Add showPicker() to <input> elements
beaufortfrancois commentedon Dec 13, 2021
FYI I've just sent an intent to ship at blink-dev: https://groups.google.com/a/chromium.org/g/blink-dev/c/YfnM0ubs53k
[InputElement] Add HTMLInputElement::showPicker() method
ziransun commentedon Mar 24, 2022
@domenic @beaufortfrancois Any chance to help with clarifying the question at https://bugs.webkit.org/show_bug.cgi?id=237192#c19? This WebKit patch is an import of the Chromium showpicker() CL. Thanks.
beaufortfrancois commentedon Mar 24, 2022
As you can see below, in Chromium
showPickerwill throw security error if type is notfilenorcolor. However it will check user gesture status for all types. See https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/html/forms/html_input_element.cc;l=2218;drc=72575413b1662ce93e9137f99303d0d5a3942f0fziransun commentedon Mar 24, 2022
Thanks @beaufortfrancois for your prompt response. Yes, this is what we have in WebKit imported code as well. What we are trying to clarify here is, we check on cross-frame and user gesture status and throw exceptions if either of those applies. What is neither applies, silently do nothing?
As per #6909 (comment), it mentioned that "It should not consume user activation.". Does it mean that silently doing nothing is intentional?
beaufortfrancois commentedon Mar 24, 2022
That is my understanding according to https://html.spec.whatwg.org/multipage/input.html#dom-input-showpicker
Let's wait on @domenic answer as well.
domenic commentedon Mar 24, 2022
Yes, that is correct.
ziransun commentedon Mar 24, 2022
Thanks both. Just to confirm - If not applicable, do nothing?
domenic commentedon Mar 24, 2022
Yes, if you read the algorithm for "show the picker, if applicable", step 4 says
ziransun commentedon Mar 24, 2022
Perfect. Thanks very much!
Add showPicker() to <input> elements
Bug 1739123 [wpt PR 31484] - Add showPicker() method to HTMLInputElem…