Skip to content

Proposal: Add a way to open programmatically a date picker #6909

Closed

Description

@beaufortfrancois

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

  1. Should we restrict the number of date pickers opened?
  2. Date pickers are modal on Mobile. They're not modal on Desktop. Should we require a user activation?

Alternative considered

Resources

Activity

  1. added
    addition/proposal
    needs implementer interest
    topic: forms
    on Jul 28, 2021
  2. tomayac commented on Jul 28, 2021

    @tomayac

    Should this also come along with an open attribute, similar to dialog?

  3. added
    agenda+
    on Jul 28, 2021
  4. domenic commented on Jul 28, 2021

    @domenic
    Member

    show a browser picker (if supported) for
    <input type="date">, <input type="month">, <input type="week">, <input type="datetime-local">, and <input type="time">

    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, and file.

  5. annevk commented on Jul 29, 2021

    @annevk
    Member

    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.)

  6. beaufortfrancois commented on Jul 29, 2021

    @beaufortfrancois
    Author

    Should this also come along with an open attribute, similar to dialog?

    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 HTMLInputElement that prevents user to show a picker? I don't think so. Thoughts?

  7. beaufortfrancois commented on Jul 29, 2021

    @beaufortfrancois
    Author

    show a browser picker (if supported) for
    <input type="date">, <input type="month">, <input type="week">, <input type="datetime-local">, and <input type="time">

    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, and file.

    FYI There's a JS API already for Contact picker

    Currently tel, email, url, password, and number have no pickers from what I can see. color however seems like a good candidate that would benefit from this.

    Having showPicker() for file may not be appropriate as we already have showOpenFilePicker().

  8. beaufortfrancois commented on Jul 29, 2021

    @beaufortfrancois
    Author

    Also <select>.

    We'd have to find out if that's something developers need.

  9. domenic commented on Jul 29, 2021

    @domenic
    Member

    In Chrome I agree those do not have pickers. In other browsers they do. (In some cases historical browsers like Opera.)

  10. Yay295 commented on Jul 29, 2021

    @Yay295
    Contributor

    Having showPicker() for file may not be appropriate as we already have showOpenFilePicker().

    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() is async though, and it returns the file that was picked. So if we want the generic method to work like showOpenFilePicker(), it should also be async and return whatever was picked.

  11. domenic commented on Aug 11, 2021

    @domenic
    Member

    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:

    Element Firefox (desktop) Firefox (mobile) Chrome (desktop) Chrome (mobile) Safari (desktop) Safari (mobile)
    <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,

      • In current browser implementations, it would:
        • Show the picker on date/month/week/time/datetime-local/color/file/select on mobile and desktop
        • Show the picker on select multiple on mobile
      • If a browser wants to e.g. make tel inputs open a contact picker at some point, then showPicker() should trigger this behavior
    • Pick one behavior for the user user clicking on a label. That behavior should either be the same as click() or the same as showPicker(), 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 for click().

    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.

  12. beaufortfrancois commented on Aug 12, 2021

    @beaufortfrancois
    Author

    Thank you @domenic for the thorough investigation.

    Here are my results on Safari Mobile. Feel free to edit your post earlier.

    Element Safari (mobile)
    <input type="date">
    <input type="month">
    <input type="week">
    <input type="time">
    <input type="datetime-local">
    <input type="color">
    <input type="file"> ❗
    <select>
  13. domenic commented on Aug 12, 2021

    @domenic
    Member

    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.

  14. 37 remaining items

  15. beaufortfrancois commented on Nov 25, 2021

    @beaufortfrancois
    Author
  16. added a commit that references this issue on Dec 8, 2021
    968bf46
  17. beaufortfrancois commented on Dec 13, 2021

    @beaufortfrancois
    Author
  18. ziransun commented on Mar 24, 2022

    @ziransun

    @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.

  19. beaufortfrancois commented on Mar 24, 2022

    @beaufortfrancois
    Author

    Is it correct that when called on an HTMLInputElement of a type that has no picker, that showPicker should check cross-frame and user gesture status, throw exceptions if either of those applies, but then if neither applies, silently do nothing?

    As you can see below, in Chromium showPicker will throw security error if type is not file nor color. 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=72575413b1662ce93e9137f99303d0d5a3942f0f

    void HTMLInputElement::showPicker(ExceptionState& exception_state) {
      LocalFrame* frame = GetDocument().GetFrame();
      if (type() != input_type_names::kFile && type() != input_type_names::kColor &&
          frame) {
        const SecurityOrigin* security_origin =
            frame->GetSecurityContext()->GetSecurityOrigin();
        const SecurityOrigin* top_security_origin =
            frame->Tree().Top().GetSecurityContext()->GetSecurityOrigin();
        if (!security_origin->IsSameOriginWith(top_security_origin)) {
          exception_state.ThrowSecurityError(
              "HTMLInputElement::showPicker() called from cross-origin iframe.");
          return;
        }
      }
    
      if (!LocalFrame::HasTransientUserActivation(frame)) {
        exception_state.ThrowDOMException(
            DOMExceptionCode::kNotAllowedError,
            "HTMLInputElement::showPicker() requires a user gesture.");
        return;
      }
    
      input_type_view_->OpenPopupView();
    }
  20. ziransun commented on Mar 24, 2022

    @ziransun

    As you can see below, in Chromium showPicker will throw security error if type is not file nor color. However it will check user gesture status for all types.

    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?

  21. beaufortfrancois commented on Mar 24, 2022

    @beaufortfrancois
    Author

    That is my understanding according to https://html.spec.whatwg.org/multipage/input.html#dom-input-showpicker

    The showPicker() method steps are:

    1. If this's relevant settings object's origin is not same origin with this's relevant settings object's top-level origin, and this's type attribute is not in the File Upload state or Color state, then throw a "SecurityError" DOMException.
    2. If this's relevant global object does not have transient activation, then throw a "NotAllowedError" DOMException.
    3. Show the picker, if applicable, for this.

    Let's wait on @domenic answer as well.

  22. domenic commented on Mar 24, 2022

    @domenic
    Member

    Yes, that is correct.

  23. ziransun commented on Mar 24, 2022

    @ziransun

    3. Show the picker, if applicable, for this.

    Thanks both. Just to confirm - If not applicable, do nothing?

  24. domenic commented on Mar 24, 2022

    @domenic
    Member

    Yes, if you read the algorithm for "show the picker, if applicable", step 4 says

    Otherwise, the user agent should show any relevant user interface for selecting a value for element, in the way it normally would when the user interacts with the control. (If no such UI applies to element, then this step does nothing.)

  25. ziransun commented on Mar 24, 2022

    @ziransun

    Yes, if you read the algorithm for "show the picker, if applicable", step 4 says

    Otherwise, the user agent should show any relevant user interface for selecting a value for element, in the way it normally would when the user interacts with the control. (If no such UI applies to element, then this step does nothing.)

    Perfect. Thanks very much!

  26. added a commit that references this issue on Jun 3, 2022
    8beb7fa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    addition/proposalneeds implementer interesttopic: forms

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions