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
  1. The markup. The same options a <select> would take.
  2. The control. The details, the radios and the draw.
  3. A <details> that closes over time.
  4. 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
  1. The markup. data-at is where the dot goes, in the 360 units of the picture.
  2. 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
  1. The markup.
  2. The mug. One shape, four sets of colours.
  3. 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
  1. The markup. A row is an option: # is black, . is empty and ? is a square of the answer.
  2. The control. The field, the table and the letters.
  3. 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
  1. The markup.
  2. 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