Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Shadow DOM examples #245

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add Shadow DOM examples
  • Loading branch information
pepelsbey committed Jan 17, 2024
commit ee13128862089ae5fbad81b8a00b68f0d787f66a
4 changes: 4 additions & 0 deletions shadow-dom/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Shadow DOM

- [Simple `<template shadowrootmode="open">` example](https://mdn.github.io/dom-examples/shadow-dom/shadowrootmode/simple.html)
- [CSS scoping with `<template shadowrootmode="open">`](https://mdn.github.io/dom-examples/shadow-dom/shadowrootmode/scoping.html)
46 changes: 46 additions & 0 deletions shadow-dom/shadowrootmode/scoping.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>CSS scoping in declarative shadow DOM</title>
<style>
body {
font-family: sans-serif;
}
</style>
</head>
<body>
<p hidden>⛔ Your browser doesn't support <code>shadowrootmode</code> attribute yet.</p>
<article>
<style>
p {
padding: 8px;
background-color: wheat;
}
</style>
<p>I'm in the DOM.</p>
</article>
<article>
<template shadowrootmode="open">
<style>
p {
padding: 8px;
background-color: plum;
}
</style>
<p>I'm in the Shadow DOM.</p>
</template>
</article>

<script>
const isShadowRootModeSupported = HTMLTemplateElement.prototype.hasOwnProperty('shadowRootMode');

document.querySelector('p[hidden]').toggleAttribute('hidden', isShadowRootModeSupported);
</script>
<!--
Used in:
- https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template
-->
</body>
</html>
19 changes: 19 additions & 0 deletions shadow-dom/shadowrootmode/simple.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Declarative shadow DOM</title>
</head>
<body>
<div id="host">
<template shadowrootmode="open">
<span>I'm in the shadow DOM</span>
</template>
</div>
<!--
Used in:
- https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_shadow_DOM
-->
</body>
</html>