Dates drawn over date inputs, text fields and radios
Five more controls from ui-feed.dev, rebuilt with HTML-in-canvas, all about dates: a night
at a campsite, a best-before code, a library renewal, a wall calendar and the stops of a day out. The browser's own date
picker is drawn by the system, outside the page, so a canvas can't draw it. I wanted to know how much of a date picker could
be made from elements that stay in the canvas, and whether the one real <input type="date"> could still be
typed in while it is drawn.
Credit: every control on this page is a rebuild of a demo on ui-feed.dev, and each section links to the original it came from. The originals are far higher fidelity than these, so go and look at them. These are examples of what HTML-in-canvas makes possible with a native element underneath, not replacements. I worked from the published demos rather than their source: I looked at what each one does, worked out the mechanism and built it again. The designs and the ideas are theirs.
Night at the lake
The field at the top is an <input type="date"> with a min and a max, so a
date can be typed into it. The button opens a <dialog> with the open attribute, which keeps
it out of the top layer and in the canvas. Each night in it is a radio with its price, and a night that is taken is a
disabled radio. The canvas draws the rows of the month coming down one after another and a yellow disc under the night you
picked. A night typed into the field that is taken makes the form invalid.
After the night at the lake (date picker) on ui-feed.dev.
Show the code the markup and the control
- The markup. One price for each night, and a dash for a night that is taken.
- The control. The date input, the dialog of radios and the draw.
- The base class. The shadow root, the canvas, the paint loop and the plain HTML fallback.
Best before
Three <input type="text" inputmode="numeric"> fields for the month, the day and the year, drawn with
no alpha. A field that can't take another digit sends the caret on, Backspace in an empty one goes back, and the arrow keys
count up and down. The canvas prints each figure in dots five wide and seven high, and the dots of a new figure spray in
one column after another. A date that doesn't exist, such as 02/30, turns the boxes red and makes the form invalid.
After the can's best-before code (date input) on ui-feed.dev.
Show the code the markup, the dots and the control
- The markup.
- The dots. A five by seven font, and the spray.
- The control. Three fields, moving between them, and the value.
Library renewal
The receipt is one element. Its last line, "Return by", is the <summary> of a
<details>, and the calendar inside it is a radio for each day. Days before today, days after the last
day a loan can be renewed to, and the days the library is shut are disabled. As the details opens, the canvas draws more of
the receipt and lifts it, so the paper looks as if it is coming out of the printer. Picking a day renews the loans due
before it.
After the library loan renewal (date picker) on ui-feed.dev.
Show the code the markup and the control
- The markup. One option for each loan.
- The control. The receipt, the calendar of radios and the draw.
- A
<details>that closes over time.
Wall calendar
A month of radios in a <table>, with two buttons to turn the page. The month is laid out twice: the
teal copy holds the radios, and a coral copy, which is inert, holds only the weekends and the square behind the
chosen day. The canvas draws the coral copy a fraction of a unit off the teal one, as a two-colour print sits on the page,
and when the page turns the coral runs a little behind. The arrow keys move by a day or a week and cross into the next
month.
After the two-colour wall calendar (calendar) on ui-feed.dev.
Show the code the markup and the control
- The markup.
- The control. Two plates, the page turn and the keys.
Day out
The stops are radios in an <ol>, so the arrow keys move between them. The list is laid out twice, once
in a light weight and once in a heavy one that is inert. The canvas draws the heavy copy over the stop that has
focus or is chosen, faded in over a fifth of a second, and grows a red dot on the line beside it.
After the day trip (timeline) on ui-feed.dev.
Show the code the markup and the control
- The markup.
- The control.
How it works
Each control is a custom element on the same base class as the pickers. Its shadow root
holds a canvas with content="drawable", and the native elements are children of that canvas.
The picker of an <input type="date"> isn't part of the page at all, so only the field can be drawn.
Everything that looks like a calendar here is a group of radios, one for each day, in a <table> or a list.
A radio gives the arrow keys, a disabled state for a day that can't be chosen and a value for the form. The controls add
what a group of radios doesn't do by itself: moving by a week, and crossing into the next month.
Two of the controls lay their content out twice. A canvas can only draw an element as it is, so a second copy with other
styles (heavier type, or only the coral ink) is the way to fade or move one look over another. The copy is
inert and hidden from assistive technology, and only the first copy takes input.
Live checks
Findings
- The field of an
<input type="date">is drawn and takes typing. Its picker isn't drawn, so every calendar on the page is a group of radios. The field's own calendar button is hidden. - A
<dialog>opened with theopenattribute is drawn with its rows coming down one by one. The point over a night answers its radio. - A taken night typed into the date input makes the form invalid through
ElementInternals.setValidity(). A disabled radio can still be checked from script, so the control skips disabled ones when it follows the field. - Three text fields behave as one date: the caret moves on, Backspace goes back, the arrow keys count, and 02/30 is refused. The dots of a figure are drawn over about a tenth of a second and then the control stops painting.
- A
<details>grows the receipt by 175 units, and the canvas lifts the paper as it grows. A day renews the loans due before it. - The arrow keys move by a day or a week and cross into the next month, which a group of radios doesn't do by itself.
- An
inertcopy of an element, styled differently, is how one look is faded or moved over another. The coral plate and the heavy list are copies. this.titleon a custom element is thetitleattribute. I kept the heading of the best-before code there, so it became a string and wasn't drawn until I renamed it.- Taps, typing and vertical swipes worked on an emulated Pixel 7. Nothing here has been run on a real phone.