<dom-portal>: draw one element in two places
The basket on the right is one ordinary <aside id="cart">. The half-size copy docked at the bottom of the
window is <dom-portal for="cart" scale="0.5">. It's the same element, drawn a second time. I wanted to know whether both copies could take input. Add something, then change a quantity or type a promo code in either one.
Portals are inline boxes, so one can sit in running text. Here is the basket again at 70%:
Show the code one element, drawn twice
- Give the element an id and point a
<dom-portal>at it. The basket is ordinary markup that knows nothing about portals. Each portal is another place to draw it. - Load the element. There is no other script: the shop code only changes the basket's DOM.
- Inside the element: draw the same element once at home and once per portal. The source sits in one large canvas that covers every place it has to appear.
drawElementImage()can be called any number of times with the same element. - Inside the element: one element has one hit box, so give it to the copy under the pointer.
updateElementGeometry()moves clicks, focus rings and the caret to that copy. - The whole element.
<script type="module" src="/lib/dom-portal.js"></script>
<aside id="cart">…</aside>
<dom-portal for="cart" scale="0.5"></dom-portal>
A live minimap
The document on the left scrolls inside a box. The strip on the right is <dom-portal for="doc" fit>: the whole
document, scaled to fit. Scroll the document, drag along the minimap to move through it, or tick a checkbox in the minimap. Because the source is inside a scrolling box, this one
uses layer="top", which draws through a canvas in the top layer so the copy isn't clipped by the scroller.
Show the code the minimap
- A portal with
fitscales the source to its box.<dom-portal-source layer="top">wraps the document because it lives inside a scroller that would clip the canvas. - The marker and dragging are plain DOM code.
How it works
- The element is wrapped. The first portal that points at
#cartwraps it in a<dom-portal-source>whose shadow root slots it into a canvas. It isn't copied: your element keeps its listeners and its state, and only its parent changes. You can also write the wrapper yourself. - One canvas spans home and every portal. An element can only be drawn into its own canvas, so that canvas is stretched over the union of all the places the element appears. It's transparent and ignores pointer events everywhere else. Because it's positioned in the page, scrolling needs no redraw.
- An element has one hit-test region. It can only be in one place at a time, so the page moves it to whichever copy the pointer is over. Focus, typing, selection and the accessibility tree all belong to the one element.
- When the union is too big (a fixed portal far from its source on a long page), the canvas becomes viewport-sized and is redrawn as you scroll.
layer="top"puts it in the top layer to escapeoverflowclipping.
Live checks
| Result | Experiment | Detail |
|---|