Component kit: newer DOM features in a drawable element
I wanted to know whether six DOM features that shipped between Chrome 133 and 152 work in a custom element that draws its children in a canvas, and what each one changes in the element's code.
<kit-slot> and <kit-field> are test elements for this page. They aren't a library to
install. Each is the smallest element that puts a child in a <canvas content="drawable"> in its shadow root,
which is what fx, <dom-portal>, <dom-lens> and <canvas-slot>
do. Each is written twice in one file: once with a feature, and once with the code that feature replaces. The page runs both
versions and counts the lines.
What the results mean for the libraries on this site
moveBefore()is now in the libraries. Addingfx="ripple"to an element, or pointing<dom-portal for>at it, wraps the element in a host. That used to remove and reinsert it, which reloads an iframe inside and drops focus.surface.jsanddom-portal.jsnow move it withmoveBefore()where the method exists.- Reference Target lets a page label a control inside a component's canvas.
<label for="host">works with one line in the element. Without it the element finds the label, copies its text toaria-labeland forwards clicks. - A scoped registry lets two versions of an element be on one page. This matters when a site and a widget it embeds each bring their own copy of a library. It doesn't change how the canvas finds slotted children.
- Manual slot assignment stops the element editing the page's markup.
<canvas-slot>could choose which child to draw without writingslotattributes on the author's elements. usingand Observable change little. Each removes a few lines.ElementImagehas no[Symbol.dispose], sousingneeds a wrapper.
Each feature in the test element
| Feature | In this browser | What it replaces in the element | Lines, old → new |
|---|---|---|---|
| Scoped custom element registry | … | A version suffix on the tag name, and a guard against defining the element twice. | … |
| Manual slot assignment | … | Writing slot="…" on the page's own elements, and a MutationObserver that keeps the attribute right. | … |
| Reference Target | … | Finding <label for> by hand, copying its text to aria-label and forwarding its clicks. | … |
| Explicit resource management | … | try/finally around every ElementImage.close(). | … |
| Observable | … | Keeping each paint and contextlost handler in a field so it can be removed later. | … |
moveBefore() | … | Putting focus back after an element is wrapped. A reloaded iframe had no fix. | … |
The line counts are taken from the #region old-… and #region new-… comments in the files this page is running. Blank lines and comments aren't counted.
Two versions of one element on one page
Each box below is a shadow root with its own CustomElementRegistry. Both define the tag kit-slot, with different
classes: the left one uses the older code for every part, the right one uses each feature this browser has. Press a button to
draw another child. The outgoing child fades out from an ElementImage snapshot.
A label outside, a field inside a canvas
<kit-field> keeps its <input> in the shadow root, as a child of the canvas that draws it. Press the label text.
Wrapping an element that is in use
The card holds a text field and an iframe that counts the seconds since it loaded. Type in the field, then wrap the card in a
<kit-slot>. With moveBefore() the caret stays in the field and the counter keeps going.
With replaceWith() the iframe loads again and the counter restarts.
Show the code each part, old and new
- Scoped registry: define the tag in the registry that is passed in.
- Before: one global registry, so a second version needs another tag name.
- This page: two shadow roots, two registries, the same tag.
- Manual slot assignment:
slot.assign(child). - Before: named slots and
slotattributes written on the children. - Reference Target: one line forwards
<label for>into the shadow root. - Before: find the labels, copy the text, forward the clicks.
DisposableStack: adopt the snapshot once, dispose once.- Before:
try/finallyat each place the fade can end. - The
usingdeclaration, in a module of its own. A browser without the syntax can't parse the file, so it's imported only after a check. - Before: the same function with
try/finally. - Observable:
canvas.when('paint'), ended by an AbortSignal. - Before: handlers kept in fields and removed by hand.
moveBefore(): wrap an element without removing it.- Before: remove, insert, put focus back.
- The same change in the library behind
fx,<dom-lens>and<physical-ui>. dom-portal.js has it too. - The whole element.
Findings
- A scoped registry doesn't change how a canvas finds slotted children. An element defined only in a shadow root's own registry draws its slotted children, and
elementFromPoint()on that shadow root returns them at the drawn position. Two classes can share the tag namekit-sloton one page. - A manually assigned child is drawn, and an unassigned child has no box. The children carry no
slotattribute. They still need thedrawableattribute, which the element adds. - Reference Target reaches a control that is a child of a canvas. A
<label for>in the page focuses the input in the shadow root and is its accessible label. The target has to be inside the shadow root: an id of a slotted light-DOM child isn't found, so<kit-slot>can't use it for its children. ElementImagehasclose()but no[Symbol.dispose].using image = canvas.captureElementImage(el)throws a TypeError. It needs a wrapper object orDisposableStack.adopt(image, (i) => i.close()).usingis syntax, so it can't be feature-checked inside the file that uses it. The function that uses it is in its own module and is loaded withimport()afternew Function()has parsed a test statement.canvas.when('paint')delivers the same events asaddEventListener, in the same frame, andtake(),map()and an AbortSignal work on them.moveBefore()into a host whose shadow root slots the node into a canvas keeps focus and doesn't reload an iframe. Thefxattribute,<dom-portal>,<dom-lens>and<physical-ui>now wrap elements this way where the method exists.- Not tested here:
:has-slottedisn't in the test browser (Chromium 157). The phone results for all six features are unknown until this page is opened there: the first table column reports them.
Live checks
| Result | Experiment | Detail |
|---|