このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

ShadowRoot: customElementRegistry プロパティ

利用可能性は限定的

この機能はベースラインではありません。最も広く使用されているブラウザーの一部で動作しません。

Want more browser support for this feature? Tell us why.

customElementRegistryShadowRoot インターフェイスの読み取り専用プロパティで、このシャドウルートに関連付けられた CustomElementRegistry オブジェクトを、設定されていない場合は null を返します。

シャドウルートの customElementRegistry は、そのシャドウツリー内の要素を、どのカスタム要素定義が使用してアップグレードするかを決定します。これは、シャドウルートが作成される際に Element.attachShadow()customElementRegistry オプションを介して設定するか、あるいは後で CustomElementRegistry.initialize() を使用して設定することができます。一度 CustomElementRegistry オブジェクトに設定されると、変更することはできません。

このプロパティは、Document オブジェクト上でも、同時に customElementRegistry というプロパティ名を通じて利用可能です。

CustomElementRegistry オブジェクト、または null です。

シャドウツリーにスコープ付きレジストリーを設定

この例では、カスタム要素定義を含むスコープ付きレジストリーを作成し、それを Element.attachShadow() に渡しています。その結果として生成されるシャドウルートの customElementRegistry プロパティには、そのスコープ付きレジストリーが反映されます。

js
const myRegistry = new CustomElementRegistry();
myRegistry.define(
  "my-element",
  class extends HTMLElement {
    connectedCallback() {
      this.textContent = "スコープ付きレジストリーからこんにちは!";
    }
  },
);

const host = document.createElement("div");
document.body.appendChild(host);

const shadow = host.attachShadow({
  mode: "open",
  customElementRegistry: myRegistry,
});

shadow.innerHTML = "<my-element></my-element>";

console.log(shadow.customElementRegistry === myRegistry); // true
console.log(shadow.querySelector("my-element").textContent);
// "スコープ付きレジストリーからこんにちは!"

仕様書

仕様書
DOM
# dom-documentorshadowroot-customelementregistry

ブラウザーの互換性

関連情報