liveBook · Manning

Adoptedcallback

As explained in Web Components in Action

adoptedCallback is a custom-element lifecycle callback invoked when an element is adopted into a different document.

adoptedCallback is a seldom-used Web Components lifecycle callback for when a custom element is adopted into a different document. It rounds out the component lifecycle alongside constructor, connectedCallback, attributeChangedCallback, observedAttributes, and disconnectedCallback. section 4.0 section 4.3

Adopting an element from an iframe

A DOM adoption scenario begins by selecting a custom element from an iframe document and passing it to document.adoptNode(el). This is the situation in which adoptedCallback becomes relevant. section 4.3

const frame = document.getElementsByTagName("iframe")[0]
const el = frame.contentWindow.document.getElementsByTagName(
       "my-custom-component")[0];
const adopted = document.adoptNode(el);

Inspect the compact adoption example, which obtains an element from an iframe and adopts it into the current document. section 4.3

How Web Components in Action covers it

How deep it goes
Introduces the callback as seldom used and shows adoption, but stops short of a worked callback implementation.
Examples
Uses an element selected from an iframe document and passed to document.adoptNode.
What you can do after
Understand when cross-document adoption makes adoptedCallback relevant in a custom element lifecycle.
How it connects
Connects custom elements with Web Components lifecycle callbacks, DOM adoption, and iframe documents.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Components in Action. The section links point to the parts of the book it draws on.