这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@jamesnw
Copy link
Contributor

@jamesnw jamesnw commented Apr 1, 2025

Description

This is an alternate method of ensuring the correct value cascades in instances where multiple values for position-area apply to the same element.

.a #b{
  position-area: end;
}
.a .b{
  position-area: start;
}

This should resolve to end due to specificity.

In other parts of the polyfill, we can simply add properties to the same declaration blocks to ensure they cascade as expected. However, position-area requires us to add properties to a generated parent element of the selected element. This PR uses :where([parent]):not([original selector]) to properly handle the cascade.

The :where() selects the specific element, and the :not() applies that rule with the same specificity of the original selector. The example above creates the following selectors-

:where([parent]):not(.a #b){ /**rules **/}
:where([parent]):not(.a .b){ /**rules **/}

The alternate method, as implemented in #296, checks for a cascade winner each time the polyfill is updated. This requires an extra read and write of the DOM on each cycle, per target/position area combo. It also requires that we add an additional property to track the cascade. My initial impression is that this is a small impact relative to what the polyfill is already doing, but it would be convenient to simply rely on the cascade directly.

Related Issue(s)

#181

Steps to test/reproduce

In the demo, go to the Cascade example.
https://deploy-preview-307--anchor-polyfill.netlify.app/position-area#cascade
If you apply the polyfill, it will be positioned at top right. If you reload the page, click the "Switch cascade button", and apply the polyfill, it will be positioned at bottom right.

Caveats

  1. This moves the earliest supported browsers to Chromium 88, Firefox 84, Safari 14, or roughly 4 years. This would only impact people using position-area, though.
  2. This selector doesn't check if the original selector still applies. For instance, in the Cascade example, if you switch the cascade, apply the polyfill, and switch the cascade again, the .cascade-override class is no longer present in the document, but the :where([parent]):not(.cascade-override #cascade-target) selector still applies. Essentially this method picks the cascade winner of all the declarations that apply to elements that exist when the polyfill is initially applied.
  3. This will not work for selectors that rely on the positioned element's location in the DOM tree, for instance ~ target, + target, > target or uses :nth selctors. (This applies to either approach due to the wrapping element)

Note that the current diff doesn't include some cleanup that could happen to remove the other method.

Comparison with the original approach

Manual Cascade (original) Auto Cascade (this) Notes
No change in support Support reduced to 4 years Probably 4 years is enough support
Responds to DOM changes (for selectors that were valid at polyfill time, but not at update time) Does not respond to DOM changes Is there a solution without :has for this?
Wrapper breaks relative selectors Wrapper breaks relative selectors Same issue
Extra DOM read/write per update per target/position area combo Cascade applied by browser Small relative to existing read/writes
Additional property to track the cascade - Small relative to existing additions

@netlify
Copy link

netlify bot commented Apr 1, 2025

Deploy Preview for anchor-polyfill ready!

Name Link
🔨 Latest commit 0074b62
🔍 Latest deploy log https://app.netlify.com/sites/anchor-polyfill/deploys/67ebfb7b4e62c70008e26101
😎 Deploy Preview https://deploy-preview-307--anchor-polyfill.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@netlify
Copy link

netlify bot commented Apr 1, 2025

Deploy Preview for anchor-position-wpt canceled.

Name Link
🔨 Latest commit 0074b62
🔍 Latest deploy log https://app.netlify.com/sites/anchor-position-wpt/deploys/67ebfb7b7c27fc00088110a7

@jamesnw jamesnw changed the base branch from main to position-area April 1, 2025 14:44
@jamesnw jamesnw mentioned this pull request Apr 1, 2025
6 tasks
@mirisuzanne
Copy link
Member

If we know what properties we're tracking, it seems like there's probably a way to watch for changes in JS? But I think we would need :has to do it in CSS. Has will have better support than anchor positions, so at some point that might become a good option.

@jamesnw
Copy link
Contributor Author

jamesnw commented Apr 1, 2025

If we know what properties we're tracking, it seems like there's probably a way to watch for changes in JS?

I think we'll need to monitor in JS, as part of #91.

But I think we would need :has to do it in CSS. Has will have better support than anchor positions, so at some point that might become a good option.

I think you're right, and we may be able to use :has for this down the road.

@jamesnw
Copy link
Contributor Author

jamesnw commented Apr 7, 2025

I'm going to close this experiment out for now- I think the downsides outweigh the benefits here, at least for now.

@jamesnw jamesnw closed this Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants