Pickin’ dates

I had the opportunity to trim some code from The Session recently. That’s always a good feeling.

In this case, it was a progressive enhancement pattern that was no longer needed. Kind of like removing a polyfill.

There are a couple of places on the site where you can input a date. This is exactly what input type="date" is for. But when I was making the interface, the support for this type of input was patchy.

So instead the interface used three select dropdowns: one for days, one for months, and one for years. Then I did a bit of feature detection and if the browser supported input type="date", I replaced the three selects with one date input.

It was a little fiddly but it worked.

Fast forward to today and input type="date" is supported across the board. So I threw away the JavaScript and updated the HTML to use date inputs by default. Nice!

I was discussing date inputs recently when I was talking to students in Amsterdam:

They’re given a PDF inheritance-tax form and told to convert it for the web.

That form included dates. The dates were all in the past so the students wanted to be able to set a max value on the datepicker. Ideally that should be done on the server, but it would be nice if you could easily do it in the browser too.

Wouldn’t it be nice if you could specify past dates like this?

<input type="date" max="today">

Or for future dates:

<input type="date" min="today">

Alas, no such syntactic sugar exists in HTML so we need to use JavaScript.

This seems like an ideal use-case for HTML web components:

Instead of all-singing, all-dancing web components, it feels a lot more elegant to use web components to augment your existing markup with just enough extra behaviour.

In this case, it would be nice to augment an existing input type="date" element. Something like this:

 <input-date-past>
   <input type="date">
 </input-date-past>

Here’s the JavaScript that does the augmentation:

 customElements.define('input-date-past', class extends HTMLElement {
     constructor() {
         super();
     }
     connectedCallback() {
         this.querySelector('input[type="date"]').setAttribute('max', new Date().toISOString().substring(0,10));
     }
 });

That’s it.

Here’s a CodePen where you can see it in action along with another HTML web component for future dates called, you guessed it, input-date-future.

See the Pen Date input HTML web components by Jeremy Keith (@adactio) on CodePen.

Responses

Kim Johannesen

@adactio Opened this on my iPhone and I was able to pick any date in both of them — I guess Safari on iOS has patchy support 😕

Elwin Schmitz

@adactio I haven’t really tested this (and I’m on an iPhone), but wouldn’t it also be declarative (and minimal JS) with something like:

<input name=”future” type=”date” onfocus=”this.min=new Date().toISOString().substring(0,10)”>

2 Shares

# Shared by Andrew Maier on Thursday, April 11th, 2024 at 12:06pm

# Shared by Bill Seitz on Thursday, April 11th, 2024 at 1:11pm

4 Likes

# Liked by Khalid ⚡️ on Thursday, April 11th, 2024 at 12:05pm

# Liked by Andrew Maier on Thursday, April 11th, 2024 at 12:06pm

# Liked by Royce Williams on Thursday, April 11th, 2024 at 2:10pm

# Liked by Dave Giunta on Wednesday, April 24th, 2024 at 3:09am

Related posts

Pickin’ dates on iOS

Mobile Safari doesn’t support the min and max attributes on date inputs.

Control

Trying to understand a different mindset to mine.

When should there be a declarative version of a JavaScript API?

If the JavaScript API requires a user gesture, maybe it’s time for a new button type.

aria-live

An exception to my general rule that ARIA attributes should be added with JavaScript.

Accessible interactions

Abstracting common interaction patterns as a starting point for accessible components.

Related links

An origin trial for a new HTML <permission> element  |  Blog  |  Chrome for Developers

This looks interesting. On the hand, it’s yet another proprietary creation by one browser vendor (boo!), but on the other hand it’s a declarative API with no JavaScript required (yay!).

Even if this particular feature doesn’t work out, I hope that this is the start of a trend for declarative access to browser features.

Tagged with

The quiet, pervasive devaluation of frontend - Josh Collinsworth blog

It’s like CSS exists in some bizarre quantum state; somehow both too complex to use, yet too simple to take seriously, all at once.

In many ways, CSS has greater impact than any other language on a user’s experience, which often directly influences success. Why, then, is its role so belittled?

Writing CSS seems to be regarded much like taking notes in a meeting, complete with the implicit sexism and devaluation of the note taker’s importance in the room.

Tagged with

Tagged with

Practical Accessibility — Practical Accessibility for web designers and developers

This online course from Sara looks superb!

I know how overwhelming and even frustrating accessibility may feel at first. But I promise you, accessibility isn’t always as hard as it seems (especially if you know where and when to start!). And my goal with this course is to make it friendlier and more approachable.

Best of all, there’s $100 off if you sign up now—that’s a 25% saving.

Tagged with

Patrick / articles / Is the developer experience on the Web so terrible?

Over the past 10 years or so, we’ve slowly but very surely transitioned to a state where frameworks are the norm, and I think it’s a problem.

I concur.

Use the frameworks and libraries that make sense for you to deliver the best UX possible. But also learn the web platform from the ground up. Take time to understand how web browsers work and render webpages. Learn HTML, CSS, JavaScript. And keep an eye, if you can, on the new things.

Tagged with

Previously on this day

5 years ago I wrote Design perception

Most companies are not digital-first. And that’s okay.

7 years ago I wrote Announcing Patterns Day: June 30th

A one-day event in Brighton dedicated to design systems, pattern libraries, and style guides.

9 years ago I wrote 100 words 020

Day twenty.

13 years ago I wrote Windows mobile media queries

Solving that pesky Windows Phone 7 problem.

14 years ago I wrote Article of doubt

Once again, the data shows confusion between sections and articles in HTML5.

15 years ago I wrote Revving up

rev=”canonical” has a posse.

17 years ago I wrote Victoriartifact

A gift from another time and place.

21 years ago I wrote Downloadable fun

A spectacular car chase, bullet time, people flying through the air…