From 68cb70ac27f5e24d519e028500ce1b890e5907e2 Mon Sep 17 00:00:00 2001 From: William Killerud Date: Thu, 2 Oct 2025 08:57:48 +0200 Subject: [PATCH 1/2] Add demo with a parent and child shadow tree In preparation for supporting more of the spec --- public/anchor-shadow-child.css | 5 +++ public/anchor-shadow-parent.css | 4 +++ shadow-root.html | 58 ++++++++++++++++++++++++++++----- 3 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 public/anchor-shadow-child.css create mode 100644 public/anchor-shadow-parent.css diff --git a/public/anchor-shadow-child.css b/public/anchor-shadow-child.css new file mode 100644 index 0000000..ee24e9c --- /dev/null +++ b/public/anchor-shadow-child.css @@ -0,0 +1,5 @@ +#child-target-positioning { + position: absolute; + right: anchor(--shadow-anchor-positioning right, 50px); + top: anchor(--shadow-anchor-positioning bottom); +} diff --git a/public/anchor-shadow-parent.css b/public/anchor-shadow-parent.css new file mode 100644 index 0000000..60b5206 --- /dev/null +++ b/public/anchor-shadow-parent.css @@ -0,0 +1,4 @@ +#parent-anchor-positioning { + anchor-name: --shadow-anchor-positioning; +} + diff --git a/shadow-root.html b/shadow-root.html index a3e1942..e441c97 100644 --- a/shadow-root.html +++ b/shadow-root.html @@ -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'; @@ -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); @@ -182,7 +198,7 @@

Works if anchor and target are both inside the same shadow root

- +