Pickers drawn over details, dialogs, radios and a scroller
Five more controls from ui-feed.dev, rebuilt with HTML-in-canvas: a drop-down, popovers, a segmented control, a row of letter boxes and a carousel. The browser already has an element for most of these, and several of them paint in the top layer, where a canvas can't draw them. I wanted to know which native elements could stand in and still be drawn, and if a scroller that the canvas draws would still scroll and snap by itself.
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.
Shoe size
The picker of a <select> is painted in the top layer, so a canvas has no picture of it. This drop-down
is a <details> with a radio for each size. The summary is the box that shows the size, the radios give
the arrow keys and the sold-out size is a disabled radio. The canvas draws the list sliding open and turns the chevron
over. A tap on a size closes the list, and an arrow key doesn't. The shoe grows a little with the size.
After the shoe size select (select) on ui-feed.dev.
Show the code the markup and the control
- The markup. The same options a
<select>would take. - The control. The details, the radios and the draw.
- A
<details>that closes over time. - The base class. The shadow root, the canvas, the paint loop and the plain HTML fallback.
Bicycle parts
Each dot is a <button> and each card is a <dialog> that is opened without
showModal(), so it stays out of the top layer and has a picture. The card holds a
<form method="dialog">, and its button adds the part or takes it off again. The canvas draws the card
growing out of its dot, the pointer under it, and the plus turning into a cross. Escape or a tap on the wall closes the
card.
After the bicycle popovers (popover) on ui-feed.dev.
Show the code the markup and the control
- The markup.
data-atis where the dot goes, in the 360 units of the picture. - The control. A button and a dialog for each part, and the draw.
Glaze
Four radios in one group. The canvas draws the bar, a thumb that slides to the checked radio in the colour of its glaze,
and the names twice: once light, and once through the thumb in a colour that shows on it. The mug is drawn once for each
glaze on a plain canvas, always the same shape, and the new glaze is wiped over the old one from the right. The line of
text is an <output>.
After the glaze picker (segmented control) on ui-feed.dev.
Show the code the markup, the mug and the control
- The markup.
- The mug. One shape, four sets of colours.
- The control.
Crossword
One <input type="text"> lies across the pink row, with a maxlength of seven. It is
drawn with no alpha, so it takes the tap, the keyboard and the caret but none of its own text shows. The canvas reads its
value and stamps each letter into a square, and draws a caret in the square where the field's caret is. The grid is a
<table> written from the rows in the markup, and the clue is a heading.
After the crossword entry (one-time code input) on ui-feed.dev.
Show the code the markup and the control
- The markup. A row is an option:
#is black,.is empty and?is a square of the answer. - The control. The field, the table and the letters.
- A draw with no alpha. It moves an element's hit region and shows nothing.
Tulips
The slides are in a <div> that scrolls sideways with scroll-snap-type. The canvas draws
that element at whatever position it has scrolled to, so a swipe, a wheel and the arrow keys are the browser's own, and
so is the snap. The tulips are drawn by the canvas on top, a little faster than the slides. The lines at the bottom are
radios: choosing one scrolls the element, and where the element comes to rest checks a radio.
After the tulip carousel (carousel) on ui-feed.dev.
Show the code the markup and the control
- The markup.
- The control. The scroller, the radios and the draw.
How it works
Each control is a custom element on the same base class as the trees and timelines. Its
shadow root holds a canvas with content="drawable", and the native elements are children of that canvas.
A canvas can only draw an element that is laid out inside it. The picker of a <select>, a
popover and a modal <dialog> are all painted in the top layer, outside the canvas, so none
of them has a picture to draw. The first two controls use elements that stay in the canvas: a <details>
with radios for the drop-down, and a <dialog> with the open attribute for each card.
Closing is the same trick each time. A closed <details> and a closed <dialog> have
nothing to draw, so the control cancels the event that would close the element, draws it shut over a fifth of a second and
then closes it.
The carousel needs no scroll code for touch. The scroller is a drawable child, the browser scrolls it and raises a paint
event at each step, and the control reads scrollLeft in its paint handler to place the tulips and the bright
marker.
Live checks
Findings
- A
<details>with radios can be drawn as a drop-down, and a<select>can't. The picker of a select is in the top layer and has no picture. The stand-in keeps the arrow keys, the disabled option and the form value. It loses the phone's own picker and typing a letter to jump to an option, and a screen reader is told about a group of radios where the original has a list box. - A
<dialog>with theopenattribute is drawn like any other child. It isn't modal, so it stays in the canvas. Its<form method="dialog">would close it at once, so the control cancels the submit, draws the card shut and then callsclose()with the button's value. The dialog ended withreturnValue"add". - For one frame after an element closes, its box is short and its picture is still the open one. Drawn at the new size, the whole list was squashed into the summary. I saw it in a screenshot, not in a check. The control draws that frame at the height the element had.
- The element drawn last answers the tap. With each name drawn after its radio, the point on a segment answered
the
<label>. That still checks the radio, but the radios are now drawn last, with no alpha. - A scroll-snap element drawn by the canvas scrolls and snaps by itself. A click on the third marker scrolled it 720 units with 45 paint events on the way, and nothing in the control listens for those to draw. Left at 980 units by a script, it snapped to 1080 and the control checked the fourth radio. A swipe by touch in the emulated phone moved one slide, and a vertical swipe on the slides scrolled the page.
- A text field drawn with no alpha still takes the tap, the focus and the typing. Its own caret is transparent,
because a caret that blinks in a picture nobody sees still raises paints. The canvas draws a caret from
selectionStartand asks for a paint twice a second while the field has focus. - The pictures are far from the originals. Their shoe, bicycle, mug and tulips are photographs. Mine are paths and gradients on a plain canvas.
- Not verified: everything on a phone. In particular the on-screen keyboard over the crossword (capitals, word suggestions, an IME), a fling on the carousel, and what a screen reader says about each control.