Skip to content
Draft
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
5 changes: 5 additions & 0 deletions public/anchor-shadow-child.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#child-target-positioning {
position: absolute;
right: anchor(--shadow-anchor-positioning right);
top: anchor(--shadow-anchor-positioning bottom);
}
3 changes: 3 additions & 0 deletions public/anchor-shadow-parent.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#parent-anchor-positioning {
anchor-name: --shadow-anchor-positioning;
}
59 changes: 50 additions & 9 deletions shadow-root.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@

if (!SUPPORTS_ANCHOR_POSITIONING) {
btn.addEventListener('click', () => {
document
.querySelector('anchor-web-component')
.applyPolyfill()
.then(() => {
btn.innerText = 'Polyfill Applied';
btn.setAttribute('disabled', '');
});
Promise.all(
[...document.querySelectorAll('[data-polyfillable]')].map((e) =>
e.applyPolyfill(),
),
).then(() => {
btn.innerText = 'Polyfill Applied';
btn.setAttribute('disabled', '');
});
});
} else {
btn.innerText = 'No Polyfill Needed';
Expand All @@ -53,13 +54,28 @@
);
}

class AnchorWebComponent extends HTMLElement {
class BaseWebComponent extends HTMLElement {
/* This would typically be run in connectedCallback() */
applyPolyfill() {
return polyfill({ root: [this.shadowRoot] });
}
}

class AnchorWebComponent extends BaseWebComponent {}
customElements.define('anchor-web-component', AnchorWebComponent);

class ParentComponent extends BaseWebComponent {}
customElements.define('parent-component', ParentComponent);

class ChildComponent extends BaseWebComponent {
connectedCallback() {
console.log({
shadowRoot: this.shadowRoot,
host: this.shadowRoot.host.parentNode.host.parentNode.parentNode,
});
}
}
customElements.define('child-component', ChildComponent);
</script>
<link rel="icon" href="/favicon.svg" type="image/svg+xml" />
<script src="https://unpkg.com/prismjs@v1.x/components/prism-core.min.js"></script>
Expand Down Expand Up @@ -182,7 +198,7 @@ <h2>
Works if anchor and target are both inside the same shadow root
</h2>
<div class="demo-elements">
<anchor-web-component>
<anchor-web-component data-polyfillable>
<template shadowrootmode="open">
<link rel="stylesheet" href="/demo.css" />
<link rel="stylesheet" href="/anchor-shadow-root.css" />
Expand Down Expand Up @@ -235,6 +251,31 @@ <h2>
&lt;/script&gt;
</code></pre>
</section>
<section id="nested-shadow-roots" class="demo-item">
<h2>
<a href="#nested-shadow-roots" aria-hidden="true">🔗</a>
Child shadow root can anchor to parent shadow root
</h2>
<div class="demo-elements">
<parent-component data-polyfillable>
<template shadowrootmode="open">
<link rel="stylesheet" href="/demo.css" />
<link rel="stylesheet" href="/anchor-shadow-parent.css" />
<div style="position: relative;">
<child-component data-polyfillable>
<template shadowrootmode="open">
<link rel="stylesheet" href="/demo.css" />
<link rel="stylesheet" href="/anchor-shadow-child.css" />
<div id="child-target-positioning" class="target">Target</div>
</template>
</child-component>

<div id="parent-anchor-positioning" class="anchor">Anchor</div>
</div>
</template>
</parent-component>
</div>
</section>
<section id="sponsor">
<h2>Sponsor OddBird’s OSS Work</h2>
<p>
Expand Down