A <datalist> drawn by a canvas
The suggestions of <input list> appear in a popup that the browser draws. It has no pseudo-element, it ignores CSS, and
it shows only each option's value and label. I wanted to know how much of a datalist a canvas can draw.
The built-in popup is out of reach, but the <datalist> element and its options are ordinary elements, so they can be
put in a canvas and drawn there.
Suggestions drawn one option at a time
Each field below is an <input list> with a <datalist>. A script moves the datalist into a canvas inside a
popover and removes the input's list attribute, which stops the built-in popup. Type in a field: options that stop matching fade
out and the rest move into the gaps. The arrow keys, Enter and Escape work as they do with a built-in datalist.
Show the code a datalist in a popover canvas
- Write an input with a datalist and add
picker="list"orpicker="chips". Without the library, or without HTML-in-canvas, this is a normal datalist. - Load the library. The page script only fills the three datalists with
<option value label>elements. - Inside the library: stop the built-in popup and move the datalist into a canvas. The canvas is in a
popover="manual"element positioned next to the input with CSS anchor positioning. - Decide which options match.
- A layout gives each matching option a place.
- Each frame, move every option a little towards its place and draw it.
drawElementImage()also moves the option's hit box, so a tap lands on the option that is drawn there. - The keyboard. The browser's datalist keyboard handling went away with the
listattribute, so this part is written by hand. - Choosing an option sets the input's value and fires
inputandchange.
Labels for the ticks of a range input
<input type="range" list> draws a tick mark for each option of its datalist and nothing else: the option's label is never shown.
Here the range input and its datalist are both children of one canvas. The canvas draws the input, then draws each
<option> under its tick, larger when the thumb is near. The labels are the real options. Tap one to jump to its value.
16px The quick brown fox jumps over the lazy dog.
Show the code datalist options as tick labels
- A range input with a datalist. The text inside each option is the label.
- One line in the page.
- Inside the library: both elements go into a canvas, which draws the input and then every option.
The built-in popup, with the input drawn in a canvas
This input keeps its list attribute and is drawn rotated by a canvas. Type in it. The expected result, by analogy with
a select in a canvas, is that the built-in popup opens upright next to the bounding box of the rotated
input, and isn't part of what the canvas draws. The test browser used for this page runs without a window and never shows that popup, so
this isn't confirmed.
Findings
- The built-in datalist popup can't be drawn or styled. It isn't an element, it has no pseudo-element like
::picker(select), andappearance: base-selectdoes nothing for it. Everything on this page works by replacing it. - A
<datalist>can be displayed and drawn. Its default style isdisplay: none. Withdisplay: blockinside a canvas, each<option drawable>in it can be passed todrawElementImage(), although the options are grandchildren of the canvas. Without thedrawableattribute on the option the call throws. - An option in a datalist shows only text.
<option>Leeds <small>LDS</small></option>parses and keeps the<small>in the DOM, but the option renders its label as plain text: the<small>has no box andcheckVisibility()is false. Only options in a select withappearance: base-selectrender their children.displayon the option also staysblockwhatever the page sets.::beforeand::afterdo render, so the hex codes and swatches above are pseudo-elements. - The typed letters can't be marked inside an option. The CSS Custom Highlight API needs a range in rendered text. The text an option shows is in the browser's internal tree, not in the option's own text node, so a highlight on the text node paints nothing.
- Hit testing follows the drawing.
elementFromPoint()at a drawn option returns that option,:hoverapplies, and a click reaches it. Options that are filtered out are moved away withupdateElementGeometry(). - Removing
listremoves everything the browser did. There is no way to keep the built-in keyboard handling and replace only the popup. Arrow keys, Enter, Escape, filtering and therole="combobox"wiring are rewritten in about 60 lines. - An
<option>in a datalist can't take focus. The active suggestion is tracked witharia-activedescendanton the input, and focus never leaves the input, so the on-screen keyboard stays open on a phone. - A range input draws its ticks when it's drawn in a canvas. The tick marks from the datalist are part of the input's picture. The labels are extra elements drawn by the canvas at positions the page works out from
min,maxand the thumb width, because the input doesn't report where its ticks are. - Not verified on real hardware. These results are from Chromium 157 driven by Playwright with a mouse and emulated touch. The built-in popup of the rotated input wasn't observed. The position of the suggestions above an on-screen keyboard is something I haven't tried on a phone.
More to try with a datalist
- Suggestions as a shader. Draw the popover with WebGL and blur or dim the options by how badly they match, with the fx library from /shader-css/.
- Suggestions inline, after the caret. Draw the best option directly after the typed text inside the input's box, as ghost text, with the input itself drawn in the same canvas.
- A datalist for
<input type="color">andtype="date". Both acceptlistfor suggested values. The same takeover would give a palette or a row of suggested dates. See /inputs/ for date and time pickers drawn by a canvas. - Rich suggestions. Replace the datalist with a customisable select that has no button, so its options render their children, and drive it from the input. That would allow a canvas or an image in each suggestion.
- A two-dimensional datalist. Place the options by meaning, for example cities on a map or colours on a wheel, and move the view to the matches as the person types.
Live checks
These run on test inputs in a panel at the bottom of the window, not on the fields above. They wait for a tap because they open test pickers on top of the page.