<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%:

<dom-portal for="cart" scale="0.5">

Show the code one element, drawn twice
  1. 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.
  2. Load the element. There is no other script: the shop code only changes the basket's DOM.
  3. 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.
  4. 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.
  5. 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
  1. A portal with fit scales the source to its box. <dom-portal-source layer="top"> wraps the document because it lives inside a scroller that would clip the canvas.
  2. The marker and dragging are plain DOM code.

How it works

Live checks

ResultExperimentDetail