|
47 | 47 | bottom: anchor(--my-anchor-style-tag start);
|
48 | 48 | right: anchor(--my-anchor-style-tag left);
|
49 | 49 | }
|
| 50 | + |
| 51 | + #anchor-manual .anchor { |
| 52 | + inline-size: fit-content; |
| 53 | + margin: 3rem auto; |
| 54 | + } |
50 | 55 | </style>
|
51 | 56 | <script type="module">
|
52 | 57 | import polyfill from '/src/index-fn.ts';
|
53 | 58 |
|
| 59 | + const SUPPORTS_ANCHOR_POSITIONING = CSS.supports('anchor-name: --a'); |
| 60 | + |
54 | 61 | const btn = document.getElementById('apply-polyfill');
|
55 | 62 |
|
56 |
| - if (!('anchorName' in document.documentElement.style)) { |
| 63 | + if (!SUPPORTS_ANCHOR_POSITIONING) { |
57 | 64 | btn.addEventListener('click', () =>
|
58 | 65 | polyfill().then((rules) => {
|
59 | 66 | btn.innerText = 'Polyfill Applied';
|
|
80 | 87 | updateAnchor.removeAttribute('data-large');
|
81 | 88 | }
|
82 | 89 | });
|
| 90 | + |
| 91 | + function prepareManualPolyfill() { |
| 92 | + // anchor style element |
| 93 | + const anchorStyleEl = document.createElement('style'); |
| 94 | + anchorStyleEl.id = 'my-style-manual-anchor'; |
| 95 | + anchorStyleEl.textContent = [ |
| 96 | + '#my-anchor-manual {', |
| 97 | + 'anchor-name: --my-anchor-manual;', |
| 98 | + '}', |
| 99 | + ].join(''); |
| 100 | + |
| 101 | + // style element |
| 102 | + const styleEl = document.createElement('style'); |
| 103 | + styleEl.id = 'my-style-manual-style-el'; |
| 104 | + styleEl.textContent = [ |
| 105 | + '#my-target-manual-style-el {', |
| 106 | + 'position: absolute;', |
| 107 | + 'bottom: anchor(--my-anchor-manual top);', |
| 108 | + 'right: anchor(--my-anchor-manual left);', |
| 109 | + '}', |
| 110 | + ].join(''); |
| 111 | + |
| 112 | + // link element |
| 113 | + const linkEl = document.createElement('link'); |
| 114 | + linkEl.id = 'my-style-manual-link-el'; |
| 115 | + linkEl.rel = 'stylesheet'; |
| 116 | + linkEl.href = '/anchor-manual.css'; |
| 117 | + |
| 118 | + document.head.append(anchorStyleEl, styleEl, linkEl); |
| 119 | + |
| 120 | + // inline style |
| 121 | + document |
| 122 | + .getElementById('my-target-manual-inline-style') |
| 123 | + ?.setAttribute( |
| 124 | + 'style', |
| 125 | + [ |
| 126 | + 'position: absolute', |
| 127 | + 'top: anchor(--my-anchor-manual bottom)', |
| 128 | + 'left: anchor(--my-anchor-manual right)', |
| 129 | + ].join(';'), |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + // These event listeners are for E2E testing only |
| 134 | + document |
| 135 | + .getElementById('prepare-manual-polyfill') |
| 136 | + ?.addEventListener('click', () => prepareManualPolyfill(), { |
| 137 | + once: true, |
| 138 | + }); |
| 139 | + document |
| 140 | + .getElementById('apply-polyfill-manually-set1') |
| 141 | + ?.addEventListener('click', () => { |
| 142 | + polyfill({ |
| 143 | + elements: [ |
| 144 | + document.getElementById('my-style-manual-anchor'), |
| 145 | + document.getElementById('my-style-manual-style-el'), |
| 146 | + ], |
| 147 | + excludeInlineStyles: true, |
| 148 | + }); |
| 149 | + }); |
| 150 | + document |
| 151 | + .getElementById('apply-polyfill-manually-set2') |
| 152 | + ?.addEventListener('click', () => { |
| 153 | + polyfill({ |
| 154 | + elements: [ |
| 155 | + document.getElementById('my-style-manual-anchor'), |
| 156 | + document.getElementById('my-style-manual-link-el'), |
| 157 | + document.getElementById('my-target-manual-inline-style'), |
| 158 | + ], |
| 159 | + excludeInlineStyles: true, |
| 160 | + }); |
| 161 | + }); |
| 162 | + document |
| 163 | + .getElementById('apply-polyfill-manually-set3') |
| 164 | + ?.addEventListener('click', () => { |
| 165 | + polyfill({ |
| 166 | + elements: [ |
| 167 | + document.getElementById('my-style-manual-anchor'), |
| 168 | + document.getElementById('my-style-manual-style-el'), |
| 169 | + ], |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + const manualBtn = document.getElementById('apply-polyfill-manually'); |
| 174 | + if (SUPPORTS_ANCHOR_POSITIONING) { |
| 175 | + manualBtn.innerText = 'Load Anchor Positioning CSS'; |
| 176 | + } |
| 177 | + manualBtn.addEventListener('click', () => { |
| 178 | + prepareManualPolyfill(); |
| 179 | + |
| 180 | + if (!SUPPORTS_ANCHOR_POSITIONING) { |
| 181 | + polyfill({ |
| 182 | + elements: [ |
| 183 | + document.getElementById('my-style-manual-anchor'), |
| 184 | + document.getElementById('my-style-manual-link-el'), |
| 185 | + document.getElementById('my-style-manual-style-el'), |
| 186 | + document.getElementById('my-target-manual-inline-style'), |
| 187 | + ], |
| 188 | + }).then((rules) => { |
| 189 | + manualBtn.innerText = 'Polyfill Applied'; |
| 190 | + console.log(rules); |
| 191 | + }); |
| 192 | + } else { |
| 193 | + manualBtn.innerText = 'Anchor Positioning CSS applied'; |
| 194 | + console.log( |
| 195 | + 'anchor-positioning is supported in this browser; polyfill skipped.', |
| 196 | + ); |
| 197 | + } |
| 198 | + manualBtn.setAttribute('disabled', ''); |
| 199 | + }); |
83 | 200 | </script>
|
84 | 201 | <script src=" https://unpkg.com/[email protected]/components/prism-core.min.js" ></script>
|
85 | 202 | <script src=" https://unpkg.com/[email protected]/plugins/autoloader/prism-autoloader.min.js" ></script>
|
@@ -1003,6 +1120,86 @@ <h2>
|
1003 | 1120 | top: anchor(--my-anchor-media-query top);
|
1004 | 1121 | right: anchor(--my-anchor-media-query right);
|
1005 | 1122 | }</code></pre>
|
| 1123 | + </section> |
| 1124 | + <section id="anchor-manual" class="demo-item" style="position: relative"> |
| 1125 | + <h2> |
| 1126 | + <a href="#manual" aria-hidden="true">🔗</a> |
| 1127 | + Manually apply polyfill to specific styles |
| 1128 | + </h2> |
| 1129 | + <button id="apply-polyfill-manually">Polyfill these elements</button> |
| 1130 | + <div id="anchor-manual-test-buttons" hidden> |
| 1131 | + <!-- These buttons are for E2E testing only --> |
| 1132 | + <button id="prepare-manual-polyfill">Prepare</button> |
| 1133 | + <button id="apply-polyfill-manually-set1">Polyfill target 1</button> |
| 1134 | + <button id="apply-polyfill-manually-set2"> |
| 1135 | + Polyfill target 2 and 3 |
| 1136 | + </button> |
| 1137 | + <button id="apply-polyfill-manually-set3"> |
| 1138 | + Polyfill target 1 and 3 |
| 1139 | + </button> |
| 1140 | + </div> |
| 1141 | + <div class="demo-elements"> |
| 1142 | + <div id="my-anchor-manual" class="anchor">Anchor</div> |
| 1143 | + <div id="my-target-manual-style-el" class="target"> |
| 1144 | + Target 1 (with <code><style></code>) |
| 1145 | + </div> |
| 1146 | + <div id="my-target-manual-link-el" class="target"> |
| 1147 | + Target 2 (with <code><link></code>) |
| 1148 | + </div> |
| 1149 | + <div id="my-target-manual-inline-style" class="target"> |
| 1150 | + Target 3 (with inline style) |
| 1151 | + </div> |
| 1152 | + </div> |
| 1153 | + <p class="note"> |
| 1154 | + With polyfill applied: Target 1, 2, and 3 are positioned at Anchor’s |
| 1155 | + top-left, top-right, and bottom-right corners respectively. |
| 1156 | + </p> |
| 1157 | + <pre><code class="language-html" data-dependencies="css,js"><style id="my-style-manual-anchor"> |
| 1158 | + #my-anchor-manual { |
| 1159 | + anchor-name: --my-anchor-manual; |
| 1160 | + } |
| 1161 | +</style> |
| 1162 | +<style id="my-style-manual-style-el"> |
| 1163 | + #my-target-manual-style-el { |
| 1164 | + position: absolute; |
| 1165 | + bottom: anchor(--my-anchor-manual top); |
| 1166 | + right: anchor(--my-anchor-manual left); |
| 1167 | + } |
| 1168 | +</style> |
| 1169 | +<link rel="stylesheet" href="/anchor-manual.css" id="my-style-manual-link-el" /> |
| 1170 | +<!-- |
| 1171 | +CSS inside the anchor-manual.css file: |
| 1172 | + |
| 1173 | +#my-target-manual-link-el { |
| 1174 | + position: absolute; |
| 1175 | + bottom: anchor(--my-anchor-manual top); |
| 1176 | + left: anchor(--my-anchor-manual right); |
| 1177 | +} |
| 1178 | +--> |
| 1179 | + |
| 1180 | +<div id="my-anchor-manual" class="anchor">...</div> |
| 1181 | +<div id="my-target-manual-style-el" class="target">...</div> |
| 1182 | +<div id="my-target-manual-link-el" class="target">...</div> |
| 1183 | +<div id="my-target-manual-inline-style" class="target" |
| 1184 | + style="position: absolute; |
| 1185 | + top: anchor(--my-anchor-manual bottom); |
| 1186 | + left: anchor(--my-anchor-manual right);" |
| 1187 | +>...</div> |
| 1188 | + |
| 1189 | +<script> |
| 1190 | + polyfill({ |
| 1191 | + elements: [ |
| 1192 | + // The <style> element for anchor |
| 1193 | + document.getElementById('my-style-manual-anchor'), |
| 1194 | + // The <style> element |
| 1195 | + document.getElementById('my-style-manual-style-el'), |
| 1196 | + // The <link> element |
| 1197 | + document.getElementById('my-style-manual-link-el'), |
| 1198 | + // The target element with inline styles |
| 1199 | + document.getElementById('my-target-manual-inline-style'), |
| 1200 | + ], |
| 1201 | + }); |
| 1202 | +</script></code></pre> |
1006 | 1203 | </section>
|
1007 | 1204 | <section id="sponsor">
|
1008 | 1205 | <h2>Sponsor OddBird's OSS Work</h2>
|
|
0 commit comments