Small controls drawn over radios, toggle buttons, a password field and checkboxes
Five more controls from ui-feed.dev, rebuilt with HTML-in-canvas. They are small ones: a carousel of typefaces, a bold, italic and underline toolbar, an avatar that comes back online, a password field with an eye, and a list of newsletters. Each is one or two native elements, and the work is in how they move. I wanted to know whether the canvas could give each one its motion while the elements keep their keys, their state and what they say to a screen reader.
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.
Type styles
A radio for each typeface, drawn as the dots under the cards, so the arrow keys and the form value are the radio group's. The cards are one row of elements, and the canvas draws each card as a slice of it, squeezed across as it moves away from the middle, so the "Aa" on a card at the side is drawn narrow. Drag the cards, tap one at the side, or use the dots.
After the type styles (carousel) on ui-feed.dev.
Show the code the markup and the control
- The markup. One option for each card, with the CSS of its "Aa".
- The control. The radios, the drag and the cards squeezed from one row.
- The base class. The shadow root, the canvas, the paint loop and the plain HTML fallback.
Text formatting
Three <button aria-pressed>s in a role="toolbar", with Left and Right moving between
them and Ctrl+B, Ctrl+I and Ctrl+U (Command on a Mac) pressing them. Each names a role="tooltip" card in
aria-describedby, filled in with its name and shortcut, which springs up over the button you point at or
focus. A pressed button's plate grows into place behind it, and the line under the bar takes the marks.
After the text formatting toolbar (tooltip) on ui-feed.dev.
Show the code the markup and the control
- The markup. The marks to start with, and the line they act on.
- The control. The toggle buttons, the keys and the tooltip.
Back online
The avatar is a <button aria-pressed>, pressed while the person is online. Away, it is a ring with
thin initials and a moon. Pressed, a dark disc grows out of the status badge across the avatar, with a bold copy of the
initials drawn only inside it, and the moon turns into a ring. An <output role="status"> says which.
After back online (avatar) on ui-feed.dev.
Show the code the markup and the control
- The markup.
onlinestarts it online. - The control. The button, the bold copy and the fill from the badge.
Show and hide
An <input type="password"> whose own text is transparent. The canvas draws a dot for each
character, popping in on a spring, at the place an invisible copy of the text in the same font says that character
is, so the dots stay under the caret. The eye is a <button aria-pressed> that switches the field to
text: the dots fade and the field's own letters show. The line fills as you type and goes green at the minimum length,
and until then the form is invalid.
After show and hide (password) on ui-feed.dev.
Show the code the markup and the control
- The markup.
- The control. The field, the dots, the eye and the length check.
Newsletter topics
A checkbox in each row's label. The list is laid out twice: once in grey, which takes input, and once in white
italic, which is inert. Each row is drawn as a slice of both, crossfaded on a spring, so a row you choose
turns italic and white as its red dot fills.
After the newsletter topics (listbox) on ui-feed.dev.
Show the code the markup and the control
- The markup. One option for each newsletter; a selected one starts chosen.
- The control. The checkboxes, the italic copy and the dots.
How it works
Each control is a custom element on the same base class as the scene controls. Its
shadow root holds a canvas with content="drawable", and the native elements are children of that canvas.
These ones are mostly about motion that an element can't do to itself. A card is drawn squeezed by scaling the context before its slice is drawn, a fill is clipped to a growing circle, a dot pops on a spring, and a row crossfades between two copies of a list. The element underneath is still where the keys, the pressed state and the value live.
Live checks
Findings
- A radio group can be the whole value of a carousel. The cards are one row drawn as slices, each scaled narrower as it leaves the middle, and the radios are drawn as the dots under them. Arrow keys, the form value and what is announced all come from the radios.
- A press on the cards had to call
preventDefault()onpointerdown. Without it,delegatesFocussent focus to the first radio, not the chosen one. - A point inside a drawn button lands on the button's own child, so a hit test has to look up with
closest('button'). The check got this wrong the first time. - The password field draws nothing itself: its text is transparent, and a hidden copy of the value in the same font gives the pitch of one character, so the dots follow the caret and the field's scroll. Shown, the field's type is
textand the dots fade out. - The chosen newsletter is drawn from an inert, hidden copy of the list in italic, crossfaded with the plain one on a spring. Only the plain list has checkboxes, and it is the one that takes the pointer.
- Focus rings are drawn only when
:focus-visiblematches, so a click with the mouse doesn't leave one behind. - Taps, drags and typing worked on an emulated Pixel 7, and none of the five painted at rest. The fonts of the originals were missing in the test browser, and nothing here has been run on a real phone.