Small interactions can make a webpage easier to use: opening a tip, changing a setting, or reusing an interface component.
What You Need to Know
Scoped custom-element registries aim to let different component trees use different definitions for the same tag name. This is still an emerging feature and needs a fallback in public-facing sites.
Try the Example
Take a look at the example. We’ll explain the important lines right below it.
<student-card>
<template shadowrootmode="open" shadowrootcustomelementregistry>
<p>Component content goes here.</p>
</template>
</student-card>
<script>
const componentRegistry = new CustomElementRegistry();
componentRegistry.define("student-card", class extends HTMLElement {});
const cardElement = document.querySelector("student-card");
componentRegistry.initialize(cardElement.shadowRoot);
</script>
Let’s Read the Code
- <template>: A template stores markup that is inactive until it is cloned or used as declarative shadow content.
- <script>: Script contains JavaScript, data, or other browser-understood script formats depending on type.
- shadowrootmode: Shadowrootmode lets an HTML parser create a declarative shadow root.
Read the named elements and attributes together. They explain how the browser connects this feature to the rest of the page.
What Happens in the Browser?
This feature has limited or experimental browser support. Check your target browsers before using it for something essential.
Do not depend on one browser. Some advanced HTML features are still being adopted. Check support before you build essential navigation or forms with them, and keep a simpler version that people can use if the advanced feature is unavailable.
Your Turn
Run this on a local development server if it loads modules or resources. Change the visible text or trigger and check what still works with scripts disabled.
Common Beginner Mistakes
- Assuming new HTML syntax works in every browser without testing.
- This feature has limited or experimental browser support. Keep a standard HTML fallback, and check compatibility before using it in a public site.
Key Takeaways
- Scoped custom-element registries aim to let different component trees use different definitions for the same tag name.
- <template>: A template stores markup that is inactive until it is cloned or used as declarative shadow content.
- This lesson explains an emerging feature; it is not a promise that the feature works in every browser. Keep a usable fallback.