|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 7 | + <title>CSS Anchor Positioning Polyfill</title> |
| 8 | + <link |
| 9 | + rel="stylesheet" |
| 10 | + href=" https://unpkg.com/[email protected]/themes/prism.css" |
| 11 | + /> |
| 12 | + <script |
| 13 | + src="https://unpkg.com/@oddbird/popover-polyfill@latest/dist/popover.min.js" |
| 14 | + crossorigin="anonymous" |
| 15 | + defer |
| 16 | + ></script> |
| 17 | + <!-- TypeKit Fonts --> |
| 18 | + <script src="https://use.typekit.net/slx1xnq.js"></script> |
| 19 | + <script> |
| 20 | + try { |
| 21 | + Typekit.load({ async: true }); |
| 22 | + } catch (e) {} |
| 23 | + </script> |
| 24 | + <link rel="stylesheet" href="/demo.css" /> |
| 25 | + <style> |
| 26 | + anchor-web-component { |
| 27 | + width: 100%; |
| 28 | + } |
| 29 | + </style> |
| 30 | + <script type="module"> |
| 31 | + import polyfill from '/src/index-fn.ts'; |
| 32 | + |
| 33 | + const SUPPORTS_ANCHOR_POSITIONING = |
| 34 | + 'anchorName' in document.documentElement.style; |
| 35 | + |
| 36 | + const btn = document.getElementById('apply-polyfill'); |
| 37 | + |
| 38 | + if (!SUPPORTS_ANCHOR_POSITIONING) { |
| 39 | + btn.addEventListener('click', () => { |
| 40 | + document |
| 41 | + .querySelector('anchor-web-component') |
| 42 | + .applyPolyfill() |
| 43 | + .then(() => { |
| 44 | + btn.innerText = 'Polyfill Applied'; |
| 45 | + btn.setAttribute('disabled', ''); |
| 46 | + }); |
| 47 | + }); |
| 48 | + } else { |
| 49 | + btn.innerText = 'No Polyfill Needed'; |
| 50 | + btn.setAttribute('disabled', ''); |
| 51 | + console.log( |
| 52 | + 'anchor-positioning is supported in this browser; polyfill skipped.', |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + class AnchorWebComponent extends HTMLElement { |
| 57 | + /* This would typically be run in connectedCallback() */ |
| 58 | + applyPolyfill() { |
| 59 | + return polyfill({ roots: [this.shadowRoot] }); |
| 60 | + } |
| 61 | + } |
| 62 | + customElements.define('anchor-web-component', AnchorWebComponent); |
| 63 | + </script> |
| 64 | + <link rel="icon" href="/favicon.svg" type="image/svg+xml" /> |
| 65 | + <script src=" https://unpkg.com/[email protected]/components/prism-core.min.js" ></script> |
| 66 | + <script src=" https://unpkg.com/[email protected]/plugins/autoloader/prism-autoloader.min.js" ></script> |
| 67 | + <script |
| 68 | + defer |
| 69 | + data-domain="anchor-positioning.oddbird.net" |
| 70 | + src="https://plausible.io/js/script.hash.outbound-links.js" |
| 71 | + ></script> |
| 72 | + </head> |
| 73 | + <body> |
| 74 | + <header> |
| 75 | + <h1><a href="/">CSS Anchor Positioning Polyfill</a></h1> |
| 76 | + <nav> |
| 77 | + <span> See also: </span> |
| 78 | + <a |
| 79 | + href="https://github.com/web-platform-tests/wpt/tree/master/css/css-anchor-position" |
| 80 | + target="_blank" |
| 81 | + rel="noopener noreferrer" |
| 82 | + >WPT examples</a |
| 83 | + > |
| 84 | + <!-- <a |
| 85 | + href="https://anchor-position-wpt.netlify.app/" |
| 86 | + target="_blank" |
| 87 | + rel="noopener noreferrer" |
| 88 | + >WPT results</a |
| 89 | + > --> |
| 90 | + <a |
| 91 | + href="https://drafts.csswg.org/css-anchor-position/" |
| 92 | + target="_blank" |
| 93 | + rel="noopener noreferrer" |
| 94 | + >Draft Spec</a |
| 95 | + > |
| 96 | + </nav> |
| 97 | + <button id="apply-polyfill" data-button="apply-polyfill" type="button"> |
| 98 | + Apply Polyfill |
| 99 | + </button> |
| 100 | + </header> |
| 101 | + <section> |
| 102 | + <h2>Anchoring Elements in the shadow DOM</h2> |
| 103 | + <p> |
| 104 | + <strong>Note: </strong>We strive to keep the polyfill up-to-date with |
| 105 | + ongoing changes to the spec, and we welcome |
| 106 | + <a |
| 107 | + href="https://github.com/oddbird/css-anchor-positioning" |
| 108 | + target="_blank" |
| 109 | + rel="noopener noreferrer" |
| 110 | + >code contributions</a |
| 111 | + > |
| 112 | + and <a href="#sponsor">financial support</a> to make that happen. |
| 113 | + </p> |
| 114 | + </section> |
| 115 | + <section id="shadow-root" class="demo-item"> |
| 116 | + <h2> |
| 117 | + <a href="#shadow-root" aria-hidden="true">🔗</a> |
| 118 | + Works if anchor and target are both inside the same shadow root |
| 119 | + </h2> |
| 120 | + <div class="demo-elements"> |
| 121 | + <anchor-web-component> |
| 122 | + <template shadowrootmode="open"> |
| 123 | + <link rel="stylesheet" href="/demo.css" /> |
| 124 | + <link rel="stylesheet" href="/anchor-shadow-root.css" /> |
| 125 | + <div style="position: relative"> |
| 126 | + <div id="shadow-target-positioning" class="target">Target</div> |
| 127 | + <div id="shadow-anchor-positioning" class="anchor">Anchor</div> |
| 128 | + </div> |
| 129 | + </template> |
| 130 | + </anchor-web-component> |
| 131 | + </div> |
| 132 | + <div class="note"> |
| 133 | + <p> |
| 134 | + With polyfill applied: Target and Anchor’s right edges line up. |
| 135 | + Target’s top edge lines up with the bottom edge of the Anchor. |
| 136 | + </p> |
| 137 | + <p> |
| 138 | + <strong>Note: </strong> this will not work across shadow root |
| 139 | + boundaries, and will not work with constructed stylesheets. |
| 140 | + </p> |
| 141 | + </div> |
| 142 | + |
| 143 | + <pre><code class="language-html" |
| 144 | +><anchor-web-component> |
| 145 | + <template shadowrootmode="open"> |
| 146 | + <style> |
| 147 | + #my-anchor-positioning { |
| 148 | + anchor-name: --my-anchor-positioning; |
| 149 | + } |
| 150 | + |
| 151 | + #my-target-positioning { |
| 152 | + position: absolute; |
| 153 | + top: anchor(--my-anchor-positioning bottom); |
| 154 | + right: anchor(--my-anchor-positioning right, 50px); |
| 155 | + } |
| 156 | + </style> |
| 157 | + <div style="position: relative"> |
| 158 | + <div id="my-target-positioning">Target</div> |
| 159 | + <div id="my-anchor-positioning">Anchor</div> |
| 160 | + </div> |
| 161 | + </template> |
| 162 | +</anchor-web-component> |
| 163 | +<script> |
| 164 | +class AnchorDemo extends HTMLElement { |
| 165 | + connectedCallback() { |
| 166 | + window.ANCHOR_POSITIONING_POLYFILL({ roots: [this.shadowRoot] }); |
| 167 | + } |
| 168 | +} |
| 169 | +customElements.define("anchor-web-component", AnchorDemo); |
| 170 | +</script> |
| 171 | +</code></pre> |
| 172 | + </section> |
| 173 | + <section id="sponsor"> |
| 174 | + <h2>Sponsor OddBird’s OSS Work</h2> |
| 175 | + <p> |
| 176 | + At OddBird, we love contributing to the languages & tools developers |
| 177 | + rely on. We’re currently working on polyfills for new Popover & Anchor |
| 178 | + Positioning functionality, as well as CSS specifications for functions, |
| 179 | + mixins, and responsive typography. Help us keep this work sustainable |
| 180 | + and centered on your needs as a developer! We display sponsor logos and |
| 181 | + avatars on our |
| 182 | + <a |
| 183 | + href="https://www.oddbird.net/polyfill/#open-source-sponsors" |
| 184 | + target="_blank" |
| 185 | + rel="noopener noreferrer" |
| 186 | + >website</a |
| 187 | + >. |
| 188 | + </p> |
| 189 | + <a |
| 190 | + href="https://github.com/sponsors/oddbird" |
| 191 | + target="_blank" |
| 192 | + rel="noopener noreferrer" |
| 193 | + >Sponsor OddBird’s OSS Work</a |
| 194 | + > |
| 195 | + </section> |
| 196 | + <footer> |
| 197 | + <p> |
| 198 | + Spec proposal by |
| 199 | + <a |
| 200 | + href="http://xanthir.com/contact/" |
| 201 | + target="_blank" |
| 202 | + rel="noopener noreferrer" |
| 203 | + >Tab Atkins-Bittner</a |
| 204 | + >. Polyfill and demo by |
| 205 | + <a |
| 206 | + href="https://www.oddbird.net/" |
| 207 | + target="_blank" |
| 208 | + rel="noopener noreferrer" |
| 209 | + >OddBird</a |
| 210 | + >. |
| 211 | + </p> |
| 212 | + </footer> |
| 213 | + </body> |
| 214 | +</html> |
0 commit comments