diff --git a/README.md b/README.md index bde6c2ea..575d6948 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ value of `window.ANCHOR_POSITIONING_POLYFILL_OPTIONS`. window.ANCHOR_POSITIONING_POLYFILL_OPTIONS = { elements: undefined, excludeInlineStyles: false, + roots: [document], useAnimationFrame: false, }; import("https://unpkg.com/@oddbird/css-anchor-positioning"); @@ -89,6 +90,7 @@ an argument. polyfill({ elements: undefined, excludeInlineStyles: false, + roots: [document], useAnimationFrame: false, }); } @@ -115,6 +117,15 @@ defined. When set to `true`, elements with eligible inline styles listed in the `elements` option will still be polyfilled, but no other elements in the document will be implicitly polyfilled. +### roots + +type: `(Document |Â HTMLElement)[]`, default: `[document]` + +By default the polyfill applies to `document`, but you can configure one or more +shadow roots the polyfill should apply to using this option. See the +[shadow DOM examples](https://anchor-positioning.oddbird.net/shadow-dom) to learn +more. + ### useAnimationFrame type: `boolean`, default: `false` @@ -150,7 +161,7 @@ following features: `align-items` properties - `position-visibility` property - dynamically added/removed anchors or targets -- anchors or targets in the shadow-dom +- anchors and targets in separate shadow roots (see https://github.com/oddbird/css-anchor-positioning/issues/191) - anchors or targets in constructed stylesheets (https://github.com/oddbird/css-anchor-positioning/issues/228) - anchor functions assigned to `inset-*` properties or `inset` shorthand diff --git a/index.html b/index.html index 3219b58e..ba6e053a 100644 --- a/index.html +++ b/index.html @@ -1379,6 +1379,16 @@
+ The polyfill can position targets and anchors inside a shadow root. See + shadow DOM examples. +
+diff --git a/public/anchor-shadow-root.css b/public/anchor-shadow-root.css new file mode 100644 index 00000000..67660003 --- /dev/null +++ b/public/anchor-shadow-root.css @@ -0,0 +1,9 @@ +#shadow-anchor-positioning { + anchor-name: --shadow-anchor-positioning; +} + +#shadow-target-positioning { + position: absolute; + right: anchor(--shadow-anchor-positioning right, 50px); + top: anchor(--shadow-anchor-positioning bottom); +} diff --git a/shadow-dom.html b/shadow-dom.html new file mode 100644 index 00000000..ce57853a --- /dev/null +++ b/shadow-dom.html @@ -0,0 +1,214 @@ + + +
+ + + ++ Note: We strive to keep the polyfill up-to-date with + ongoing changes to the spec, and we welcome + code contributions + and financial support to make that happen. +
++ With polyfill applied: Target and Anchor’s right edges line up. + Target’s top edge lines up with the bottom edge of the Anchor. +
++ Note: this will not work across shadow root + boundaries, and will not work with constructed stylesheets. +
+<anchor-web-component>
+ <template shadowrootmode="open">
+ <style>
+ #my-anchor-positioning {
+ anchor-name: --my-anchor-positioning;
+ }
+
+ #my-target-positioning {
+ position: absolute;
+ top: anchor(--my-anchor-positioning bottom);
+ right: anchor(--my-anchor-positioning right, 50px);
+ }
+ </style>
+ <div style="position: relative">
+ <div id="my-target-positioning">Target</div>
+ <div id="my-anchor-positioning">Anchor</div>
+ </div>
+ </template>
+</anchor-web-component>
+<script>
+class AnchorDemo extends HTMLElement {
+ connectedCallback() {
+ window.ANCHOR_POSITIONING_POLYFILL({ roots: [this.shadowRoot] });
+ }
+}
+customElements.define("anchor-web-component", AnchorDemo);
+</script>
+
+ + At OddBird, we love contributing to the languages & tools developers + rely on. We’re currently working on polyfills for new Popover & Anchor + Positioning functionality, as well as CSS specifications for functions, + mixins, and responsive typography. Help us keep this work sustainable + and centered on your needs as a developer! We display sponsor logos and + avatars on our + website. +
+ Sponsor OddBird’s OSS Work +